SimpCfg:WIP:Variant TypeDef, to_str and std::get

Cleanup the use of the variant

Initialize and << op stringstream seperately.
This commit is contained in:
HanishKVC 2024-04-30 17:03:52 +05:30
parent ee1a62c876
commit 1dc7fd0e85

View file

@ -61,15 +61,26 @@ std::string str_trim_single(std::string sin, std::string trimChars=" \t\n") {
} }
typedef std::variant<std::string, bool, int64_t, double> SimpCfgData;
class SimpCfg { class SimpCfg {
private: private:
std::map<std::string, std::map<std::string, std::variant<std::string, bool, int64_t, double>>> mapV = {}; std::map<std::string, std::map<std::string, SimpCfgData>> mapV = {};
std::regex rInt {R"(^[-+]?\d+$)"}; std::regex rInt {R"(^[-+]?\d+$)"};
std::regex rFloat {R"(^[-+]?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?$)"}; std::regex rFloat {R"(^[-+]?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?$)"};
public: 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<typename SupportedDataType> template<typename SupportedDataType>
void set_value(const std::string &group, const std::string &key, const SupportedDataType &value) { void set_value(const std::string &group, const std::string &key, const SupportedDataType &value) {
auto &gm = mapV[group]; auto &gm = mapV[group];
@ -110,19 +121,18 @@ public:
auto gm = mapV[group]; auto gm = mapV[group];
#ifdef SC_DEBUG #ifdef SC_DEBUG
for(auto k: gm) { for(auto k: gm) {
std::stringstream ss << k.second; LDBUG_LN("DBUG:SC:%s:Iterate:%s:%s", __func__, k.first.c_str(), to_str(k.second).c_str());
LDBUG_LN("DBUG:SC:%s:Iterate:%s:%s", __func__, k.first.c_str(), ss.str().c_str());
} }
#endif #endif
if (gm.find(key) == gm.end()) { 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()); LWARN_LN("DBUG:SC:%s:%s:%s:%s[default]", __func__, group.c_str(), key.c_str(), ss.str().c_str());
return defaultValue; return defaultValue;
} }
auto value = gm[key]; auto value = gm[key];
std::stringstream ss << defaultValue; LDBUG_LN("DBUG:SC:%s:%s:%s:%s", __func__, group.c_str(), key.c_str(), to_str(value).c_str());
LDBUG_LN("DBUG:SC:%s:%s:%s:%s", __func__, group.c_str(), key.c_str(), ss.str().c_str()); return std::get<SupportedDataType>(value);
return value;
} }
std::string get_string(const std::string &group, const std::string &key, const std::string &defaultValue) { std::string get_string(const std::string &group, const std::string &key, const std::string &defaultValue) {