Main MRPT website > C++ reference for MRPT 1.9.9
config/CLoadableOptions.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 
11 #include <string>
12 #include <stdexcept>
13 #include <iosfwd>
14 
15 namespace mrpt
16 {
17 namespace config
18 {
19 // Frwd. decls:
20 class CConfigFileBase;
21 
22 /** This is a virtual base class for sets of options than can be loaded from
23  * and/or saved to configuration plain-text files.
24  * \todo Automatize this class thru a proxy auxiliary class where variables are
25  * registered from pointers, etc...
26  * \ingroup mrpt_base_grp
27  */
29 {
30  protected:
31  /** Used to print variable info from dumpToTextStream with the macro
32  * LOADABLEOPTS_DUMP_VAR */
33  static void dumpVar_int(std::ostream& out, const char* varName, int v);
34  static void dumpVar_float(std::ostream& out, const char* varName, float v);
35  static void dumpVar_double(
36  std::ostream& out, const char* varName, double v);
37  static void dumpVar_bool(std::ostream& out, const char* varName, bool v);
38  static void dumpVar_string(
39  std::ostream& out, const char* varName, const std::string& v);
40 
41  public:
42  /** This method load the options from a ".ini"-like file or memory-stored
43  * string list.
44  * Only those parameters found in the given "section" and having
45  * the same name that the variable are loaded. Those not found in
46  * the file will stay with their previous values (usually the default
47  * values loaded at initialization). An example of an ".ini" file:
48  * \code
49  * [section]
50  * resolution = 0.10 // blah blah...
51  * modeSelection = 1 // 0=blah, 1=blah,...
52  * \endcode
53  *
54  * \sa loadFromConfigFileName, saveToConfigFile
55  */
56  virtual void loadFromConfigFile(
58  const std::string& section) = 0;
59 
60  /** Behaves like loadFromConfigFile, but you can pass directly a file name
61  * and a temporary CConfigFile object will be created automatically to load
62  * the file.
63  * \sa loadFromConfigFile
64  */
66  const std::string& config_file, const std::string& section);
67 
68  /** This method saves the options to a ".ini"-like file or memory-stored
69  * string list.
70  * \sa loadFromConfigFile, saveToConfigFileName
71  */
72  virtual void saveToConfigFile(
74  const std::string& section) const;
75 
76  /** Behaves like saveToConfigFile, but you can pass directly a file name and
77  * a temporary CConfigFile object will be created automatically to save the
78  * file.
79  * \sa saveToConfigFile, loadFromConfigFileName
80  */
82  const std::string& config_file, const std::string& section) const;
83 
84  /** Just like \a dumpToTextStream() but sending the text to the console
85  * (std::cout) */
86  void dumpToConsole() const;
87 
88  /** This method should clearly display all the contents of the structure in
89  * textual form, sending it to a std::ostream.
90  * The default implementation in this base class relies on \a
91  * saveToConfigFile() to generate a plain text representation of all the
92  * parameters.
93  */
94  virtual void dumpToTextStream(std::ostream& out) const;
95 
96  /** Virtual destructor */
97  virtual ~CLoadableOptions() {}
98 }; // End of class def.
99 
100 /** Macro for dumping a variable to a stream, within the method
101  * "dumpToTextStream(out)" (Variable types are: int, double, float, bool, string
102  */
103 #define LOADABLEOPTS_DUMP_VAR(variableName, variableType) \
104  { \
105  dumpVar_##variableType( \
106  out, #variableName, static_cast<variableType>(variableName)); \
107  }
108 
109 /** Macro for dumping a variable to a stream, transforming the argument from
110  * radians to degrees. */
111 #define LOADABLEOPTS_DUMP_VAR_DEG(variableName) \
112  { \
113  dumpVar_double( \
114  out, #variableName, RAD2DEG(static_cast<double>(variableName))); \
115  }
116 
117 } // end NS config
118 } // end NS mrpt
static void dumpVar_int(std::ostream &out, const char *varName, int v)
Used to print variable info from dumpToTextStream with the macro LOADABLEOPTS_DUMP_VAR.
static void dumpVar_double(std::ostream &out, const char *varName, double v)
This is a virtual base class for sets of options than can be loaded from and/or saved to configuratio...
virtual void saveToConfigFile(mrpt::config::CConfigFileBase &target, const std::string &section) const
This method saves the options to a ".ini"-like file or memory-stored string list. ...
virtual ~CLoadableOptions()
Virtual destructor.
This class allows loading and storing values and vectors of different types from a configuration text...
static void dumpVar_bool(std::ostream &out, const char *varName, bool v)
static void dumpVar_string(std::ostream &out, const char *varName, const std::string &v)
void saveToConfigFileName(const std::string &config_file, const std::string &section) const
Behaves like saveToConfigFile, but you can pass directly a file name and a temporary CConfigFile obje...
mrpt::config::CConfigFileBase CConfigFileBase
virtual void dumpToTextStream(std::ostream &out) const
This method should clearly display all the contents of the structure in textual form, sending it to a std::ostream.
GLsizei const GLchar ** string
Definition: glext.h:4101
void loadFromConfigFileName(const std::string &config_file, const std::string &section)
Behaves like loadFromConfigFile, but you can pass directly a file name and a temporary CConfigFile ob...
const GLdouble * v
Definition: glext.h:3678
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
static void dumpVar_float(std::ostream &out, const char *varName, float v)
virtual void loadFromConfigFile(const mrpt::config::CConfigFileBase &source, const std::string &section)=0
This method load the options from a ".ini"-like file or memory-stored string list.
GLsizei GLsizei GLchar * source
Definition: glext.h:4082
void dumpToConsole() const
Just like dumpToTextStream() but sending the text to the console (std::cout)



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019