3 #ifndef DUNE_PARAMETERTREE_HH
4 #define DUNE_PARAMETERTREE_HH
44 typedef std::vector<std::string>
KeyVector;
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>
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())
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>
288 return std::basic_string<char, traits, Allocator>(trimmed.begin(),
294 struct ParameterTree::Parser< bool > {
298 return std::tolower(c);
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>
328 template<
typename T, std::
size_t n>
329 struct ParameterTree::Parser<
array<T, n> > {
338 template<
typename T,
typename A>
339 struct ParameterTree::Parser<
std::vector<T, A> > {
340 static std::vector<T, A>
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]);
354 #endif // DUNE_PARAMETERTREE_HH
std::string & operator[](const std::string &key)
get value reference for key
Definition: parametertree.cc:112
static std::basic_string< char, traits, Allocator > parse(const std::string &str)
Definition: parametertree.hh:286
iterator end()
Definition: array.hh:114
static array< T, n > parse(const std::string &str)
Definition: parametertree.hh:331
bool hasSub(const std::string &sub) const
test for substructure
Definition: parametertree.cc:61
Simple fixed size array class. This replaces std::array, if that is not available.
Definition: array.hh:40
Dune namespace.
Definition: alignment.hh:13
static bool parse(const std::string &str)
Definition: parametertree.hh:303
A few common exception classes.
Iterator begin()
begin iterator
Definition: densevector.hh:296
char c
Definition: alignment.hh:37
KeyVector subKeys
Definition: parametertree.hh:227
bool hasKey(const std::string &key) const
test for key
Definition: parametertree.cc:44
A free function to provide the demangled class name of a given object or type as a string...
Iterator end()
end iterator
Definition: densevector.hh:302
static std::string ltrim(const std::string &s)
Definition: parametertree.cc:187
void report(std::ostream &stream=std::cout, const std::string &prefix="") const
print distinct substructure to stream
Definition: parametertree.cc:25
ParameterTree & sub(const std::string &sub)
get substructure by name
Definition: parametertree.cc:78
static std::vector< T, A > parse(const std::string &str)
Definition: parametertree.hh:341
vector space out of a tensor product of fields.
Definition: densematrix.hh:38
std::map< std::string, std::string > values
Definition: parametertree.hh:229
static void parseRange(const std::string &str, Iterator it, const Iterator &end)
Definition: parametertree.hh:237
std::map< std::string, ParameterTree > subs
Definition: parametertree.hh:230
static std::string rtrim(const std::string &s)
Definition: parametertree.cc:196
iterator begin()
Definition: array.hh:104
int operator()(int c)
Definition: parametertree.hh:296
Hierarchical structure of string parameters.
Definition: parametertree.hh:33
std::vector< std::string > KeyVector
storage for key lists
Definition: parametertree.hh:38
Fallback implementation of the std::array class (a static array)
Default exception class for range errors.
Definition: exceptions.hh:280
static FieldVector< T, n > parse(const std::string &str)
Definition: parametertree.hh:321
#define DUNE_THROW(E, m)
Definition: exceptions.hh:244
static std::vector< std::string > split(const std::string &s)
Definition: parametertree.cc:205
Implements a vector constructed from a given type representing a field and a compile-time given size...
const KeyVector & getSubKeys() const
get substructure keys
Definition: parametertree.cc:226
KeyVector valueKeys
Definition: parametertree.hh:226
ParameterTree()
Create new empty ParameterTree.
Definition: parametertree.cc:22
const KeyVector & getValueKeys() const
get value keys
Definition: parametertree.cc:221