Main MRPT website > C++ reference
MRPT logo
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-2014, 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 #ifndef CLoadableOptions_H
10 #define CLoadableOptions_H
11 
12 #include <mrpt/utils/utils_defs.h>
14 #include <mrpt/utils/CStream.h>
15 #include <mrpt/system/os.h>
16 
17 /*---------------------------------------------------------------
18  Class
19  ---------------------------------------------------------------*/
20 namespace mrpt
21 {
22 namespace utils
23 {
24  /** This is a virtual base class for sets of options than can be loaded from and/or saved to configuration plain-text files.
25  * \todo Automatize this class thru a proxy auxiliary class where variables are registered from pointers, etc...
26  * \ingroup mrpt_base_grp
27  */
29  {
30  protected:
31 
32  /** Used to print variable info from dumpToTextStream with the macro LOADABLEOPTS_DUMP_VAR */
33  static void dumpVar_int( CStream &out, const char *varName, int v );
34  static void dumpVar_float( CStream &out, const char *varName, float v );
35  static void dumpVar_double( CStream &out, const char *varName, double v );
36  static void dumpVar_bool( CStream &out, const char *varName, bool v );
37  static void dumpVar_string( CStream &out, const char *varName, const std::string &v );
38 
39 
40  public:
41  /** This method load the options from a ".ini"-like file or memory-stored 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::utils::CConfigFileBase & source,
56  const std::string & section) = 0;
57 
58  /** Behaves like loadFromConfigFile, but you can pass directly a file name and a temporary CConfigFile object will be created automatically to load the file.
59  * \sa loadFromConfigFile
60  */
61  void loadFromConfigFileName(
62  const std::string & config_file,
63  const std::string & section);
64 
65  /** This method saves the options to a ".ini"-like file or memory-stored string list.
66  * \sa loadFromConfigFile, saveToConfigFileName
67  */
68  virtual void saveToConfigFile(
70  const std::string &section) const
71  {
72  THROW_EXCEPTION("The child class does not implement this method.");
73  }
74 
75  /** Behaves like saveToConfigFile, but you can pass directly a file name and a temporary CConfigFile object will be created automatically to save the file.
76  * \sa saveToConfigFile, loadFromConfigFileName
77  */
78  void saveToConfigFileName(
79  const std::string &config_file,
80  const std::string &section) const;
81 
82  /** Just like \a dumpToTextStream() but sending the text to the console (std::cout) */
83  void dumpToConsole() const;
84 
85  /** This method should clearly display all the contents of the structure in textual form, sending it to a CStream.
86  * The default implementation in this base class relies on \a saveToConfigFile() to generate a plain text representation of all the parameters.
87  */
88  virtual void dumpToTextStream(CStream &out) const;
89 
90  /** Virtual destructor
91  */
93  {
94  }
95 
96  }; // End of class def.
97 
98  /** Macro for dumping a variable to a stream, within the method "dumpToTextStream(out)" (Variable types are: int, double, float, bool, string */
99  #define LOADABLEOPTS_DUMP_VAR(variableName,variableType) { dumpVar_##variableType(out, #variableName,static_cast<variableType>(variableName)); }
100 
101  /** Macro for dumping a variable to a stream, transforming the argument from radians to degrees. */
102  #define LOADABLEOPTS_DUMP_VAR_DEG(variableName) { dumpVar_double(out, #variableName,RAD2DEG(static_cast<double>(variableName))); }
103 
104  } // End of namespace
105 } // end of namespace
106 #endif
#define THROW_EXCEPTION(msg)
This class allows loading and storing values and vectors of different types from a configuration text...
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:36
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
This is a virtual base class for sets of options than can be loaded from and/or saved to configuratio...
virtual void saveToConfigFile(mrpt::utils::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.



Page generated by Doxygen 1.8.14 for MRPT 1.1.0 SVN: at lun oct 28 00:54:49 CET 2019 Hosted on:
SourceForge.net Logo