/usr/include/ompl/base/GenericParam.h is in libompl-dev 0.14.2+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | /*********************************************************************
* Software License Agreement (BSD License)
*
* Copyright (c) 2011, Willow Garage
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the Willow Garage nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/
/* Author: Ioan Sucan */
#ifndef OMPL_BASE_GENERIC_PARAM_
#define OMPL_BASE_GENERIC_PARAM_
#include "ompl/util/Console.h"
#include "ompl/util/ClassForward.h"
#include <boost/function.hpp>
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <map>
namespace ompl
{
namespace base
{
/// @cond IGNORE
/** \brief Forward declaration of ompl::base::GenericParam */
OMPL_CLASS_FORWARD(GenericParam);
/// @endcond
/** \brief Motion planning algorithms often employ parameters
to guide their exploration process. (e.g., goal
biasing). Motion planners (and some of their components)
use this class to declare what the parameters are, in a
generic way, so that they can be set externally. */
class GenericParam
{
public:
/** \brief The constructor of a parameter takes the name of the parameter (\e name) */
GenericParam(const std::string &name) : name_(name)
{
}
virtual ~GenericParam(void)
{
}
/** \brief Get the name of the parameter */
const std::string& getName(void) const
{
return name_;
}
/** \brief Set the name of the parameter */
void setName(const std::string &name)
{
name_ = name;
}
/** \brief Set the value of the parameter. The value is taken in as a string, but converted to the type of that parameter. */
virtual bool setValue(const std::string &value) = 0;
/** \brief Retrieve the value of the parameter, as a string. */
virtual std::string getValue(void) const = 0;
/** \brief Assignment operator by type. This is just for convenience, as it just calls setValue() */
template<typename T>
GenericParam& operator=(const T &value)
{
try
{
setValue(boost::lexical_cast<std::string>(value));
}
catch (boost::bad_lexical_cast &e)
{
OMPL_WARN("Invalid value format specified for parameter '%s': %s", name_.c_str(), e.what());
}
return *this;
}
/** \brief Set a suggested range */
void setRangeSuggestion(const std::string &rangeSuggestion)
{
rangeSuggestion_ = rangeSuggestion;
}
/** \brief Get the suggested range of values */
const std::string& getRangeSuggestion(void) const
{
return rangeSuggestion_;
}
protected:
/** \brief The name of the parameter */
std::string name_;
/** \brief Suggested range for the parameter
This can be used to provide a hint to, e.g., a GUI. The
convention used in OMPL is to denote ranges for the
following types as follows:
- \c bool: "0,1"
- \c enum: "<enum_val0>,<enum_val1>,<enum_val2>,..."
- \c int, \c double: either "first:last" or "first:stepsize:last".
In the first case, the stepsize is assumed to be 1. It is
important to use floating point representations for double
ranges (i.e., "1." instead of "1") to make sure the type is
deduced correctly.
*/
std::string rangeSuggestion_;
};
/** \brief This is a helper class that instantiates parameters with different data types. */
template<typename T>
class SpecificParam : public GenericParam
{
public:
/** \brief The type for the 'setter' function for this parameter */
typedef boost::function<void(T)> SetterFn;
/** \brief The type for the 'getter' function for this parameter */
typedef boost::function<T()> GetterFn;
/** \brief An explicit instantiation of a parameter \e name requires the \e setter function and optionally the \e
getter function. */
SpecificParam(const std::string &name, const SetterFn &setter, const GetterFn &getter = GetterFn()) :
GenericParam(name), setter_(setter), getter_(getter)
{
if (!setter_)
OMPL_ERROR("Setter function must be specified for parameter");
}
virtual ~SpecificParam(void)
{
}
virtual bool setValue(const std::string &value)
{
bool result = true;
try
{
if (setter_)
setter_(boost::lexical_cast<T>(value));
}
catch (boost::bad_lexical_cast &e)
{
result = false;
OMPL_WARN("Invalid value format specified for parameter '%s': %s", name_.c_str(), e.what());
}
if (getter_)
OMPL_DEBUG("The value of parameter '%s' is now: '%s'", name_.c_str(), getValue().c_str());
else
OMPL_DEBUG("The value of parameter '%s' was set to: '%s'", name_.c_str(), value.c_str());
return result;
}
virtual std::string getValue(void) const
{
if (getter_)
try
{
return boost::lexical_cast<std::string>(getter_());
}
catch (boost::bad_lexical_cast &e)
{
OMPL_WARN("Unable to parameter '%s' to string: %s", name_.c_str(), e.what());
return "";
}
else
return "";
}
protected:
/** \brief The setter function for this parameter */
SetterFn setter_;
/** \brief The getter function for this parameter */
GetterFn getter_;
};
/** \brief Maintain a set of parameters */
class ParamSet
{
public:
/** \brief This function declares a parameter \e name, and specifies the \e setter and \e getter functions. */
template<typename T>
void declareParam(const std::string &name, const typename SpecificParam<T>::SetterFn &setter,
const typename SpecificParam<T>::GetterFn &getter = typename SpecificParam<T>::GetterFn())
{
params_[name].reset(new SpecificParam<T>(name, setter, getter));
}
/** \brief Add a parameter to the set */
void add(const GenericParamPtr ¶m);
/** \brief Remove a parameter from the set */
void remove(const std::string &name);
/** \brief Include the params of a different ParamSet into this one. Optionally include a prefix for each of the parameters */
void include(const ParamSet &other, const std::string &prefix = "");
/** \brief Algorithms in OMPL often have parameters that
can be set externally. While each algorithm will have
their own getter and setter functions specifically for
those parameters, this function allows setting
parameters generically, for any algorithm that
declares parameters, by specifying the parameter name
\e key and its value \e value (both as string, but \e
value is cast to the type desired by the corresponding
setter). Under the hood, this calls SpecificParam::setValue().
This ability makes it easy to automatically configure
using external sources (e.g., a configuration
file). The function returns true if the parameter was
parsed and set successfully and false otherwise. */
bool setParam(const std::string &key, const std::string &value);
/** \brief Get the value of the parameter named \e key. Store the value as string in \e value and return true if the parameter was found. Return false otherwise. */
bool getParam(const std::string &key, std::string &value) const;
/** \brief Set the values for a set of parameters. The parameter names are the keys in the map \e kv.
The corresponding key values in \e kv are set as the parameter values.
Return true if all parameters were set successfully. This function simply calls setParam() multiple times.
If \e ignoreUnknown is true, then no attempt is made to set unknown
parameters (and thus no errors are reported) */
bool setParams(const std::map<std::string, std::string> &kv, bool ignoreUnknown = false);
/** \brief Get the known parameter as a map from names to their values cast as string */
void getParams(std::map<std::string, std::string> ¶ms) const;
/** \brief List the names of the known parameters */
void getParamNames(std::vector<std::string> ¶ms) const;
/** \brief List the values of the known parameters, in the same order as getParamNames() */
void getParamValues(std::vector<std::string> &vals) const;
/** \brief Get the map from parameter names to parameter descriptions */
const std::map<std::string, GenericParamPtr>& getParams(void) const;
/** \brief Get the parameter that corresponds to a specified name. An empty shared ptr is returned if the parameter does not exist */
const GenericParamPtr& getParam(const std::string &key) const;
/** \brief Check whether this set of parameters includes the parameter named \e key */
bool hasParam(const std::string &key) const;
/** \brief Access operator for parameters, by name. If the parameter is not defined, an exception is thrown */
GenericParam& operator[](const std::string &key);
/** \brief Get the number of parameters maintained by this instance */
std::size_t size(void) const
{
return params_.size();
}
/** \brief Clear all the set parameters */
void clear(void);
/** \brief Print the parameters to a stream */
void print(std::ostream &out) const;
private:
std::map<std::string, GenericParamPtr> params_;
};
}
}
#endif
|