Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef __POINTMATCHER_PARAMETRIZABLE_H
00037 #define __POINTMATCHER_PARAMETRIZABLE_H
00038
00039 #include <stdexcept>
00040 #include <vector>
00041 #include <map>
00042 #include <set>
00043 #include <string>
00044 #include <boost/lexical_cast.hpp>
00045 #include <limits>
00046
00047
00048 namespace PointMatcherSupport
00049 {
00051 template<typename Target>
00052 inline Target lexical_cast_scalar_to_string(const std::string& arg)
00053 {
00054 if (arg == "inf")
00055 return std::numeric_limits<Target>::infinity();
00056 else if (arg == "-inf")
00057 return -std::numeric_limits<Target>::infinity();
00058 else if (arg == "nan")
00059 return std::numeric_limits<Target>::quiet_NaN();
00060 else
00061 return boost::lexical_cast<Target>(arg);
00062 }
00063
00065 template<typename Target, typename Source>
00066 inline Target lexical_cast(const Source& arg)
00067 {
00068 return boost::lexical_cast<Target>(arg);
00069 }
00070
00072 template<>
00073 inline float lexical_cast(const std::string& arg) { return lexical_cast_scalar_to_string<float>(arg); }
00075 template<>
00076 inline double lexical_cast(const std::string& arg) { return lexical_cast_scalar_to_string<double>(arg); }
00077
00078
00079
00081 template<typename S>
00082 std::string toParam(const S& value)
00083 {
00084 return lexical_cast<std::string>(value);
00085 }
00086
00088 struct Parametrizable
00089 {
00091 struct InvalidParameter: std::runtime_error
00092 {
00093 InvalidParameter(const std::string& reason);
00094 };
00095
00097 typedef bool(*LexicalComparison)(std::string a, std::string b);
00098
00100 template<typename S>
00101 static bool Comp(std::string a, std::string b)
00102 {
00103 return lexical_cast<S>(a) < lexical_cast<S>(b);
00104 }
00105
00107 struct ParameterDoc
00108 {
00109 std::string name;
00110 std::string doc;
00111 std::string defaultValue;
00112 std::string minValue;
00113 std::string maxValue;
00114 LexicalComparison comp;
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 ParameterDoc(const std::string& name, const std::string& doc, const std::string& defaultValue, const std::string& minValue, const std::string& maxValue, LexicalComparison comp);
00128 ParameterDoc(const std::string& name, const std::string& doc, const std::string& defaultValue);
00129
00130 friend std::ostream& operator<< (std::ostream& o, const ParameterDoc& p);
00131 };
00132
00134 struct ParametersDoc : public std::vector<ParameterDoc>
00135 {
00136 ParametersDoc(std::initializer_list<ParameterDoc> list);
00137 ParametersDoc();
00138
00139 friend std::ostream& operator<< (std::ostream& o, const ParametersDoc& p);
00140 };
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 typedef std::string Parameter;
00152 typedef std::map<std::string, Parameter> Parameters;
00153 typedef std::set<std::string> ParametersUsed;
00154
00155 const std::string className;
00156 const ParametersDoc parametersDoc;
00157 Parameters parameters;
00158 ParametersUsed parametersUsed;
00159
00160 Parametrizable();
00161 Parametrizable(const std::string& className, const ParametersDoc paramsDoc, const Parameters& params);
00162 virtual ~Parametrizable();
00163
00164 std::string getParamValueString(const std::string& paramName);
00165
00167 template<typename S>
00168 S get(const std::string& paramName) { return lexical_cast<S>(getParamValueString(paramName)); }
00169
00171 friend std::ostream& operator<< (std::ostream& o, const Parametrizable& p) { o << p.parametersDoc; return o; }
00172 };
00173 }
00174
00175 #endif // __POINTMATCHER_PARAMETRIZABLE_H