5#ifndef DUNE_PARAMETERTREE_HH
6#define DUNE_PARAMETERTREE_HH
64 bool hasKey(
const std::string& key)
const;
74 bool hasSub(
const std::string&
sub)
const;
85 std::string&
operator[] (
const std::string& key);
97 const std::string&
operator[] (
const std::string& key)
const;
107 void report(std::ostream& stream = std::cout,
108 const std::string& prefix =
"")
const;
136 std::string
get(
const std::string& key,
const std::string& defaultValue)
const;
148 std::string
get(
const std::string& key,
const char* defaultValue)
const;
161 T
get(
const std::string& key,
const T& defaultValue)
const {
177 T
get(
const std::string& key)
const {
180 <<
"' not found in ParameterTree (prefix " +
prefix_ +
")");
182 return Parser<T>::parse((*
this)[key]);
187 <<
"\" for key \"" <<
prefix_ <<
"." << key <<
"\""
221 std::map<std::string, ParameterTree>
subs_;
223 static std::string
ltrim(
const std::string& s);
224 static std::string
rtrim(
const std::string& s);
225 static std::vector<std::string>
split(
const std::string & s);
228 template<
class Iterator>
230 Iterator it,
const Iterator &end)
232 typedef typename std::iterator_traits<Iterator>::value_type Value;
233 std::istringstream s(str);
235 s.imbue(std::locale::classic());
237 for(; it != end; ++it, ++n) {
241 << className<Value>()
242 <<
" (" << n <<
" items were extracted successfully)");
247 if(not s.fail() or not s.eof())
249 << n <<
" items of type "
250 << className<Value>() <<
" (more items than the range can hold)");
255 struct ParameterTree::Parser {
256 static T parse(
const std::string& str) {
258 std::istringstream s(str);
260 s.imbue(std::locale::classic());
263 DUNE_THROW(RangeError,
" as a " << className<T>());
267 if ((! s.fail()) || (! s.eof()))
268 DUNE_THROW(RangeError,
" as a " << className<T>());
276 template<
typename traits,
typename Allocator>
277 struct ParameterTree::Parser<
std::basic_string<char, traits, Allocator> > {
278 static std::basic_string<char, traits, Allocator>
281 return std::basic_string<char, traits, Allocator>(trimmed.begin(),
287 struct ParameterTree::Parser< bool > {
291 return std::tolower(c, std::locale::classic());
297 std::string ret = str;
299 std::transform(ret.begin(), ret.end(), ret.begin(), ToLower());
301 if (ret ==
"yes" || ret ==
"true")
304 if (ret ==
"no" || ret ==
"false")
307 return (Parser<int>::parse(ret) != 0);
311 template<
typename T,
int n>
321 template<
typename T, std::
size_t n>
322 struct ParameterTree::Parser<
std::array<T, n> > {
323 static std::array<T, n>
325 std::array<T, n> val;
331 template<std::
size_t n>
332 struct ParameterTree::Parser<
std::bitset<n> > {
333 static std::bitset<n>
336 std::vector<std::string>
sub =
split(str);
339 <<
"because of unmatching size " <<
sub.size());
340 for (std::size_t i=0; i<n; ++i) {
341 val[i] = ParameterTree::Parser<bool>::parse(
sub[i]);
347 template<
typename T,
typename A>
348 struct ParameterTree::Parser<
std::vector<T, A> > {
349 static std::vector<T, A>
351 std::vector<std::string>
sub =
split(str);
352 std::vector<T, A> vec;
353 for (
unsigned int i=0; i<
sub.size(); ++i) {
354 T val = ParameterTree::Parser<T>::parse(
sub[i]);
A few common exception classes.
A free function to provide the demangled class name of a given object or type as a string.
Implements a vector constructed from a given type representing a field and a compile-time given size.
const char * what() const noexcept override
output internal message buffer
Definition exceptions.cc:37
#define DUNE_THROW(E, m)
Definition exceptions.hh:218
Dune namespace.
Definition alignedallocator.hh:13
vector space out of a tensor product of fields.
Definition fvector.hh:91
Iterator begin()
begin iterator
Definition densevector.hh:347
Iterator end()
end iterator
Definition densevector.hh:353
Default exception class for range errors.
Definition exceptions.hh:254
Hierarchical structure of string parameters.
Definition parametertree.hh:37
KeyVector valueKeys_
Definition parametertree.hh:217
std::map< std::string, ParameterTree > subs_
Definition parametertree.hh:221
static const ParameterTree empty_
Definition parametertree.hh:213
static std::string rtrim(const std::string &s)
Definition parametertree.cc:213
std::string get(const std::string &key, const std::string &defaultValue) const
get value as string
Definition parametertree.cc:188
static std::string ltrim(const std::string &s)
Definition parametertree.cc:204
void report(std::ostream &stream=std::cout, const std::string &prefix="") const
print distinct substructure to stream
Definition parametertree.cc:32
std::vector< std::string > KeyVector
storage for key lists
Definition parametertree.hh:50
static void parseRange(const std::string &str, Iterator it, const Iterator &end)
Definition parametertree.hh:229
ParameterTree()
Create new empty ParameterTree.
Definition parametertree.cc:20
std::string & operator[](const std::string &key)
get value reference for key
Definition parametertree.cc:153
KeyVector subKeys_
Definition parametertree.hh:218
ParameterTree & sub(const std::string &sub)
get substructure by name
Definition parametertree.cc:106
bool hasKey(const std::string &key) const
test for key
Definition parametertree.cc:51
const KeyVector & getSubKeys() const
get substructure keys
Definition parametertree.cc:243
T get(const std::string &key) const
Get value.
Definition parametertree.hh:177
bool hasSub(const std::string &sub) const
test for substructure
Definition parametertree.cc:79
std::map< std::string, std::string > values_
Definition parametertree.hh:220
T get(const std::string &key, const T &defaultValue) const
get value converted to a certain type
Definition parametertree.hh:161
std::string prefix_
Definition parametertree.hh:215
static std::vector< std::string > split(const std::string &s)
Definition parametertree.cc:222
const KeyVector & getValueKeys() const
get value keys
Definition parametertree.cc:238
static std::basic_string< char, traits, Allocator > parse(const std::string &str)
Definition parametertree.hh:279
static bool parse(const std::string &str)
Definition parametertree.hh:296
char operator()(char c)
Definition parametertree.hh:289
static FieldVector< T, n > parse(const std::string &str)
Definition parametertree.hh:314
static std::array< T, n > parse(const std::string &str)
Definition parametertree.hh:324
static std::bitset< n > parse(const std::string &str)
Definition parametertree.hh:334
static std::vector< T, A > parse(const std::string &str)
Definition parametertree.hh:350