From 4c9fbc250a307f65829f947a4dbca7b246585012 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucca=20Jim=C3=A9nez=20K=C3=B6nings?= Date: Fri, 17 May 2024 11:53:51 +0200 Subject: [PATCH] Change concepts in Settings.set + add test for remaining set overloads MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lucca Jiménez Könings --- include/TSettings.h | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/include/TSettings.h b/include/TSettings.h index 89adc65..e8c0e36 100644 --- a/include/TSettings.h +++ b/include/TSettings.h @@ -183,8 +183,7 @@ struct Settings { return map->at(key); } - template StringLike> - void set(Key key, const StringLike& value) { + void set(Key key, const std::string& value) { auto map = SettingsMap.synchronize(); if (!map->contains(key)) { throw std::logic_error { "Undefined setting key accessed in Settings::set(std::string)" }; @@ -194,8 +193,8 @@ struct Settings { } map->at(key) = value; } - - void set(Key key, int value) { + template T> + void set(Key key, T value) { auto map = SettingsMap.synchronize(); if (!map->contains(key)) { throw std::logic_error { "Undefined setting key accessed in Settings::set(int)" }; @@ -205,8 +204,8 @@ struct Settings { } map->at(key) = value; } - - void set(Key key, bool value) { + template T> + void set(Key key, T value) { auto map = SettingsMap.synchronize(); if (!map->contains(key)) { throw std::logic_error { "Undefined setting key accessed in Settings::set(bool)" }; @@ -216,11 +215,6 @@ struct Settings { } 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 getACLMap() const { return *InputAccessMapping; @@ -329,6 +323,8 @@ TEST_CASE("settings variant functions") { CHECK_EQ(settings.getAsString(Settings::General_Name), "hello, world"); settings.set(Settings::General_Name, std::string("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_NOTHROW(settings.set(Settings::General_Debug, false));