00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://www.mrpt.org/ | 00005 | | 00006 | Copyright (C) 2005-2011 University of Malaga | 00007 | | 00008 | This software was written by the Machine Perception and Intelligent | 00009 | Robotics Lab, University of Malaga (Spain). | 00010 | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> | 00011 | | 00012 | This file is part of the MRPT project. | 00013 | | 00014 | MRPT is free software: you can redistribute it and/or modify | 00015 | it under the terms of the GNU General Public License as published by | 00016 | the Free Software Foundation, either version 3 of the License, or | 00017 | (at your option) any later version. | 00018 | | 00019 | MRPT is distributed in the hope that it will be useful, | 00020 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00021 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 00022 | GNU General Public License for more details. | 00023 | | 00024 | You should have received a copy of the GNU General Public License | 00025 | along with MRPT. If not, see <http://www.gnu.org/licenses/>. | 00026 | | 00027 +---------------------------------------------------------------------------+ */ 00028 00029 #ifndef mrpt_utils_parameters_H 00030 #define mrpt_utils_parameters_H 00031 00032 #include <cstdarg> 00033 #include <map> 00034 #include <string> 00035 00036 namespace mrpt 00037 { 00038 namespace utils 00039 { 00040 /** For usage when passing a dynamic number of (numeric) arguments to a function, by name. 00041 * \code 00042 * TParameters<double> p; // or TParametersDouble 00043 * p["v_max"] = 1.0; // Write 00044 * ... 00045 * cout << p["w_max"]; // Read, even if "p" was const. 00046 * \endcode 00047 * 00048 * A default list of parameters can be passed to the constructor as a sequence 00049 * of pairs "name, value", which MUST end in a NULL name string. Names MUST BE "const char*" 00050 * (that is, "old plain strings" are OK), not std::string objects!. 00051 * See this example: 00052 * 00053 * \code 00054 * TParameters<double> p("par1",2.0, "par2",-4.5, "par3",9.0, NULL); // MUST end with a NULL 00055 * \endcode 00056 * 00057 * <b>VERY IMPORTANT:</b> If you use the NULL-ended constructor above, make sure all the values are of the proper 00058 * type or it will crash in runtime. For example, in a TParametersDouble all values must be double's, so 00059 * if you type "10" the compiler will make it an "int". Instead, write "10.0". 00060 * \ingroup mrpt_base_grp 00061 * \sa the example in MRPT/samples/params-by-name 00062 */ 00063 template <typename T> 00064 struct TParameters : public std::map<std::string,T> 00065 { 00066 typedef std::map<std::string,T> BASE; 00067 /** Default constructor (initializes empty) */ 00068 TParameters() : BASE() { } 00069 /** Constructor with a list of initial values (see the description and use example in mrpt::utils::TParameters) */ 00070 TParameters(const char *nam1,...) : BASE() { 00071 if (!nam1) return; // No parameters 00072 T val; 00073 va_list args; 00074 va_start(args,nam1); 00075 // 1st one out of the loop: 00076 val = va_arg(args,T); 00077 BASE::operator[](std::string(nam1)) = val; 00078 // Loop until NULL: 00079 const char *nam; 00080 do{ 00081 nam = va_arg(args,const char*); 00082 if (nam) { 00083 val = va_arg(args,T); 00084 BASE::operator[](std::string(nam)) = val; 00085 } 00086 } while (nam); 00087 va_end(args); 00088 } 00089 inline bool has(const std::string &s) const { return std::map<std::string,T>::end()!=BASE::find(s); } 00090 /** A const version of the [] operator, for usage as read-only. 00091 * \exception std::logic_error On parameter not present. Please, check existence with "has" before reading. 00092 */ 00093 inline T operator[](const std::string &s) const { 00094 typename BASE::const_iterator it =BASE::find(s); 00095 if (BASE::end()==it) 00096 throw std::logic_error(std::string("Parameter '")+s+std::string("' is not present.").c_str()); 00097 return it->second; 00098 } 00099 /** A const version of the [] operator and with a default value in case the parameter is not set (for usage as read-only). 00100 */ 00101 inline T getWithDefaultVal(const std::string &s, const T& defaultVal) const { 00102 typename BASE::const_iterator it =BASE::find(s); 00103 if (BASE::end()==it) 00104 return defaultVal; 00105 else return it->second; 00106 } 00107 /** The write (non-const) version of the [] operator. */ 00108 inline T & operator[](const std::string &s) { return BASE::operator[](s); } 00109 00110 /** Dumps to console the output from getAsString() */ 00111 inline void dumpToConsole() const { std::cout << getAsString(); } 00112 00113 /** Returns a multi-like string representation of the parameters like : 'nam = val\nnam2 = val2...' */ 00114 inline std::string getAsString() const { std::string s; getAsString(s); return s; } 00115 00116 /** Returns a multi-like string representation of the parameters like : 'nam = val\nnam2 = val2...' */ 00117 void getAsString(std::string &s) const { 00118 size_t maxStrLen = 10; 00119 for (typename BASE::const_iterator it=BASE::begin();it!=BASE::end();++it) maxStrLen = std::max(maxStrLen, it->first.size() ); 00120 maxStrLen++; 00121 std::stringstream str; 00122 for (typename BASE::const_iterator it=BASE::begin();it!=BASE::end();++it) 00123 str << it->first << std::string(maxStrLen-it->first.size(),' ') << " = " << it->second << std::endl; 00124 s = str.str(); 00125 } 00126 }; 00127 00128 typedef TParameters<double> TParametersDouble; //!< See the generic template mrpt::utils::TParameters 00129 typedef TParameters<std::string> TParametersString; //!< See the generic template mrpt::utils::TParameters 00130 00131 } // end namespace 00132 } 00133 00134 #endif 00135
| Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011 |