Change concepts in Settings.set + add test for remaining set overloads

Signed-off-by: Lucca Jiménez Könings <development@jimkoen.com>
This commit is contained in:
Lucca Jiménez Könings 2024-05-17 11:53:51 +02:00
parent 31ce0cc7de
commit 4c9fbc250a
No known key found for this signature in database

View File

@ -183,8 +183,7 @@ struct Settings {
return map->at(key); return map->at(key);
} }
template <std::constructible_from<std::string> StringLike> void set(Key key, const std::string& value) {
void set(Key key, const StringLike& value) {
auto map = SettingsMap.synchronize(); auto map = SettingsMap.synchronize();
if (!map->contains(key)) { if (!map->contains(key)) {
throw std::logic_error { "Undefined setting key accessed in Settings::set(std::string)" }; throw std::logic_error { "Undefined setting key accessed in Settings::set(std::string)" };
@ -194,8 +193,8 @@ struct Settings {
} }
map->at(key) = value; map->at(key) = value;
} }
template<std::same_as<int> T>
void set(Key key, int value) { void set(Key key, T value) {
auto map = SettingsMap.synchronize(); auto map = SettingsMap.synchronize();
if (!map->contains(key)) { if (!map->contains(key)) {
throw std::logic_error { "Undefined setting key accessed in Settings::set(int)" }; throw std::logic_error { "Undefined setting key accessed in Settings::set(int)" };
@ -205,8 +204,8 @@ struct Settings {
} }
map->at(key) = value; map->at(key) = value;
} }
template<std::same_as<bool> T>
void set(Key key, bool value) { void set(Key key, T value) {
auto map = SettingsMap.synchronize(); auto map = SettingsMap.synchronize();
if (!map->contains(key)) { if (!map->contains(key)) {
throw std::logic_error { "Undefined setting key accessed in Settings::set(bool)" }; throw std::logic_error { "Undefined setting key accessed in Settings::set(bool)" };
@ -216,11 +215,6 @@ struct Settings {
} }
map->at(key) = value; map->at(key) = value;
} }
// Additional set overload for const char*, to avoid implicit conversions when set is
// invoked with string literals rather than std::strings
void set(Key key, const char* value) {
set(key, std::string(value));
}
const std::unordered_map<ComposedKey, SettingsAccessControl> getACLMap() const { const std::unordered_map<ComposedKey, SettingsAccessControl> getACLMap() const {
return *InputAccessMapping; return *InputAccessMapping;
@ -329,6 +323,8 @@ TEST_CASE("settings variant functions") {
CHECK_EQ(settings.getAsString(Settings::General_Name), "hello, world"); CHECK_EQ(settings.getAsString(Settings::General_Name), "hello, world");
settings.set(Settings::General_Name, std::string("hello, world")); settings.set(Settings::General_Name, std::string("hello, world"));
CHECK_EQ(settings.getAsString(Settings::General_Name), "hello, world"); CHECK_EQ(settings.getAsString(Settings::General_Name), "hello, world");
settings.set(Settings::General_MaxPlayers, 12);
CHECK_EQ(settings.getAsInt(Settings::General_MaxPlayers), 12);
CHECK_THROWS(settings.set(Settings::General_Debug, "hello, world")); CHECK_THROWS(settings.set(Settings::General_Debug, "hello, world"));
CHECK_NOTHROW(settings.set(Settings::General_Debug, false)); CHECK_NOTHROW(settings.set(Settings::General_Debug, false));