Remove boost::bimap with simple FindKeyByValue method

This commit is contained in:
casey langen 2022-12-04 12:35:42 -08:00
parent a2b958ba48
commit 3637d61938
3 changed files with 24 additions and 22 deletions

View File

@ -35,6 +35,7 @@
#pragma once #pragma once
#include <string> #include <string>
#include <unordered_map>
#include "Util.h" #include "Util.h"
//#define ENABLE_DEBUG 1 //#define ENABLE_DEBUG 1
@ -222,26 +223,26 @@ namespace broadcast {
static const std::string play_queue_changed = "play_queue_changed"; static const std::string play_queue_changed = "play_queue_changed";
} }
static auto PLAYBACK_STATE_TO_STRING = makeBimap<musik::core::sdk::PlaybackState, std::string>({ static auto PLAYBACK_STATE_TO_STRING = std::unordered_map<musik::core::sdk::PlaybackState, std::string>({
{ musik::core::sdk::PlaybackState::Stopped, "stopped" }, { musik::core::sdk::PlaybackState::Stopped, "stopped" },
{ musik::core::sdk::PlaybackState::Playing, "playing" }, { musik::core::sdk::PlaybackState::Playing, "playing" },
{ musik::core::sdk::PlaybackState::Prepared, "prepared" }, { musik::core::sdk::PlaybackState::Prepared, "prepared" },
{ musik::core::sdk::PlaybackState::Paused, "paused" } { musik::core::sdk::PlaybackState::Paused, "paused" }
}); });
static auto REPEAT_MODE_TO_STRING = makeBimap<musik::core::sdk::RepeatMode, std::string>({ static auto REPEAT_MODE_TO_STRING = std::unordered_map<musik::core::sdk::RepeatMode, std::string>({
{ musik::core::sdk::RepeatMode::None, "none" }, { musik::core::sdk::RepeatMode::None, "none" },
{ musik::core::sdk::RepeatMode::Track, "track" }, { musik::core::sdk::RepeatMode::Track, "track" },
{ musik::core::sdk::RepeatMode::List, "list" } { musik::core::sdk::RepeatMode::List, "list" }
}); });
static auto REPLAYGAIN_MODE_TO_STRING = makeBimap<musik::core::sdk::ReplayGainMode, std::string>({ static auto REPLAYGAIN_MODE_TO_STRING = std::unordered_map<musik::core::sdk::ReplayGainMode, std::string>({
{ musik::core::sdk::ReplayGainMode::Disabled, "disabled" }, { musik::core::sdk::ReplayGainMode::Disabled, "disabled" },
{ musik::core::sdk::ReplayGainMode::Album, "album" }, { musik::core::sdk::ReplayGainMode::Album, "album" },
{ musik::core::sdk::ReplayGainMode::Track, "track" }, { musik::core::sdk::ReplayGainMode::Track, "track" },
}); });
static auto TRANSPORT_TYPE_TO_STRING = makeBimap<musik::core::sdk::TransportType, std::string>({ static auto TRANSPORT_TYPE_TO_STRING = std::unordered_map<musik::core::sdk::TransportType, std::string>({
{ musik::core::sdk::TransportType::Gapless, "gapless" }, { musik::core::sdk::TransportType::Gapless, "gapless" },
{ musik::core::sdk::TransportType::Crossfade, "crossfade" }, { musik::core::sdk::TransportType::Crossfade, "crossfade" },
}); });

View File

@ -35,10 +35,8 @@
#pragma once #pragma once
#include <string> #include <string>
#include <algorithm>
#pragma warning(push, 0) #include <unordered_map>
#include <boost/bimap.hpp>
#pragma warning(pop)
#include <musikcore/sdk/IPreferences.h> #include <musikcore/sdk/IPreferences.h>
#include <musikcore/sdk/IValue.h> #include <musikcore/sdk/IValue.h>
@ -53,11 +51,12 @@ extern __thread char threadLocalBuffer[4096];
extern thread_local char threadLocalBuffer[4096]; extern thread_local char threadLocalBuffer[4096];
#endif #endif
template <typename L, typename R> template <typename K, typename V>
boost::bimap<L, R> typename std::unordered_map<K, V>::const_iterator
static makeBimap(std::initializer_list<typename boost::bimap<L, R>::value_type> list) { static FindKeyByValue(const std::unordered_map<K, V>& map, const V& value) {
/* http://stackoverflow.com/a/31841462 */ return std::find_if(map.begin(), map.end(), [&value](const std::pair<K, V> &p) {
return boost::bimap<L, R>(list.begin(), list.end()); return p.second == value;
});
} }
static std::string GetPreferenceString( static std::string GetPreferenceString(

View File

@ -1417,7 +1417,7 @@ void WebSocketServer::RespondWithGetGainSettings(connection_hdl connection, json
float preampGain = context.environment->GetPreampGain(); float preampGain = context.environment->GetPreampGain();
this->RespondWithOptions(connection, request, { this->RespondWithOptions(connection, request, {
{ key::replaygain_mode, REPLAYGAIN_MODE_TO_STRING.left.find(replayGainMode)->second }, { key::replaygain_mode, REPLAYGAIN_MODE_TO_STRING.find(replayGainMode)->second },
{ key::preamp_gain, preampGain } { key::preamp_gain, preampGain }
}); });
} }
@ -1429,10 +1429,11 @@ void WebSocketServer::RespondWithSetGainSettings(connection_hdl connection, json
float currentGain = context.environment->GetPreampGain(); float currentGain = context.environment->GetPreampGain();
auto currentMode = context.environment->GetReplayGainMode(); auto currentMode = context.environment->GetReplayGainMode();
auto currentModeString = REPLAYGAIN_MODE_TO_STRING.left.find(currentMode)->second; auto currentModeString = REPLAYGAIN_MODE_TO_STRING.find(currentMode)->second;
ReplayGainMode newMode = REPLAYGAIN_MODE_TO_STRING.right.find( ReplayGainMode newMode = FindKeyByValue<musik::core::sdk::ReplayGainMode, std::string>(
options.value(key::replaygain_mode, currentModeString))->second; REPLAYGAIN_MODE_TO_STRING,
options.value(key::replaygain_mode, currentModeString))->first;
float newGain = options.value(key::preamp_gain, currentGain); float newGain = options.value(key::preamp_gain, currentGain);
@ -1494,20 +1495,21 @@ void WebSocketServer::RespondWithSetEqualizerSettings(connection_hdl connection,
void WebSocketServer::RespondWithGetTransportType(connection_hdl connection, json& request) { void WebSocketServer::RespondWithGetTransportType(connection_hdl connection, json& request) {
auto type = context.environment->GetTransportType(); auto type = context.environment->GetTransportType();
this->RespondWithOptions(connection, request, { this->RespondWithOptions(connection, request, {
{ key::type, TRANSPORT_TYPE_TO_STRING.left.find(type)->second } { key::type, TRANSPORT_TYPE_TO_STRING.find(type)->second }
}); });
} }
void WebSocketServer::RespondWithSetTransportType(connection_hdl connection, json& request) { void WebSocketServer::RespondWithSetTransportType(connection_hdl connection, json& request) {
auto& options = request[message::options]; auto& options = request[message::options];
std::string currentType = TRANSPORT_TYPE_TO_STRING.left std::string currentType = TRANSPORT_TYPE_TO_STRING
.find(context.environment->GetTransportType())->second; .find(context.environment->GetTransportType())->second;
auto newType = options.value(key::type, currentType); auto newType = options.value(key::type, currentType);
if (currentType != newType) { if (currentType != newType) {
auto enumType = TRANSPORT_TYPE_TO_STRING.right.find(newType)->second; auto enumType = FindKeyByValue<musik::core::sdk::TransportType, std::string>(
TRANSPORT_TYPE_TO_STRING, newType)->first;
context.environment->SetTransportType(enumType); context.environment->SetTransportType(enumType);
} }
@ -1607,8 +1609,8 @@ json WebSocketServer::WebSocketServer::ReadTrackMetadata(ITrack* track) {
} }
void WebSocketServer::BuildPlaybackOverview(json& options) { void WebSocketServer::BuildPlaybackOverview(json& options) {
options[key::state] = PLAYBACK_STATE_TO_STRING.left.find(context.playback->GetPlaybackState())->second; options[key::state] = PLAYBACK_STATE_TO_STRING.find(context.playback->GetPlaybackState())->second;
options[key::repeat_mode] = REPEAT_MODE_TO_STRING.left.find(context.playback->GetRepeatMode())->second; options[key::repeat_mode] = REPEAT_MODE_TO_STRING.find(context.playback->GetRepeatMode())->second;
options[key::volume] = context.playback->GetVolume(); options[key::volume] = context.playback->GetVolume();
options[key::shuffled] = context.playback->IsShuffled(); options[key::shuffled] = context.playback->IsShuffled();
options[key::muted] = context.playback->IsMuted(); options[key::muted] = context.playback->IsMuted();