aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2018-11-01 11:44:11 -0700
committerChris Robinson <[email protected]>2018-11-01 11:44:11 -0700
commitcd68530ab4d340d61f524ea2d03176ed775e4e68 (patch)
treedde40c739a5e46bba3aaa60fb46fe1b98f16be07
parent5d092a1c588eb3369e9e488b5fc608ce4288aacb (diff)
Simplify a couple loops
-rw-r--r--Alc/backends/pulseaudio.cpp76
1 files changed, 36 insertions, 40 deletions
diff --git a/Alc/backends/pulseaudio.cpp b/Alc/backends/pulseaudio.cpp
index b0c64d26..a6f6564d 100644
--- a/Alc/backends/pulseaudio.cpp
+++ b/Alc/backends/pulseaudio.cpp
@@ -509,6 +509,10 @@ static void pulse_close(pa_threaded_mainloop *loop, pa_context *context, pa_stre
struct DevMap {
std::string name;
std::string device_name;
+
+ DevMap(std::string name_, std::string devname_)
+ : name{std::move(name_)}, device_name{std::move(devname_)}
+ { }
};
static std::vector<DevMap> PlaybackDevices;
@@ -595,34 +599,30 @@ static void PulsePlayback_deviceCallback(pa_context *UNUSED(context), const pa_s
return;
}
+ /* Skip this device is if it's already in the list. */
if(std::find_if(PlaybackDevices.cbegin(), PlaybackDevices.cend(),
[info](const DevMap &entry) -> bool
{ return entry.device_name == info->name; }
) != PlaybackDevices.cend())
return;
- PlaybackDevices.emplace_back();
+ /* Make sure the display name (description) is unique. Append a number
+ * counter as needed.
+ */
+ int count{1};
+ std::string newname{info->description};
+ while(std::find_if(PlaybackDevices.cbegin(), PlaybackDevices.cend(),
+ [&newname](const DevMap &entry) -> bool
+ { return entry.name == newname; }
+ ) != PlaybackDevices.cend())
+ {
+ newname = info->description;
+ newname += " #";
+ newname += std::to_string(++count);
+ }
+ PlaybackDevices.emplace_back(std::move(newname), info->name);
DevMap &newentry = PlaybackDevices.back();
- int count{0};
- while(1)
- {
- newentry.name = info->description;
- if(count != 0)
- {
- newentry.name += " #";
- newentry.name += std::to_string(count+1);
- }
-
- if(std::find_if(PlaybackDevices.cbegin(), PlaybackDevices.cend()-1,
- [&newentry](const DevMap &entry) -> bool
- { return entry.name == newentry.name; }
- ) == PlaybackDevices.cend()-1)
- break;
- count++;
- }
- newentry.device_name = info->name;
-
TRACE("Got device \"%s\", \"%s\"\n", newentry.name.c_str(), newentry.device_name.c_str());
}
@@ -1310,34 +1310,30 @@ static void PulseCapture_deviceCallback(pa_context *UNUSED(context), const pa_so
return;
}
+ /* Skip this device is if it's already in the list. */
if(std::find_if(CaptureDevices.cbegin(), CaptureDevices.cend(),
[info](const DevMap &entry) -> bool
{ return entry.device_name == info->name; }
) != CaptureDevices.cend())
return;
- CaptureDevices.emplace_back();
+ /* Make sure the display name (description) is unique. Append a number
+ * counter as needed.
+ */
+ int count{1};
+ std::string newname{info->description};
+ while(std::find_if(CaptureDevices.cbegin(), CaptureDevices.cend(),
+ [&newname](const DevMap &entry) -> bool
+ { return entry.name == newname; }
+ ) != CaptureDevices.cend())
+ {
+ newname = info->description;
+ newname += " #";
+ newname += std::to_string(++count);
+ }
+ CaptureDevices.emplace_back(std::move(newname), std::string{info->name});
DevMap &newentry = CaptureDevices.back();
- int count{0};
- while(1)
- {
- newentry.name = info->description;
- if(count != 0)
- {
- newentry.name += " #";
- newentry.name += std::to_string(count+1);
- }
-
- if(std::find_if(CaptureDevices.cbegin(), CaptureDevices.cend()-1,
- [&newentry](const DevMap &entry) -> bool
- { return entry.name == newentry.name; }
- ) == CaptureDevices.cend()-1)
- break;
- count++;
- }
- newentry.device_name = info->name;
-
TRACE("Got device \"%s\", \"%s\"\n", newentry.name.c_str(), newentry.device_name.c_str());
}