MRPT  1.9.9
TParameters.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/config.h>
12 #include <mrpt/core/common.h> // Disable MSVC warning 4251 in this class
13 #include <algorithm> // std::max
14 #include <cstdarg>
15 #include <cstdio>
16 #include <iostream>
17 #include <map>
18 #include <sstream>
19 #include <stdexcept>
20 #include <string>
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>
55 {
56  using BASE = std::map<std::string, T>;
58  using iterator = typename BASE::iterator;
59  using const_iterator = typename BASE::const_iterator;
60 
61  /** Default constructor (initializes empty) */
63  /** Constructor with a list of initial values (see the description and use
64  * example in mrpt::system::TParameters) */
65  TParameters(std::initializer_list<typename BASE::value_type> init)
66  : base(init)
67  {
68  }
69  inline bool has(const std::string& s) const
70  {
71  return base.end() != base.find(s);
72  }
73  /** A const version of the [] operator, for usage as read-only.
74  * \exception std::logic_error On parameter not present. Please, check
75  * existence with "has" before reading.
76  */
77  inline T operator[](const std::string& s) const
78  {
79  using namespace std::string_literals;
80  auto it = base.find(s);
81  if (base.end() == it)
82  throw std::logic_error(
83  ("Parameter '"s + s + "' is not present."s).c_str());
84  return it->second;
85  }
86  /** A const version of the [] operator and with a default value in case the
87  * parameter is not set (for usage as read-only).
88  */
89  template <typename RET>
90  inline RET getWithDefaultVal(
91  const std::string& s, const RET& defaultVal) const
92  {
93  auto it = base.find(s);
94  if (base.end() == it)
95  return defaultVal;
96  else
97  return static_cast<RET>(it->second);
98  }
99  /** The write (non-const) version of the [] operator. */
100  inline T& operator[](const std::string& s) { return base[s]; }
101  /** Dumps to console the output from getAsString() */
102  inline void dumpToConsole() const
103  {
104  ::fputs(getAsString().c_str(), stdout);
105  }
106 
107  /** Returns a multi-line string representation of the parameters like : 'nam
108  * = val\nnam2 = val2...' */
109  inline std::string getAsString() const
110  {
111  std::string s;
112  getAsString(s);
113  return s;
114  }
115 
116  /** Returns a multi-line string representation of the parameters like : 'nam
117  * = val\nnam2 = val2...' */
118  void getAsString(std::string& s) const
119  {
120  size_t maxStrLen = 10;
121  for (const auto& e : *this)
122  maxStrLen = std::max(maxStrLen, e.first.size());
123  maxStrLen++;
124  std::stringstream str;
125  for (const auto& e : *this)
126  str << e.first << std::string(maxStrLen - e.first.size(), ' ')
127  << " = " << e.second << std::endl;
128  s = str.str();
129  }
130 
131  inline void clear() { base.clear(); }
132  iterator find(const std::string& key) { return base.find(key); }
133  const_iterator find(const std::string& key) const { return base.find(key); }
134  iterator begin() noexcept { return base.begin(); }
135  const_iterator begin() const noexcept { return base.begin(); }
136  iterator end() noexcept { return base.end(); }
137  const_iterator end() const noexcept { return base.end(); }
138 };
139 
140 /** See the generic template mrpt::system::TParameters */
142 /** See the generic template mrpt::system::TParameters */
144 
145 } // namespace mrpt::system
iterator begin() noexcept
Definition: TParameters.h:134
TParameters()
Default constructor (initializes empty)
Definition: TParameters.h:62
iterator end() noexcept
Definition: TParameters.h:136
const_iterator begin() const noexcept
Definition: TParameters.h:135
GLdouble s
Definition: glext.h:3682
bool has(const std::string &s) const
Definition: TParameters.h:69
const_iterator end() const noexcept
Definition: TParameters.h:137
std::map< std::string, double > BASE
Definition: TParameters.h:56
std::string getAsString() const
Returns a multi-line string representation of the parameters like : &#39;nam = val = val2...&#39;.
Definition: TParameters.h:109
GLsizei const GLchar ** string
Definition: glext.h:4116
T & operator[](const std::string &s)
The write (non-const) version of the [] operator.
Definition: TParameters.h:100
iterator find(const std::string &key)
Definition: TParameters.h:132
T operator[](const std::string &s) const
A const version of the [] operator, for usage as read-only.
Definition: TParameters.h:77
RET getWithDefaultVal(const std::string &s, const RET &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:90
const_iterator find(const std::string &key) const
Definition: TParameters.h:133
TParameters(std::initializer_list< typename BASE::value_type > init)
Constructor with a list of initial values (see the description and use example in mrpt::system::TPara...
Definition: TParameters.h:65
void dumpToConsole() const
Dumps to console the output from getAsString()
Definition: TParameters.h:102
typename BASE::const_iterator const_iterator
Definition: TParameters.h:59
typename BASE::iterator iterator
Definition: TParameters.h:58
For usage when passing a dynamic number of (numeric) arguments to a function, by name.
Definition: TParameters.h:54
void getAsString(std::string &s) const
Returns a multi-line string representation of the parameters like : &#39;nam = val = val2...&#39;.
Definition: TParameters.h:118



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019