Dune Core Modules (2.5.2)

parametertree.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_PARAMETERTREE_HH
4 #define DUNE_PARAMETERTREE_HH
5 
10 #include <array>
11 #include <cstddef>
12 #include <iostream>
13 #include <istream>
14 #include <iterator>
15 #include <locale>
16 #include <map>
17 #include <ostream>
18 #include <sstream>
19 #include <string>
20 #include <typeinfo>
21 #include <vector>
22 #include <algorithm>
23 #include <bitset>
24 
26 #include <dune/common/fvector.hh>
27 #include <dune/common/classname.hh>
28 
29 namespace Dune {
30 
35  {
36  // class providing a single static parse() function, used by the
37  // generic get() method
38  template<typename T>
39  struct Parser;
40 
41  public:
42 
45  typedef std::vector<std::string> KeyVector;
46 
49  ParameterTree();
50 
51 
59  bool hasKey(const std::string& key) const;
60 
61 
69  bool hasSub(const std::string& sub) const;
70 
71 
80  std::string& operator[] (const std::string& key);
81 
82 
92  const std::string& operator[] (const std::string& key) const;
93 
94 
102  void report(std::ostream& stream = std::cout,
103  const std::string& prefix = "") const;
104 
105 
111  ParameterTree& sub(const std::string& sub);
112 
113 
120  const ParameterTree& sub(const std::string& sub, bool fail_if_missing = false) const;
121 
122 
131  std::string get(const std::string& key, const std::string& defaultValue) const;
132 
143  std::string get(const std::string& key, const char* defaultValue) const;
144 
145 
155  template<typename T>
156  T get(const std::string& key, const T& defaultValue) const {
157  if(hasKey(key))
158  return get<T>(key);
159  else
160  return defaultValue;
161  }
162 
171  template <class T>
172  T get(const std::string& key) const {
173  if(not hasKey(key))
174  DUNE_THROW(Dune::RangeError, "Key '" << key
175  << "' not found in ParameterTree (prefix " + prefix_ + ")");
176  try {
177  return Parser<T>::parse((*this)[key]);
178  }
179  catch(const RangeError& e) {
180  // rethrow the error and add more information
181  DUNE_THROW(RangeError, "Cannot parse value \"" << (*this)[key]
182  << "\" for key \"" << prefix_ << "." << key << "\""
183  << e.what());
184  }
185  }
186 
194  const KeyVector& getValueKeys() const;
195 
196 
204  const KeyVector& getSubKeys() const;
205 
206  protected:
207 
208  static const ParameterTree empty_;
209 
210  std::string prefix_;
211 
212  KeyVector valueKeys_;
213  KeyVector subKeys_;
214 
215  std::map<std::string, std::string> values_;
216  std::map<std::string, ParameterTree> subs_;
217 
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);
221 
222  // parse into a fixed-size range of iterators
223  template<class Iterator>
224  static void parseRange(const std::string &str,
225  Iterator it, const Iterator &end)
226  {
227  typedef typename std::iterator_traits<Iterator>::value_type Value;
228  std::istringstream s(str);
229  std::size_t n = 0;
230  for(; it != end; ++it, ++n) {
231  s >> *it;
232  if(!s)
233  DUNE_THROW(RangeError, "as a range of items of type "
234  << className<Value>()
235  << " (" << n << " items were extracted successfully)");
236  }
237  Value dummy;
238  s >> dummy;
239  // now extraction should have failed, and eof should be set
240  if(not s.fail() or not s.eof())
241  DUNE_THROW(RangeError, "as a range of "
242  << n << " items of type "
243  << className<Value>() << " (more items than the range can hold)");
244  }
245  };
246 
247  template<typename T>
248  struct ParameterTree::Parser {
249  static T parse(const std::string& str) {
250  T val;
251  std::istringstream s(str);
252  // make sure we are in locale "C"
253  s.imbue(std::locale::classic());
254  s >> val;
255  if(!s)
256  DUNE_THROW(RangeError, " as a " << className<T>());
257  T dummy;
258  s >> dummy;
259  // now extraction should have failed, and eof should be set
260  if ((! s.fail()) || (! s.eof()))
261  DUNE_THROW(RangeError, " as a " << className<T>());
262  return val;
263  }
264  };
265 
266  // "How do I convert a string into a wstring in C++?" "Why, that very simple
267  // son. You just need a these hundred lines of code."
268  // Instead im gonna restrict myself to string with charT=char here
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(),
275  trimmed.end());
276  }
277  };
278 
279  template<>
280  struct ParameterTree::Parser< bool > {
281  struct ToLower {
282  char operator()(char c)
283  {
284  return std::tolower(c, std::locale::classic());
285  }
286  };
287 
288  static bool
289  parse(const std::string& str) {
290  std::string ret = str;
291 
292  std::transform(ret.begin(), ret.end(), ret.begin(), ToLower());
293 
294  if (ret == "yes" || ret == "true")
295  return true;
296 
297  if (ret == "no" || ret == "false")
298  return false;
299 
300  return (Parser<int>::parse(ret) != 0);
301  }
302  };
303 
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());
310  return val;
311  }
312  };
313 
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());
320  return val;
321  }
322  };
323 
324  template<std::size_t n>
325  struct ParameterTree::Parser<std::bitset<n> > {
326  static std::bitset<n>
327  parse(const std::string& str) {
328  std::bitset<n> val;
329  std::vector<std::string> sub = split(str);
330  if (sub.size() != n)
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]);
335  }
336  return val;
337  }
338  };
339 
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]);
348  vec.push_back(val);
349  }
350  return vec;
351  }
352  };
353 
354 } // end namespace Dune
355 
356 #endif // DUNE_PARAMETERTREE_HH
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:39
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
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 12, 22:29, 2024)