5#ifndef DUNE_PARAMETERTREE_HH
6#define DUNE_PARAMETERTREE_HH
61 bool hasKey(
const std::string& key)
const;
71 bool hasSub(
const std::string&
sub)
const;
82 std::string&
operator[] (
const std::string& key);
94 const std::string&
operator[] (
const std::string& key)
const;
104 void report(std::ostream& stream = std::cout,
105 const std::string& prefix =
"")
const;
133 std::string
get(
const std::string& key,
const std::string& defaultValue)
const;
145 std::string
get(
const std::string& key,
const char* defaultValue)
const;
158 T
get(
const std::string& key,
const T& defaultValue)
const {
174 T
get(
const std::string& key)
const {
177 <<
"' not found in ParameterTree (prefix " +
prefix_ +
")");
179 return Parser<T>::parse((*
this)[key]);
184 <<
"\" for key \"" <<
prefix_ <<
"." << key <<
"\""
218 std::map<std::string, ParameterTree>
subs_;
220 static std::string
ltrim(
const std::string& s);
221 static std::string
rtrim(
const std::string& s);
222 static std::vector<std::string>
split(
const std::string & s);
225 template<
class Iterator>
227 Iterator it,
const Iterator &end)
229 typedef typename std::iterator_traits<Iterator>::value_type Value;
230 std::istringstream s(str);
232 s.imbue(std::locale::classic());
234 for(; it != end; ++it, ++n) {
238 << className<Value>()
239 <<
" (" << n <<
" items were extracted successfully)");
244 if(not s.fail() or not s.eof())
246 << n <<
" items of type "
247 << className<Value>() <<
" (more items than the range can hold)");
252 struct ParameterTree::Parser {
253 static T parse(
const std::string& str) {
255 std::istringstream s(str);
257 s.imbue(std::locale::classic());
260 DUNE_THROW(RangeError,
" as a " << className<T>());
264 if ((! s.fail()) || (! s.eof()))
265 DUNE_THROW(RangeError,
" as a " << className<T>());
273 template<
typename traits,
typename Allocator>
274 struct ParameterTree::Parser<
std::basic_string<char, traits, Allocator> > {
275 static std::basic_string<char, traits, Allocator>
278 return std::basic_string<char, traits, Allocator>(trimmed.begin(),
284 struct ParameterTree::Parser< bool > {
288 return std::tolower(c, std::locale::classic());
294 std::string ret = str;
296 std::transform(ret.begin(), ret.end(), ret.begin(), ToLower());
298 if (ret ==
"yes" || ret ==
"true")
301 if (ret ==
"no" || ret ==
"false")
304 return (Parser<int>::parse(ret) != 0);
308 template<
typename T,
int n>
318 template<
typename T, std::
size_t n>
319 struct ParameterTree::Parser<
std::array<T, n> > {
320 static std::array<T, n>
322 std::array<T, n> val;
328 template<std::
size_t n>
329 struct ParameterTree::Parser<
std::bitset<n> > {
330 static std::bitset<n>
333 std::vector<std::string>
sub =
split(str);
336 <<
"because of unmatching size " <<
sub.size());
337 for (std::size_t i=0; i<n; ++i) {
338 val[i] = ParameterTree::Parser<bool>::parse(
sub[i]);
344 template<
typename T,
typename A>
345 struct ParameterTree::Parser<
std::vector<T, A> > {
346 static std::vector<T, A>
348 std::vector<std::string>
sub =
split(str);
349 std::vector<T, A> vec;
350 for (
unsigned int i=0; i<
sub.size(); ++i) {
351 T val = ParameterTree::Parser<T>::parse(
sub[i]);
Implements a vector constructed from a given type representing a field and a compile-time given size.
A free function to provide the demangled class name of a given object or type as a string.
A few common exception classes.
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:95
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:214
std::map< std::string, ParameterTree > subs_
Definition parametertree.hh:218
static const ParameterTree empty_
Definition parametertree.hh:210
static std::string rtrim(const std::string &s)
Definition parametertree.cc:210
std::string get(const std::string &key, const std::string &defaultValue) const
get value as string
Definition parametertree.cc:185
static std::string ltrim(const std::string &s)
Definition parametertree.cc:201
void report(std::ostream &stream=std::cout, const std::string &prefix="") const
print distinct substructure to stream
Definition parametertree.cc:29
std::vector< std::string > KeyVector
storage for key lists
Definition parametertree.hh:47
static void parseRange(const std::string &str, Iterator it, const Iterator &end)
Definition parametertree.hh:226
ParameterTree()
Create new empty ParameterTree.
Definition parametertree.cc:24
std::string & operator[](const std::string &key)
get value reference for key
Definition parametertree.cc:150
KeyVector subKeys_
Definition parametertree.hh:215
ParameterTree & sub(const std::string &sub)
get substructure by name
Definition parametertree.cc:103
bool hasKey(const std::string &key) const
test for key
Definition parametertree.cc:48
const KeyVector & getSubKeys() const
get substructure keys
Definition parametertree.cc:240
T get(const std::string &key) const
Get value.
Definition parametertree.hh:174
bool hasSub(const std::string &sub) const
test for substructure
Definition parametertree.cc:76
std::map< std::string, std::string > values_
Definition parametertree.hh:217
T get(const std::string &key, const T &defaultValue) const
get value converted to a certain type
Definition parametertree.hh:158
std::string prefix_
Definition parametertree.hh:212
static std::vector< std::string > split(const std::string &s)
Definition parametertree.cc:219
const KeyVector & getValueKeys() const
get value keys
Definition parametertree.cc:235
static std::basic_string< char, traits, Allocator > parse(const std::string &str)
Definition parametertree.hh:276
static bool parse(const std::string &str)
Definition parametertree.hh:293
char operator()(char c)
Definition parametertree.hh:286
static FieldVector< T, n > parse(const std::string &str)
Definition parametertree.hh:311
static std::array< T, n > parse(const std::string &str)
Definition parametertree.hh:321
static std::bitset< n > parse(const std::string &str)
Definition parametertree.hh:331
static std::vector< T, A > parse(const std::string &str)
Definition parametertree.hh:347