MRPT  2.0.3
config/CConfigFile.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
12 #include <mrpt/core/pimpl.h>
13 
14 namespace mrpt::config
15 {
16 /** This class allows loading and storing values and vectors of different types
17  * from ".ini" files easily.
18  * The contents of the file will be modified by "write" operations in memory,
19  * and will be saved back
20  * to the file at the destructor, and only if at least one write operation
21  * has been applied.
22  *
23  * Use base class `CConfigFileBase`'s methods
24  * `read_{int,float,double,string,...}()` and `write()` to actually read and
25  * write values.
26  *
27  * See: \ref config_file_format
28  *
29  * \ingroup mrpt_base_grp
30  */
32 {
33  private:
34  /** The name of the file */
35  std::string m_file;
36  /** The interface to the file: */
37  struct Impl;
39  /** If modified since load. */
40  bool m_modified;
41 
42  protected:
43  /** A virtual method to write a generic string */
44  void writeString(
45  const std::string& section, const std::string& name,
46  const std::string& str) override;
47 
48  /** A virtual method to read a generic string.
49  * \exception std::exception If the key name is not found and
50  * "failIfNotFound" is true. Otherwise the "defaultValue" is returned. */
51  std::string readString(
52  const std::string& section, const std::string& name,
53  const std::string& defaultStr,
54  bool failIfNotFound = false) const override;
55 
56  public:
57  /** Constructor that opens a configuration file. */
58  CConfigFile(const std::string& fileName);
59 
60  /** Constructor, does not open any file. You should call "setFileName"
61  * before reading or writting or otherwise nothing will be read and write
62  * operations will be eventually lost.
63  * However, it's perfectly right to use this object without an associated
64  * file, in which case it will behave as an "in-memory" file.
65  */
66  CConfigFile();
67 
68  /** Associate this object with the given file, so future read/write
69  * operations will be applied to that file (it's synchronized at
70  * destruction) */
71  void setFileName(const std::string& fil_path);
72 
73  /** Dumps the changes to the physical configuration file now, not waiting
74  * until destruction. */
75  void writeNow();
76 
77  /** Discard saving (current) changes to physical file upon destruction */
78  void discardSavingChanges();
79 
80  /** Returns the file currently open by this object. */
81  std::string getAssociatedFile() const { return m_file; }
82  /** Destructor */
83  ~CConfigFile() override;
84 
85  /** Returns a list with all the section names. */
86  void getAllSections(std::vector<std::string>& sections) const override;
87 
88  /** Empties the "config file" */
89  void clear() override;
90 
91  /** Returs a list with all the keys into a section. */
92  void getAllKeys(const std::string& section, std::vector<std::string>& keys)
93  const override;
94 
95 }; // End of class def.
96 
97 } // namespace mrpt::config
mrpt::config
Definition: config/CConfigFile.h:14
mrpt::config::CConfigFile::getAllSections
void getAllSections(std::vector< std::string > &sections) const override
Returns a list with all the section names.
Definition: CConfigFile.cpp:147
mrpt::config::CConfigFile::m_impl
mrpt::pimpl< Impl > m_impl
Definition: config/CConfigFile.h:37
mrpt::config::CConfigFile::Impl
Definition: CConfigFile.cpp:21
mrpt::config::CConfigFile::CConfigFile
CConfigFile()
Constructor, does not open any file.
Definition: CConfigFile.cpp:44
mrpt::config::CConfigFile::readString
std::string readString(const std::string &section, const std::string &name, const std::string &defaultStr, bool failIfNotFound=false) const override
A virtual method to read a generic string.
Definition: CConfigFile.cpp:113
mrpt::pimpl
spimpl::impl_ptr< T > pimpl
Definition: pimpl.h:15
mrpt::config::CConfigFile::discardSavingChanges
void discardSavingChanges()
Discard saving (current) changes to physical file upon destruction.
Definition: CConfigFile.cpp:82
mrpt::config::CConfigFile::setFileName
void setFileName(const std::string &fil_path)
Associate this object with the given file, so future read/write operations will be applied to that fi...
Definition: CConfigFile.cpp:57
mrpt::config::CConfigFile::m_file
std::string m_file
The name of the file.
Definition: config/CConfigFile.h:35
mrpt::config::CConfigFile::getAssociatedFile
std::string getAssociatedFile() const
Returns the file currently open by this object.
Definition: config/CConfigFile.h:81
CConfigFileBase.h
mrpt::config::CConfigFileBase
This class allows loading and storing values and vectors of different types from a configuration text...
Definition: config/CConfigFileBase.h:44
mrpt::config::CConfigFile::~CConfigFile
~CConfigFile() override
Destructor.
Definition: CConfigFile.cpp:84
mrpt::config::CConfigFile::writeNow
void writeNow()
Dumps the changes to the physical configuration file now, not waiting until destruction.
Definition: CConfigFile.cpp:71
mrpt::config::CConfigFile::writeString
void writeString(const std::string &section, const std::string &name, const std::string &str) override
A virtual method to write a generic string
Definition: CConfigFile.cpp:96
mrpt::config::CConfigFile
This class allows loading and storing values and vectors of different types from "....
Definition: config/CConfigFile.h:31
pimpl.h
mrpt::config::CConfigFile::getAllKeys
void getAllKeys(const std::string &section, std::vector< std::string > &keys) const override
Returs a list with all the keys into a section.
Definition: CConfigFile.cpp:162
mrpt::config::CConfigFile::clear
void clear() override
Empties the "config file".
Definition: CConfigFile.cpp:175
mrpt::config::CConfigFile::m_modified
bool m_modified
If modified since load.
Definition: config/CConfigFile.h:40



Page generated by Doxygen 1.8.17 for MRPT 2.0.3 at Fri May 29 13:06:46 UTC 2020