diff --git a/src/plugins/server/Constants.h b/src/plugins/server/Constants.h index 5f5c45b38..60847d5be 100644 --- a/src/plugins/server/Constants.h +++ b/src/plugins/server/Constants.h @@ -35,6 +35,7 @@ #pragma once #include +#include #include "Util.h" //#define ENABLE_DEBUG 1 @@ -222,26 +223,26 @@ namespace broadcast { static const std::string play_queue_changed = "play_queue_changed"; } -static auto PLAYBACK_STATE_TO_STRING = makeBimap({ +static auto PLAYBACK_STATE_TO_STRING = std::unordered_map({ { musik::core::sdk::PlaybackState::Stopped, "stopped" }, { musik::core::sdk::PlaybackState::Playing, "playing" }, { musik::core::sdk::PlaybackState::Prepared, "prepared" }, { musik::core::sdk::PlaybackState::Paused, "paused" } }); -static auto REPEAT_MODE_TO_STRING = makeBimap({ +static auto REPEAT_MODE_TO_STRING = std::unordered_map({ { musik::core::sdk::RepeatMode::None, "none" }, { musik::core::sdk::RepeatMode::Track, "track" }, { musik::core::sdk::RepeatMode::List, "list" } }); -static auto REPLAYGAIN_MODE_TO_STRING = makeBimap({ +static auto REPLAYGAIN_MODE_TO_STRING = std::unordered_map({ { musik::core::sdk::ReplayGainMode::Disabled, "disabled" }, { musik::core::sdk::ReplayGainMode::Album, "album" }, { musik::core::sdk::ReplayGainMode::Track, "track" }, }); -static auto TRANSPORT_TYPE_TO_STRING = makeBimap({ +static auto TRANSPORT_TYPE_TO_STRING = std::unordered_map({ { musik::core::sdk::TransportType::Gapless, "gapless" }, { musik::core::sdk::TransportType::Crossfade, "crossfade" }, }); diff --git a/src/plugins/server/Util.h b/src/plugins/server/Util.h index 99f77b97d..000754f68 100644 --- a/src/plugins/server/Util.h +++ b/src/plugins/server/Util.h @@ -35,10 +35,8 @@ #pragma once #include - -#pragma warning(push, 0) -#include -#pragma warning(pop) +#include +#include #include #include @@ -53,11 +51,12 @@ extern __thread char threadLocalBuffer[4096]; extern thread_local char threadLocalBuffer[4096]; #endif -template -boost::bimap -static makeBimap(std::initializer_list::value_type> list) { - /* http://stackoverflow.com/a/31841462 */ - return boost::bimap(list.begin(), list.end()); +template +typename std::unordered_map::const_iterator +static FindKeyByValue(const std::unordered_map& map, const V& value) { + return std::find_if(map.begin(), map.end(), [&value](const std::pair &p) { + return p.second == value; + }); } static std::string GetPreferenceString( diff --git a/src/plugins/server/WebSocketServer.cpp b/src/plugins/server/WebSocketServer.cpp index 26ada77f6..34c6f6a08 100644 --- a/src/plugins/server/WebSocketServer.cpp +++ b/src/plugins/server/WebSocketServer.cpp @@ -1417,7 +1417,7 @@ void WebSocketServer::RespondWithGetGainSettings(connection_hdl connection, json float preampGain = context.environment->GetPreampGain(); 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 } }); } @@ -1429,10 +1429,11 @@ void WebSocketServer::RespondWithSetGainSettings(connection_hdl connection, json float currentGain = context.environment->GetPreampGain(); 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( - options.value(key::replaygain_mode, currentModeString))->second; + ReplayGainMode newMode = FindKeyByValue( + REPLAYGAIN_MODE_TO_STRING, + options.value(key::replaygain_mode, currentModeString))->first; 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) { auto type = context.environment->GetTransportType(); 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) { 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; auto newType = options.value(key::type, currentType); if (currentType != newType) { - auto enumType = TRANSPORT_TYPE_TO_STRING.right.find(newType)->second; + auto enumType = FindKeyByValue( + TRANSPORT_TYPE_TO_STRING, newType)->first; context.environment->SetTransportType(enumType); } @@ -1607,8 +1609,8 @@ json WebSocketServer::WebSocketServer::ReadTrackMetadata(ITrack* track) { } void WebSocketServer::BuildPlaybackOverview(json& options) { - options[key::state] = PLAYBACK_STATE_TO_STRING.left.find(context.playback->GetPlaybackState())->second; - options[key::repeat_mode] = REPEAT_MODE_TO_STRING.left.find(context.playback->GetRepeatMode())->second; + options[key::state] = PLAYBACK_STATE_TO_STRING.find(context.playback->GetPlaybackState())->second; + options[key::repeat_mode] = REPEAT_MODE_TO_STRING.find(context.playback->GetRepeatMode())->second; options[key::volume] = context.playback->GetVolume(); options[key::shuffled] = context.playback->IsShuffled(); options[key::muted] = context.playback->IsMuted();