Main MRPT website > C++ reference
MRPT logo
TParameters.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 
29 #ifndef mrpt_utils_parameters_H
30 #define mrpt_utils_parameters_H
31 
32 #include <cstdarg>
33 #include <map>
34 #include <string>
35 
36 namespace mrpt
37 {
38  namespace utils
39  {
40  /** For usage when passing a dynamic number of (numeric) arguments to a function, by name.
41  * \code
42  * TParameters<double> p; // or TParametersDouble
43  * p["v_max"] = 1.0; // Write
44  * ...
45  * cout << p["w_max"]; // Read, even if "p" was const.
46  * \endcode
47  *
48  * A default list of parameters can be passed to the constructor as a sequence
49  * of pairs "name, value", which MUST end in a NULL name string. Names MUST BE "const char*"
50  * (that is, "old plain strings" are OK), not std::string objects!.
51  * See this example:
52  *
53  * \code
54  * TParameters<double> p("par1",2.0, "par2",-4.5, "par3",9.0, NULL); // MUST end with a NULL
55  * \endcode
56  *
57  * <b>VERY IMPORTANT:</b> If you use the NULL-ended constructor above, make sure all the values are of the proper
58  * type or it will crash in runtime. For example, in a TParametersDouble all values must be double's, so
59  * if you type "10" the compiler will make it an "int". Instead, write "10.0".
60  * \ingroup mrpt_base_grp
61  * \sa the example in MRPT/samples/params-by-name
62  */
63  template <typename T>
64  struct TParameters : public std::map<std::string,T>
65  {
66  typedef std::map<std::string,T> BASE;
67  /** Default constructor (initializes empty) */
68  TParameters() : BASE() { }
69  /** Constructor with a list of initial values (see the description and use example in mrpt::utils::TParameters) */
70  TParameters(const char *nam1,...) : BASE() {
71  if (!nam1) return; // No parameters
72  T val;
73  va_list args;
74  va_start(args,nam1);
75  // 1st one out of the loop:
76  val = va_arg(args,T);
77  BASE::operator[](std::string(nam1)) = val;
78  // Loop until NULL:
79  const char *nam;
80  do{
81  nam = va_arg(args,const char*);
82  if (nam) {
83  val = va_arg(args,T);
84  BASE::operator[](std::string(nam)) = val;
85  }
86  } while (nam);
87  va_end(args);
88  }
89  inline bool has(const std::string &s) const { return std::map<std::string,T>::end()!=BASE::find(s); }
90  /** A const version of the [] operator, for usage as read-only.
91  * \exception std::logic_error On parameter not present. Please, check existence with "has" before reading.
92  */
93  inline T operator[](const std::string &s) const {
94  typename BASE::const_iterator it =BASE::find(s);
95  if (BASE::end()==it)
96  throw std::logic_error(std::string("Parameter '")+s+std::string("' is not present.").c_str());
97  return it->second;
98  }
99  /** A const version of the [] operator and with a default value in case the parameter is not set (for usage as read-only).
100  */
101  inline T getWithDefaultVal(const std::string &s, const T& defaultVal) const {
102  typename BASE::const_iterator it =BASE::find(s);
103  if (BASE::end()==it)
104  return defaultVal;
105  else return it->second;
106  }
107  /** The write (non-const) version of the [] operator. */
108  inline T & operator[](const std::string &s) { return BASE::operator[](s); }
109 
110  /** Dumps to console the output from getAsString() */
111  inline void dumpToConsole() const { std::cout << getAsString(); }
112 
113  /** Returns a multi-like string representation of the parameters like : 'nam = val\nnam2 = val2...' */
114  inline std::string getAsString() const { std::string s; getAsString(s); return s; }
115 
116  /** Returns a multi-like string representation of the parameters like : 'nam = val\nnam2 = val2...' */
117  void getAsString(std::string &s) const {
118  size_t maxStrLen = 10;
119  for (typename BASE::const_iterator it=BASE::begin();it!=BASE::end();++it) maxStrLen = std::max(maxStrLen, it->first.size() );
120  maxStrLen++;
121  std::stringstream str;
122  for (typename BASE::const_iterator it=BASE::begin();it!=BASE::end();++it)
123  str << it->first << std::string(maxStrLen-it->first.size(),' ') << " = " << it->second << std::endl;
124  s = str.str();
125  }
126  };
127 
128  typedef TParameters<double> TParametersDouble; //!< See the generic template mrpt::utils::TParameters
129  typedef TParameters<std::string> TParametersString; //!< See the generic template mrpt::utils::TParameters
130 
131  } // end namespace
132 }
133 
134 #endif
135 



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013