Main MRPT website > C++ reference for MRPT 1.9.9
TParameters.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-2017, 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 
10 #ifndef mrpt_utils_parameters_H
11 #define mrpt_utils_parameters_H
12 
13 #include <cstdarg>
14 #include <cstdio>
15 #include <map>
16 #include <string>
17 #include <sstream>
18 #include <iostream>
19 #include <stdexcept>
20 #include <mrpt/config.h>
21 #include <mrpt/utils/compiler_fixes.h> // Disable MSVC warning 4251 in this class
22 
23 namespace mrpt
24 {
25 namespace utils
26 {
27 /** For usage when passing a dynamic number of (numeric) arguments to a
28  * function, by name.
29  * \code
30  * TParameters<double> p; // or TParametersDouble
31  * p["v_max"] = 1.0; // Write
32  * ...
33  * cout << p["w_max"]; // Read, even if "p" was const.
34  * \endcode
35  *
36  * A default list of parameters can be passed to the constructor as a sequence
37  * of pairs "name, value", which MUST end in a nullptr name string. Names
38  * MUST BE "const char*"
39  * (that is, "old plain strings" are OK), not std::string objects!.
40  * See this example:
41  *
42  * \code
43  * TParameters<double> p("par1",2.0, "par2",-4.5, "par3",9.0, nullptr); //
44  * MUST end with a NULL
45  * \endcode
46  *
47  * <b>VERY IMPORTANT:</b> If you use the NULL-ended constructor above, make
48  * sure all the values are of the proper
49  * type or it will crash in runtime. For example, in a TParametersDouble all
50  * values must be double's, so
51  * if you type "10" the compiler will make it an "int". Instead, write
52  * "10.0".
53  * \ingroup mrpt_base_grp
54  * \sa the example in MRPT/samples/params-by-name
55  */
56 template <typename T>
57 struct TParameters : public std::map<std::string, T>
58 {
59  typedef std::map<std::string, T> BASE;
60  /** Default constructor (initializes empty) */
61  TParameters() : BASE() {}
62  /** Constructor with a list of initial values (see the description and use
63  * example in mrpt::utils::TParameters) */
64  TParameters(const char* nam1, ...) : BASE()
65  {
66  if (!nam1) return; // No parameters
67  T val;
68  va_list args;
69  va_start(args, nam1);
70  // 1st one out of the loop:
71  val = va_arg(args, T);
73  // Loop until NULL:
74  const char* nam;
75  do
76  {
77  nam = va_arg(args, const char*);
78  if (nam)
79  {
80  val = va_arg(args, T);
82  }
83  } while (nam);
84  va_end(args);
85  }
86  inline bool has(const std::string& s) const
87  {
89  }
90  /** A const version of the [] operator, for usage as read-only.
91  * \exception std::logic_error On parameter not present. Please, check
92  * existence with "has" before reading.
93  */
94  inline T operator[](const std::string& s) const
95  {
96  typename BASE::const_iterator it = BASE::find(s);
97  if (BASE::end() == it)
98  throw std::logic_error(
99  std::string("Parameter '") + s +
100  std::string("' is not present.").c_str());
101  return it->second;
102  }
103  /** A const version of the [] operator and with a default value in case the
104  * parameter is not set (for usage as read-only).
105  */
106  inline T getWithDefaultVal(const std::string& s, const T& defaultVal) const
107  {
108  typename BASE::const_iterator it = BASE::find(s);
109  if (BASE::end() == it)
110  return defaultVal;
111  else
112  return it->second;
113  }
114  /** The write (non-const) version of the [] operator. */
115  inline T& operator[](const std::string& s) { return BASE::operator[](s); }
116  /** Dumps to console the output from getAsString() */
117  inline void dumpToConsole() const
118  {
119  ::fputs(getAsString().c_str(), stdout);
120  }
121 
122  /** Returns a multi-line string representation of the parameters like : 'nam
123  * = val\nnam2 = val2...' */
124  inline std::string getAsString() const
125  {
126  std::string s;
127  getAsString(s);
128  return s;
129  }
130 
131  /** Returns a multi-line string representation of the parameters like : 'nam
132  * = val\nnam2 = val2...' */
133  void getAsString(std::string& s) const
134  {
135  size_t maxStrLen = 10;
136  for (typename BASE::const_iterator it = BASE::begin();
137  it != BASE::end(); ++it)
138  maxStrLen = std::max(maxStrLen, it->first.size());
139  maxStrLen++;
140  std::stringstream str;
141  for (typename BASE::const_iterator it = BASE::begin();
142  it != BASE::end(); ++it)
143  str << it->first << std::string(maxStrLen - it->first.size(), ' ')
144  << " = " << it->second << std::endl;
145  s = str.str();
146  }
147 };
148 
149 /** See the generic template mrpt::utils::TParameters */
151 /** See the generic template mrpt::utils::TParameters */
153 
154 } // end namespace
155 }
156 
157 #endif
std::map< std::string, T > BASE
Definition: TParameters.h:59
For usage when passing a dynamic number of (numeric) arguments to a function, by name.
Definition: TParameters.h:57
std::string getAsString() const
Returns a multi-line string representation of the parameters like : &#39;nam = val = val2...&#39;.
Definition: TParameters.h:124
TParameters< double > TParametersDouble
See the generic template mrpt::utils::TParameters.
Definition: TParameters.h:150
EIGEN_STRONG_INLINE iterator begin()
Definition: eigen_plugins.h:29
const Scalar * const_iterator
Definition: eigen_plugins.h:27
const_iterator find(const KEY &key) const
Definition: ts_hash_map.h:220
GLdouble s
Definition: glext.h:3676
GLuint GLuint end
Definition: glext.h:3528
VALUE & operator[](const KEY &key)
Write/read via [i] operator, that creates an element if it didn&#39;t exist already.
Definition: ts_hash_map.h:200
TParameters< std::string > TParametersString
See the generic template mrpt::utils::TParameters.
Definition: TParameters.h:152
int val
Definition: mrpt_jpeglib.h:955
void dumpToConsole() const
Dumps to console the output from getAsString()
Definition: TParameters.h:117
TParameters(const char *nam1,...)
Constructor with a list of initial values (see the description and use example in mrpt::utils::TParam...
Definition: TParameters.h:64
T operator[](const std::string &s) const
A const version of the [] operator, for usage as read-only.
Definition: TParameters.h:94
GLsizei const GLchar ** string
Definition: glext.h:4101
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
T & operator[](const std::string &s)
The write (non-const) version of the [] operator.
Definition: TParameters.h:115
T getWithDefaultVal(const std::string &s, const T &defaultVal) const
A const version of the [] operator and with a default value in case the parameter is not set (for usa...
Definition: TParameters.h:106
TParameters()
Default constructor (initializes empty)
Definition: TParameters.h:61
bool has(const std::string &s) const
Definition: TParameters.h:86
void getAsString(std::string &s) const
Returns a multi-line string representation of the parameters like : &#39;nam = val = val2...&#39;.
Definition: TParameters.h:133



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019