class mrpt::config::CConfigFileBase

Python API: mrpt.config.CConfigFileBase

Overview

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: Configuration file format in MRPT

#include <mrpt/config/CConfigFileBase.h>

class CConfigFileBase
{
public:
    // construction

    CConfigFileBase();
    CConfigFileBase(const CConfigFileBase&);
    CConfigFileBase(CConfigFileBase&&);

    // methods

    template <typename data_t, typename = std::enable_if_t<!std::is_enum<data_t>::value>>
    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>
    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()
        );

    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;

    uint32_t read_uint32_t(
        const std::string& section,
        const std::string& name,
        uint32_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;

    template <class VECTOR_TYPE>
    void read_vector(
        const std::string& section,
        const std::string& name,
        const VECTOR_TYPE& defaultValue,
        VECTOR_TYPE& outValues,
        bool failIfNotFound = false
        ) const;

    template <class MATRIX_TYPE>
    void read_matrix(
        const std::string& section,
        const std::string& name,
        MATRIX_TYPE& outMatrix,
        const MATRIX_TYPE& defaultMatrix = MATRIX_TYPE(),
        bool failIfNotFound = false
        ) const;

    template <typename ENUMTYPE>
    ENUMTYPE read_enum(
        const std::string& section,
        const std::string& name,
        const ENUMTYPE& defaultValue,
        bool failIfNotFound = false
        ) const;

    CConfigFileBase& operator = (const CConfigFileBase&);
    CConfigFileBase& operator = (CConfigFileBase&&);
    virtual void getAllSections(std::vector<std::string>& sections) const = 0;
    std::vector<std::string> sections() const;
    virtual void getAllKeys(const std::string& section, std::vector<std::string>& keys) const = 0;
    std::vector<std::string> keys(const std::string& section) const;
    bool sectionExists(const std::string& section_name) const;
    bool keyExists(const std::string& section, const std::string& key) const;
    void setContentFromYAML(const std::string& yaml_block);
    std::string getContentAsYAML() const;
    virtual void clear() = 0;

    template <typename enum_t, typename = std::enable_if_t<std::is_enum<enum_t>::value>>
    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()
        );
};

// direct descendants

class CConfigFile;
class CConfigFileMemory;
class CConfigFilePrefixer;

Methods

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>
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.

Parameters:

std::exception

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

template <class MATRIX_TYPE>
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

Parameters:

std::exception

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

template <typename ENUMTYPE>
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 );

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

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

Returns a list with all the section names.

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.

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.

See also:

getContentAsYAML()

std::string getContentAsYAML() const

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

See also:

setContentFromYAML()

virtual void clear() = 0

Empties the “config file”.