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