Go to the documentation of this file.
36 #ifndef __POINTMATCHER_PARAMETRIZABLE_H
37 #define __POINTMATCHER_PARAMETRIZABLE_H
44 #include <boost/lexical_cast.hpp>
46 #define BOOST_ASSIGN_MAX_PARAMS 6
47 #include <boost/assign/list_inserter.hpp>
48 #include <boost/algorithm/string.hpp>
54 template<
typename Target>
58 return std::numeric_limits<Target>::infinity();
59 else if (arg ==
"-inf")
60 return -std::numeric_limits<Target>::infinity();
61 else if (arg ==
"nan")
62 return std::numeric_limits<Target>::quiet_NaN();
64 return boost::lexical_cast<Target>(arg);
68 template<
typename Target>
71 return lexical_cast_scalar_to_string<Target>(
std::string(arg));
76 template<
typename Target,
typename Source>
79 return boost::lexical_cast<Target>(arg);
87 if (cleanedInput.find(
'[') != 0)
89 throw std::runtime_error(
"Vector parameter '" + arg +
"' must start with '['");
91 if (cleanedInput.find(
']') != cleanedInput.size()-1)
93 throw std::runtime_error(
"Vector parameter '" + arg +
"' must end with ']'");
95 cleanedInput.erase(std::remove(cleanedInput.begin(), cleanedInput.end(),
'['), cleanedInput.end());
96 cleanedInput.erase(std::remove(cleanedInput.begin(), cleanedInput.end(),
' '), cleanedInput.end());
97 cleanedInput.erase(std::remove(cleanedInput.begin(), cleanedInput.end(),
']'), cleanedInput.end());
100 std::vector<std::string> tokens;
101 boost::algorithm::split(tokens, cleanedInput, boost::algorithm::is_any_of(
","));
103 std::vector<T> result;
104 result.reserve(tokens.size());
105 for(
const auto& token: tokens)
107 auto value = lexical_cast_scalar_to_string<T>(token);
108 result.push_back(value);
125 return lexical_cast<std::string>(value);
132 std::ostringstream oss;
134 std::copy(input.begin(), input.end() - 1,
135 std::ostream_iterator<T>(oss,
", "));
136 oss << input.back() <<
"]";
156 return lexical_cast<S>(a) < lexical_cast<S>(b);
226 #endif // __POINTMATCHER_PARAMETRIZABLE_H
friend std::ostream & operator<<(std::ostream &o, const Parametrizable &p)
Dump the documentation of this object to a stream.
LexicalComparison comp
pointer to comparison function
Parametrizable()
Construct a documentation of parameters from a description in the source.
std::string toParam(const S &value)
Return the string value using lexical_cast.
const ParametersDoc parametersDoc
documentation of parameters
std::string doc
short documentation
ParametersUsed parametersUsed
parameters whose value has actually been read
S get(const std::string ¶mName)
Return the value of paramName, lexically-casted to S.
std::ostream & operator<<(std::ostream &o, const Parametrizable::ParameterDoc &p)
Dump the documentation of this parameter to a stream.
friend std::ostream & operator<<(std::ostream &o, const ParameterDoc &p)
Dump the documentation of this parameter to a stream.
std::set< std::string > ParametersUsed
Parameters whose value has been read.
bool(* LexicalComparison)(std::string a, std::string b)
A function that returns whether a is smaller than b.
Target lexical_cast_scalar_to_string(const std::string &arg)
A lexical casting function that is an improvements over boost::lexical_cast that can handle "inf",...
std::vector< ParameterDoc > ParametersDoc
The documentation of all parameters.
std::string defaultValue
default value
Parameters parameters
parameters with their values encoded in string
ParameterDoc(const std::string &name, const std::string &doc, const std::string &defaultValue, const std::string &minValue, const std::string &maxValue, LexicalComparison comp)
Construct a parameter documentation with bounds.
std::string maxValue
if bounds are checked, maxmimu value
static bool Comp(std::string a, std::string b)
Return whether a < b, lexically casted to S.
std::vector< T > getVector(const std::string ¶mName)
Return the value of paramName, lexically-casted std::vector<S>
An exception thrown when one tries to fetch the value of an unexisting parameter.
std::string getParamValueString(const std::string ¶mName)
Get the value of a parameter, as a string.
virtual ~Parametrizable()
Virtual destructor, do nothing.
InvalidParameter(const std::string &reason)
Construct an invalid-parameter exception.
std::string Parameter
alias
The superclass of classes that are constructed using generic parameters. This class provides the para...
The documentation of a parameter.
Target lexical_cast(const Source &arg)
General case of lexical cast, use boost.
Functions and classes that are not dependant on scalar type are defined in this namespace.
std::string minValue
if bounds are checked, minimum value
const std::string className
name of the class
std::map< std::string, Parameter > Parameters
Parameters stored as a map of string->string.
std::vector< T > lexical_cast_vector(const std::string &arg)
Special case of lexical cast to std::vector<T>