Class CConfigFileBase

Inheritance Relationships

Derived Types

Class Documentation

class CConfigFileBase

This class allows loading and storing values and vectors of different types from a configuration text, which can be implemented as a “.ini” file, a memory-stored string, etc… This is a virtual class, use only as a pointer to an implementation of one of the derived classes.

See: config_file_format

Subclassed by mrpt::config::CConfigFile, mrpt::config::CConfigFileMemory, mrpt::config::CConfigFilePrefixer

Save a configuration parameter. Optionally pads with spaces up to

the desired width in number of characters (-1: no fill), and add a final comment field at the end of the line (a “// “ prefix is automatically inserted).

template<typename data_t, typename = std::enable_if_t<!std::is_enum<data_t>::value>>
inline void write(const std::string &section, 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())
template<typename data_t>
inline void write(const std::string &section, 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 write(const std::string &section, const std::string &name, double value, const int name_padding_width = -1, const int value_padding_width = -1, const std::string &comment = std::string())
void write(const std::string &section, const std::string &name, float value, const int name_padding_width = -1, const int value_padding_width = -1, const std::string &comment = std::string())

Read a configuration parameter, launching exception if key name is

not found and failIfNotFound=true

double read_double(const std::string &section, const std::string &name, double defaultValue, bool failIfNotFound = false) const
float read_float(const std::string &section, const std::string &name, float defaultValue, bool failIfNotFound = false) const
bool read_bool(const std::string &section, const std::string &name, bool defaultValue, bool failIfNotFound = false) const
int read_int(const std::string &section, const std::string &name, int defaultValue, bool failIfNotFound = false) const
uint64_t read_uint64_t(const std::string &section, const std::string &name, uint64_t defaultValue, bool failIfNotFound = false) const
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound = false) const
std::string read_string_first_word(const std::string &section, 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 eliminate possible comments at the end of the line)

template<class VECTOR_TYPE>
inline void read_vector(const std::string &section, 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 … ]”, where spaces could also be commas.

Throws:

std::exception – If the key name is not found and “failIfNotFound” is true. Otherwise the “defaultValue” is returned.

template<class MATRIX_TYPE>
inline void read_matrix(const std::string &section, 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 ; 7 8 9]”. This template method can be instantiated for matrices of the types: int, long, unsinged int, unsigned long, float, double, long double

Throws:

std::exception – If the key name is not found and “failIfNotFound” is true. Otherwise the “defaultValue” is returned.

template<typename ENUMTYPE>
inline ENUMTYPE read_enum(const std::string &section, 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 symbolic name, for example: In the code:

enum my_type_t { type_foo=0, type_bar };
In the config file:
   [section]
   type   = type_bar   // Use the symbolic name, or
   type   = 1          // use the numerical value (both lines will be
equivalent)
Which can be loaded with:
cfgfile.read_enum<my_type_t>("section","type", type_foo );

Note

For an enum type to work with this template it is required that it defines a specialization of mrpt::typemeta::TEnumType

Public Functions

CConfigFileBase() = default
CConfigFileBase(const CConfigFileBase&) = default
CConfigFileBase &operator=(const CConfigFileBase&) = default
CConfigFileBase(CConfigFileBase&&) = default
CConfigFileBase &operator=(CConfigFileBase&&) = default
virtual ~CConfigFileBase()

dtor

virtual void getAllSections(std::vector<std::string> &sections) const = 0

Returns a list with all the section names.

inline std::vector<std::string> sections() const

Returns, by value, a list with all the section names.

virtual void getAllKeys(const std::string &section, std::vector<std::string> &keys) const = 0

Returns a list with all the keys into a section

inline std::vector<std::string> keys(const std::string &section) const

Returns, by value, a list with all the keys into a section

bool sectionExists(const std::string &section_name) const

Checks if a given section exists (name is case insensitive)

See also

keyExists()

bool keyExists(const std::string &section, const std::string &key) const

Checks if a given key exists inside a section (case insensitive)

See also

sectionExists()

void setContentFromYAML(const std::string &yaml_block)

Changes the contents of the virtual “config file” from a text block containing a YAML configuration text. Refer to unit test yaml2config_unittest.cpp for examples of use.

std::string getContentAsYAML() const

Returns a text block representing the contents of the config file in YAML format.

virtual void clear() = 0

Empties the “config file”

template<typename enum_t, typename = std::enable_if_t<std::is_enum<enum_t>::value>>
inline void write(const std::string &section, 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())

Protected Functions

virtual void writeString(const std::string &section, const std::string &name, const std::string &str) = 0

A virtual method to write a generic string.

void writeString(const std::string &section, const std::string &name, const std::string &str, const int name_padding_width, const int value_padding_width, const std::string &comment)

Write a generic string with optional padding and a comment field (“//

…”) at the end of the line.

virtual std::string readString(const std::string &section, const std::string &name, const std::string &defaultStr, bool failIfNotFound = false) const = 0

A virtual method to read a generic string.

Throws:

std::exception – If the key name is not found and “failIfNotFound” is true. Otherwise the “defaultValue” is returned.