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



Page generated by Doxygen 1.8.14 for MRPT 1.4.0 SVN: at Sat Jul 14 16:13:21 UTC 2018