15 #include <type_traits> 23 template <
typename ENUMTYPE>
29 class CConfigFilePrefixer;
59 const std::string& str,
const int name_padding_width,
60 const int value_padding_width,
const std::string& comment);
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;
97 virtual void clear() = 0;
104 const int name_padding_width = -1,
const int value_padding_width = -1,
109 name_padding_width, value_padding_width, comment);
121 const data_t&
value,
const int name_padding_width = -1,
122 const int value_padding_width = -1,
127 value_padding_width, comment);
129 template <
typename data_t>
132 const std::vector<data_t>&
value,
const int name_padding_width = -1,
133 const int value_padding_width = -1,
137 for (
typename std::vector<data_t>::const_iterator it =
value.begin();
138 it !=
value.end(); ++it)
144 section,
name,
s, name_padding_width, value_padding_width, comment);
148 const int name_padding_width = -1,
const int value_padding_width = -1,
152 const int name_padding_width = -1,
const int value_padding_width = -1,
162 double defaultValue,
bool failIfNotFound =
false)
const;
165 bool failIfNotFound =
false)
const;
168 bool failIfNotFound =
false)
const;
171 bool failIfNotFound =
false)
const;
174 uint64_t defaultValue,
bool failIfNotFound =
false)
const;
177 const std::string& defaultValue,
bool failIfNotFound =
false)
const;
183 const std::string& defaultValue,
bool failIfNotFound =
false)
const;
188 template <
class VECTOR_TYPE>
191 const VECTOR_TYPE& defaultValue, VECTOR_TYPE& outValues,
192 bool failIfNotFound =
false)
const 196 std::vector<std::string> tokens;
199 if (tokens.size() == 0)
201 outValues = defaultValue;
206 const size_t N = tokens.size();
208 for (
size_t i = 0; i < N; i++)
210 double val = std::stod(tokens[i]);
223 template <
class MATRIX_TYPE>
226 MATRIX_TYPE& outMatrix,
227 const MATRIX_TYPE& defaultMatrix = MATRIX_TYPE(),
228 bool failIfNotFound =
false)
const 232 outMatrix = defaultMatrix;
236 if (!outMatrix.fromMatlabStringFormat(aux))
262 template <
typename ENUMTYPE>
265 const ENUMTYPE& defaultValue,
bool failIfNotFound =
false)
const 270 if (sVal.empty())
return defaultValue;
272 if (::isdigit(sVal[0]))
274 return static_cast<ENUMTYPE
>(::atoi(&sVal[0]));
282 catch (std::exception&)
285 "Invalid value '%s' for enum type while reading key='%s'.",
286 sVal.c_str(),
name.c_str());
300 #define MRPT_LOAD_CONFIG_VAR( \ 301 variableName, variableType, configFileObject, sectionNameStr) \ 303 variableName = configFileObject.read_##variableType( \ 304 sectionNameStr, #variableName, variableName); \ 309 #define MRPT_LOAD_CONFIG_VAR_CS(variableName, variableType) \ 310 MRPT_LOAD_CONFIG_VAR(variableName, variableType, c, s) 314 #define MRPT_LOAD_CONFIG_VAR_DEGREES( \ 315 variableName, configFileObject, sectionNameStr) \ 317 variableName = mrpt::DEG2RAD(configFileObject.read_double( \ 318 sectionNameStr, #variableName, mrpt::RAD2DEG(variableName))); \ 323 #define MRPT_LOAD_CONFIG_VAR_DEGREES_NO_DEFAULT( \ 324 variableName, configFileObject, sectionNameStr) \ 326 variableName = mrpt::DEG2RAD(configFileObject.read_double( \ 327 sectionNameStr, #variableName, mrpt::RAD2DEG(variableName), \ 331 #define MRPT_LOAD_CONFIG_VAR_CAST( \ 332 variableName, variableType, variableTypeCast, configFileObject, \ 335 variableName = static_cast<variableTypeCast>( \ 336 configFileObject.read_##variableType( \ 337 sectionNameStr, #variableName, variableName)); \ 340 #define MRPT_LOAD_HERE_CONFIG_VAR( \ 341 variableName, variableType, targetVariable, configFileObject, \ 343 targetVariable = configFileObject.read_##variableType( \ 344 sectionNameStr, #variableName, targetVariable, false); 346 #define MRPT_LOAD_HERE_CONFIG_VAR_NO_DEFAULT( \ 347 variableName, variableType, targetVariable, configFileObject, \ 352 targetVariable = configFileObject.read_##variableType( \ 353 sectionNameStr, #variableName, targetVariable, true); \ 355 catch (std::exception&) \ 357 THROW_EXCEPTION(mrpt::format( \ 358 "Value for '%s' not found in config file in section '%s'", \ 359 static_cast<const char*>(#variableName), \ 360 std::string(sectionNameStr).c_str())); \ 364 #define MRPT_LOAD_HERE_CONFIG_VAR_DEGREES( \ 365 variableName, variableType, targetVariable, configFileObject, \ 367 targetVariable = mrpt::DEG2RAD(configFileObject.read_##variableType( \ 368 sectionNameStr, #variableName, mrpt::RAD2DEG(targetVariable), false)); 370 #define MRPT_LOAD_HERE_CONFIG_VAR_DEGREES_NO_DEFAULT( \ 371 variableName, variableType, targetVariable, configFileObject, \ 377 mrpt::DEG2RAD(configFileObject.read_##variableType( \ 378 sectionNameStr, #variableName, targetVariable, true)); \ 380 catch (std::exception&) \ 382 THROW_EXCEPTION(mrpt::format( \ 383 "Value for '%s' not found in config file in section '%s'", \ 384 static_cast<const char*>(#variableName), \ 385 std::string(sectionNameStr).c_str())); \ 389 #define MRPT_LOAD_CONFIG_VAR_NO_DEFAULT( \ 390 variableName, variableType, configFileObject, sectionNameStr) \ 394 variableName = configFileObject.read_##variableType( \ 395 sectionNameStr, #variableName, variableName, true); \ 397 catch (std::exception&) \ 399 THROW_EXCEPTION(mrpt::format( \ 400 "Value for '%s' not found in config file in section '%s'", \ 401 static_cast<const char*>(#variableName), \ 402 std::string(sectionNameStr).c_str())); \ 408 #define MRPT_LOAD_CONFIG_VAR_REQUIRED_CS(variableName, variableType) \ 409 MRPT_LOAD_CONFIG_VAR_NO_DEFAULT(variableName, variableType, c, s) 411 #define MRPT_LOAD_CONFIG_VAR_CAST_NO_DEFAULT( \ 412 variableName, variableType, variableTypeCast, configFileObject, \ 417 variableName = static_cast<variableTypeCast>( \ 418 configFileObject.read_##variableType( \ 419 sectionNameStr, #variableName, variableName, true)); \ 421 catch (std::exception&) \ 423 THROW_EXCEPTION(mrpt::format( \ 424 "Value for '%s' not found in config file in section '%s'", \ 425 static_cast<const char*>(#variableName), \ 426 std::string(sectionNameStr).c_str())); \ 430 #define MRPT_LOAD_HERE_CONFIG_VAR_CAST( \ 431 variableName, variableType, variableTypeCast, targetVariable, \ 432 configFileObject, sectionNameStr) \ 434 static_cast<variableTypeCast>(configFileObject.read_##variableType( \ 435 sectionNameStr, #variableName, targetVariable)); 437 #define MRPT_LOAD_HERE_CONFIG_VAR_CAST_NO_DEFAULT( \ 438 variableName, variableType, variableTypeCast, targetVariable, \ 439 configFileObject, sectionNameStr) \ 443 targetVariable = static_cast<variableTypeCast>( \ 444 configFileObject.read_##variableType( \ 445 sectionNameStr, #variableName, targetVariable, true)); \ 447 catch (std::exception&) \ 449 THROW_EXCEPTION(mrpt::format( \ 450 "Value for '%s' not found in config file in section '%s'", \ 451 static_cast<const char*>(#variableName), \ 452 std::string(sectionNameStr).c_str())); \ 456 #define MRPT_SAVE_CONFIG_VAR(variableName, configFileObject, sectionNameStr) \ 458 configFileObject.write(sectionNameStr, #variableName, variableName); \ 461 #define MRPT_SAVE_CONFIG_VAR_DEGREES( \ 462 variableName, configFileObject, sectionNameStr) \ 464 configFileObject.write( \ 465 sectionNameStr, #variableName, mrpt::RAD2DEG(variableName)); \ 468 #define MRPT_SAVE_CONFIG_VAR_COMMENT(variableName, __comment) \ 471 s, #variableName, variableName, \ 472 mrpt::config::MRPT_SAVE_NAME_PADDING(), \ 473 mrpt::config::MRPT_SAVE_VALUE_PADDING(), __comment); \ 475 #define MRPT_SAVE_CONFIG_VAR_DEGREES_COMMENT( \ 476 __entryName, __variable, __comment) \ 479 s, __entryName, mrpt::RAD2DEG(__variable), \ 480 mrpt::config::MRPT_SAVE_NAME_PADDING(), \ 481 mrpt::config::MRPT_SAVE_VALUE_PADDING(), __comment); \
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
GLsizei const GLchar ** string
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
unsigned __int64 uint64_t
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
GLuint const GLchar * name
std::string std::string to_string(T v)
Just like std::to_string(), but with an overloaded version for std::string arguments.
GLsizei const GLfloat * value
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.