5#ifndef DUNE_PARAMETERTREE_HH
6#define DUNE_PARAMETERTREE_HH
61 bool hasKey(
const std::string& key)
const;
71 bool hasSub(
const std::string&
sub)
const;
82 std::string&
operator[] (
const std::string& key);
94 const std::string&
operator[] (
const std::string& key)
const;
104 void report(std::ostream& stream = std::cout,
105 const std::string& prefix =
"")
const;
133 std::string
get(
const std::string& key,
const std::string& defaultValue)
const;
145 std::string
get(
const std::string& key,
const char* defaultValue)
const;
158 T
get(
const std::string& key,
const T& defaultValue)
const {
174 T
get(
const std::string& key)
const {
177 <<
"' not found in ParameterTree (prefix " + prefix_ +
")");
179 return Parser<T>::parse((*
this)[key]);
184 <<
"\" for key \"" << prefix_ <<
"." << key <<
"\""
217 std::map<std::string, std::string> values_;
218 std::map<std::string, ParameterTree> subs_;
220 static std::string ltrim(
const std::string& s);
221 static std::string rtrim(
const std::string& s);
222 static std::vector<std::string> split(
const std::string & s);
225 template<
class Iterator>
226 static void parseRange(
const std::string &str,
227 Iterator it,
const Iterator &end)
229 typedef typename std::iterator_traits<Iterator>::value_type Value;
230 std::istringstream s(str);
232 s.imbue(std::locale::classic());
234 for(; it != end; ++it, ++n) {
238 << className<Value>()
239 <<
" (" << n <<
" items were extracted successfully)");
244 if(not s.fail() or not s.eof())
246 << n <<
" items of type "
247 << className<Value>() <<
" (more items than the range can hold)");
252 struct ParameterTree::Parser {
253 static T parse(
const std::string& str) {
255 std::istringstream s(str);
257 s.imbue(std::locale::classic());
260 DUNE_THROW(RangeError,
" as a " << className<T>());
264 if ((! s.fail()) || (! s.eof()))
265 DUNE_THROW(RangeError,
" as a " << className<T>());
273 template<
typename traits,
typename Allocator>
274 struct ParameterTree::Parser<
std::basic_string<char, traits, Allocator> > {
275 static std::basic_string<char, traits, Allocator>
276 parse(
const std::string& str) {
277 std::string trimmed = ltrim(rtrim(str));
278 return std::basic_string<char, traits, Allocator>(trimmed.begin(),
284 struct ParameterTree::Parser< bool > {
286 char operator()(
char c)
288 return std::tolower(c, std::locale::classic());
293 parse(
const std::string& str) {
294 std::string ret = str;
296 std::transform(ret.begin(), ret.end(), ret.begin(), ToLower());
298 if (ret ==
"yes" || ret ==
"true")
301 if (ret ==
"no" || ret ==
"false")
304 return (Parser<int>::parse(ret) != 0);
308 template<
typename T,
int n>
309 struct ParameterTree::Parser<FieldVector<T, n> > {
310 static FieldVector<T, n>
311 parse(
const std::string& str) {
312 FieldVector<T, n> val;
313 parseRange(str, val.begin(), val.end());
318 template<
typename T, std::
size_t n>
319 struct ParameterTree::Parser<
std::array<T, n> > {
320 static std::array<T, n>
321 parse(
const std::string& str) {
322 std::array<T, n> val;
323 parseRange(str, val.begin(), val.end());
328 template<std::
size_t n>
329 struct ParameterTree::Parser<
std::bitset<n> > {
330 static std::bitset<n>
331 parse(
const std::string& str) {
333 std::vector<std::string>
sub = split(str);
335 DUNE_THROW(RangeError,
"as a bitset<" << n <<
"> "
336 <<
"because of unmatching size " <<
sub.size());
337 for (std::size_t i=0; i<n; ++i) {
338 val[i] = ParameterTree::Parser<bool>::parse(
sub[i]);
344 template<
typename T,
typename A>
345 struct ParameterTree::Parser<
std::vector<T, A> > {
346 static std::vector<T, A>
347 parse(
const std::string& str) {
348 std::vector<std::string>
sub = split(str);
349 std::vector<T, A> vec;
350 for (
unsigned int i=0; i<
sub.size(); ++i) {
351 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:185
void report(std::ostream &stream=std::cout, const std::string &prefix="") const
print distinct substructure to stream
Definition: parametertree.cc:29
std::vector< std::string > KeyVector
storage for key lists
Definition: parametertree.hh:47
ParameterTree()
Create new empty ParameterTree.
Definition: parametertree.cc:24
std::string & operator[](const std::string &key)
get value reference for key
Definition: parametertree.cc:150
ParameterTree & sub(const std::string &sub)
get substructure by name
Definition: parametertree.cc:103
bool hasKey(const std::string &key) const
test for key
Definition: parametertree.cc:48
const KeyVector & getSubKeys() const
get substructure keys
Definition: parametertree.cc:240
T get(const std::string &key) const
Get value.
Definition: parametertree.hh:174
bool hasSub(const std::string &sub) const
test for substructure
Definition: parametertree.cc:76
T get(const std::string &key, const T &defaultValue) const
get value converted to a certain type
Definition: parametertree.hh:158
const KeyVector & getValueKeys() const
get value keys
Definition: parametertree.cc:235
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.
const char * what() const noexcept override
output internal message buffer
Definition: exceptions.cc:37
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13