From 3658dafdcbbd114caaf81cb27cf6ccc07045b0aa Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sun, 30 Jun 2019 16:15:15 -0700 Subject: Use an optional for ConfigValueStr --- Alc/alc.cpp | 41 +++++++++++++++++++++++++---------------- Alc/alconfig.cpp | 7 +++---- Alc/alconfig.h | 4 +++- Alc/backends/alsa.cpp | 29 +++++++++++------------------ Alc/backends/oss.cpp | 25 +++++++++++-------------- Alc/backends/solaris.cpp | 11 ++++++----- Alc/hrtf.cpp | 30 ++++++++++++++++-------------- Alc/mixvoice.cpp | 5 ++--- Alc/panning.cpp | 19 +++++++++---------- 9 files changed, 86 insertions(+), 85 deletions(-) diff --git a/Alc/alc.cpp b/Alc/alc.cpp index ec9953fe..7cf28ed3 100644 --- a/Alc/alc.cpp +++ b/Alc/alc.cpp @@ -1024,8 +1024,9 @@ void alc_initconfig(void) #ifdef HAVE_NEON capfilter |= CPU_CAP_NEON; #endif - if(ConfigValueStr(nullptr, nullptr, "disable-cpu-exts", &str)) + if(auto cpuopt = ConfigValueStr(nullptr, nullptr, "disable-cpu-exts")) { + str = cpuopt->c_str(); if(strcasecmp(str, "all") == 0) capfilter = 0; else @@ -1094,15 +1095,20 @@ void alc_initconfig(void) if(ConfigValueFloat(nullptr, "reverb", "boost", &valf)) ReverbBoost *= std::pow(10.0f, valf / 20.0f); - const char *devs{getenv("ALSOFT_DRIVERS")}; - if((devs && devs[0]) || ConfigValueStr(nullptr, nullptr, "drivers", &devs)) + auto devopt = ConfigValueStr(nullptr, nullptr, "drivers"); + if(const char *devs{getenv("ALSOFT_DRIVERS")}) + { + if(devs[0]) + devopt = al::optional{al::in_place, devs}; + } + if(devopt) { auto backendlist_cur = std::begin(BackendList); bool endlist{true}; - const char *next = devs; + const char *next{devopt->c_str()}; do { - devs = next; + const char *devs{next}; while(isspace(devs[0])) devs++; next = strchr(devs, ','); @@ -1182,9 +1188,9 @@ void alc_initconfig(void) if(!CaptureFactory) WARN("No capture backend available!\n"); - if(ConfigValueStr(nullptr, nullptr, "excludefx", &str)) + if(auto exclopt = ConfigValueStr(nullptr, nullptr, "excludefx")) { - const char *next = str; + const char *next{exclopt->c_str()}; do { str = next; next = strchr(str, ','); @@ -1203,9 +1209,10 @@ void alc_initconfig(void) } InitEffect(&DefaultEffect); - str = getenv("ALSOFT_DEFAULT_REVERB"); - if((str && str[0]) || ConfigValueStr(nullptr, nullptr, "default-reverb", &str)) - LoadReverbPreset(str, &DefaultEffect); + auto defrevopt = ConfigValueStr(nullptr, nullptr, "default-reverb"); + if((str=getenv("ALSOFT_DEFAULT_REVERB")) && str[0]) + defrevopt = al::optional{al::in_place, str}; + if(defrevopt) LoadReverbPreset(defrevopt->c_str(), &DefaultEffect); } #define DO_INITCONFIG() std::call_once(alc_config_once, [](){alc_initconfig();}) @@ -1919,9 +1926,9 @@ static ALCenum UpdateDeviceParams(ALCdevice *device, const ALCint *attrList) device->HrtfStatus = ALC_HRTF_DISABLED_SOFT; if(device->Type != Loopback) { - const char *hrtf; - if(ConfigValueStr(device->DeviceName.c_str(), nullptr, "hrtf", &hrtf)) + if(auto hrtfopt = ConfigValueStr(device->DeviceName.c_str(), nullptr, "hrtf")) { + const char *hrtf{hrtfopt->c_str()}; if(strcasecmp(hrtf, "true") == 0) hrtf_userreq = Hrtf_Enable; else if(strcasecmp(hrtf, "false") == 0) @@ -3743,8 +3750,7 @@ START_API_FUNC } deviceName = device->DeviceName.c_str(); - const ALCchar *fmt{}; - if(ConfigValueStr(deviceName, nullptr, "channels", &fmt)) + if(auto chanopt = ConfigValueStr(deviceName, nullptr, "channels")) { static constexpr struct ChannelMap { const char name[16]; @@ -3763,6 +3769,7 @@ START_API_FUNC { "ambi3", DevFmtAmbi3D, 3 }, }; + const ALCchar *fmt{chanopt->c_str()}; auto iter = std::find_if(std::begin(chanlist), std::end(chanlist), [fmt](const ChannelMap &entry) -> bool { return strcasecmp(entry.name, fmt) == 0; } @@ -3776,7 +3783,7 @@ START_API_FUNC device->Flags.set(); } } - if(ConfigValueStr(deviceName, nullptr, "sample-type", &fmt)) + if(auto typeopt = ConfigValueStr(deviceName, nullptr, "sample-type")) { static constexpr struct TypeMap { const char name[16]; @@ -3791,6 +3798,7 @@ START_API_FUNC { "float32", DevFmtFloat }, }; + const ALCchar *fmt{typeopt->c_str()}; auto iter = std::find_if(std::begin(typelist), std::end(typelist), [fmt](const TypeMap &entry) -> bool { return strcasecmp(entry.name, fmt) == 0; } @@ -3842,8 +3850,9 @@ START_API_FUNC device->NumStereoSources = 1; device->NumMonoSources = device->SourcesMax - device->NumStereoSources; - if(ConfigValueStr(deviceName, nullptr, "ambi-format", &fmt)) + if(auto ambiopt = ConfigValueStr(deviceName, nullptr, "ambi-format")) { + const ALCchar *fmt{ambiopt->c_str()}; if(strcasecmp(fmt, "fuma") == 0) { if(device->mAmbiOrder > 3) diff --git a/Alc/alconfig.cpp b/Alc/alconfig.cpp index 8b299afc..c5abd6ab 100644 --- a/Alc/alconfig.cpp +++ b/Alc/alconfig.cpp @@ -492,13 +492,12 @@ int ConfigValueExists(const char *devName, const char *blockName, const char *ke return val[0] != 0; } -int ConfigValueStr(const char *devName, const char *blockName, const char *keyName, const char **ret) +al::optional ConfigValueStr(const char *devName, const char *blockName, const char *keyName) { const char *val = GetConfigValue(devName, blockName, keyName, ""); - if(!val[0]) return 0; + if(!val[0]) return al::nullopt; - *ret = val; - return 1; + return al::optional{al::in_place, val}; } al::optional ConfigValueInt(const char *devName, const char *blockName, const char *keyName) diff --git a/Alc/alconfig.h b/Alc/alconfig.h index 275fed75..9b1c09aa 100644 --- a/Alc/alconfig.h +++ b/Alc/alconfig.h @@ -1,6 +1,8 @@ #ifndef ALCONFIG_H #define ALCONFIG_H +#include + #include "aloptional.h" void ReadALConfig(); @@ -9,7 +11,7 @@ int ConfigValueExists(const char *devName, const char *blockName, const char *ke const char *GetConfigValue(const char *devName, const char *blockName, const char *keyName, const char *def); int GetConfigValueBool(const char *devName, const char *blockName, const char *keyName, int def); -int ConfigValueStr(const char *devName, const char *blockName, const char *keyName, const char **ret); +al::optional ConfigValueStr(const char *devName, const char *blockName, const char *keyName); al::optional ConfigValueInt(const char *devName, const char *blockName, const char *keyName); al::optional ConfigValueUInt(const char *devName, const char *blockName, const char *keyName); int ConfigValueFloat(const char *devName, const char *blockName, const char *keyName, float *ret); diff --git a/Alc/backends/alsa.cpp b/Alc/backends/alsa.cpp index ebdc6129..aeb38585 100644 --- a/Alc/backends/alsa.cpp +++ b/Alc/backends/alsa.cpp @@ -207,11 +207,6 @@ ALSA_FUNCS(MAKE_FUNC); struct DevMap { std::string name; std::string device_name; - - template - DevMap(StrT0&& name_, StrT1&& devname_) - : name{std::forward(name_)}, device_name{std::forward(devname_)} - { } }; al::vector PlaybackDevices; @@ -233,10 +228,9 @@ al::vector probe_devices(snd_pcm_stream_t stream) snd_pcm_info_t *pcminfo; snd_pcm_info_malloc(&pcminfo); - devlist.emplace_back(alsaDevice, + devlist.emplace_back(DevMap{alsaDevice, GetConfigValue(nullptr, "alsa", (stream==SND_PCM_STREAM_PLAYBACK) ? "device" : "capture", - "default") - ); + "default")}); if(stream == SND_PCM_STREAM_PLAYBACK) { @@ -254,15 +248,15 @@ al::vector probe_devices(snd_pcm_stream_t stream) } const char *oldsep{sep++}; - devlist.emplace_back(std::string(customdevs, oldsep), - next ? std::string(sep, next++) : std::string(sep)); + devlist.emplace_back(DevMap{std::string(customdevs, oldsep), + next ? std::string(sep, next++) : std::string(sep)}); const auto &entry = devlist.back(); TRACE("Got device \"%s\", \"%s\"\n", entry.name.c_str(), entry.device_name.c_str()); } } - const char *main_prefix{"plughw:"}; - ConfigValueStr(nullptr, "alsa", prefix_name(stream), &main_prefix); + const std::string main_prefix{ + ConfigValueStr(nullptr, "alsa", prefix_name(stream)).value_or("plughw:")}; int card{-1}; int err{snd_card_next(&card)}; @@ -288,9 +282,8 @@ al::vector probe_devices(snd_pcm_stream_t stream) name = prefix_name(stream); name += '-'; name += cardid; - - const char *card_prefix{main_prefix}; - ConfigValueStr(nullptr, "alsa", name.c_str(), &card_prefix); + const std::string card_prefix{ + ConfigValueStr(nullptr, "alsa", name.c_str()).value_or(main_prefix)}; int dev{-1}; while(1) @@ -315,8 +308,8 @@ al::vector probe_devices(snd_pcm_stream_t stream) name += cardid; name += '-'; name += std::to_string(dev); - const char *device_prefix{card_prefix}; - ConfigValueStr(nullptr, "alsa", name.c_str(), &device_prefix); + const std::string device_prefix{ + ConfigValueStr(nullptr, "alsa", name.c_str()).value_or(card_prefix)}; /* "CardName, PcmName (CARD=cardid,DEV=dev)" */ name = cardname; @@ -335,7 +328,7 @@ al::vector probe_devices(snd_pcm_stream_t stream) device += ",DEV="; device += std::to_string(dev); - devlist.emplace_back(std::move(name), std::move(device)); + devlist.emplace_back(DevMap{std::move(name), std::move(device)}); const auto &entry = devlist.back(); TRACE("Got device \"%s\", \"%s\"\n", entry.name.c_str(), entry.device_name.c_str()); } diff --git a/Alc/backends/oss.cpp b/Alc/backends/oss.cpp index 0a28fcd8..e5b74334 100644 --- a/Alc/backends/oss.cpp +++ b/Alc/backends/oss.cpp @@ -80,17 +80,12 @@ namespace { constexpr char DefaultName[] = "OSS Default"; -const char *DefaultPlayback{"/dev/dsp"}; -const char *DefaultCapture{"/dev/dsp"}; +std::string DefaultPlayback{"/dev/dsp"}; +std::string DefaultCapture{"/dev/dsp"}; struct DevMap { std::string name; std::string device_name; - - template - DevMap(StrT0&& name_, StrT1&& devname_) - : name{std::forward(name_)}, device_name{std::forward(devname_)} - { } }; bool checkName(const al::vector &list, const std::string &name) @@ -111,7 +106,7 @@ al::vector CaptureDevices; #define DSP_CAP_INPUT 0x00010000 void ALCossListPopulate(al::vector *devlist, int type) { - devlist->emplace_back(DefaultName, (type==DSP_CAP_INPUT) ? DefaultCapture : DefaultPlayback); + devlist->emplace_back(DevMap{DefaultName, (type==DSP_CAP_INPUT) ? DefaultCapture : DefaultPlayback}); } #else @@ -156,7 +151,7 @@ void ALCossListAppend(al::vector *list, const char *handle, size_t hlen, newname += std::to_string(++count); } - list->emplace_back(std::move(newname), std::move(devname)); + list->emplace_back(DevMap{std::move(newname), std::move(devname)}); const DevMap &entry = list->back(); TRACE("Got device \"%s\", \"%s\"\n", entry.name.c_str(), entry.device_name.c_str()); @@ -212,7 +207,7 @@ done: close(fd); fd = -1; - const char *defdev{(type_flag==DSP_CAP_INPUT) ? DefaultCapture : DefaultPlayback}; + const char *defdev{((type_flag==DSP_CAP_INPUT) ? DefaultCapture : DefaultPlayback).c_str()}; auto iter = std::find_if(devlist->cbegin(), devlist->cend(), [defdev](const DevMap &entry) -> bool { return entry.device_name == defdev; } @@ -331,7 +326,7 @@ int OSSPlayback::mixerProc() ALCenum OSSPlayback::open(const ALCchar *name) { - const char *devname{DefaultPlayback}; + const char *devname{DefaultPlayback.c_str()}; if(!name) name = DefaultName; else @@ -548,7 +543,7 @@ int OSScapture::recordProc() ALCenum OSScapture::open(const ALCchar *name) { - const char *devname{DefaultCapture}; + const char *devname{DefaultCapture.c_str()}; if(!name) name = DefaultName; else @@ -700,8 +695,10 @@ BackendFactory &OSSBackendFactory::getFactory() bool OSSBackendFactory::init() { - ConfigValueStr(nullptr, "oss", "device", &DefaultPlayback); - ConfigValueStr(nullptr, "oss", "capture", &DefaultCapture); + if(auto devopt = ConfigValueStr(nullptr, "oss", "device")) + DefaultPlayback = std::move(*devopt); + if(auto capopt = ConfigValueStr(nullptr, "oss", "capture")) + DefaultCapture = std::move(*capopt); return true; } diff --git a/Alc/backends/solaris.cpp b/Alc/backends/solaris.cpp index 5c378bff..5c12ad72 100644 --- a/Alc/backends/solaris.cpp +++ b/Alc/backends/solaris.cpp @@ -52,7 +52,7 @@ namespace { constexpr ALCchar solaris_device[] = "Solaris Default"; -const char *solaris_driver = "/dev/audio"; +std::string solaris_driver{"/dev/audio"}; struct SolarisBackend final : public BackendBase { @@ -149,10 +149,10 @@ ALCenum SolarisBackend::open(const ALCchar *name) else if(strcmp(name, solaris_device) != 0) return ALC_INVALID_VALUE; - mFd = ::open(solaris_driver, O_WRONLY); + mFd = ::open(solaris_driver.c_str(), O_WRONLY); if(mFd == -1) { - ERR("Could not open %s: %s\n", solaris_driver, strerror(errno)); + ERR("Could not open %s: %s\n", solaris_driver.c_str(), strerror(errno)); return ALC_INVALID_VALUE; } @@ -267,7 +267,8 @@ BackendFactory &SolarisBackendFactory::getFactory() bool SolarisBackendFactory::init() { - ConfigValueStr(nullptr, "solaris", "device", &solaris_driver); + if(auto devopt = ConfigValueStr(nullptr, "solaris", "device")) + solaris_driver = std::move(*devopt); return true; } @@ -282,7 +283,7 @@ void SolarisBackendFactory::probe(DevProbe type, std::string *outnames) { #ifdef HAVE_STAT struct stat buf; - if(stat(solaris_driver, &buf) == 0) + if(stat(solaris_driver.c_str(), &buf) == 0) #endif outnames->append(solaris_device, sizeof(solaris_device)); } diff --git a/Alc/hrtf.cpp b/Alc/hrtf.cpp index 6b3edd9f..d86c4ecf 100644 --- a/Alc/hrtf.cpp +++ b/Alc/hrtf.cpp @@ -1214,9 +1214,9 @@ al::vector EnumerateHrtf(const char *devname) al::vector list; bool usedefaults{true}; - const char *pathlist{""}; - if(ConfigValueStr(devname, nullptr, "hrtf-paths", &pathlist)) + if(auto pathopt = ConfigValueStr(devname, nullptr, "hrtf-paths")) { + const char *pathlist{pathopt->c_str()}; while(pathlist && *pathlist) { const char *next, *end; @@ -1262,20 +1262,22 @@ al::vector EnumerateHrtf(const char *devname) AddBuiltInEntry(list, "Built-In 48000hz", IDR_DEFAULT_48000_MHR); } - const char *defaulthrtf{""}; - if(!list.empty() && ConfigValueStr(devname, nullptr, "default-hrtf", &defaulthrtf)) + if(!list.empty()) { - auto iter = std::find_if(list.begin(), list.end(), - [defaulthrtf](const EnumeratedHrtf &entry) -> bool - { return entry.name == defaulthrtf; } - ); - if(iter == list.end()) - WARN("Failed to find default HRTF \"%s\"\n", defaulthrtf); - else if(iter != list.begin()) + if(auto defhrtfopt = ConfigValueStr(devname, nullptr, "default-hrtf")) { - EnumeratedHrtf entry{*iter}; - list.erase(iter); - list.insert(list.begin(), entry); + auto iter = std::find_if(list.begin(), list.end(), + [&defhrtfopt](const EnumeratedHrtf &entry) -> bool + { return entry.name == *defhrtfopt; } + ); + if(iter == list.end()) + WARN("Failed to find default HRTF \"%s\"\n", defhrtfopt->c_str()); + else if(iter != list.begin()) + { + EnumeratedHrtf entry{std::move(*iter)}; + list.erase(iter); + list.insert(list.begin(), std::move(entry)); + } } } diff --git a/Alc/mixvoice.cpp b/Alc/mixvoice.cpp index 9408d096..9b0196d9 100644 --- a/Alc/mixvoice.cpp +++ b/Alc/mixvoice.cpp @@ -155,10 +155,9 @@ ResamplerFunc SelectResampler(Resampler resampler) void aluInitMixer() { - const char *str; - - if(ConfigValueStr(nullptr, nullptr, "resampler", &str)) + if(auto resopt = ConfigValueStr(nullptr, nullptr, "resampler")) { + const char *str{resopt->c_str()}; if(strcasecmp(str, "point") == 0 || strcasecmp(str, "none") == 0) ResamplerDefault = PointResampler; else if(strcasecmp(str, "linear") == 0) diff --git a/Alc/panning.cpp b/Alc/panning.cpp index 864d7f28..aa290821 100644 --- a/Alc/panning.cpp +++ b/Alc/panning.cpp @@ -553,9 +553,9 @@ void InitHrtfPanning(ALCdevice *device) */ device->mRenderMode = HrtfRender; ALsizei ambi_order{1}; - const char *mode; - if(ConfigValueStr(device->DeviceName.c_str(), nullptr, "hrtf-mode", &mode)) + if(auto modeopt = ConfigValueStr(device->DeviceName.c_str(), nullptr, "hrtf-mode")) { + const char *mode{modeopt->c_str()}; if(strcasecmp(mode, "basic") == 0) { ERR("HRTF mode \"%s\" deprecated, substituting \"%s\"\n", mode, "ambi2"); @@ -783,11 +783,10 @@ void aluInitRenderer(ALCdevice *device, ALint hrtf_id, HrtfRequestMode hrtf_appr AmbDecConf conf{}; if(layout) { - const char *fname; - if(ConfigValueStr(devname, "decoder", layout, &fname)) + if(auto decopt = ConfigValueStr(devname, "decoder", layout)) { - if(!conf.load(fname)) - ERR("Failed to load layout file %s\n", fname); + if(!conf.load(decopt->c_str())) + ERR("Failed to load layout file %s\n", decopt->c_str()); else if(conf.Speakers.size() > MAX_OUTPUT_CHANNELS) ERR("Unsupported speaker count %zu (max %d)\n", conf.Speakers.size(), MAX_OUTPUT_CHANNELS); @@ -814,9 +813,9 @@ void aluInitRenderer(ALCdevice *device, ALint hrtf_id, HrtfRequestMode hrtf_appr bool headphones{device->IsHeadphones != AL_FALSE}; if(device->Type != Loopback) { - const char *mode; - if(ConfigValueStr(device->DeviceName.c_str(), nullptr, "stereo-mode", &mode)) + if(auto modeopt = ConfigValueStr(device->DeviceName.c_str(), nullptr, "stereo-mode")) { + const char *mode{modeopt->c_str()}; if(strcasecmp(mode, "headphones") == 0) headphones = true; else if(strcasecmp(mode, "speakers") == 0) @@ -916,9 +915,9 @@ no_hrtf: } } - const char *mode; - if(ConfigValueStr(device->DeviceName.c_str(), nullptr, "stereo-encoding", &mode)) + if(auto encopt = ConfigValueStr(device->DeviceName.c_str(), nullptr, "stereo-encoding")) { + const char *mode{encopt->c_str()}; if(strcasecmp(mode, "uhj") == 0) device->mRenderMode = NormalRender; else if(strcasecmp(mode, "panpot") != 0) -- cgit v1.2.3