Go to the documentation of this file.
3 #ifndef DUNE_PARAMETERTREE_HH
4 #define DUNE_PARAMETERTREE_HH
45 typedef std::vector<std::string>
KeyVector;
59 bool hasKey(
const std::string& key)
const;
69 bool hasSub(
const std::string&
sub)
const;
80 std::string&
operator[] (
const std::string& key);
92 const std::string&
operator[] (
const std::string& key)
const;
102 void report(std::ostream& stream = std::cout,
103 const std::string& prefix =
"")
const;
131 std::string
get(
const std::string& key,
const std::string& defaultValue)
const;
143 std::string
get(
const std::string& key,
const char* defaultValue)
const;
156 T
get(
const std::string& key,
const T& defaultValue)
const {
172 T
get(
const std::string& key)
const {
175 <<
"' not found in ParameterTree (prefix " +
prefix_ +
")");
177 return Parser<T>::parse((*
this)[key]);
182 <<
"\" for key \"" <<
prefix_ <<
"." << key <<
"\""
216 std::map<std::string, ParameterTree>
subs_;
218 static std::string
ltrim(
const std::string& s);
219 static std::string
rtrim(
const std::string& s);
220 static std::vector<std::string>
split(
const std::string & s);
223 template<
class Iterator>
225 Iterator it,
const Iterator &end)
227 typedef typename std::iterator_traits<Iterator>::value_type Value;
228 std::istringstream s(str);
230 s.imbue(std::locale::classic());
232 for(; it != end; ++it, ++n) {
236 << className<Value>()
237 <<
" (" << n <<
" items were extracted successfully)");
242 if(not s.fail() or not s.eof())
244 << n <<
" items of type "
245 << className<Value>() <<
" (more items than the range can hold)");
250 struct ParameterTree::Parser {
251 static T parse(
const std::string& str) {
253 std::istringstream s(str);
255 s.imbue(std::locale::classic());
258 DUNE_THROW(RangeError,
" as a " << className<T>());
262 if ((! s.fail()) || (! s.eof()))
263 DUNE_THROW(RangeError,
" as a " << className<T>());
271 template<
typename traits,
typename Allocator>
272 struct ParameterTree::Parser<std::basic_string<char, traits, Allocator> > {
273 static std::basic_string<char, traits, Allocator>
276 return std::basic_string<char, traits, Allocator>(trimmed.begin(),
282 struct ParameterTree::Parser< bool > {
286 return std::tolower(c, std::locale::classic());
292 std::string ret = str;
294 std::transform(ret.begin(), ret.end(), ret.begin(), ToLower());
296 if (ret ==
"yes" || ret ==
"true")
299 if (ret ==
"no" || ret ==
"false")
302 return (Parser<int>::parse(ret) != 0);
306 template<
typename T,
int n>
316 template<
typename T, std::
size_t n>
317 struct ParameterTree::Parser<std::array<T, n> > {
318 static std::array<T, n>
320 std::array<T, n> val;
326 template<std::
size_t n>
327 struct ParameterTree::Parser<std::bitset<n> > {
328 static std::bitset<n>
331 std::vector<std::string>
sub =
split(str);
334 <<
"because of unmatching size " <<
sub.size());
335 for (std::size_t i=0; i<n; ++i) {
342 template<
typename T,
typename A>
343 struct ParameterTree::Parser<std::vector<T, A> > {
344 static std::vector<T, A>
346 std::vector<std::string>
sub =
split(str);
347 std::vector<T, A> vec;
348 for (
unsigned int i=0; i<
sub.size(); ++i) {
349 T val = ParameterTree::Parser<T>::parse(
sub[i]);
358 #endif // DUNE_PARAMETERTREE_HH
static std::vector< std::string > split(const std::string &s)
Definition: parametertree.cc:217
ParameterTree()
Create new empty ParameterTree.
Definition: parametertree.cc:22
A few common exception classes.
static std::string rtrim(const std::string &s)
Definition: parametertree.cc:208
char operator()(char c)
Definition: parametertree.hh:284
std::string get(const std::string &key, const std::string &defaultValue) const
get value as string
Definition: parametertree.cc:183
KeyVector subKeys_
Definition: parametertree.hh:213
std::map< std::string, std::string > values_
Definition: parametertree.hh:215
static std::array< T, n > parse(const std::string &str)
Definition: parametertree.hh:319
Default exception class for range errors.
Definition: exceptions.hh:252
const char * what() const noexcept override
output internal message buffer
Definition: exceptions.cc:35
static std::string ltrim(const std::string &s)
Definition: parametertree.cc:199
Implements a vector constructed from a given type representing a field and a compile-time given size.
std::string prefix_
Definition: parametertree.hh:210
std::string & operator[](const std::string &key)
get value reference for key
Definition: parametertree.cc:148
A free function to provide the demangled class name of a given object or type as a string.
static bool parse(const std::string &str)
Definition: parametertree.hh:291
T get(const std::string &key, const T &defaultValue) const
get value converted to a certain type
Definition: parametertree.hh:156
const KeyVector & getSubKeys() const
get substructure keys
Definition: parametertree.cc:238
static std::basic_string< char, traits, Allocator > parse(const std::string &str)
Definition: parametertree.hh:274
static std::vector< T, A > parse(const std::string &str)
Definition: parametertree.hh:345
std::map< std::string, ParameterTree > subs_
Definition: parametertree.hh:216
vector space out of a tensor product of fields.
Definition: densematrix.hh:42
const KeyVector & getValueKeys() const
get value keys
Definition: parametertree.cc:233
ParameterTree & sub(const std::string &sub)
get substructure by name
Definition: parametertree.cc:101
KeyVector valueKeys_
Definition: parametertree.hh:212
T get(const std::string &key) const
Get value.
Definition: parametertree.hh:172
static std::bitset< n > parse(const std::string &str)
Definition: parametertree.hh:329
static const ParameterTree empty_
Definition: parametertree.hh:208
std::vector< std::string > KeyVector
storage for key lists
Definition: parametertree.hh:39
Hierarchical structure of string parameters.
Definition: parametertree.hh:34
void report(std::ostream &stream=std::cout, const std::string &prefix="") const
print distinct substructure to stream
Definition: parametertree.cc:27
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
static FieldVector< T, n > parse(const std::string &str)
Definition: parametertree.hh:309
bool hasKey(const std::string &key) const
test for key
Definition: parametertree.cc:46
static void parseRange(const std::string &str, Iterator it, const Iterator &end)
Definition: parametertree.hh:224
Dune namespace.
Definition: alignedallocator.hh:13
bool hasSub(const std::string &sub) const
test for substructure
Definition: parametertree.cc:74