9 #ifndef CConfigFileBase_H
10 #define CConfigFileBase_H
17 #include <type_traits>
24 class CConfigFilePrefixer;
54 const std::string& str,
const int name_padding_width,
55 const int value_padding_width,
const std::string& comment);
62 const std::string& defaultStr,
bool failIfNotFound =
false)
const = 0;
78 template <
typename enum_t,
82 const int name_padding_width = -1,
const int value_padding_width = -1,
87 name_padding_width, value_padding_width, comment);
94 template <
typename data_t,
98 const data_t&
value,
const int name_padding_width = -1,
99 const int value_padding_width = -1,
102 std::stringstream ss;
103 ss.flags(ss.flags() | std::ios::boolalpha);
106 section,
name, ss.str(), name_padding_width, value_padding_width,
109 template <
typename data_t>
112 const std::vector<data_t>&
value,
const int name_padding_width = -1,
113 const int value_padding_width = -1,
116 std::stringstream ss;
117 ss.flags(ss.flags() | std::ios::boolalpha);
119 it !=
value.end(); ++it)
122 section,
name, ss.str(), name_padding_width, value_padding_width,
127 const int name_padding_width = -1,
const int value_padding_width = -1,
131 const int name_padding_width = -1,
const int value_padding_width = -1,
141 double defaultValue,
bool failIfNotFound =
false)
const;
144 bool failIfNotFound =
false)
const;
147 bool failIfNotFound =
false)
const;
150 bool failIfNotFound =
false)
const;
153 uint64_t defaultValue,
bool failIfNotFound =
false)
const;
156 const std::string& defaultValue,
bool failIfNotFound =
false)
const;
162 const std::string& defaultValue,
bool failIfNotFound =
false)
const;
167 template <
class VECTOR_TYPE>
170 const VECTOR_TYPE& defaultValue, VECTOR_TYPE& outValues,
171 bool failIfNotFound =
false)
const
175 std::vector<std::string> tokens;
178 if (tokens.size() == 0)
180 outValues = defaultValue;
185 const size_t N = tokens.size();
187 for (
size_t i = 0; i < N; i++)
189 std::stringstream ss(tokens[i]);
202 template <
class MATRIX_TYPE>
205 MATRIX_TYPE& outMatrix,
206 const MATRIX_TYPE& defaultMatrix = MATRIX_TYPE(),
207 bool failIfNotFound =
false)
const
211 outMatrix = defaultMatrix;
215 if (!outMatrix.fromMatlabStringFormat(aux))
241 template <
typename ENUMTYPE>
244 const ENUMTYPE& defaultValue,
bool failIfNotFound =
false)
const
249 if (sVal.empty())
return defaultValue;
251 if (::isdigit(sVal[0]))
253 return static_cast<ENUMTYPE
>(::atoi(&sVal[0]));
261 catch (std::exception&)
264 "Invalid value '%s' for enum type while reading key='%s'.",
265 sVal.c_str(),
name.c_str())
279 #define MRPT_LOAD_CONFIG_VAR( \
280 variableName, variableType, configFileObject, sectionNameStr) \
282 variableName = configFileObject.read_##variableType( \
283 sectionNameStr, #variableName, variableName); \
288 #define MRPT_LOAD_CONFIG_VAR_CS(variableName, variableType) \
289 MRPT_LOAD_CONFIG_VAR(variableName, variableType, c, s)
293 #define MRPT_LOAD_CONFIG_VAR_DEGREES( \
294 variableName, configFileObject, sectionNameStr) \
296 variableName = mrpt::utils::DEG2RAD( \
297 configFileObject.read_double( \
298 sectionNameStr, #variableName, \
299 mrpt::utils::RAD2DEG(variableName))); \
304 #define MRPT_LOAD_CONFIG_VAR_DEGREES_NO_DEFAULT( \
305 variableName, configFileObject, sectionNameStr) \
307 variableName = mrpt::utils::DEG2RAD( \
308 configFileObject.read_double( \
309 sectionNameStr, #variableName, \
310 mrpt::utils::RAD2DEG(variableName), true)); \
313 #define MRPT_LOAD_CONFIG_VAR_CAST( \
314 variableName, variableType, variableTypeCast, configFileObject, \
317 variableName = static_cast<variableTypeCast>( \
318 configFileObject.read_##variableType( \
319 sectionNameStr, #variableName, variableName)); \
322 #define MRPT_LOAD_HERE_CONFIG_VAR( \
323 variableName, variableType, targetVariable, configFileObject, \
325 targetVariable = configFileObject.read_##variableType( \
326 sectionNameStr, #variableName, targetVariable, false);
328 #define MRPT_LOAD_HERE_CONFIG_VAR_NO_DEFAULT( \
329 variableName, variableType, targetVariable, configFileObject, \
334 targetVariable = configFileObject.read_##variableType( \
335 sectionNameStr, #variableName, targetVariable, true); \
337 catch (std::exception&) \
341 "Value for '%s' not found in config file in section '%s'", \
342 static_cast<const char*>(#variableName), \
343 std::string(sectionNameStr).c_str())); \
347 #define MRPT_LOAD_HERE_CONFIG_VAR_DEGREES( \
348 variableName, variableType, targetVariable, configFileObject, \
350 targetVariable = mrpt::utils::DEG2RAD( \
351 configFileObject.read_##variableType( \
352 sectionNameStr, #variableName, \
353 mrpt::utils::RAD2DEG(targetVariable), false));
355 #define MRPT_LOAD_HERE_CONFIG_VAR_DEGREES_NO_DEFAULT( \
356 variableName, variableType, targetVariable, configFileObject, \
361 targetVariable = mrpt::utils::DEG2RAD( \
362 configFileObject.read_##variableType( \
363 sectionNameStr, #variableName, targetVariable, true)); \
365 catch (std::exception&) \
369 "Value for '%s' not found in config file in section '%s'", \
370 static_cast<const char*>(#variableName), \
371 std::string(sectionNameStr).c_str())); \
375 #define MRPT_LOAD_CONFIG_VAR_NO_DEFAULT( \
376 variableName, variableType, configFileObject, sectionNameStr) \
380 variableName = configFileObject.read_##variableType( \
381 sectionNameStr, #variableName, variableName, true); \
383 catch (std::exception&) \
387 "Value for '%s' not found in config file in section '%s'", \
388 static_cast<const char*>(#variableName), \
389 std::string(sectionNameStr).c_str())); \
395 #define MRPT_LOAD_CONFIG_VAR_REQUIRED_CS(variableName, variableType) \
396 MRPT_LOAD_CONFIG_VAR_NO_DEFAULT(variableName, variableType, c, s)
398 #define MRPT_LOAD_CONFIG_VAR_CAST_NO_DEFAULT( \
399 variableName, variableType, variableTypeCast, configFileObject, \
404 variableName = static_cast<variableTypeCast>( \
405 configFileObject.read_##variableType( \
406 sectionNameStr, #variableName, variableName, true)); \
408 catch (std::exception&) \
412 "Value for '%s' not found in config file in section '%s'", \
413 static_cast<const char*>(#variableName), \
414 std::string(sectionNameStr).c_str())); \
418 #define MRPT_LOAD_HERE_CONFIG_VAR_CAST( \
419 variableName, variableType, variableTypeCast, targetVariable, \
420 configFileObject, sectionNameStr) \
421 targetVariable = static_cast<variableTypeCast>( \
422 configFileObject.read_##variableType( \
423 sectionNameStr, #variableName, targetVariable));
425 #define MRPT_LOAD_HERE_CONFIG_VAR_CAST_NO_DEFAULT( \
426 variableName, variableType, variableTypeCast, targetVariable, \
427 configFileObject, sectionNameStr) \
431 targetVariable = static_cast<variableTypeCast>( \
432 configFileObject.read_##variableType( \
433 sectionNameStr, #variableName, targetVariable, true)); \
435 catch (std::exception&) \
439 "Value for '%s' not found in config file in section '%s'", \
440 static_cast<const char*>(#variableName), \
441 std::string(sectionNameStr).c_str())); \
445 #define MRPT_SAVE_CONFIG_VAR(variableName, configFileObject, sectionNameStr) \
447 configFileObject.write(sectionNameStr, #variableName, variableName); \
450 #define MRPT_SAVE_CONFIG_VAR_DEGREES( \
451 variableName, configFileObject, sectionNameStr) \
453 configFileObject.write( \
454 sectionNameStr, #variableName, \
455 mrpt::utils::RAD2DEG(variableName)); \
458 #define MRPT_SAVE_CONFIG_VAR_COMMENT(variableName, __comment) \
461 s, #variableName, variableName, \
462 mrpt::utils::MRPT_SAVE_NAME_PADDING(), \
463 mrpt::utils::MRPT_SAVE_VALUE_PADDING(), __comment); \
465 #define MRPT_SAVE_CONFIG_VAR_DEGREES_COMMENT( \
466 __entryName, __variable, __comment) \
469 s, __entryName, mrpt::utils::RAD2DEG(__variable), \
470 mrpt::utils::MRPT_SAVE_NAME_PADDING(), \
471 mrpt::utils::MRPT_SAVE_VALUE_PADDING(), __comment); \
This class allows loading and storing values and vectors of different types from a configuration text...
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())
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 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())
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 void getAllKeys(const std::string §ion, vector_string &keys) const =0
Returs a list with all the keys into a section.
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 ...
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...
bool sectionExists(const std::string §ion_name) const
Checks if a given section exists (name is case insensitive)
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 ; ...
bool read_bool(const std::string §ion, const std::string &name, bool defaultValue, bool failIfNotFound=false) const
double read_double(const std::string §ion, const std::string &name, double defaultValue, bool failIfNotFound=false) const
std::string read_string(const std::string §ion, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
int read_int(const std::string §ion, const std::string &name, int defaultValue, bool failIfNotFound=false) const
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 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.
virtual ~CConfigFileBase()
dtor
float read_float(const std::string §ion, const std::string &name, float defaultValue, bool failIfNotFound=false) const
uint64_t read_uint64_t(const std::string §ion, const std::string &name, uint64_t defaultValue, bool failIfNotFound=false) const
virtual void getAllSections(vector_string §ions) const =0
Returns a list with all the section names.
A wrapper for other CConfigFileBase-based objects that prefixes a given token to every key and/or sec...
const Scalar * const_iterator
GLuint const GLchar * name
GLsizei const GLfloat * value
GLsizei const GLchar ** string
std::vector< std::string > vector_string
A type for passing a vector of strings.
void tokenize(const std::string &inString, const std::string &inDelimiters, std::deque< std::string > &outTokens, bool skipBlankTokens=true) noexcept
Tokenizes a string according to a set of delimiting characters.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
int MRPT_SAVE_VALUE_PADDING()
int MRPT_SAVE_NAME_PADDING()
Default padding sizes for macros MRPT_SAVE_CONFIG_VAR_COMMENT(), etc.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
unsigned __int64 uint64_t
A helper class that can convert an enum value into its textual representation, and viceversa.
static ENUMTYPE name2value(const std::string &name)
Gives the numerical name for a given enum text name.