15 #include <type_traits> 23 template <
typename ENUMTYPE>
29 class CConfigFilePrefixer;
52 const std::string& section,
const std::string& name,
53 const std::string& str) = 0;
58 const std::string& section,
const std::string& name,
59 const std::string& str,
const int name_padding_width,
60 const int value_padding_width,
const std::string& comment);
66 const std::string& section,
const std::string& name,
67 const std::string& defaultStr,
bool failIfNotFound =
false)
const = 0;
74 virtual void getAllSections(std::vector<std::string>& sections)
const = 0;
78 const std::string& section, std::vector<std::string>& keys)
const = 0;
86 bool keyExists(
const std::string& section,
const std::string& key)
const;
102 virtual void clear() = 0;
106 typename = std::enable_if_t<std::is_enum<enum_t>::value>>
108 const std::string& section,
const std::string& name, enum_t value,
109 const int name_padding_width = -1,
const int value_padding_width = -1,
110 const std::string& comment = std::string())
114 name_padding_width, value_padding_width, comment);
123 typename = std::enable_if_t<!std::is_enum<data_t>::value>>
125 const std::string& section,
const std::string& name,
126 const data_t& value,
const int name_padding_width = -1,
127 const int value_padding_width = -1,
128 const std::string& comment = std::string())
132 value_padding_width, comment);
134 template <
typename data_t>
136 const std::string& section,
const std::string& name,
137 const std::vector<data_t>& value,
const int name_padding_width = -1,
138 const int value_padding_width = -1,
139 const std::string& comment = std::string())
142 for (
typename std::vector<data_t>::const_iterator it = value.begin();
143 it != value.end(); ++it)
149 section, name, s, name_padding_width, value_padding_width, comment);
152 const std::string& section,
const std::string& name,
double value,
153 const int name_padding_width = -1,
const int value_padding_width = -1,
154 const std::string& comment = std::string());
156 const std::string& section,
const std::string& name,
float value,
157 const int name_padding_width = -1,
const int value_padding_width = -1,
158 const std::string& comment = std::string());
166 const std::string& section,
const std::string& name,
167 double defaultValue,
bool failIfNotFound =
false)
const;
169 const std::string& section,
const std::string& name,
float defaultValue,
170 bool failIfNotFound =
false)
const;
172 const std::string& section,
const std::string& name,
bool defaultValue,
173 bool failIfNotFound =
false)
const;
175 const std::string& section,
const std::string& name,
int defaultValue,
176 bool failIfNotFound =
false)
const;
178 const std::string& section,
const std::string& name,
179 uint64_t defaultValue,
bool failIfNotFound =
false)
const;
181 const std::string& section,
const std::string& name,
182 const std::string& defaultValue,
bool failIfNotFound =
false)
const;
187 const std::string& section,
const std::string& name,
188 const std::string& defaultValue,
bool failIfNotFound =
false)
const;
193 template <
class VECTOR_TYPE>
195 const std::string& section,
const std::string& name,
196 const VECTOR_TYPE& defaultValue, VECTOR_TYPE& outValues,
197 bool failIfNotFound =
false)
const 199 std::string aux(
readString(section, name,
"", failIfNotFound));
201 std::vector<std::string> tokens;
204 if (tokens.size() == 0)
206 outValues = defaultValue;
211 const size_t N = tokens.size();
213 for (
size_t i = 0; i < N; i++)
215 double val = std::stod(tokens[i]);
217 static_cast<typename VECTOR_TYPE::value_type
>(
val);
229 template <
class MATRIX_TYPE>
231 const std::string& section,
const std::string& name,
232 MATRIX_TYPE& outMatrix,
233 const MATRIX_TYPE& defaultMatrix = MATRIX_TYPE(),
234 bool failIfNotFound =
false)
const 236 std::string aux =
readString(section, name,
"", failIfNotFound);
238 outMatrix = defaultMatrix;
242 if (!outMatrix.fromMatlabStringFormat(aux))
268 template <
typename ENUMTYPE>
270 const std::string& section,
const std::string& name,
271 const ENUMTYPE& defaultValue,
bool failIfNotFound =
false)
const 274 const std::string sVal =
276 if (sVal.empty())
return defaultValue;
278 if (::isdigit(sVal[0]))
280 return static_cast<ENUMTYPE
>(::atoi(&sVal[0]));
288 catch (std::exception&)
291 "Invalid value '%s' for enum type while reading key='%s'.",
292 sVal.c_str(), name.c_str());
306 #define MRPT_LOAD_CONFIG_VAR( \ 307 variableName, variableType, configFileObject, sectionNameStr) \ 309 variableName = configFileObject.read_##variableType( \ 310 sectionNameStr, #variableName, variableName); \ 315 #define MRPT_LOAD_CONFIG_VAR_CS(variableName, variableType) \ 316 MRPT_LOAD_CONFIG_VAR(variableName, variableType, c, s) 320 #define MRPT_LOAD_CONFIG_VAR_DEGREES( \ 321 variableName, configFileObject, sectionNameStr) \ 323 variableName = mrpt::DEG2RAD(configFileObject.read_double( \ 324 sectionNameStr, #variableName, mrpt::RAD2DEG(variableName))); \ 326 #define MRPT_LOAD_CONFIG_VAR_DEGREESf( \ 327 variableName, configFileObject, sectionNameStr) \ 329 variableName = mrpt::DEG2RAD(configFileObject.read_float( \ 330 sectionNameStr, #variableName, mrpt::RAD2DEG(variableName))); \ 335 #define MRPT_LOAD_CONFIG_VAR_DEGREES_NO_DEFAULT( \ 336 variableName, configFileObject, sectionNameStr) \ 338 variableName = mrpt::DEG2RAD(configFileObject.read_double( \ 339 sectionNameStr, #variableName, mrpt::RAD2DEG(variableName), \ 343 #define MRPT_LOAD_CONFIG_VAR_CAST( \ 344 variableName, variableType, variableTypeCast, configFileObject, \ 347 variableName = static_cast<variableTypeCast>( \ 348 configFileObject.read_##variableType( \ 349 sectionNameStr, #variableName, variableName)); \ 352 #define MRPT_LOAD_HERE_CONFIG_VAR( \ 353 variableName, variableType, targetVariable, configFileObject, \ 355 targetVariable = configFileObject.read_##variableType( \ 356 sectionNameStr, #variableName, targetVariable, false); 358 #define MRPT_LOAD_HERE_CONFIG_VAR_NO_DEFAULT( \ 359 variableName, variableType, targetVariable, configFileObject, \ 364 targetVariable = configFileObject.read_##variableType( \ 365 sectionNameStr, #variableName, targetVariable, true); \ 367 catch (std::exception&) \ 369 THROW_EXCEPTION(mrpt::format( \ 370 "Value for '%s' not found in config file in section '%s'", \ 371 static_cast<const char*>(#variableName), \ 372 std::string(sectionNameStr).c_str())); \ 376 #define MRPT_LOAD_HERE_CONFIG_VAR_DEGREES( \ 377 variableName, variableType, targetVariable, configFileObject, \ 379 targetVariable = mrpt::DEG2RAD(configFileObject.read_##variableType( \ 380 sectionNameStr, #variableName, mrpt::RAD2DEG(targetVariable), false)); 382 #define MRPT_LOAD_HERE_CONFIG_VAR_DEGREES_NO_DEFAULT( \ 383 variableName, variableType, targetVariable, configFileObject, \ 389 mrpt::DEG2RAD(configFileObject.read_##variableType( \ 390 sectionNameStr, #variableName, targetVariable, true)); \ 392 catch (std::exception&) \ 394 THROW_EXCEPTION(mrpt::format( \ 395 "Value for '%s' not found in config file in section '%s'", \ 396 static_cast<const char*>(#variableName), \ 397 std::string(sectionNameStr).c_str())); \ 401 #define MRPT_LOAD_CONFIG_VAR_NO_DEFAULT( \ 402 variableName, variableType, configFileObject, sectionNameStr) \ 406 variableName = configFileObject.read_##variableType( \ 407 sectionNameStr, #variableName, variableName, true); \ 409 catch (std::exception&) \ 411 THROW_EXCEPTION(mrpt::format( \ 412 "Value for '%s' not found in config file in section '%s'", \ 413 static_cast<const char*>(#variableName), \ 414 std::string(sectionNameStr).c_str())); \ 420 #define MRPT_LOAD_CONFIG_VAR_REQUIRED_CS(variableName, variableType) \ 421 MRPT_LOAD_CONFIG_VAR_NO_DEFAULT(variableName, variableType, c, s) 423 #define MRPT_LOAD_CONFIG_VAR_CAST_NO_DEFAULT( \ 424 variableName, variableType, variableTypeCast, configFileObject, \ 429 variableName = static_cast<variableTypeCast>( \ 430 configFileObject.read_##variableType( \ 431 sectionNameStr, #variableName, variableName, true)); \ 433 catch (std::exception&) \ 435 THROW_EXCEPTION(mrpt::format( \ 436 "Value for '%s' not found in config file in section '%s'", \ 437 static_cast<const char*>(#variableName), \ 438 std::string(sectionNameStr).c_str())); \ 442 #define MRPT_LOAD_HERE_CONFIG_VAR_CAST( \ 443 variableName, variableType, variableTypeCast, targetVariable, \ 444 configFileObject, sectionNameStr) \ 446 static_cast<variableTypeCast>(configFileObject.read_##variableType( \ 447 sectionNameStr, #variableName, targetVariable)); 449 #define MRPT_LOAD_HERE_CONFIG_VAR_CAST_NO_DEFAULT( \ 450 variableName, variableType, variableTypeCast, targetVariable, \ 451 configFileObject, sectionNameStr) \ 455 targetVariable = static_cast<variableTypeCast>( \ 456 configFileObject.read_##variableType( \ 457 sectionNameStr, #variableName, targetVariable, true)); \ 459 catch (std::exception&) \ 461 THROW_EXCEPTION(mrpt::format( \ 462 "Value for '%s' not found in config file in section '%s'", \ 463 static_cast<const char*>(#variableName), \ 464 std::string(sectionNameStr).c_str())); \ 468 #define MRPT_SAVE_CONFIG_VAR(variableName, configFileObject, sectionNameStr) \ 470 configFileObject.write(sectionNameStr, #variableName, variableName); \ 473 #define MRPT_SAVE_CONFIG_VAR_DEGREES( \ 474 variableName, configFileObject, sectionNameStr) \ 476 configFileObject.write( \ 477 sectionNameStr, #variableName, mrpt::RAD2DEG(variableName)); \ 480 #define MRPT_SAVE_CONFIG_VAR_COMMENT(variableName, __comment) \ 483 s, #variableName, variableName, \ 484 mrpt::config::MRPT_SAVE_NAME_PADDING(), \ 485 mrpt::config::MRPT_SAVE_VALUE_PADDING(), __comment); \ 487 #define MRPT_SAVE_CONFIG_VAR_DEGREES_COMMENT( \ 488 __entryName, __variable, __comment) \ 491 s, __entryName, mrpt::RAD2DEG(__variable), \ 492 mrpt::config::MRPT_SAVE_NAME_PADDING(), \ 493 mrpt::config::MRPT_SAVE_VALUE_PADDING(), __comment); \
std::string to_string(T v)
Just like std::to_string(), but with an overloaded version for std::string arguments.
virtual void getAllKeys(const std::string §ion, std::vector< std::string > &keys) const =0
Returs a list with all the keys into a section.
std::string read_string(const std::string §ion, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
int MRPT_SAVE_NAME_PADDING()
Default padding sizes for macros MRPT_SAVE_CONFIG_VAR_COMMENT(), etc.
void read_matrix(const std::string §ion, const std::string &name, MATRIX_TYPE &outMatrix, const MATRIX_TYPE &defaultMatrix=MATRIX_TYPE(), bool failIfNotFound=false) const
Reads a configuration parameter as a matrix written in a matlab-like format - for example: "[2 3 4 ; ...
void write(const std::string §ion, const std::string &name, const data_t &value, const int name_padding_width=-1, const int value_padding_width=-1, const std::string &comment=std::string())
float read_float(const std::string §ion, const std::string &name, float defaultValue, bool failIfNotFound=false) const
virtual void writeString(const std::string §ion, const std::string &name, const std::string &str)=0
A virtual method to write a generic string.
void tokenize(const std::string &inString, const std::string &inDelimiters, OUT_CONTAINER &outTokens, bool skipBlankTokens=true) noexcept
Tokenizes a string according to a set of delimiting characters.
int read_int(const std::string §ion, const std::string &name, int defaultValue, bool failIfNotFound=false) const
ENUMTYPE read_enum(const std::string §ion, const std::string &name, const ENUMTYPE &defaultValue, bool failIfNotFound=false) const
Reads an "enum" value, where the value in the config file can be either a numerical value or the symb...
This class allows loading and storing values and vectors of different types from a configuration text...
void setContentFromYAML(const std::string &yaml_block)
Changes the contents of the virtual "config file" from a text block containing a YAML configuration t...
virtual void getAllSections(std::vector< std::string > §ions) const =0
Returns a list with all the section names.
uint64_t read_uint64_t(const std::string §ion, const std::string &name, uint64_t defaultValue, bool failIfNotFound=false) const
int MRPT_SAVE_VALUE_PADDING()
std::string read_string_first_word(const std::string §ion, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
Reads a configuration parameter of type "string", and keeps only the first word (this can be used to ...
bool sectionExists(const std::string §ion_name) const
Checks if a given section exists (name is case insensitive)
A wrapper for other CConfigFileBase-based objects that prefixes a given token to every key and/or sec...
double read_double(const std::string §ion, const std::string &name, double defaultValue, bool failIfNotFound=false) const
bool keyExists(const std::string §ion, const std::string &key) const
Checks if a given key exists inside a section (case insensitive)
void write(const std::string §ion, const std::string &name, enum_t value, const int name_padding_width=-1, const int value_padding_width=-1, const std::string &comment=std::string())
virtual ~CConfigFileBase()
dtor
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
virtual void clear()=0
Empties the "config file".
bool read_bool(const std::string §ion, const std::string &name, bool defaultValue, bool failIfNotFound=false) const
std::string getContentAsYAML() const
Returns a text block representing the contents of the config file in YAML format. ...
void write(const std::string §ion, const std::string &name, const std::vector< data_t > &value, const int name_padding_width=-1, const int value_padding_width=-1, const std::string &comment=std::string())
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
void read_vector(const std::string §ion, const std::string &name, const VECTOR_TYPE &defaultValue, VECTOR_TYPE &outValues, bool failIfNotFound=false) const
Reads a configuration parameter of type vector, stored in the file as a string: "[v1 v2 v3 ...
virtual std::string readString(const std::string §ion, const std::string &name, const std::string &defaultStr, bool failIfNotFound=false) const =0
A virtual method to read a generic string.