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;
130 std::string
get(
const std::string& key,
const std::string& defaultValue)
const;
142 std::string
get(
const std::string& key,
const char* defaultValue)
const;
155 T
get(
const std::string& key,
const T& defaultValue)
const {
171 T
get(
const std::string& key)
const {
174 <<
"' not found in ParameterTree (prefix " + prefix_ +
")");
176 return Parser<T>::parse((*
this)[key]);
181 <<
"\" for key \"" << prefix_ <<
"." << key <<
"\""
211 std::map<std::string, std::string> values_;
212 std::map<std::string, ParameterTree> subs_;
214 static std::string ltrim(
const std::string& s);
215 static std::string rtrim(
const std::string& s);
216 static std::vector<std::string> split(
const std::string & s);
219 template<
class Iterator>
220 static void parseRange(
const std::string &str,
221 Iterator it,
const Iterator &end)
223 typedef typename std::iterator_traits<Iterator>::value_type Value;
224 std::istringstream s(str);
226 for(; it != end; ++it, ++n) {
230 << className<Value>()
231 <<
" (" << n <<
" items were extracted successfully)");
236 if(not s.fail() or not s.eof())
238 << n <<
" items of type "
239 << className<Value>() <<
" (more items than the range can hold)");
244 struct ParameterTree::Parser {
245 static T parse(
const std::string& str) {
247 std::istringstream s(str);
249 s.imbue(std::locale::classic());
252 DUNE_THROW(RangeError,
" as a " << className<T>());
256 if ((! s.fail()) || (! s.eof()))
257 DUNE_THROW(RangeError,
" as a " << className<T>());
265 template<
typename traits,
typename Allocator>
266 struct ParameterTree::Parser<
std::basic_string<char, traits, Allocator> > {
267 static std::basic_string<char, traits, Allocator>
268 parse(
const std::string& str) {
269 std::string trimmed = ltrim(rtrim(str));
270 return std::basic_string<char, traits, Allocator>(trimmed.begin(),
276 struct ParameterTree::Parser< bool > {
278 char operator()(
char c)
280 return std::tolower(c, std::locale::classic());
285 parse(
const std::string& str) {
286 std::string ret = str;
288 std::transform(ret.begin(), ret.end(), ret.begin(), ToLower());
290 if (ret ==
"yes" || ret ==
"true")
293 if (ret ==
"no" || ret ==
"false")
296 return (Parser<int>::parse(ret) != 0);
300 template<
typename T,
int n>
301 struct ParameterTree::Parser<FieldVector<T, n> > {
302 static FieldVector<T, n>
303 parse(
const std::string& str) {
304 FieldVector<T, n> val;
305 parseRange(str, val.begin(), val.end());
310 template<
typename T, std::
size_t n>
311 struct ParameterTree::Parser<array<T, n> > {
313 parse(
const std::string& str) {
315 parseRange(str, val.begin(), val.end());
320 template<std::
size_t n>
321 struct ParameterTree::Parser<
std::bitset<n> > {
322 static std::bitset<n>
323 parse(
const std::string& str) {
325 std::vector<std::string>
sub = split(str);
327 DUNE_THROW(RangeError,
"as a bitset<" << n <<
"> "
328 <<
"because of unmatching size " <<
sub.size());
329 for (std::size_t i=0; i<n; ++i) {
330 val[i] = ParameterTree::Parser<bool>::parse(
sub[i]);
336 template<
typename T,
typename A>
337 struct ParameterTree::Parser<
std::vector<T, A> > {
338 static std::vector<T, A>
339 parse(
const std::string& str) {
340 std::vector<std::string>
sub = split(str);
341 std::vector<T, A> vec;
342 for (
unsigned int i=0; i<
sub.size(); ++i) {
343 T val = ParameterTree::Parser<T>::parse(
sub[i]);
Fallback implementation of the std::array class (a static array)
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:154
void report(std::ostream &stream=std::cout, const std::string &prefix="") const
print distinct substructure to stream
Definition: parametertree.cc:25
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:114
ParameterTree & sub(const std::string &sub)
get substructure by name
Definition: parametertree.cc:78
bool hasKey(const std::string &key) const
test for key
Definition: parametertree.cc:44
const KeyVector & getSubKeys() const
get substructure keys
Definition: parametertree.cc:209
T get(const std::string &key) const
Get value.
Definition: parametertree.hh:171
bool hasSub(const std::string &sub) const
test for substructure
Definition: parametertree.cc:61
T get(const std::string &key, const T &defaultValue) const
get value converted to a certain type
Definition: parametertree.hh:155
const KeyVector & getValueKeys() const
get value keys
Definition: parametertree.cc:204
Default exception class for range errors.
Definition: exceptions.hh:279
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.
const std::string & what() const
output internal message buffer
Definition: exceptions.hh:199
#define DUNE_THROW(E, m)
Definition: exceptions.hh:243
Dune namespace.
Definition: alignment.hh:10