MRPT  1.9.9
config/CConfigFile.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://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 */
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. */
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  virtual ~CConfigFile();
84 
85  /** Returns a list with all the section names. */
86  virtual void getAllSections(
87  std::vector<std::string>& sections) const override;
88 
89  /** Returs a list with all the keys into a section. */
90  virtual void getAllKeys(
91  const std::string& section,
92  std::vector<std::string>& keys) const override;
93 
94 }; // End of class def.
95 
96 }
97 
mrpt::pimpl< Impl > m_impl
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:95
virtual 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...
GLsizei const GLchar ** string
Definition: glext.h:4101
void writeNow()
Dumps the changes to the physical configuration file now, not waiting until destruction.
Definition: CConfigFile.cpp:72
GLuint const GLchar * name
Definition: glext.h:4054
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:58
void discardSavingChanges()
Discard saving (current) changes to physical file upon destruction.
Definition: CConfigFile.cpp:83
virtual void getAllKeys(const std::string &section, std::vector< std::string > &keys) const override
Returs a list with all the keys into a section.
virtual ~CConfigFile()
Destructor.
Definition: CConfigFile.cpp:87



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020