3#ifndef DUNE_PARAMETERTREE_HH
4#define DUNE_PARAMETERTREE_HH
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 <<
"\""
215 std::map<std::string, std::string> values_;
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>
224 static void parseRange(
const std::string &str,
225 Iterator it,
const Iterator &end)
227 typedef typename std::iterator_traits<Iterator>::value_type Value;
228 std::istringstream s(str);
230 for(; it != end; ++it, ++n) {
234 << className<Value>()
235 <<
" (" << n <<
" items were extracted successfully)");
240 if(not s.fail() or not s.eof())
242 << n <<
" items of type "
243 << className<Value>() <<
" (more items than the range can hold)");
248 struct ParameterTree::Parser {
249 static T parse(
const std::string& str) {
251 std::istringstream s(str);
253 s.imbue(std::locale::classic());
256 DUNE_THROW(RangeError,
" as a " << className<T>());
260 if ((! s.fail()) || (! s.eof()))
261 DUNE_THROW(RangeError,
" as a " << className<T>());
269 template<
typename traits,
typename Allocator>
270 struct ParameterTree::Parser<
std::basic_string<char, traits, Allocator> > {
271 static std::basic_string<char, traits, Allocator>
272 parse(
const std::string& str) {
273 std::string trimmed = ltrim(rtrim(str));
274 return std::basic_string<char, traits, Allocator>(trimmed.begin(),
280 struct ParameterTree::Parser< bool > {
282 char operator()(
char c)
284 return std::tolower(c, std::locale::classic());
289 parse(
const std::string& str) {
290 std::string ret = str;
292 std::transform(ret.begin(), ret.end(), ret.begin(), ToLower());
294 if (ret ==
"yes" || ret ==
"true")
297 if (ret ==
"no" || ret ==
"false")
300 return (Parser<int>::parse(ret) != 0);
304 template<
typename T,
int n>
305 struct ParameterTree::Parser<FieldVector<T, n> > {
306 static FieldVector<T, n>
307 parse(
const std::string& str) {
308 FieldVector<T, n> val;
309 parseRange(str, val.begin(), val.end());
314 template<
typename T, std::
size_t n>
315 struct ParameterTree::Parser<
std::array<T, n> > {
316 static std::array<T, n>
317 parse(
const std::string& str) {
318 std::array<T, n> val;
319 parseRange(str, val.begin(), val.end());
324 template<std::
size_t n>
325 struct ParameterTree::Parser<
std::bitset<n> > {
326 static std::bitset<n>
327 parse(
const std::string& str) {
329 std::vector<std::string>
sub = split(str);
331 DUNE_THROW(RangeError,
"as a bitset<" << n <<
"> "
332 <<
"because of unmatching size " <<
sub.size());
333 for (std::size_t i=0; i<n; ++i) {
334 val[i] = ParameterTree::Parser<bool>::parse(
sub[i]);
340 template<
typename T,
typename A>
341 struct ParameterTree::Parser<
std::vector<T, A> > {
342 static std::vector<T, A>
343 parse(
const std::string& str) {
344 std::vector<std::string>
sub = split(str);
345 std::vector<T, A> vec;
346 for (
unsigned int i=0; i<
sub.size(); ++i) {
347 T val = ParameterTree::Parser<T>::parse(
sub[i]);
Hierarchical structure of string parameters.
Definition: parametertree.hh:35
std::string get(const std::string &key, const std::string &defaultValue) const
get value as string
Definition: parametertree.cc:183
void report(std::ostream &stream=std::cout, const std::string &prefix="") const
print distinct substructure to stream
Definition: parametertree.cc:27
std::vector< std::string > KeyVector
storage for key lists
Definition: parametertree.hh:45
ParameterTree()
Create new empty ParameterTree.
Definition: parametertree.cc:22
std::string & operator[](const std::string &key)
get value reference for key
Definition: parametertree.cc:148
ParameterTree & sub(const std::string &sub)
get substructure by name
Definition: parametertree.cc:101
bool hasKey(const std::string &key) const
test for key
Definition: parametertree.cc:46
const KeyVector & getSubKeys() const
get substructure keys
Definition: parametertree.cc:238
T get(const std::string &key) const
Get value.
Definition: parametertree.hh:172
bool hasSub(const std::string &sub) const
test for substructure
Definition: parametertree.cc:74
T get(const std::string &key, const T &defaultValue) const
get value converted to a certain type
Definition: parametertree.hh:156
const KeyVector & getValueKeys() const
get value keys
Definition: parametertree.cc:233
Default exception class for range errors.
Definition: exceptions.hh:252
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.
virtual const char * what() const noexcept
output internal message buffer
Definition: exceptions.cc:35
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
Dune namespace.
Definition: alignment.hh:11