From 744bd1e289502f3ab413a4026f3dc9a6b6e07a72 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Wed, 3 Jan 2024 19:46:59 -0800 Subject: Avoid redundant definitions --- al/source.cpp | 3 --- alc/alc.cpp | 5 +++-- alc/backends/sdl2.cpp | 26 ++++++++++++++++---------- alc/context.h | 1 - 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/al/source.cpp b/al/source.cpp index 27144389..b3af9428 100644 --- a/al/source.cpp +++ b/al/source.cpp @@ -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); diff --git a/alc/alc.cpp b/alc/alc.cpp index b63317e7..b629a3fc 100644 --- a/alc/alc.cpp +++ b/alc/alc.cpp @@ -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; @@ -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; diff --git a/alc/backends/sdl2.cpp b/alc/backends/sdl2.cpp index 49b9713e..b69f17fd 100644 --- a/alc/backends/sdl2.cpp +++ b/alc/backends/sdl2.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include "almalloc.h" #include "alnumeric.h" @@ -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} { } @@ -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) { @@ -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); } @@ -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; } diff --git a/alc/context.h b/alc/context.h index 86ef140d..e27d10d3 100644 --- a/alc/context.h +++ b/alc/context.h @@ -203,7 +203,6 @@ public: static ALeffect sDefaultEffect; #ifdef ALSOFT_EAX -public: bool hasEax() const noexcept { return mEaxIsInitialized; } bool eaxIsCapable() const noexcept; -- cgit v1.2.3