Main MRPT website > C++ reference
MRPT logo
CLoadableOptions.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CLoadableOptions_H
29 #define CLoadableOptions_H
30 
31 #include <mrpt/utils/utils_defs.h>
33 #include <mrpt/utils/CStream.h>
34 #include <mrpt/system/os.h>
35 
36 /*---------------------------------------------------------------
37  Class
38  ---------------------------------------------------------------*/
39 namespace mrpt
40 {
41 namespace utils
42 {
43  /** This is a virtual base class for sets of options than can be loaded from and/or saved to configuration plain-text files.
44  * \todo Automatize this class thru a proxy auxiliary class where variables are registered from pointers, etc...
45  * \ingroup mrpt_base_grp
46  */
48  {
49  protected:
50 
51  /** Used to print variable info from dumpToTextStream with the macro LOADABLEOPTS_DUMP_VAR */
52  static void dumpVar_int( CStream &out, const char *varName, int v );
53  static void dumpVar_float( CStream &out, const char *varName, float v );
54  static void dumpVar_double( CStream &out, const char *varName, double v );
55  static void dumpVar_bool( CStream &out, const char *varName, bool v );
56  static void dumpVar_string( CStream &out, const char *varName, const std::string &v );
57 
58 
59  public:
60  /** This method load the options from a ".ini"-like file or memory-stored string list.
61  * Only those parameters found in the given "section" and having
62  * the same name that the variable are loaded. Those not found in
63  * the file will stay with their previous values (usually the default
64  * values loaded at initialization). An example of an ".ini" file:
65  * \code
66  * [section]
67  * resolution=0.10 ; blah blah...
68  * modeSelection=1 ; 0=blah, 1=blah,...
69  * \endcode
70  *
71  * \sa loadFromConfigFileName, saveToConfigFile
72  */
73  virtual void loadFromConfigFile(
74  const mrpt::utils::CConfigFileBase &source,
75  const std::string &section) = 0;
76 
77  /** Behaves like loadFromConfigFile, but you can pass directly a file name and a temporary CConfigFile object will be created automatically to load the file.
78  * \sa loadFromConfigFile
79  */
80  void loadFromConfigFileName(
81  const std::string &config_file,
82  const std::string &section);
83 
84  /** This method saves the options to a ".ini"-like file or memory-stored string list.
85  * \sa loadFromConfigFile, saveToConfigFileName
86  */
87  virtual void saveToConfigFile(
89  const std::string &section) /*= 0*/
90  {
91  THROW_EXCEPTION("The child class does not implement this method.");
92  }
93 
94 
95  /** Behaves like saveToConfigFile, but you can pass directly a file name and a temporary CConfigFile object will be created automatically to save the file.
96  * \sa saveToConfigFile, loadFromConfigFileName
97  */
98  void saveToConfigFileName(
99  const std::string &config_file,
100  const std::string &section);
101 
102  /** This method must display clearly all the contents of the structure in textual form, sending it to a CStream.
103  */
104  void dumpToConsole() const;
105 
106  /** This method must display clearly all the contents of the structure in textual form, sending it to a CStream.
107  */
108  virtual void dumpToTextStream( CStream &out) const = 0;
109 
110  /** Virtual destructor
111  */
112  virtual ~CLoadableOptions()
113  {
114  }
115 
116  }; // End of class def.
117 
118  /** Macro for dumping a variable to a stream, within the method "dumpToTextStream(out)" (Variable types are: int, double, float, bool, string */
119  #define LOADABLEOPTS_DUMP_VAR(variableName,variableType) { dumpVar_##variableType(out, #variableName,static_cast<variableType>(variableName)); }
120 
121  /** Macro for dumping a variable to a stream, transforming the argument from radians to degrees. */
122  #define LOADABLEOPTS_DUMP_VAR_DEG(variableName) { dumpVar_double(out, #variableName,RAD2DEG(static_cast<double>(variableName))); }
123 
124  } // End of namespace
125 } // end of namespace
126 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013