Main MRPT website > C++ reference
MRPT logo
CLoadableOptions.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (c) 2005-2013, Individual contributors, see AUTHORS file |
7  | Copyright (c) 2005-2013, MAPIR group, University of Malaga |
8  | Copyright (c) 2012-2013, University of Almeria |
9  | All rights reserved. |
10  | |
11  | Redistribution and use in source and binary forms, with or without |
12  | modification, are permitted provided that the following conditions are |
13  | met: |
14  | * Redistributions of source code must retain the above copyright |
15  | notice, this list of conditions and the following disclaimer. |
16  | * Redistributions in binary form must reproduce the above copyright |
17  | notice, this list of conditions and the following disclaimer in the |
18  | documentation and/or other materials provided with the distribution. |
19  | * Neither the name of the copyright holders nor the |
20  | names of its contributors may be used to endorse or promote products |
21  | derived from this software without specific prior written permission.|
22  | |
23  | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24  | 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
25  | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR|
26  | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE |
27  | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL|
28  | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR|
29  | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
30  | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
31  | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
32  | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
33  | POSSIBILITY OF SUCH DAMAGE. |
34  +---------------------------------------------------------------------------+ */
35 #ifndef CLoadableOptions_H
36 #define CLoadableOptions_H
37 
38 #include <mrpt/utils/utils_defs.h>
40 #include <mrpt/utils/CStream.h>
41 #include <mrpt/system/os.h>
42 
43 /*---------------------------------------------------------------
44  Class
45  ---------------------------------------------------------------*/
46 namespace mrpt
47 {
48 namespace utils
49 {
50  /** This is a virtual base class for sets of options than can be loaded from and/or saved to configuration plain-text files.
51  * \todo Automatize this class thru a proxy auxiliary class where variables are registered from pointers, etc...
52  * \ingroup mrpt_base_grp
53  */
55  {
56  protected:
57 
58  /** Used to print variable info from dumpToTextStream with the macro LOADABLEOPTS_DUMP_VAR */
59  static void dumpVar_int( CStream &out, const char *varName, int v );
60  static void dumpVar_float( CStream &out, const char *varName, float v );
61  static void dumpVar_double( CStream &out, const char *varName, double v );
62  static void dumpVar_bool( CStream &out, const char *varName, bool v );
63  static void dumpVar_string( CStream &out, const char *varName, const std::string &v );
64 
65 
66  public:
67  /** This method load the options from a ".ini"-like file or memory-stored string list.
68  * Only those parameters found in the given "section" and having
69  * the same name that the variable are loaded. Those not found in
70  * the file will stay with their previous values (usually the default
71  * values loaded at initialization). An example of an ".ini" file:
72  * \code
73  * [section]
74  * resolution = 0.10 // blah blah...
75  * modeSelection = 1 // 0=blah, 1=blah,...
76  * \endcode
77  *
78  * \sa loadFromConfigFileName, saveToConfigFile
79  */
80  virtual void loadFromConfigFile(
81  const mrpt::utils::CConfigFileBase & source,
82  const std::string & section) = 0;
83 
84  /** Behaves like loadFromConfigFile, but you can pass directly a file name and a temporary CConfigFile object will be created automatically to load the file.
85  * \sa loadFromConfigFile
86  */
87  void loadFromConfigFileName(
88  const std::string & config_file,
89  const std::string & section);
90 
91  /** This method saves the options to a ".ini"-like file or memory-stored string list.
92  * \sa loadFromConfigFile, saveToConfigFileName
93  */
94  virtual void saveToConfigFile(
96  const std::string &section) const
97  {
98  THROW_EXCEPTION("The child class does not implement this method.");
99  }
100 
101  /** Behaves like saveToConfigFile, but you can pass directly a file name and a temporary CConfigFile object will be created automatically to save the file.
102  * \sa saveToConfigFile, loadFromConfigFileName
103  */
104  void saveToConfigFileName(
105  const std::string &config_file,
106  const std::string &section) const;
107 
108  /** Just like \a dumpToTextStream() but sending the text to the console (std::cout) */
109  void dumpToConsole() const;
110 
111  /** This method should clearly display all the contents of the structure in textual form, sending it to a CStream.
112  * The default implementation in this base class relies on \a saveToConfigFile() to generate a plain text representation of all the parameters.
113  */
114  virtual void dumpToTextStream(CStream &out) const;
115 
116  /** Virtual destructor
117  */
119  {
120  }
121 
122  }; // End of class def.
123 
124  /** Macro for dumping a variable to a stream, within the method "dumpToTextStream(out)" (Variable types are: int, double, float, bool, string */
125  #define LOADABLEOPTS_DUMP_VAR(variableName,variableType) { dumpVar_##variableType(out, #variableName,static_cast<variableType>(variableName)); }
126 
127  /** Macro for dumping a variable to a stream, transforming the argument from radians to degrees. */
128  #define LOADABLEOPTS_DUMP_VAR_DEG(variableName) { dumpVar_double(out, #variableName,RAD2DEG(static_cast<double>(variableName))); }
129 
130  } // End of namespace
131 } // end of namespace
132 #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:62
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.0.2 SVN: at lun oct 28 00:52:41 CET 2019 Hosted on:
SourceForge.net Logo