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
#include <string>
#include <unordered_map>
#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<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::Playing, "playing" },
{ musik::core::sdk::PlaybackState::Prepared, "prepared" },
{ 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::Track, "track" },
{ 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::Album, "album" },
{ 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::Crossfade, "crossfade" },
});

View File

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

View File

@ -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<musik::core::sdk::ReplayGainMode, std::string>(
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<musik::core::sdk::TransportType, std::string>(
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();