class mrpt::config::CConfigFile
Python API: mrpt.config.CConfigFile
Overview
This class allows loading and storing values and vectors of different types from “.ini” files easily.
The contents of the file will be modified by “write” operations in memory, and will be saved back to the file at the destructor, and only if at least one write operation has been applied.
Use base class CConfigFileBase ‘s methods read_{int,float,double,string,...}() and write() to actually read and write values.
See: Configuration file format in MRPT
#include <mrpt/config/CConfigFile.h> class CConfigFile: public mrpt::config::CConfigFileBase { public: // structs struct Impl; // construction CConfigFile(const std::string& fileName); CConfigFile(); // methods void setFileName(const std::string& fil_path); void writeNow(); void discardSavingChanges(); std::string getAssociatedFile() const; virtual void getAllSections(std::vector<std::string>& sections) const; virtual void clear(); virtual void getAllKeys(const std::string& section, std::vector<std::string>& keys) const; };
Inherited Members
public: // 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() );
Construction
CConfigFile(const std::string& fileName)
Constructor associating with a given configuration filename.
If the file exists, it loads and parses its contents; otherwise, it silently just start with an empty configuration file in memory. The file will be written upon destruction, or at any time using writeNow()
CConfigFile()
Constructor, does not open any file.
You should call “setFileName” before reading or writing or otherwise nothing will be read and write operations will be eventually lost. However, it’s perfectly right to use this object without an associated file, in which case it will behave as an “in-memory” file.
Methods
void setFileName(const std::string& fil_path)
Associate this object with the given file, reading its contents right now.
Upon destruction, the updated contents will be written to that file.
void writeNow()
Dumps the changes to the physical configuration file now, not waiting until destruction.
Parameters:
std::runtime_error |
Upon error writing. |
void discardSavingChanges()
Discard saving (current) changes to physical file upon destruction.
std::string getAssociatedFile() const
Returns the file currently open by this object.
virtual void getAllSections(std::vector<std::string>& sections) const
Returns a list with all the section names.
virtual void clear()
Empties the “config file”.
virtual void getAllKeys( const std::string& section, std::vector<std::string>& keys ) const
Returns a list with all the keys into a section.