Main MRPT website > C++ reference for MRPT 1.5.3
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 
21 namespace mrpt
22 {
23  namespace utils
24  {
25  /** For usage when passing a dynamic number of (numeric) arguments to a 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 NULL name string. Names MUST BE "const char*"
35  * (that is, "old plain strings" are OK), not std::string objects!.
36  * See this example:
37  *
38  * \code
39  * TParameters<double> p("par1",2.0, "par2",-4.5, "par3",9.0, NULL); // MUST end with a NULL
40  * \endcode
41  *
42  * <b>VERY IMPORTANT:</b> If you use the NULL-ended constructor above, make sure all the values are of the proper
43  * type or it will crash in runtime. For example, in a TParametersDouble all values must be double's, so
44  * if you type "10" the compiler will make it an "int". Instead, write "10.0".
45  * \ingroup mrpt_base_grp
46  * \sa the example in MRPT/samples/params-by-name
47  */
48  template <typename T>
49  struct TParameters : public std::map<std::string,T>
50  {
51  typedef std::map<std::string,T> BASE;
52  /** Default constructor (initializes empty) */
53  TParameters() : BASE() { }
54  /** Constructor with a list of initial values (see the description and use example in mrpt::utils::TParameters) */
55  TParameters(const char *nam1,...) : BASE() {
56  if (!nam1) return; // No parameters
57  T val;
58  va_list args;
59  va_start(args,nam1);
60  // 1st one out of the loop:
61  val = va_arg(args,T);
62  BASE::operator[](std::string(nam1)) = val;
63  // Loop until NULL:
64  const char *nam;
65  do{
66  nam = va_arg(args,const char*);
67  if (nam) {
68  val = va_arg(args,T);
69  BASE::operator[](std::string(nam)) = val;
70  }
71  } while (nam);
72  va_end(args);
73  }
74  inline bool has(const std::string &s) const { return std::map<std::string,T>::end()!=BASE::find(s); }
75  /** A const version of the [] operator, for usage as read-only.
76  * \exception std::logic_error On parameter not present. Please, check existence with "has" before reading.
77  */
78  inline T operator[](const std::string &s) const {
79  typename BASE::const_iterator it =BASE::find(s);
80  if (BASE::end()==it)
81  throw std::logic_error(std::string("Parameter '")+s+std::string("' is not present.").c_str());
82  return it->second;
83  }
84  /** A const version of the [] operator and with a default value in case the parameter is not set (for usage as read-only).
85  */
86  inline T getWithDefaultVal(const std::string &s, const T& defaultVal) const {
87  typename BASE::const_iterator it =BASE::find(s);
88  if (BASE::end()==it)
89  return defaultVal;
90  else return it->second;
91  }
92  /** The write (non-const) version of the [] operator. */
93  inline T & operator[](const std::string &s) { return BASE::operator[](s); }
94 
95  /** Dumps to console the output from getAsString() */
96  inline void dumpToConsole() const { ::fputs(getAsString().c_str(),stdout); }
97 
98  /** Returns a multi-line string representation of the parameters like : 'nam = val\nnam2 = val2...' */
99  inline std::string getAsString() const { std::string s; getAsString(s); return s; }
100 
101  /** Returns a multi-line string representation of the parameters like : 'nam = val\nnam2 = val2...' */
102  void getAsString(std::string &s) const {
103  size_t maxStrLen = 10;
104  for (typename BASE::const_iterator it=BASE::begin();it!=BASE::end();++it) maxStrLen = std::max(maxStrLen, it->first.size() );
105  maxStrLen++;
106  std::stringstream str;
107  for (typename BASE::const_iterator it=BASE::begin();it!=BASE::end();++it)
108  str << it->first << std::string(maxStrLen-it->first.size(),' ') << " = " << it->second << std::endl;
109  s = str.str();
110  }
111  };
112 
113  typedef TParameters<double> TParametersDouble; //!< See the generic template mrpt::utils::TParameters
114  typedef TParameters<std::string> TParametersString; //!< See the generic template mrpt::utils::TParameters
115 
116  } // end namespace
117 }
118 
119 #endif
120 
For usage when passing a dynamic number of (numeric) arguments to a function, by name.
Definition: TParameters.h:49
std::string getAsString() const
Returns a multi-line string representation of the parameters like : &#39;nam = val = val2...&#39;.
Definition: TParameters.h:99
EIGEN_STRONG_INLINE iterator end()
Definition: eigen_plugins.h:27
TParameters< double > TParametersDouble
See the generic template mrpt::utils::TParameters.
Definition: TParameters.h:113
EIGEN_STRONG_INLINE iterator begin()
Definition: eigen_plugins.h:26
const Scalar * const_iterator
Definition: eigen_plugins.h:24
const_iterator find(const KEY &key) const
Definition: ts_hash_map.h:139
T max(const T v0, const T v1)
Definition: exprtk.hpp:1331
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:123
TParameters< std::string > TParametersString
See the generic template mrpt::utils::TParameters.
Definition: TParameters.h:114
void dumpToConsole() const
Dumps to console the output from getAsString()
Definition: TParameters.h:96
TParameters(const char *nam1,...)
Constructor with a list of initial values (see the description and use example in mrpt::utils::TParam...
Definition: TParameters.h:55
T operator[](const std::string &s) const
A const version of the [] operator, for usage as read-only.
Definition: TParameters.h:78
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:93
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:86
std::map< std::string, T > BASE
Definition: TParameters.h:51
TParameters()
Default constructor (initializes empty)
Definition: TParameters.h:53
bool has(const std::string &s) const
Definition: TParameters.h:74
void getAsString(std::string &s) const
Returns a multi-line string representation of the parameters like : &#39;nam = val = val2...&#39;.
Definition: TParameters.h:102



Page generated by Doxygen 1.8.13 for MRPT 1.5.3 at Mon Oct 30 10:27:08 UTC 2017