29 #include <QStringList>
30 #include <QTextStream>
36 mgx_EXPORT QTextStream&
operator>>(QTextStream& ss,
bool b);
122 explicit Parms(
int verboseLevel = 1);
127 Parms(
const QString& parmFile,
int verboseLevel = 1);
137 VerboseLevel = (vl < 0) ? 0 : vl;
151 return Parameters.empty();
171 template <
typename T>
bool operator()(
const QString& section,
const QString& key, T& value)
const;
180 bool operator()(
const QString& section,
const QString& key,
bool& value)
const;
186 bool operator()(
const QString& section,
const QString& key,
int& value)
const;
192 bool operator()(
const QString& section,
const QString& key,
float& value)
const;
198 bool operator()(
const QString& section,
const QString& key,
double& value)
const;
207 bool operator()(
const QString& section,
const QString& key, std::string& value)
const;
217 bool operator()(
const QString& section,
const QString& key, QString& value)
const;
224 template <
typename T>
bool operator()(
const QString& section,
const QString& key, T& value,
const T& def);
240 template <
typename Container>
bool all(
const QString& section,
const QString& key, Container& value);
247 bool all(
const QString& section,
const QString& key, std::vector<bool>& value);
253 bool all(
const QString& section,
const QString& key, std::vector<int>& value);
259 bool all(
const QString& section,
const QString& key, std::vector<float>& value);
265 bool all(
const QString& section,
const QString& key, std::vector<double>& value);
271 bool all(
const QString& section,
const QString& key, std::vector<std::string>& value);
277 bool all(
const QString& section,
const QString& key, std::vector<QString>& value);
283 bool all(
const QString& section,
const QString& key, QStringList& value);
299 template <
typename T,
typename Container>
bool all(
const QString& section, std::map<QString, Container>& values);
306 bool all(
const QString& section,
std::map<QString, std::vector<bool> >& value);
312 bool all(
const QString& section,
std::map<QString, std::vector<int> >& value);
318 bool all(
const QString& section,
std::map<QString, std::vector<float> >& value);
324 bool all(
const QString& section,
std::map<QString, std::vector<double> >& value);
330 bool all(
const QString& section,
std::map<QString, std::vector<std::string> >& value);
336 bool all(
const QString& section,
std::map<QString, std::vector<QString> >& value);
342 bool all(
const QString& section, std::map<QString, QStringList>& value);
355 bool extractValues(
const QString& section,
const QString& key, QStringList& values)
const;
365 bool readValue(
const QString& value,
bool& variable)
const;
373 bool readValue(
const QString& value, std::string& variable)
const;
381 bool readValue(
const QString& value, QString& variable)
const;
390 template <
typename T>
bool readValue(
const QString& value, std::vector<T>& variable)
const;
399 template <
typename T>
bool readValue(
const QString& value, std::list<T>& variable)
const;
408 template <
typename T>
bool readValue(
const QString& value, std::set<T>& variable)
const;
417 template <
typename T>
bool readValue(
const QString& value, T& variable)
const;
419 template <
typename T,
typename InsertIterator>
bool readContainer(
const QString& value, InsertIterator container)
const;
424 QString ParmFileName;
431 bool check(QString& key)
const;
437 std::map<QString, QStringList> Parameters;
461 template <
typename T,
typename InsertIterator>
bool Parms::readContainer(
const QString& value, InsertIterator it)
const
464 QTextStream iss(&val, QIODevice::ReadOnly);
465 while(!iss.atEnd() and iss.status() == QTextStream::Ok) {
473 template <
typename T>
bool Parms::readValue(
const QString& value, std::vector<T>& variable)
const
475 return readContainer<T>(value, std::back_insert_iterator<std::vector<T> >(variable));
478 template <
typename T>
bool Parms::readValue(
const QString& value, std::list<T>& variable)
const
480 return readContainer<T>(value, std::back_insert_iterator<std::list<T> >(variable));
483 template <
typename T>
bool Parms::readValue(
const QString& value, std::set<T>& variable)
const
485 return readContainer<T>(value, std::insert_iterator<std::set<T> >(variable, variable.end()));
488 template <
typename T>
bool Parms::readValue(
const QString& value, T& variable)
const
491 QTextStream iss(&val, QIODevice::ReadOnly);
493 return iss.status() == QTextStream::Ok;
496 template <
typename T>
bool Parms::operator()(
const QString& section,
const QString& key, T& value)
const
499 if(!extractValues(section, key, values))
502 if((values.size() > 1) && (VerboseLevel > 1)) {
503 Information::err <<
"Parms::operator():Warning multiple value for key [" << section <<
"]" << key
504 <<
", last one used.\n" << flush;
507 if(!readValue(values.back(), value)) {
508 if(VerboseLevel > 0) {
509 Information::err <<
"Parms::operator():Error getting value for key [" << section <<
"]" << key <<
" value "
510 << values.back() <<
"\n" << flush;
517 template <
typename T>
bool Parms::operator()(
const QString& section,
const QString& key, T& value,
const T& def)
521 if(!(*
this)(section, key, value)) {
523 if(VerboseLevel > 2) {
524 Information::err <<
"Parms::operator()::Info key [" << section <<
"]" << key
525 <<
" not found, using default value" << endl;
533 template <
typename Container>
bool Parms::all(
const QString& section,
const QString& key, Container& value)
536 typedef typename Container::value_type T;
539 if(!extractValues(section, key, values))
542 std::insert_iterator<Container> it(value, value.end());
543 forall(
const QString& val, values) {
545 if(readValue(val, single_value)) {
546 *it++ = (
const T&)single_value;
548 if(VerboseLevel > 2) {
549 Information::err <<
"Parms::all:Error reading key [" << section <<
"]" << key <<
" with value " << val
559 template <
typename T,
typename Container>
560 bool Parms::all(
const QString& section, std::map<QString, Container>& result)
564 typedef std::map<QString, QStringList>::value_type value_type;
566 int pos = section.size() + 1;
567 forall(
const value_type& pair, Parameters) {
568 QString sec(pair.first.mid(0, pos - 1));
571 QString key(pair.first.mid(pos));
572 Container& value = result[key];
574 forall(
const QString& val, pair.second) {
576 if(readValue(val, single_value)) {
577 value.push_back(single_value);
579 if(VerboseLevel > 2) {
580 Information::err <<
"Parms::all:Error reading key [" << section <<
"]" << key
581 <<
" with value " << val << endl;