/usr/include/dune/common/parametertree.hh is in libdune-common-dev 2.3.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_PARAMETERTREE_HH
#define DUNE_PARAMETERTREE_HH
/** \file
* \brief A hierarchical structure of string parameters
*/
#include <cstddef>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <ostream>
#include <sstream>
#include <string>
#include <typeinfo>
#include <vector>
#include <algorithm>
#include <cctype>
#include <dune/common/array.hh>
#include <dune/common/exceptions.hh>
#include <dune/common/fvector.hh>
#include <dune/common/classname.hh>
namespace Dune {
/** \brief Hierarchical structure of string parameters
* \ingroup Common
*/
class ParameterTree
{
// class providing a single static parse() function, used by the
// generic get() method
template<typename T>
struct Parser;
public:
/** \brief storage for key lists
*/
typedef std::vector<std::string> KeyVector;
/** \brief Create new empty ParameterTree
*/
ParameterTree();
/** \brief test for key
*
* Tests whether given key exists.
*
* \param key key name
* \return true if key exists in structure, otherwise false
*/
bool hasKey(const std::string& key) const;
/** \brief test for substructure
*
* Tests whether given substructure exists.
*
* \param sub substructure name
* \return true if substructure exists in structure, otherwise false
*/
bool hasSub(const std::string& sub) const;
/** \brief get value reference for key
*
* Returns reference to value for given key name.
* This creates the key, if not existent.
*
* \param key key name
* \return reference to corresponding value
*/
std::string& operator[] (const std::string& key);
/** \brief get value reference for key
*
* Returns reference to value for given key name.
* This creates the key, if not existent.
*
* \param key key name
* \return reference to corresponding value
* \throw Dune::RangeError if key is not found
*/
const std::string& operator[] (const std::string& key) const;
/** \brief print distinct substructure to stream
*
* Prints all entries with given prefix.
*
* \param stream Stream to print to
* \param prefix for key and substructure names
*/
void report(std::ostream& stream = std::cout,
const std::string& prefix = "") const;
/** \brief get substructure by name
*
* \param sub substructure name
* \return reference to substructure
*/
ParameterTree& sub(const std::string& sub);
/** \brief get const substructure by name
*
* \param sub substructure name
* \return reference to substructure
*/
const ParameterTree& sub(const std::string& sub) const;
/** \brief get value as string
*
* Returns pure string value for given key.
*
* \param key key name
* \param defaultValue default if key does not exist
* \return value as string
*/
std::string get(const std::string& key, const std::string& defaultValue) const;
/** \brief get value as string
*
* Returns pure string value for given key.
*
* \todo This is a hack so get("my_key", "xyz") compiles
* (without this method "xyz" resolves to bool instead of std::string)
* \param key key name
* \param defaultValue default if key does not exist
* \return value as string
*/
std::string get(const std::string& key, const char* defaultValue) const;
/** \brief get value as int
*
* Returns value for given key interpreted as int.
*
* \param key key name
* \param defaultValue default if key does not exist
* \return value as int
*/
int get(const std::string& key, int defaultValue) const;
/** \brief get value as double
*
* Returns value for given key interpreted as double.
*
* \param key key name
* \param defaultValue default if key does not exist
* \return value as double
*/
double get(const std::string& key, double defaultValue) const;
/** \brief get value converted to a certain type
*
* Returns value as type T for given key.
*
* \tparam T type of returned value.
* \param key key name
* \param defaultValue default if key does not exist
* \return value converted to T
*/
template<typename T>
T get(const std::string& key, const T& defaultValue) const {
if(hasKey(key))
return get<T>(key);
else
return defaultValue;
}
/** \brief Get value
*
* \tparam T Type of the value
* \param key Key name
* \throws RangeError if key does not exist
* \throws NotImplemented Type is not supported
* \return value as T
*/
template <class T>
T get(const std::string& key) const {
if(not hasKey(key))
DUNE_THROW(RangeError, "Key '" << key << "' not found in parameter "
"file!");
try {
return Parser<T>::parse((*this)[key]);
}
catch(const RangeError&) {
DUNE_THROW(RangeError, "Cannot parse value \"" <<
(*this)[key] << "\" for key \"" << key << "\" as a " <<
className<T>());
}
}
/** \brief get value keys
*
* Returns a vector of all keys associated to (key,values) entries in
* order of appearance
*
* \return reference to entry vector
*/
const KeyVector& getValueKeys() const;
/** \brief get substructure keys
*
* Returns a vector of all keys associated to (key,substructure) entries
* in order of appearance
*
* \return reference to entry vector
*/
const KeyVector& getSubKeys() const;
protected:
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);
std::size_t n = 0;
for(; it != end; ++it, ++n) {
s >> *it;
if(!s)
DUNE_THROW(RangeError, "Cannot parse value \"" << str << "\" 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, "Cannot parse value \"" << str << "\" 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);
s >> val;
if(!s)
DUNE_THROW(RangeError, "Cannot parse value \"" << str << "\" as a " <<
className<T>());
T dummy;
s >> dummy;
// now extraction should have failed, and eof should be set
if(not s.fail() or not s.eof())
DUNE_THROW(RangeError, "Cannot parse value \"" << str << "\" 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 im gonna 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 {
int operator()(int c)
{
return std::tolower(c);
}
};
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<array<T, n> > {
static array<T, n>
parse(const std::string& str) {
array<T, n> val;
parseRange(str, val.begin(), val.end());
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
|