From 561f50930ec76aa04bba0d46ba881e009ad9d224 Mon Sep 17 00:00:00 2001 From: HanishKVC Date: Wed, 1 May 2024 17:00:05 +0530 Subject: [PATCH] SimpCfg: initial go at adding support for spreadout arrays By having a seperate entry for each element of the array, the existing logic itself can be repurposed with minimal additions. --- common/simpcfg.hpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/common/simpcfg.hpp b/common/simpcfg.hpp index 4461dd1ba..9028bf598 100644 --- a/common/simpcfg.hpp +++ b/common/simpcfg.hpp @@ -183,6 +183,32 @@ public: return get_value(group, key, defaultValue, __func__); } + + template + std::vector get_array(const std::string &group, const std::string &key, const std::vector &defaultValue, const std::string &callerName="") { + auto gm = mapV[group]; + std::vector array; + int i = 0; + while(true) { + std::stringstream ssArrayKey; + ssArrayKey << key << "-" << i; + auto arrayKey = ssArrayKey.str(); + if (gm.find(arrayKey) == gm.end()) { + break; + } + array.push_back(std::get(gm[arrayKey])); + } + if (array.empty()) { + std::stringstream ss; + ss << defaultValue; + LWARN_LN("DBUG:SC:%s_%s:%s:%s:%s[default]", __func__, callerName.c_str(), group.c_str(), key.c_str(), ss.str().c_str()); + return defaultValue; + } + LDBUG_LN("DBUG:SC:%s_%s:%s:%s:%s", __func__, callerName.c_str(), group.c_str(), key.c_str(), to_str(value).c_str()); + return array; + } + + void load(const std::string &fname) { std::ifstream f {fname}; if (!f) { @@ -281,6 +307,11 @@ int main(int argc, char **argv) { sc.get_string("mistral", "system-prefix", "Not found"); sc.get_string("\"mistral\"", "\"system-prefix\"", "Not found"); + + sc.set_int64("testme", "keyA300-0", 330); + sc.set_int64("testme", "keyA300-1", 331); + sc.set_int64("testme", "keyA300-2", 332); + sc.get_array("testme", "keyA300", {1, 2, 3}); return 0; } #endif