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
00037 #ifndef OMPL_BASE_GENERIC_PARAM_
00038 #define OMPL_BASE_GENERIC_PARAM_
00039
00040 #include "ompl/util/Console.h"
00041 #include "ompl/util/ClassForward.h"
00042 #include <boost/function.hpp>
00043 #include <boost/lexical_cast.hpp>
00044 #include <iostream>
00045 #include <string>
00046 #include <vector>
00047 #include <map>
00048
00049 namespace ompl
00050 {
00051 namespace base
00052 {
00053
00055
00056 ClassForward(GenericParam);
00058
00064 class GenericParam
00065 {
00066 public:
00067
00069 GenericParam(const std::string &name) : name_(name)
00070 {
00071 }
00072
00073 virtual ~GenericParam(void)
00074 {
00075 }
00076
00078 const std::string& getName(void) const
00079 {
00080 return name_;
00081 }
00082
00084 void setName(const std::string &name)
00085 {
00086 name_ = name;
00087 }
00088
00090 virtual bool setValue(const std::string &value) = 0;
00091
00093 virtual std::string getValue(void) const = 0;
00094
00096 template<typename T>
00097 GenericParam& operator=(const T &value)
00098 {
00099 try
00100 {
00101 setValue(boost::lexical_cast<std::string>(value));
00102 }
00103 catch (boost::bad_lexical_cast &e)
00104 {
00105 logWarn("Invalid value format specified for parameter '%s': %s", name_.c_str(), e.what());
00106 }
00107 return *this;
00108 }
00109
00110 protected:
00111
00113 std::string name_;
00114 };
00115
00116
00118 template<typename T>
00119 class SpecificParam : public GenericParam
00120 {
00121 public:
00122
00124 typedef boost::function<void(T)> SetterFn;
00125
00127 typedef boost::function<T()> GetterFn;
00128
00131 SpecificParam(const std::string &name, const SetterFn &setter, const GetterFn &getter = GetterFn()) :
00132 GenericParam(name), setter_(setter), getter_(getter)
00133 {
00134 if (!setter_)
00135 logError("Setter function must be specified for parameter");
00136 }
00137
00138 virtual ~SpecificParam(void)
00139 {
00140 }
00141
00142 virtual bool setValue(const std::string &value)
00143 {
00144 bool result = true;
00145 try
00146 {
00147 if (setter_)
00148 setter_(boost::lexical_cast<T>(value));
00149 }
00150 catch (boost::bad_lexical_cast &e)
00151 {
00152 result = false;
00153 logWarn("Invalid value format specified for parameter '%s': %s", name_.c_str(), e.what());
00154 }
00155
00156 if (getter_)
00157 logDebug("The value of parameter '%s' is now: '%s'", name_.c_str(), getValue().c_str());
00158 else
00159 logDebug("The value of parameter '%s' was set to: '%s'", name_.c_str(), value.c_str());
00160 return result;
00161 }
00162
00163 virtual std::string getValue(void) const
00164 {
00165 if (getter_)
00166 try
00167 {
00168 return boost::lexical_cast<std::string>(getter_());
00169 }
00170 catch (boost::bad_lexical_cast &e)
00171 {
00172 logWarn("Unable to parameter '%s' to string: %s", name_.c_str(), e.what());
00173 return "";
00174 }
00175 else
00176 return "";
00177 }
00178
00179 protected:
00180
00182 SetterFn setter_;
00183
00185 GetterFn getter_;
00186 };
00187
00189 class ParamSet
00190 {
00191 public:
00192
00194 template<typename T>
00195 void declareParam(const std::string &name, const typename SpecificParam<T>::SetterFn &setter,
00196 const typename SpecificParam<T>::GetterFn &getter = typename SpecificParam<T>::GetterFn())
00197 {
00198 params_[name].reset(new SpecificParam<T>(name, setter, getter));
00199 }
00200
00202 void add(const GenericParamPtr ¶m);
00203
00205 void remove(const std::string &name);
00206
00208 void include(const ParamSet &other, const std::string &prefix = "");
00209
00223 bool setParam(const std::string &key, const std::string &value);
00224
00226 bool getParam(const std::string &key, std::string &value) const;
00227
00233 bool setParams(const std::map<std::string, std::string> &kv, bool ignoreUnknown = false);
00234
00236 void getParams(std::map<std::string, std::string> ¶ms) const;
00237
00239 void getParamNames(std::vector<std::string> ¶ms) const;
00240
00242 void getParamValues(std::vector<std::string> &vals) const;
00243
00245 const std::map<std::string, GenericParamPtr>& getParams(void) const;
00246
00248 const GenericParamPtr& getParam(const std::string &key) const;
00249
00251 bool hasParam(const std::string &key) const;
00252
00254 GenericParam& operator[](const std::string &key);
00255
00257 std::size_t size(void) const
00258 {
00259 return params_.size();
00260 }
00261
00263 void clear(void);
00264
00266 void print(std::ostream &out) const;
00267
00268 private:
00269
00270 std::map<std::string, GenericParamPtr> params_;
00271 };
00272 }
00273 }
00274
00275 #endif