From 1dc7fd0e85ee8ee2a6a96125f5f52313f7a5c492 Mon Sep 17 00:00:00 2001 From: HanishKVC Date: Tue, 30 Apr 2024 17:03:52 +0530 Subject: [PATCH] SimpCfg:WIP:Variant TypeDef, to_str and std::get Cleanup the use of the variant Initialize and << op stringstream seperately. --- common/simpcfg.hpp | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/common/simpcfg.hpp b/common/simpcfg.hpp index abe75c99a..07985ba86 100644 --- a/common/simpcfg.hpp +++ b/common/simpcfg.hpp @@ -61,15 +61,26 @@ std::string str_trim_single(std::string sin, std::string trimChars=" \t\n") { } +typedef std::variant SimpCfgData; + class SimpCfg { private: - std::map>> mapV = {}; + std::map> mapV = {}; std::regex rInt {R"(^[-+]?\d+$)"}; std::regex rFloat {R"(^[-+]?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?$)"}; public: + std::string to_str(const SimpCfgData &value) { + auto visitor = [](auto value) { + std::stringstream ss; + ss << value; + return ss.str(); + }; + return std::visit(visitor, value); + } + template void set_value(const std::string &group, const std::string &key, const SupportedDataType &value) { auto &gm = mapV[group]; @@ -110,19 +121,18 @@ public: auto gm = mapV[group]; #ifdef SC_DEBUG for(auto k: gm) { - std::stringstream ss << k.second; - LDBUG_LN("DBUG:SC:%s:Iterate:%s:%s", __func__, k.first.c_str(), ss.str().c_str()); + LDBUG_LN("DBUG:SC:%s:Iterate:%s:%s", __func__, k.first.c_str(), to_str(k.second).c_str()); } #endif if (gm.find(key) == gm.end()) { - std::stringstream ss << defaultValue; + std::stringstream ss; + ss << defaultValue; LWARN_LN("DBUG:SC:%s:%s:%s:%s[default]", __func__, group.c_str(), key.c_str(), ss.str().c_str()); return defaultValue; } auto value = gm[key]; - std::stringstream ss << defaultValue; - LDBUG_LN("DBUG:SC:%s:%s:%s:%s", __func__, group.c_str(), key.c_str(), ss.str().c_str()); - return value; + LDBUG_LN("DBUG:SC:%s:%s:%s:%s", __func__, group.c_str(), key.c_str(), to_str(value).c_str()); + return std::get(value); } std::string get_string(const std::string &group, const std::string &key, const std::string &defaultValue) {