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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019