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



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019