3 #ifndef _UTILS_CONFIGMAP_HPP_
4 #define _UTILS_CONFIGMAP_HPP_
13 #include <Utils/IntelliLog.h>
32 std::map<std::string, uint64_t> u64Map;
33 std::map<std::string, int64_t> i64Map;
34 std::map<std::string, double> doubleMap;
35 std::map<std::string, std::string> strMap;
37 void spilt(
const std::string s,
const std::string &c, vector<std::string> &v) {
38 std::string::size_type pos1, pos2;
41 while (std::string::npos != pos2) {
42 v.push_back(s.substr(pos1, pos2 - pos1));
44 pos1 = pos2 + c.size();
45 pos2 = s.find(c, pos1);
47 if (pos1 != s.length())
48 v.push_back(s.substr(pos1));
58 void edit(std::string key, uint64_t value) {
66 void edit(std::string key, int64_t value) {
74 void edit(std::string key,
double value) {
75 doubleMap[key] = value;
82 void edit(std::string key, std::string value) {
91 return (u64Map.count(key) == 1);
99 return (i64Map.count(key) == 1);
107 return (doubleMap.count(key) == 1);
115 return (strMap.count(key) == 1);
123 return existU64(key) || existI64(key) || existDouble(key) || existString(key);
132 return u64Map.at(key);
141 return i64Map.at(key);
150 return doubleMap.at(key);
159 return strMap.at(key);
167 std::string
toString(std::string separator =
"\t", std::string newLine =
"\n") {
168 std::string str =
"key" + separator +
"value" + separator +
"type" + newLine;
169 for (
auto &iter : u64Map) {
170 std::string col = iter.first + separator + to_string(iter.second) + separator +
"U64" + newLine;
173 for (
auto &iter : i64Map) {
174 std::string col = iter.first + separator + to_string(iter.second) + separator +
"I64" + newLine;
177 for (
auto &iter : doubleMap) {
178 std::string col = iter.first + separator + to_string(iter.second) + separator +
"Double" + newLine;
181 for (
auto &iter : strMap) {
182 std::string col = iter.first + separator + (iter.second) + separator +
"String" + newLine;
192 for (
auto &iter : u64Map) {
193 dest.
edit(iter.first, (uint64_t) iter.second);
195 for (
auto &iter : i64Map) {
196 dest.
edit(iter.first, (int64_t) iter.second);
198 for (
auto &iter : doubleMap) {
199 dest.
edit(iter.first, (
double) iter.second);
201 for (
auto &iter : strMap) {
202 dest.
edit(iter.first, (std::string) iter.second);
212 bool toFile(std::string fname, std::string separator =
",", std::string newLine =
"\n") {
218 of << toString(separator, newLine);
229 bool fromFile(std::string fname, std::string separator =
",", std::string newLine =
"\n") {
232 assert(separator.data());
233 assert(newLine.data());
239 while (std::getline(ins, readStr, newLine.data()[0])) {
240 vector<std::string> cols;
242 spilt(readStr, separator, cols);
244 if (cols.size() >= 3) {
245 istringstream iss(cols[1]);
246 if (cols[2] ==
"U64") {
249 edit(cols[0], (uint64_t) value);
250 }
else if (cols[2] ==
"I64") {
253 edit(cols[0], (int64_t) value);
254 }
else if (cols[2] ==
"Double") {
257 edit(cols[0], (
double) value);
258 }
else if (cols[2] ==
"String") {
259 edit(cols[0], (std::string) cols[1]);
275 int64_t
tryI64(
string key, int64_t defaultValue = 0,
bool showWarning =
false) {
276 int64_t ru = defaultValue;
277 if (this->existI64(key)) {
278 ru = this->getI64(key);
282 INTELLI_WARNING(
"Leaving " + key +
" as blank, will use " + to_string(defaultValue) +
" instead");
295 uint64_t
tryU64(
string key, uint64_t defaultValue = 0,
bool showWarning =
false) {
296 uint64_t ru = defaultValue;
297 if (this->existU64(key)) {
298 ru = this->getU64(key);
302 INTELLI_WARNING(
"Leaving " + key +
" as blank, will use " + to_string(defaultValue) +
" instead");
316 double tryDouble(
string key,
double defaultValue = 0,
bool showWarning =
false) {
317 double ru = defaultValue;
318 if (this->existDouble(key)) {
319 ru = this->getDouble(key);
323 INTELLI_WARNING(
"Leaving " + key +
" as blank, will use " + to_string(defaultValue) +
" instead");
336 string tryString(
string key,
string defaultValue =
"",
bool showWarning =
false) {
337 string ru = defaultValue;
338 if (this->existString(key)) {
339 ru = this->getString(key);
343 INTELLI_WARNING(
"Leaving " + key +
" as blank, will use " + (defaultValue) +
" instead");
362 #define newConfigMap make_shared<INTELLI::ConfigMap>
The unified map structure to store configurations in a key-value style.
Definition: ConfigMap.hpp:30
std::shared_ptr< ConfigMap > ConfigMapPtr
The class to describe a shared pointer to ConfigMap.
Definition: ConfigMap.hpp:356
void edit(std::string key, std::string value)
Edit the config map. If not exit the config, will create new, or will overwrite.
Definition: ConfigMap.hpp:82
std::string toString(std::string separator="\t", std::string newLine="\n")
convert the whole map to std::string and retuen
Definition: ConfigMap.hpp:167
bool fromFile(std::string fname, std::string separator=",", std::string newLine="\n")
update the whole map from file
Definition: ConfigMap.hpp:229
bool existI64(std::string key)
To detect whether the key exists and related to a I64.
Definition: ConfigMap.hpp:98
void edit(std::string key, int64_t value)
Edit the config map. If not exit the config, will create new, or will overwrite.
Definition: ConfigMap.hpp:66
bool toFile(std::string fname, std::string separator=",", std::string newLine="\n")
convert the whole map to file
Definition: ConfigMap.hpp:212
std::string getString(std::string key)
To get a std::string value by key.
Definition: ConfigMap.hpp:158
double getDouble(std::string key)
To get a double value by key.
Definition: ConfigMap.hpp:149
void cloneInto(ConfigMap &dest)
clone this config into destination
Definition: ConfigMap.hpp:191
int64_t tryI64(string key, int64_t defaultValue=0, bool showWarning=false)
Try to get an I64 from config map, if not exist, use default value instead.
Definition: ConfigMap.hpp:275
void edit(std::string key, double value)
Edit the config map. If not exit the config, will create new, or will overwrite.
Definition: ConfigMap.hpp:74
uint64_t getU64(std::string key)
To get a U64 value by key.
Definition: ConfigMap.hpp:131
bool existU64(std::string key)
To detect whether the key exists and related to a U64.
Definition: ConfigMap.hpp:90
int64_t getI64(std::string key)
To get a I64 value by key.
Definition: ConfigMap.hpp:140
uint64_t tryU64(string key, uint64_t defaultValue=0, bool showWarning=false)
Try to get an U64 from config map, if not exist, use default value instead.
Definition: ConfigMap.hpp:295
bool existDouble(std::string key)
To detect whether the key exists and related to a double.
Definition: ConfigMap.hpp:106
bool existString(std::string key)
To detect whether the key exists and related to a std::string.
Definition: ConfigMap.hpp:114
double tryDouble(string key, double defaultValue=0, bool showWarning=false)
Try to get a double from config map, if not exist, use default value instead.
Definition: ConfigMap.hpp:316
string tryString(string key, string defaultValue="", bool showWarning=false)
Try to get an String from config map, if not exist, use default value instead.
Definition: ConfigMap.hpp:336
bool exist(std::string key)
To detect whether the key exists.
Definition: ConfigMap.hpp:122
void edit(std::string key, uint64_t value)
Edit the config map. If not exit the config, will create new, or will overwrite.
Definition: ConfigMap.hpp:58