MRPT  2.0.4
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_config_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::pimpl< Impl > m_impl
~CConfigFile() override
Destructor.
Definition: CConfigFile.cpp:84
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
void getAllSections(std::vector< std::string > &sections) const override
Returns a list with all the section names.
spimpl::impl_ptr< T > pimpl
Definition: pimpl.h:15
bool m_modified
If modified since load.
This class allows loading and storing values and vectors of different types from ".ini" files easily.
CConfigFile()
Constructor, does not open any file.
Definition: CConfigFile.cpp:44
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.
This class allows loading and storing values and vectors of different types from a configuration text...
void writeNow()
Dumps the changes to the physical configuration file now, not waiting until destruction.
Definition: CConfigFile.cpp:71
std::string m_file
The name of the file.
std::string getAssociatedFile() const
Returns the file currently open by this object.
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
void discardSavingChanges()
Discard saving (current) changes to physical file upon destruction.
Definition: CConfigFile.cpp:82
void getAllKeys(const std::string &section, std::vector< std::string > &keys) const override
Returs a list with all the keys into a section.
void clear() override
Empties the "config file".



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020