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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020