All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
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), msg_(&msgDefault_)
00070             {
00071             }
00072 
00075             GenericParam(const std::string &name, const msg::Interface &context) : name_(name), msg_(&context)
00076             {
00077             }
00078 
00079             virtual ~GenericParam(void)
00080             {
00081             }
00082 
00084             const std::string& getName(void) const
00085             {
00086                 return name_;
00087             }
00088 
00090             void setName(const std::string &name)
00091             {
00092                 name_ = name;
00093             }
00094 
00096             virtual bool setValue(const std::string &value) = 0;
00097 
00099             virtual std::string getValue(void) const = 0;
00100 
00101         private:
00102 
00103             msg::Interface msgDefault_;
00104 
00105         protected:
00106 
00108             std::string           name_;
00109 
00111             const msg::Interface *msg_;
00112         };
00113 
00114 
00116         template<typename T>
00117         class SpecificParam : public GenericParam
00118         {
00119         public:
00120 
00122             typedef boost::function<void(T)> SetterFn;
00123 
00125             typedef boost::function<T()>     GetterFn;
00126 
00129             SpecificParam(const std::string &name, const SetterFn &setter, const GetterFn &getter = GetterFn()) :
00130                 GenericParam(name), setter_(setter), getter_(getter)
00131             {
00132             }
00133 
00134 
00137             SpecificParam(const std::string &name, const msg::Interface &context, const SetterFn &setter, const GetterFn &getter = GetterFn()) :
00138                 GenericParam(name, context), setter_(setter), getter_(getter)
00139             {
00140             }
00141 
00142             virtual ~SpecificParam(void)
00143             {
00144             }
00145 
00146             virtual bool setValue(const std::string &value)
00147             {
00148                 bool result = true;
00149                 try
00150                 {
00151                     setter_(boost::lexical_cast<T>(value));
00152                 }
00153                 catch (boost::bad_lexical_cast &e)
00154                 {
00155                     result = false;
00156                     msg_->warn("Invalid value format specified for parameter '%s': %s", name_.c_str(), e.what());
00157                 }
00158 
00159                 if (getter_)
00160                     msg_->debug("The value of parameter '" + name_ + "' is now: '" + getValue() + "'");
00161                 else
00162                     msg_->debug("The value of parameter '" + name_ + "' was set to: '" + value + "'");
00163                 return result;
00164             }
00165 
00166             virtual std::string getValue(void) const
00167             {
00168                 if (getter_)
00169                     try
00170                     {
00171                         return boost::lexical_cast<std::string>(getter_());
00172                     }
00173                     catch (boost::bad_lexical_cast &e)
00174                     {
00175                         msg_->warn("Unable to parameter '%s' to string: %s", name_.c_str(), e.what());
00176                         return "";
00177                     }
00178                 else
00179                     return "";
00180             }
00181 
00182         protected:
00183 
00185             SetterFn setter_;
00186 
00188             GetterFn getter_;
00189         };
00190 
00192         class ParamSet
00193         {
00194         public:
00195 
00197             template<typename T>
00198             void declareParam(const std::string &name, const msg::Interface &context, const typename SpecificParam<T>::SetterFn &setter,
00199                               const typename SpecificParam<T>::GetterFn &getter = typename SpecificParam<T>::GetterFn())
00200             {
00201                 params_[name].reset(new SpecificParam<T>(name, context, setter, getter));
00202             }
00203 
00205             template<typename T>
00206             void declareParam(const std::string &name, const typename SpecificParam<T>::SetterFn &setter,
00207                               const typename SpecificParam<T>::GetterFn &getter = typename SpecificParam<T>::GetterFn())
00208             {
00209                 params_[name].reset(new SpecificParam<T>(name, setter, getter));
00210             }
00211 
00213             void add(const GenericParamPtr &param);
00214 
00216             void remove(const std::string &name);
00217 
00219             void include(const ParamSet &other, const std::string &prefix = "");
00220 
00234             bool setParam(const std::string &key, const std::string &value);
00235 
00237             bool getParam(const std::string &key, std::string &value) const;
00238 
00244             bool setParams(const std::map<std::string, std::string> &kv, bool ignoreUnknown = false);
00245 
00247             void getParams(std::map<std::string, std::string> &params) const;
00248 
00250             void getParamNames(std::vector<std::string> &params) const;
00251 
00253             void getParamValues(std::vector<std::string> &vals) const;
00254 
00256             const std::map<std::string, GenericParamPtr>& getParams(void) const;
00257 
00259             const GenericParamPtr& getParam(const std::string &key) const;
00260 
00262             bool hasParam(const std::string &key) const;
00263 
00265             std::size_t size(void) const
00266             {
00267                 return params_.size();
00268             }
00269 
00271             void clear(void);
00272 
00274             void print(std::ostream &out) const;
00275 
00276         private:
00277 
00278             std::map<std::string, GenericParamPtr> params_;
00279         };
00280     }
00281 }
00282 
00283 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends