SimpCfg:WIP:Switch to C++ Variant type - initial skeleton

This commit is contained in:
HanishKVC 2024-04-30 16:35:37 +05:30
parent 7302b3ab36
commit ee1a62c876

View file

@ -14,6 +14,8 @@
#include <vector> #include <vector>
#include <fstream> #include <fstream>
#include <regex> #include <regex>
#include <variant>
#include <sstream>
#define SC_DEBUG #define SC_DEBUG
@ -60,30 +62,33 @@ std::string str_trim_single(std::string sin, std::string trimChars=" \t\n") {
class SimpCfg { class SimpCfg {
private: private:
std::map<std::string, std::map<std::string, std::string>> mapStrings = {}; 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, bool>> mapBools = {};
std::map<std::string, std::map<std::string, int64_t>> mapInt64s = {};
std::map<std::string, std::map<std::string, double>> mapDoubles = {};
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:
void set_string(const std::string &group, const std::string &key, const std::string &value) {
auto &gm = mapStrings[group]; template<typename SupportedDataType>
void set_value(const std::string &group, const std::string &key, const SupportedDataType &value) {
auto &gm = mapV[group];
gm[key] = value; gm[key] = value;
LDBUG_LN("DBUG:SC:%s:%s:%s:%s", __func__, group.c_str(), key.c_str(), value.c_str()); std::stringstream ss;
ss << value;
LDBUG_LN("DBUG:SC:%s:%s:%s:%s", __func__, group.c_str(), key.c_str(), ss.str().c_str());
}
void set_string(const std::string &group, const std::string &key, const std::string &value) {
set_value(group, key, value);
} }
void set_bool(const std::string &group, const std::string &key, bool value) { void set_bool(const std::string &group, const std::string &key, bool value) {
auto &gm = mapBools[group]; set_value(group, key, value);
gm[key] = value;
LDBUG_LN("DBUG:SC:%s:%s:%s:%d", __func__, group.c_str(), key.c_str(), value);
} }
void set_int64(const std::string &group, const std::string &key, int64_t value) { void set_int64(const std::string &group, const std::string &key, int64_t value) {
auto &gm = mapInt64s[group]; set_value(group, key, value);
gm[key] = value;
LDBUG_LN("DBUG:SC:%s:%s:%s:%lld", __func__, group.c_str(), key.c_str(), value);
} }
void set_int64(const std::string &group, const std::string &key, std::string &value) { void set_int64(const std::string &group, const std::string &key, std::string &value) {
@ -92,9 +97,7 @@ public:
} }
void set_double(const std::string &group, const std::string &key, double value) { void set_double(const std::string &group, const std::string &key, double value) {
auto &gm = mapDoubles[group]; set_value(group, key, value);
gm[key] = value;
LDBUG_LN("DBUG:SC:%s:%s:%s:%f[%e]", __func__, group.c_str(), key.c_str(), value, value);
} }
void set_double(const std::string &group, const std::string &key, std::string &value) { void set_double(const std::string &group, const std::string &key, std::string &value) {
@ -102,68 +105,40 @@ public:
set_double(group, key, dvalue); set_double(group, key, dvalue);
} }
std::string get_string(const std::string &group, const std::string &key, const std::string &defaultValue) { template<typename SupportedDataType>
auto gm = mapStrings[group]; SupportedDataType get_value(const std::string &group, const std::string &key, const SupportedDataType &defaultValue) {
auto gm = mapV[group];
#ifdef SC_DEBUG #ifdef SC_DEBUG
for(auto k: gm) { for(auto k: gm) {
LDBUG_LN("DBUG:SC:%s:Iterate:%s:%s", __func__, k.first.c_str(), k.second.c_str()); std::stringstream ss << k.second;
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()) {
LWARN_LN("DBUG:SC:%s:%s:%s:%s[default]", __func__, group.c_str(), key.c_str(), defaultValue.c_str()); std::stringstream ss << defaultValue;
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];
LDBUG_LN("DBUG:SC:%s:%s:%s:%s", __func__, group.c_str(), key.c_str(), value.c_str()); 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; return value;
} }
std::string get_string(const std::string &group, const std::string &key, const std::string &defaultValue) {
return get_value(group, key, defaultValue);
}
bool get_bool(const std::string &group, const std::string &key, bool defaultValue) { bool get_bool(const std::string &group, const std::string &key, bool defaultValue) {
auto gm = mapBools[group]; return get_value(group, key, defaultValue);
#ifdef SC_DEBUG
for(auto k: gm) {
LDBUG_LN("DBUG:SC:%s:Iterate:%s:%d", __func__, k.first.c_str(), k.second);
}
#endif
if (gm.find(key) == gm.end()) {
LWARN_LN("DBUG:SC:%s:%s:%s:%d[default]", __func__, group.c_str(), key.c_str(), defaultValue);
return defaultValue;
}
auto value = gm[key];
LDBUG_LN("DBUG:SC:%s:%s:%s:%d", __func__, group.c_str(), key.c_str(), value);
return value;
} }
int64_t get_int64(const std::string &group, const std::string &key, int64_t defaultValue) { int64_t get_int64(const std::string &group, const std::string &key, int64_t defaultValue) {
auto gm = mapInt64s[group]; return get_value(group, key, defaultValue);
#ifdef SC_DEBUG
for(auto k: gm) {
LDBUG_LN("DBUG:SC:%s:Iterate:%s:%lld", __func__, k.first.c_str(), k.second);
}
#endif
if (gm.find(key) == gm.end()) {
LWARN_LN("DBUG:SC:%s:%s:%s:%lld[default]", __func__, group.c_str(), key.c_str(), defaultValue);
return defaultValue;
}
auto value = gm[key];
LDBUG_LN("DBUG:SC:%s:%s:%s:%lld", __func__, group.c_str(), key.c_str(), value);
return value;
} }
double get_double(const std::string &group, const std::string &key, double defaultValue) { double get_double(const std::string &group, const std::string &key, double defaultValue) {
auto gm = mapDoubles[group]; return get_value(group, key, defaultValue);
#ifdef SC_DEBUG
for(auto k: gm) {
LDBUG_LN("DBUG:SC:%s:Iterate:%s:%f[%e]", __func__, k.first.c_str(), k.second, k.second);
}
#endif
if (gm.find(key) == gm.end()) {
LWARN_LN("DBUG:SC:%s:%s:%s:%f[%e][default]", __func__, group.c_str(), key.c_str(), defaultValue, defaultValue);
return defaultValue;
}
auto value = gm[key];
LDBUG_LN("DBUG:SC:%s:%s:%s:%f[%e]", __func__, group.c_str(), key.c_str(), value, value);
return value;
} }
void load(const std::string &fname) { void load(const std::string &fname) {