Main MRPT website > C++ reference for 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
15 {
16 namespace config
17 {
18 /** This class allows loading and storing values and vectors of different types
19  * from ".ini" files easily.
20  * The contents of the file will be modified by "write" operations in memory,
21  * and will be saved back
22  * to the file at the destructor, and only if at least one write operation
23  * has been applied.
24  *
25  * Use base class `CConfigFileBase`'s methods
26  * `read_{int,float,double,string,...}()` and `write()` to actually read and
27  * write values.
28  *
29  * See: \ref config_file_format
30  *
31  * \ingroup mrpt_base_grp
32  */
34 {
35  private:
36  /** The name of the file */
38  /** The interface to the file: */
39  struct Impl;
41  /** If modified since load. */
42  bool m_modified;
43 
44  protected:
45  /** A virtual method to write a generic string */
46  void writeString(
47  const std::string& section, const std::string& name,
48  const std::string& str) override;
49 
50  /** A virtual method to read a generic string.
51  * \exception std::exception If the key name is not found and
52  * "failIfNotFound" is true. Otherwise the "defaultValue" is returned. */
54  const std::string& section, const std::string& name,
55  const std::string& defaultStr,
56  bool failIfNotFound = false) const override;
57 
58  public:
59  /** Constructor that opens a configuration file. */
60  CConfigFile(const std::string& fileName);
61 
62  /** Constructor, does not open any file. You should call "setFileName"
63  * before reading or writting or otherwise nothing will be read and write
64  * operations will be eventually lost.
65  * However, it's perfectly right to use this object without an associated
66  * file, in which case it will behave as an "in-memory" file.
67  */
68  CConfigFile();
69 
70  /** Associate this object with the given file, so future read/write
71  * operations will be applied to that file (it's synchronized at
72  * destruction) */
73  void setFileName(const std::string& fil_path);
74 
75  /** Dumps the changes to the physical configuration file now, not waiting
76  * until destruction. */
77  void writeNow();
78 
79  /** Discard saving (current) changes to physical file upon destruction */
80  void discardSavingChanges();
81 
82  /** Returns the file currently open by this object. */
83  std::string getAssociatedFile() const { return m_file; }
84  /** Destructor */
85  virtual ~CConfigFile();
86 
87  /** Returns a list with all the section names. */
88  virtual void getAllSections(
89  std::vector<std::string>& sections) const override;
90 
91  /** Returs a list with all the keys into a section. */
92  virtual void getAllKeys(
93  const std::string& section,
94  std::vector<std::string>& keys) const override;
95 
96 }; // End of class def.
97 
98 } // End of namespace
99 } // end of namespace
mrpt::config::CConfigFile::getAllSections
virtual void getAllSections(std::vector< std::string > &sections) const override
Returns a list with all the section names.
Definition: CConfigFile.cpp:146
mrpt::config::CConfigFile::m_impl
mrpt::pimpl< Impl > m_impl
Definition: config/CConfigFile.h:39
mrpt::config::CConfigFile::~CConfigFile
virtual ~CConfigFile()
Destructor.
Definition: CConfigFile.cpp:87
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:111
mrpt::pimpl
spimpl::impl_ptr< T > pimpl
Definition: pimpl.h:15
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::config::CConfigFile::discardSavingChanges
void discardSavingChanges()
Discard saving (current) changes to physical file upon destruction.
Definition: CConfigFile.cpp:83
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:58
name
GLuint const GLchar * name
Definition: glext.h:4054
mrpt::config::CConfigFile::m_file
std::string m_file
The name of the file.
Definition: config/CConfigFile.h:37
mrpt::config::CConfigFile::getAssociatedFile
std::string getAssociatedFile() const
Returns the file currently open by this object.
Definition: config/CConfigFile.h:83
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::writeNow
void writeNow()
Dumps the changes to the physical configuration file now, not waiting until destruction.
Definition: CConfigFile.cpp:72
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:95
mrpt::config::CConfigFile
This class allows loading and storing values and vectors of different types from "....
Definition: config/CConfigFile.h:33
pimpl.h
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::config::CConfigFile::getAllKeys
virtual 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:161
mrpt::config::CConfigFile::m_modified
bool m_modified
If modified since load.
Definition: config/CConfigFile.h:42



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST