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 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>
274 parse(
const std::string& str) {
275 std::string trimmed = ltrim(rtrim(str));
276 return std::basic_string<char, traits, Allocator>(trimmed.begin(),
282 struct ParameterTree::Parser< bool > {
284 char operator()(
char c)
286 return std::tolower(c, std::locale::classic());
291 parse(
const std::string& str) {
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>
307 struct ParameterTree::Parser<FieldVector<T, n> > {
308 static FieldVector<T, n>
309 parse(
const std::string& str) {
310 FieldVector<T, n> val;
311 parseRange(str, val.begin(), val.end());
316 template<
typename T, std::
size_t n>
317 struct ParameterTree::Parser<
std::array<T, n> > {
318 static std::array<T, n>
319 parse(
const std::string& str) {
320 std::array<T, n> val;
321 parseRange(str, val.begin(), val.end());
326 template<std::
size_t n>
327 struct ParameterTree::Parser<
std::bitset<n> > {
328 static std::bitset<n>
329 parse(
const std::string& str) {
331 std::vector<std::string>
sub = split(str);
333 DUNE_THROW(RangeError,
"as a bitset<" << n <<
"> "
334 <<
"because of unmatching size " <<
sub.size());
335 for (std::size_t i=0; i<n; ++i) {
336 val[i] = ParameterTree::Parser<bool>::parse(
sub[i]);
342 template<
typename T,
typename A>
343 struct ParameterTree::Parser<
std::vector<T, A> > {
344 static std::vector<T, A>
345 parse(
const std::string& str) {
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]);
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.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
Dune namespace.
Definition: alignedallocator.hh:11