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 <<
"\""
220 std::map<std::string, std::string> values_;
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>
229 static void parseRange(
const std::string &str,
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>
279 parse(
const std::string& str) {
280 std::string trimmed = ltrim(rtrim(str));
281 return std::basic_string<char, traits, Allocator>(trimmed.begin(),
287 struct ParameterTree::Parser< bool > {
289 char operator()(
char c)
291 return std::tolower(c, std::locale::classic());
296 parse(
const std::string& str) {
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>
312 struct ParameterTree::Parser<FieldVector<T, n> > {
313 static FieldVector<T, n>
314 parse(
const std::string& str) {
315 FieldVector<T, n> val;
316 parseRange(str, val.begin(), val.end());
321 template<
typename T, std::
size_t n>
322 struct ParameterTree::Parser<
std::array<T, n> > {
323 static std::array<T, n>
324 parse(
const std::string& str) {
325 std::array<T, n> val;
326 parseRange(str, val.begin(), val.end());
331 template<std::
size_t n>
332 struct ParameterTree::Parser<
std::bitset<n> > {
333 static std::bitset<n>
334 parse(
const std::string& str) {
336 std::vector<std::string>
sub = split(str);
338 DUNE_THROW(RangeError,
"as a bitset<" << n <<
"> "
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>
350 parse(
const std::string& str) {
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]);
Hierarchical structure of string parameters.
Definition: parametertree.hh:37
std::string get(const std::string &key, const std::string &defaultValue) const
get value as string
Definition: parametertree.cc:188
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
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
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
T get(const std::string &key, const T &defaultValue) const
get value converted to a certain type
Definition: parametertree.hh:161
const KeyVector & getValueKeys() const
get value keys
Definition: parametertree.cc:238
Default exception class for range errors.
Definition: exceptions.hh:254
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:218
Dune namespace.
Definition: alignedallocator.hh:13