All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
src/ompl/base/GenericParam.h
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2011, Willow Garage
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Willow Garage nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 /* Author: Ioan Sucan */
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 &param);
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> &params) const;
00237 
00239             void getParamNames(std::vector<std::string> &params) 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
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines