Skip to content

Commit

Permalink
Avoid redundant definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
kcat committed Jan 4, 2024
1 parent 8c80f29 commit 744bd1e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
3 changes: 0 additions & 3 deletions al/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3648,9 +3648,6 @@ SourceSubList::~SourceSubList()


#ifdef ALSOFT_EAX
constexpr const ALsource::EaxFxSlotIds ALsource::eax4_fx_slot_ids;
constexpr const ALsource::EaxFxSlotIds ALsource::eax5_fx_slot_ids;

void ALsource::eaxInitialize(ALCcontext *context) noexcept
{
assert(context != nullptr);
Expand Down
5 changes: 3 additions & 2 deletions alc/alc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2057,7 +2057,7 @@ ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *Device, ALCenum para
ProbeAllDevicesList();

/* Copy first entry as default. */
alcDefaultAllDevicesSpecifier = alcAllDevicesList.c_str();
alcDefaultAllDevicesSpecifier = alcAllDevicesList.substr(0, alcAllDevicesList.find('\0'));
value = alcDefaultAllDevicesSpecifier.c_str();
break;

Expand All @@ -2066,7 +2066,8 @@ ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *Device, ALCenum para
ProbeCaptureDeviceList();

/* Copy first entry as default. */
alcCaptureDefaultDeviceSpecifier = alcCaptureDeviceList.c_str();
alcCaptureDefaultDeviceSpecifier = alcCaptureDeviceList.substr(0,
alcCaptureDeviceList.find('\0'));
value = alcCaptureDefaultDeviceSpecifier.c_str();
break;

Expand Down
26 changes: 16 additions & 10 deletions alc/backends/sdl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <cstdlib>
#include <cstring>
#include <string>
#include <string_view>

#include "almalloc.h"
#include "alnumeric.h"
Expand All @@ -46,8 +47,9 @@ namespace {
#define DEVNAME_PREFIX ""
#endif

/* NOLINTNEXTLINE(*-avoid-c-arrays) */
constexpr char defaultDeviceName[]{DEVNAME_PREFIX "Default Device"};
constexpr auto getDevicePrefix() noexcept -> std::string_view { return DEVNAME_PREFIX; }
constexpr auto getDefaultDeviceName() noexcept -> std::string_view
{ return DEVNAME_PREFIX "Default Device"; }

struct Sdl2Backend final : public BackendBase {
Sdl2Backend(DeviceBase *device) noexcept : BackendBase{device} { }
Expand Down Expand Up @@ -107,6 +109,7 @@ void Sdl2Backend::open(std::string_view name)
/* Passing nullptr to SDL_OpenAudioDevice opens a default, which isn't
* necessarily the first in the list.
*/
const auto defaultDeviceName = getDefaultDeviceName();
SDL_AudioDeviceID devid;
if(name.empty() || name == defaultDeviceName)
{
Expand All @@ -115,13 +118,13 @@ void Sdl2Backend::open(std::string_view name)
}
else
{
const size_t prefix_len = strlen(DEVNAME_PREFIX);
if(name.length() >= prefix_len && strncmp(name.data(), DEVNAME_PREFIX, prefix_len) == 0)
const auto namePrefix = getDevicePrefix();
if(name.size() >= namePrefix.size() && name.substr(0, namePrefix.size()) == namePrefix)
{
/* Copy the string_view to a string to ensure it's null terminated
* for this call.
*/
const std::string devname{name.substr(prefix_len)};
const std::string devname{name.substr(namePrefix.size())};
devid = SDL_OpenAudioDevice(devname.c_str(), SDL_FALSE, &want, &have,
SDL_AUDIO_ALLOW_ANY_CHANGE);
}
Expand Down Expand Up @@ -216,13 +219,16 @@ std::string SDL2BackendFactory::probe(BackendType type)
int num_devices{SDL_GetNumAudioDevices(SDL_FALSE)};

/* Includes null char. */
outnames.append(defaultDeviceName, sizeof(defaultDeviceName));
outnames += getDefaultDeviceName();
outnames += '\0';
for(int i{0};i < num_devices;++i)
{
std::string name{DEVNAME_PREFIX};
name += SDL_GetAudioDeviceName(i, SDL_FALSE);
if(!name.empty())
outnames.append(name.c_str(), name.length()+1);
outnames += getDevicePrefix();
if(const char *name = SDL_GetAudioDeviceName(i, SDL_FALSE))
outnames += name;
else
outnames += "Unknown Device Name #"+std::to_string(i);
outnames += '\0';
}
return outnames;
}
Expand Down
1 change: 0 additions & 1 deletion alc/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ struct ALCcontext : public al::intrusive_ref<ALCcontext>, ContextBase {
static ALeffect sDefaultEffect;

#ifdef ALSOFT_EAX
public:
bool hasEax() const noexcept { return mEaxIsInitialized; }
bool eaxIsCapable() const noexcept;

Expand Down

0 comments on commit 744bd1e

Please sign in to comment.