All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
GenericParam.cpp
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 #include "ompl/base/GenericParam.h"
00038 
00039 bool ompl::base::ParamSet::setParam(const std::string &key, const std::string &value)
00040 {
00041     std::map<std::string, GenericParamPtr>::const_iterator it = params_.find(key);
00042     if (it != params_.end())
00043         return it->second->setValue(value);
00044     else
00045     {
00046         msg::Interface msg;
00047         msg.error("Parameter '" + key + "' was not found");
00048         return false;
00049     }
00050 }
00051 
00052 bool ompl::base::ParamSet::setParams(const std::map<std::string, std::string> &kv, bool ignoreUnknown)
00053 {
00054     bool result = true;
00055     for (std::map<std::string, std::string>::const_iterator it = kv.begin() ; it != kv.end() ; ++it)
00056     {
00057         if (ignoreUnknown)
00058             if (!hasParam(it->first))
00059                 continue;
00060         bool r = setParam(it->first, it->second);
00061         result = result && r;
00062     }
00063     return result;
00064 }
00065 
00066 bool ompl::base::ParamSet::getParam(const std::string &key, std::string &value) const
00067 {
00068     std::map<std::string, GenericParamPtr>::const_iterator it = params_.find(key);
00069     if (it != params_.end())
00070     {
00071         value = it->second->getValue();
00072         return true;
00073     }
00074     return false;
00075 }
00076 
00077 void ompl::base::ParamSet::getParamNames(std::vector<std::string> &params) const
00078 {
00079     params.clear();
00080     params.reserve(params_.size());
00081     for (std::map<std::string, GenericParamPtr>::const_iterator it = params_.begin() ; it != params_.end() ; ++it)
00082         params.push_back(it->first);
00083 }
00084 
00085 void ompl::base::ParamSet::getParamValues(std::vector<std::string> &vals) const
00086 {
00087     std::vector<std::string> names;
00088     getParamNames(names);
00089     vals.resize(names.size());
00090     for (std::size_t i = 0 ; i < names.size() ; ++i)
00091         vals[i] = params_.find(names[i])->second->getValue();
00092 }
00093 
00094 const std::map<std::string, ompl::base::GenericParamPtr>& ompl::base::ParamSet::getParams(void) const
00095 {
00096     return params_;
00097 }
00098 
00099 const ompl::base::GenericParamPtr& ompl::base::ParamSet::getParam(const std::string &key) const
00100 {
00101     static GenericParamPtr empty;
00102     std::map<std::string, GenericParamPtr>::const_iterator it = params_.find(key);
00103     if (it != params_.end())
00104         return it->second;
00105     else
00106         return empty;
00107 }
00108 
00109 void ompl::base::ParamSet::getParams(std::map<std::string, std::string> &params) const
00110 {
00111     for (std::map<std::string, GenericParamPtr>::const_iterator it = params_.begin() ; it != params_.end() ; ++it)
00112         params[it->first] = it->second->getValue();
00113 }
00114 
00115 bool ompl::base::ParamSet::hasParam(const std::string &key) const
00116 {
00117     return params_.find(key) != params_.end();
00118 }
00119 
00120 void ompl::base::ParamSet::include(const ParamSet &other, const std::string &prefix)
00121 {
00122     const std::map<std::string, GenericParamPtr> &p = other.getParams();
00123     if (prefix.empty())
00124         for (std::map<std::string, GenericParamPtr>::const_iterator it = p.begin() ; it != p.end() ; ++it)
00125             params_[it->first] = it->second;
00126     else
00127         for (std::map<std::string, GenericParamPtr>::const_iterator it = p.begin() ; it != p.end() ; ++it)
00128             params_[prefix + "." + it->first] = it->second;
00129 }
00130 
00131 void ompl::base::ParamSet::add(const GenericParamPtr &param)
00132 {
00133     params_[param->getName()] = param;
00134 }
00135 
00136 void ompl::base::ParamSet::remove(const std::string &name)
00137 {
00138     params_.erase(name);
00139 }
00140 
00141 void ompl::base::ParamSet::clear(void)
00142 {
00143     params_.clear();
00144 }
00145 
00146 void ompl::base::ParamSet::print(std::ostream &out) const
00147 {
00148     for (std::map<std::string, GenericParamPtr>::const_iterator it = params_.begin() ; it != params_.end() ; ++it)
00149         out << it->first << " = " << it->second->getValue() << std::endl;
00150 }