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