3 #ifndef DUNE_PARAMETERTREE_HH 4 #define DUNE_PARAMETERTREE_HH 45 typedef std::vector<std::string>
KeyVector;
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 <<
"\"" 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>
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());
256 if ((! s.fail()) || (! s.eof()))
265 template<
typename traits,
typename Allocator>
266 struct ParameterTree::Parser<
std::basic_string<char, traits, Allocator> > {
267 static std::basic_string<char, traits, Allocator>
270 return std::basic_string<char, traits, Allocator>(trimmed.begin(),
276 struct ParameterTree::Parser< bool > {
280 return std::tolower(c, std::locale::classic());
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>
310 template<
typename T, std::
size_t n>
311 struct ParameterTree::Parser<array<T, n> > {
320 template<std::
size_t n>
321 struct ParameterTree::Parser<
std::bitset<n> > {
322 static std::bitset<n>
325 std::vector<std::string>
sub =
split(str);
328 <<
"because of unmatching size " << sub.size());
329 for (std::size_t i=0; i<n; ++i) {
336 template<
typename T,
typename A>
337 struct ParameterTree::Parser<
std::vector<T, A> > {
338 static std::vector<T, A>
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]);
352 #endif // DUNE_PARAMETERTREE_HH static std::basic_string< char, traits, Allocator > parse(const std::string &str)
Definition: parametertree.hh:268
static std::string ltrim(const std::string &s)
Definition: parametertree.cc:170
void report(std::ostream &stream=std::cout, const std::string &prefix="") const
print distinct substructure to stream
Definition: parametertree.cc:25
static std::string rtrim(const std::string &s)
Definition: parametertree.cc:179
static array< T, n > parse(const std::string &str)
Definition: parametertree.hh:313
vector space out of a tensor product of fields.
Definition: densematrix.hh:36
std::vector< std::string > KeyVector
storage for key lists
Definition: parametertree.hh:39
static bool parse(const std::string &str)
Definition: parametertree.hh:285
std::map< std::string, std::string > values_
Definition: parametertree.hh:211
Fallback implementation of the std::array class (a static array)
ParameterTree()
Create new empty ParameterTree.
Definition: parametertree.cc:22
const KeyVector & getSubKeys() const
get substructure keys
Definition: parametertree.cc:209
KeyVector subKeys_
Definition: parametertree.hh:209
std::string prefix_
Definition: parametertree.hh:206
bool hasSub(const std::string &sub) const
test for substructure
Definition: parametertree.cc:61
static std::vector< T, A > parse(const std::string &str)
Definition: parametertree.hh:339
static std::vector< std::string > split(const std::string &s)
Definition: parametertree.cc:188
char operator()(char c)
Definition: parametertree.hh:278
const KeyVector & getValueKeys() const
get value keys
Definition: parametertree.cc:204
std::string & operator[](const std::string &key)
get value reference for key
Definition: parametertree.cc:114
Iterator end()
end iterator
Definition: densevector.hh:313
KeyVector valueKeys_
Definition: parametertree.hh:208
static FieldVector< T, n > parse(const std::string &str)
Definition: parametertree.hh:303
std::map< std::string, ParameterTree > subs_
Definition: parametertree.hh:212
char c
Definition: alignment.hh:33
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:243
const std::string & what() const
output internal message buffer
Definition: exceptions.hh:199
Hierarchical structure of string parameters.
Definition: parametertree.hh:34
ParameterTree & sub(const std::string &sub)
get substructure by name
Definition: parametertree.cc:78
static std::bitset< n > parse(const std::string &str)
Definition: parametertree.hh:323
Default exception class for range errors.
Definition: exceptions.hh:279
bool hasKey(const std::string &key) const
test for key
Definition: parametertree.cc:44
static void parseRange(const std::string &str, Iterator it, const Iterator &end)
Definition: parametertree.hh:220
Dune namespace.
Definition: alignment.hh:9
Iterator begin()
begin iterator
Definition: densevector.hh:307
Implements a vector constructed from a given type representing a field and a compile-time given size...
A free function to provide the demangled class name of a given object or type as a string...