3#ifndef DUNE_PARAMETERTREE_HH
4#define DUNE_PARAMETERTREE_HH
58 bool hasKey(
const std::string& key)
const;
68 bool hasSub(
const std::string&
sub)
const;
79 std::string&
operator[] (
const std::string& key);
91 const std::string&
operator[] (
const std::string& key)
const;
101 void report(std::ostream& stream = std::cout,
102 const std::string& prefix =
"")
const;
129 std::string
get(
const std::string& key,
const std::string& defaultValue)
const;
141 std::string
get(
const std::string& key,
const char* defaultValue)
const;
152 int get(
const std::string& key,
int defaultValue)
const;
163 double get(
const std::string& key,
double defaultValue)
const;
176 T
get(
const std::string& key,
const T& defaultValue)
const {
192 T
get(
const std::string& key)
const {
197 return Parser<T>::parse((*
this)[key]);
201 (*
this)[key] <<
"\" for key \"" << key <<
"\" as a " <<
229 std::map<std::string, std::string> values;
230 std::map<std::string, ParameterTree> subs;
231 static std::string ltrim(
const std::string& s);
232 static std::string rtrim(
const std::string& s);
233 static std::vector<std::string> split(
const std::string & s);
236 template<
class Iterator>
237 static void parseRange(
const std::string &str,
238 Iterator it,
const Iterator &end)
240 typedef typename std::iterator_traits<Iterator>::value_type Value;
241 std::istringstream s(str);
243 for(; it != end; ++it, ++n) {
247 "range of items of type " << className<Value>() <<
" "
248 "(" << n <<
" items were extracted successfully)");
253 if(not s.fail() or not s.eof())
254 DUNE_THROW(RangeError,
"Cannot parse value \"" << str <<
"\" as a "
255 "range of " << n <<
" items of type "
256 << className<Value>() <<
" (more items than the range "
262 struct ParameterTree::Parser {
263 static T parse(
const std::string& str) {
265 std::istringstream s(str);
268 DUNE_THROW(RangeError,
"Cannot parse value \"" << str <<
"\" as a " <<
273 if(not s.fail() or not s.eof())
274 DUNE_THROW(RangeError,
"Cannot parse value \"" << str <<
"\" as a " <<
283 template<
typename traits,
typename Allocator>
284 struct ParameterTree::Parser<
std::basic_string<char, traits, Allocator> > {
285 static std::basic_string<char, traits, Allocator>
286 parse(
const std::string& str) {
287 std::string trimmed = ltrim(rtrim(str));
288 return std::basic_string<char, traits, Allocator>(trimmed.begin(),
294 struct ParameterTree::Parser< bool > {
296 int operator()(
int c)
298 return std::tolower(c);
303 parse(
const std::string& str) {
304 std::string ret = str;
306 std::transform(ret.begin(), ret.end(), ret.begin(), ToLower());
308 if (ret ==
"yes" || ret ==
"true")
311 if (ret ==
"no" || ret ==
"false")
314 return (Parser<int>::parse(ret) != 0);
318 template<
typename T,
int n>
319 struct ParameterTree::Parser<FieldVector<T, n> > {
320 static FieldVector<T, n>
321 parse(
const std::string& str) {
322 FieldVector<T, n> val;
323 parseRange(str, val.begin(), val.end());
328 template<
typename T, std::
size_t n>
329 struct ParameterTree::Parser<array<T, n> > {
331 parse(
const std::string& str) {
333 parseRange(str, val.begin(), val.end());
338 template<
typename T,
typename A>
339 struct ParameterTree::Parser<
std::vector<T, A> > {
340 static std::vector<T, A>
341 parse(
const std::string& str) {
342 std::vector<std::string>
sub = split(str);
343 std::vector<T, A> vec;
344 for (
unsigned int i=0; i<
sub.size(); ++i) {
345 T val = ParameterTree::Parser<T>::parse(
sub[i]);
Fallback implementation of the std::array class (a static array)
Hierarchical structure of string parameters.
Definition: parametertree.hh:34
std::string get(const std::string &key, const std::string &defaultValue) const
get value as string
Definition: parametertree.cc:153
void report(std::ostream &stream=std::cout, const std::string &prefix="") const
print distinct substructure to stream
Definition: parametertree.cc:25
std::vector< std::string > KeyVector
storage for key lists
Definition: parametertree.hh:44
ParameterTree()
Create new empty ParameterTree.
Definition: parametertree.cc:22
std::string & operator[](const std::string &key)
get value reference for key
Definition: parametertree.cc:112
ParameterTree & sub(const std::string &sub)
get substructure by name
Definition: parametertree.cc:78
bool hasKey(const std::string &key) const
test for key
Definition: parametertree.cc:44
const KeyVector & getSubKeys() const
get substructure keys
Definition: parametertree.cc:226
T get(const std::string &key) const
Get value.
Definition: parametertree.hh:192
bool hasSub(const std::string &sub) const
test for substructure
Definition: parametertree.cc:61
T get(const std::string &key, const T &defaultValue) const
get value converted to a certain type
Definition: parametertree.hh:176
const KeyVector & getValueKeys() const
get value keys
Definition: parametertree.cc:221
Default exception class for range errors.
Definition: exceptions.hh:280
A free function to provide the demangled class name of a given object or type as a string.
A few common exception classes.
Implements a vector constructed from a given type representing a field and a compile-time given size.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:244
Dune namespace.
Definition: alignment.hh:14