35 #ifndef _Wolframe_CONFIG_VALUE_PARSER_HPP_INCLUDED
36 #define _Wolframe_CONFIG_VALUE_PARSER_HPP_INCLUDED
41 #include <boost/type_traits.hpp>
42 #include <boost/lexical_cast.hpp>
43 #include <boost/algorithm/string.hpp>
44 #include <boost/utility/value_init.hpp>
45 #include <boost/type_traits/is_enum.hpp>
46 #include <boost/utility.hpp>
47 #include <boost/limits.hpp>
56 template <
typename T>
typename boost::enable_if<boost::is_enum<T>, T>::type
increment(
const T& i) {
return (T)((int)i + 1); }
57 template <
typename T>
typename boost::disable_if<boost::is_enum<T>, T>::type
increment(
const T& i) { T rt = i;
return ++rt; }
80 template <
typename ValueType>
81 bool check(
const ValueType&,
string&)
const
91 template <
typename ValueType>
92 bool parse( ValueType& value,
const string& token,
string& explanation)
const
94 using boost::bad_lexical_cast;
97 value = boost::lexical_cast<ValueType>( token);
99 catch (bad_lexical_cast& e)
101 explanation = e.what();
111 template <
typename DomainValueType>
117 RangeDomain(
const DomainValueType& min_,
const DomainValueType& max_=std::numeric_limits<DomainValueType>::max()) :
m_min(min_),
m_max(max_){}
123 rt.append( boost::lexical_cast<string>(
m_min));
125 rt.append( boost::lexical_cast<string>(
m_max));
134 template <
typename ValueType>
135 bool check(
const ValueType& val,
string& explanation)
const
137 if (val < m_min || val >
m_max)
139 explanation =
"value out of range, expected value in the range of ";
153 template <
typename DomainValueType>
163 template <
typename ValueType>
164 bool check(
const ValueType& val,
string& explanation)
const
166 ValueType empty = boost::value_initialized<ValueType>();
169 explanation =
"value expected to be other than '";
170 explanation.append( boost::lexical_cast<std::string>( empty));
171 explanation.append(
"'");
192 for (ii=0; ii<
m_size; ii++)
194 if (ii>0) rt.append(
", ");
196 rt.append( boost::lexical_cast<string>(
m_ar[ii]));
208 template <
typename ValueType>
209 bool parse( ValueType& value,
const string& token,
string& explanation)
const
212 value = boost::value_initialized<ValueType>();
216 if (boost::algorithm::iequals( token,
m_ar[ii]))
221 explanation =
"must be one of ";
241 static const char* ar[
NofBooleanEnum ] = {
"false",
"true",
"0",
"1",
"off",
"on",
"no",
"yes",
"deny",
"allow"};
248 return (booleanEnumIdx & 0x01);
259 bool parse(
bool& value,
const string& token,
string& explanation)
const
261 unsigned int enumval = 0;
279 template <
typename Value,
class Domain>
280 static bool getValue(
const char* module,
const char* name,
const string& token, Value& value,
const Domain& domain,
bool* isDefined=0)
282 using std::bad_alloc;
283 using std::exception;
290 LOG_ERROR <<
loggingScope(module) <<
"duplicate definition of configuration element '" << name <<
"'";
294 string errorExplanation;
295 if (domain.parse( value, token, errorExplanation) && domain.check( value, errorExplanation))
297 if (isDefined) *isDefined =
true;
300 LOG_ERROR <<
loggingScope(module) <<
"invalid value '" << token <<
"' for configuration element '" << name <<
"' (" << errorExplanation <<
")";
305 LOG_ERROR <<
loggingScope(module) <<
"out of memory when parsing configuration element '" << name <<
"'";
310 LOG_ERROR <<
loggingScope(module) <<
"illegal value for configuration element '" << name <<
"' (" << e.what() <<
")";
324 template <
typename Value>
325 static bool getValue(
const char* module,
const char* name,
const string& token, Value& value,
bool* isDefined=0)
327 return getValue<Value,BaseTypeDomain>( module, name, token, value,
BaseTypeDomain(), isDefined);
342 template <
typename Value,
class Domain>
343 static bool getValue(
const char* module,
const std::pair<const std::string, const types::PropertyTree::Node>& decl, Value& value,
const Domain& domain,
bool* isDefined=0)
347 if (decl.second.begin() != decl.second.end())
349 LOG_ERROR <<
loggingScope(module) <<
"atomic value expected for configuration element '" << decl.first.c_str() <<
"'";
352 return getValue( module, decl.first.c_str(), decl.second.data(), value, domain, isDefined);
354 catch (std::bad_alloc&)
356 LOG_ERROR <<
loggingScope(module) <<
"out of memory when parsing configuration element '" << decl.first.c_str() <<
"'";
371 template <
typename Value>
372 static bool getValue(
const char* module,
const std::pair<const std::string, const types::PropertyTree::Node>& decl, Value& value,
bool* isDefined=0)
374 return getValue<Value,BaseTypeDomain>( module, decl, value,
BaseTypeDomain(), isDefined);
394 #endif // _Wolframe_CONFIG_VALUE_PARSER_HPP_INCLUDED
Top-level include file for logging (version 1)
bool check(const ValueType &val, string &explanation) const
Checks if the configuration value is in the expected domain, e.g. non empty.
Definition: valueParser.hpp:164
DomainValueType m_min
Definition: valueParser.hpp:146
static bool getValue(const char *module, const char *name, const string &token, Value &value, const Domain &domain, bool *isDefined=0)
Get the value of a configration token with a domain restriction that is checked.
Definition: valueParser.hpp:280
BaseTypeDomain()
Constructor.
Definition: valueParser.hpp:76
static bool getBooleanValue(unsigned int booleanEnumIdx)
Returns the boolean value of an accepted token.
Definition: valueParser.hpp:246
bool check(const ValueType &, string &) const
Checks if the configuration value is in the expected domain.
Definition: valueParser.hpp:81
static const std::string loggingScope(const char *scope)
Return the header of a configuration parse error message issued, specifying the definition scope of t...
Definition: valueParser.hpp:380
bool parse(bool &value, const string &token, string &explanation) const
Parses a boolean value.
Definition: valueParser.hpp:259
boost::enable_if< boost::is_enum< T >, T >::type increment(const T &i)
Definition: valueParser.hpp:56
std::string string
Definition: valueParser.hpp:66
Describes a value domain for a range of values.
Definition: valueParser.hpp:112
Definition: valueParser.hpp:235
static bool getValue(const char *module, const std::pair< const std::string, const types::PropertyTree::Node > &decl, Value &value, const Domain &domain, bool *isDefined=0)
Get the value of a configration token with a domain restriction that is checked.
Definition: valueParser.hpp:343
bool check(const ValueType &val, string &explanation) const
Checks if the configuration value is in the expected domain.
Definition: valueParser.hpp:135
Configuration parser for parsing the tokens of the configuration.
Definition: valueParser.hpp:63
bool parse(ValueType &value, const string &token, string &explanation) const
Parses the token string and maps it to its value representation.
Definition: valueParser.hpp:92
Describes the value domain of a boolean value as enumeration of all possible values.
Definition: valueParser.hpp:232
#define LOG_ERROR
Definition: logger-v1.hpp:109
NonEmptyDomain()
constructor
Definition: valueParser.hpp:157
unsigned int m_size
Definition: valueParser.hpp:226
Definition: valueParser.hpp:154
Describes a value domain for an enumerable fixed set of values.
Definition: valueParser.hpp:180
string getDomainDescription() const
Get the description of the set of values allowed as string for the error log message.
Definition: valueParser.hpp:188
BoolDomain()
Constructor.
Definition: valueParser.hpp:253
static bool getValue(const char *module, const char *name, const string &token, Value &value, bool *isDefined=0)
Get the value of a configration token without additional domain restriction.
Definition: valueParser.hpp:325
Interface to a key value tree based on boost::property_tree::ptree with position info for better erro...
static bool getValue(const char *module, const std::pair< const std::string, const types::PropertyTree::Node > &decl, Value &value, bool *isDefined=0)
Get the value of a configration token without additional domain restriction.
Definition: valueParser.hpp:372
const char ** m_ar
Definition: valueParser.hpp:227
bool parse(ValueType &value, const string &token, string &explanation) const
Parses the token string and maps it to its value representation.
Definition: valueParser.hpp:209
static const char ** booleanEnum()
returns the accepted string representations of a boolean
Definition: valueParser.hpp:239
DomainValueType m_max
Definition: valueParser.hpp:147
string getDomainDescription() const
Get the description of the set of values allowed as string for the error log message.
Definition: valueParser.hpp:120
Describes a domain without value restriction except the one given by the C++ value type itself...
Definition: valueParser.hpp:73
EnumDomain(unsigned int nofelems, const char **elems)
Constructor.
Definition: valueParser.hpp:185
RangeDomain(const DomainValueType &min_, const DomainValueType &max_=std::numeric_limits< DomainValueType >::max())
Constructor.
Definition: valueParser.hpp:117