Loading...
Searching...
No Matches
parametertree.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_PARAMETERTREE_HH
#define DUNE_PARAMETERTREE_HH
#include <array>
#include <cstddef>
#include <iostream>
#include <istream>
#include <iterator>
#include <locale>
#include <map>
#include <ostream>
#include <sstream>
#include <string>
#include <typeinfo>
#include <vector>
#include <algorithm>
#include <bitset>
#include <dune/common/exceptions.hh>
#include <dune/common/fvector.hh>
#include <dune/common/classname.hh>
namespace Dune {
class ParameterTree
{
// class providing a single static parse() function, used by the
// generic get() method
template<typename T>
struct Parser;
struct EmptyTag {};
ParameterTree(EmptyTag);
public:
typedef std::vector<std::string> KeyVector;
ParameterTree();
bool hasKey(const std::string& key) const;
bool hasSub(const std::string& sub) const;
std::string& operator[] (const std::string& key);
const std::string& operator[] (const std::string& key) const;
void report(std::ostream& stream = std::cout,
const std::string& prefix = "") const;
ParameterTree& sub(const std::string& sub);
const ParameterTree& sub(const std::string& sub, bool fail_if_missing = false) const;
std::string get(const std::string& key, const std::string& defaultValue) const;
std::string get(const std::string& key, const char* defaultValue) const;
template<typename T>
T get(const std::string& key, const T& defaultValue) const {
if(hasKey(key))
return get<T>(key);
else
return defaultValue;
}
template <class T>
T get(const std::string& key) const {
if(not hasKey(key))
DUNE_THROW(Dune::RangeError, "Key '" << key
<< "' not found in ParameterTree (prefix " + prefix_ + ")");
try {
return Parser<T>::parse((*this)[key]);
}
catch(const RangeError& e) {
// rethrow the error and add more information
DUNE_THROW(RangeError, "Cannot parse value \"" << (*this)[key]
<< "\" for key \"" << prefix_ << "." << key << "\""
<< e.what());
}
}
const KeyVector& getValueKeys() const;
const KeyVector& getSubKeys() const;
protected:
static const ParameterTree empty_;
std::string prefix_;
KeyVector valueKeys_;
KeyVector subKeys_;
std::map<std::string, std::string> values_;
std::map<std::string, ParameterTree> subs_;
static std::string ltrim(const std::string& s);
static std::string rtrim(const std::string& s);
static std::vector<std::string> split(const std::string & s);
// parse into a fixed-size range of iterators
template<class Iterator>
static void parseRange(const std::string &str,
Iterator it, const Iterator &end)
{
typedef typename std::iterator_traits<Iterator>::value_type Value;
std::istringstream s(str);
// make sure we are in locale "C"
s.imbue(std::locale::classic());
std::size_t n = 0;
for(; it != end; ++it, ++n) {
s >> *it;
if(!s)
DUNE_THROW(RangeError, "as a range of items of type "
<< className<Value>()
<< " (" << n << " items were extracted successfully)");
}
Value dummy;
s >> dummy;
// now extraction should have failed, and eof should be set
if(not s.fail() or not s.eof())
DUNE_THROW(RangeError, "as a range of "
<< n << " items of type "
<< className<Value>() << " (more items than the range can hold)");
}
};
template<typename T>
struct ParameterTree::Parser {
static T parse(const std::string& str) {
T val;
std::istringstream s(str);
// make sure we are in locale "C"
s.imbue(std::locale::classic());
s >> val;
if(!s)
DUNE_THROW(RangeError, " as a " << className<T>());
char dummy;
s >> dummy;
// now extraction should have failed, and eof should be set
if ((! s.fail()) || (! s.eof()))
DUNE_THROW(RangeError, " as a " << className<T>());
return val;
}
};
// "How do I convert a string into a wstring in C++?" "Why, that very simple
// son. You just need a these hundred lines of code."
// Instead I'm going to restrict myself to string with charT=char here.
template<typename traits, typename Allocator>
struct ParameterTree::Parser<std::basic_string<char, traits, Allocator> > {
static std::basic_string<char, traits, Allocator>
parse(const std::string& str) {
std::string trimmed = ltrim(rtrim(str));
return std::basic_string<char, traits, Allocator>(trimmed.begin(),
trimmed.end());
}
};
template<>
struct ParameterTree::Parser< bool > {
struct ToLower {
char operator()(char c)
{
return std::tolower(c, std::locale::classic());
}
};
static bool
parse(const std::string& str) {
std::string ret = str;
std::transform(ret.begin(), ret.end(), ret.begin(), ToLower());
if (ret == "yes" || ret == "true")
return true;
if (ret == "no" || ret == "false")
return false;
return (Parser<int>::parse(ret) != 0);
}
};
template<typename T, int n>
struct ParameterTree::Parser<FieldVector<T, n> > {
static FieldVector<T, n>
parse(const std::string& str) {
FieldVector<T, n> val;
parseRange(str, val.begin(), val.end());
return val;
}
};
template<typename T, std::size_t n>
struct ParameterTree::Parser<std::array<T, n> > {
static std::array<T, n>
parse(const std::string& str) {
std::array<T, n> val;
parseRange(str, val.begin(), val.end());
return val;
}
};
template<std::size_t n>
struct ParameterTree::Parser<std::bitset<n> > {
static std::bitset<n>
parse(const std::string& str) {
std::bitset<n> val;
std::vector<std::string> sub = split(str);
if (sub.size() != n)
DUNE_THROW(RangeError, "as a bitset<" << n << "> "
<< "because of unmatching size " << sub.size());
for (std::size_t i=0; i<n; ++i) {
val[i] = ParameterTree::Parser<bool>::parse(sub[i]);
}
return val;
}
};
template<typename T, typename A>
struct ParameterTree::Parser<std::vector<T, A> > {
static std::vector<T, A>
parse(const std::string& str) {
std::vector<std::string> sub = split(str);
std::vector<T, A> vec;
for (unsigned int i=0; i<sub.size(); ++i) {
T val = ParameterTree::Parser<T>::parse(sub[i]);
vec.push_back(val);
}
return vec;
}
};
} // end namespace Dune
#endif // DUNE_PARAMETERTREE_HH
exceptions.hh
A few common exception classes.
classname.hh
A free function to provide the demangled class name of a given object or type as a string.
fvector.hh
Implements a vector constructed from a given type representing a field and a compile-time given size.
DUNE_THROW
#define DUNE_THROW(E,...)
Definition exceptions.hh:314
Dune::Exception::what
const char * what() const noexcept override
output internal message buffer
Definition exceptions.cc:37
std
STL namespace.
Dune
Dune namespace
Definition alignedallocator.hh:13
Dune::className
std::string className()
Provide the demangled class name of a type T as a string.
Definition classname.hh:47
Dune::FieldVector
vector space out of a tensor product of fields.
Definition fvector.hh:97
Dune::DenseVector::end
constexpr Iterator end()
end iterator
Definition densevector.hh:368
Dune::DenseVector::begin
constexpr Iterator begin()
begin iterator
Definition densevector.hh:362
Dune::RangeError
Default exception class for range errors.
Definition exceptions.hh:348
Dune::ParameterTree::valueKeys_
KeyVector valueKeys_
Definition parametertree.hh:217
Dune::ParameterTree::subs_
std::map< std::string, ParameterTree > subs_
Definition parametertree.hh:221
Dune::ParameterTree::empty_
static const ParameterTree empty_
Definition parametertree.hh:213
Dune::ParameterTree::rtrim
static std::string rtrim(const std::string &s)
Definition parametertree.cc:213
Dune::ParameterTree::get
std::string get(const std::string &key, const std::string &defaultValue) const
get value as string
Definition parametertree.cc:188
Dune::ParameterTree::ltrim
static std::string ltrim(const std::string &s)
Definition parametertree.cc:204
Dune::ParameterTree::report
void report(std::ostream &stream=std::cout, const std::string &prefix="") const
print distinct substructure to stream
Definition parametertree.cc:32
Dune::ParameterTree::KeyVector
std::vector< std::string > KeyVector
storage for key lists
Definition parametertree.hh:50
Dune::ParameterTree::parseRange
static void parseRange(const std::string &str, Iterator it, const Iterator &end)
Definition parametertree.hh:229
Dune::ParameterTree::ParameterTree
ParameterTree()
Create new empty ParameterTree.
Definition parametertree.cc:20
Dune::ParameterTree::operator[]
std::string & operator[](const std::string &key)
get value reference for key
Definition parametertree.cc:153
Dune::ParameterTree::subKeys_
KeyVector subKeys_
Definition parametertree.hh:218
Dune::ParameterTree::sub
ParameterTree & sub(const std::string &sub)
get substructure by name
Definition parametertree.cc:106
Dune::ParameterTree::hasKey
bool hasKey(const std::string &key) const
test for key
Definition parametertree.cc:51
Dune::ParameterTree::getSubKeys
const KeyVector & getSubKeys() const
get substructure keys
Definition parametertree.cc:243
Dune::ParameterTree::get
T get(const std::string &key) const
Get value.
Definition parametertree.hh:177
Dune::ParameterTree::hasSub
bool hasSub(const std::string &sub) const
test for substructure
Definition parametertree.cc:79
Dune::ParameterTree::values_
std::map< std::string, std::string > values_
Definition parametertree.hh:220
Dune::ParameterTree::get
T get(const std::string &key, const T &defaultValue) const
get value converted to a certain type
Definition parametertree.hh:161
Dune::ParameterTree::prefix_
std::string prefix_
Definition parametertree.hh:215
Dune::ParameterTree::split
static std::vector< std::string > split(const std::string &s)
Definition parametertree.cc:222
Dune::ParameterTree::getValueKeys
const KeyVector & getValueKeys() const
get value keys
Definition parametertree.cc:238
Dune::ParameterTree::Parser< std::basic_string< char, traits, Allocator > >::parse
static std::basic_string< char, traits, Allocator > parse(const std::string &str)
Definition parametertree.hh:279
Dune::ParameterTree::Parser< bool >::parse
static bool parse(const std::string &str)
Definition parametertree.hh:296
Dune::ParameterTree::Parser< bool >::ToLower
Definition parametertree.hh:288
Dune::ParameterTree::Parser< bool >::ToLower::operator()
char operator()(char c)
Definition parametertree.hh:289
Dune::ParameterTree::Parser< FieldVector< T, n > >::parse
static FieldVector< T, n > parse(const std::string &str)
Definition parametertree.hh:314
Dune::ParameterTree::Parser< std::array< T, n > >::parse
static std::array< T, n > parse(const std::string &str)
Definition parametertree.hh:324
Dune::ParameterTree::Parser< std::bitset< n > >::parse
static std::bitset< n > parse(const std::string &str)
Definition parametertree.hh:334
Dune::ParameterTree::Parser< std::vector< T, A > >::parse
static std::vector< T, A > parse(const std::string &str)
Definition parametertree.hh:350