MorphoGraphX
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Parms.hpp
Go to the documentation of this file.
1 #ifndef __PARMS_HPP__
2 #define __PARMS_HPP__
3 
9 #include <Config.hpp>
10 
11 #include <Forall.hpp>
12 #include <Information.hpp>
13 
14 #include <iostream>
15 #include <iterator>
16 #include <list>
17 #include <map>
18 #include <QString>
19 #include <QStringList>
20 #include <QTextStream>
21 #include <set>
22 #include <sstream>
23 #include <string>
24 #include <vector>
25 
26 mgx_EXPORT QTextStream& operator>>(QTextStream& ss, bool b);
27 
28 namespace mgx { namespace util {
105  class mgx_EXPORT Parms {
106  public:
110  explicit Parms(int verboseLevel = 1);
111 
115  Parms(const QString& parmFile, int verboseLevel = 1);
116 
117  ~Parms();
118 
124  void verboseLevel(int vl) {
125  VerboseLevel = (vl < 0) ? 0 : vl;
126  }
127 
131  bool isLoaded() const {
132  return loaded;
133  }
134 
138  bool empty() const {
139  return Parameters.empty();
140  }
141 
159  template <typename T> bool operator()(const QString& section, const QString& key, T& value);
160 
168  bool operator()(const QString& section, const QString& key, bool& value);
174  bool operator()(const QString& section, const QString& key, int& value);
180  bool operator()(const QString& section, const QString& key, float& value);
186  bool operator()(const QString& section, const QString& key, double& value);
195  bool operator()(const QString& section, const QString& key, std::string& value);
196 
205  bool operator()(const QString& section, const QString& key, QString& value);
206 
212  template <typename T> bool operator()(const QString& section, const QString& key, T& value, const T& def);
213 
228  template <typename Container> bool all(const QString& section, const QString& key, Container& value);
229 
235  bool all(const QString& section, const QString& key, std::vector<bool>& value);
241  bool all(const QString& section, const QString& key, std::vector<int>& value);
247  bool all(const QString& section, const QString& key, std::vector<float>& value);
253  bool all(const QString& section, const QString& key, std::vector<double>& value);
259  bool all(const QString& section, const QString& key, std::vector<std::string>& value);
265  bool all(const QString& section, const QString& key, std::vector<QString>& value);
271  bool all(const QString& section, const QString& key, QStringList& value);
272 
287  template <typename T, typename Container> bool all(const QString& section, std::map<QString, Container>& values);
288 
294  bool all(const QString& section, std::map<QString, std::vector<bool> >& value);
300  bool all(const QString& section, std::map<QString, std::vector<int> >& value);
306  bool all(const QString& section, std::map<QString, std::vector<float> >& value);
312  bool all(const QString& section, std::map<QString, std::vector<double> >& value);
318  bool all(const QString& section, std::map<QString, std::vector<std::string> >& value);
324  bool all(const QString& section, std::map<QString, std::vector<QString> >& value);
330  bool all(const QString& section, std::map<QString, QStringList>& value);
331 
332  private:
336  void init();
337 
343  bool extractValues(const QString& section, const QString& key, QStringList& values);
344 
353  bool readValue(const QString& value, bool& variable);
361  bool readValue(const QString& value, std::string& variable);
369  bool readValue(const QString& value, QString& variable);
378  template <typename T> bool readValue(const QString& value, std::vector<T>& variable);
387  template <typename T> bool readValue(const QString& value, std::list<T>& variable);
396  template <typename T> bool readValue(const QString& value, std::set<T>& variable);
405  template <typename T> bool readValue(const QString& value, T& variable);
406 
407  template <typename T, typename InsertIterator> bool readContainer(const QString& value, InsertIterator container);
408 
412  QString ParmFileName;
413 
419  bool check(QString& key);
420 
425  std::map<QString, QStringList> Parameters;
426 
430  QString Section;
431 
435  int VerboseLevel;
436 
441  bool CheckExist;
442 
446  bool loaded;
447  };
448 
449  template <typename T, typename InsertIterator> bool Parms::readContainer(const QString& value, InsertIterator it)
450  {
451  QString val(value);
452  QTextStream iss(&val, QIODevice::ReadOnly);
453  while(!iss.atEnd() and iss.status() == QTextStream::Ok) {
454  T v;
455  iss >> v;
456  *it++ = v;
457  }
458  return true;
459  }
460 
461  template <typename T> bool Parms::readValue(const QString& value, std::vector<T>& variable)
462  {
463  return readContainer<T>(value, std::back_insert_iterator<std::vector<T> >(variable));
464  }
465 
466  template <typename T> bool Parms::readValue(const QString& value, std::list<T>& variable)
467  {
468  return readContainer<T>(value, std::back_insert_iterator<std::list<T> >(variable));
469  }
470 
471  template <typename T> bool Parms::readValue(const QString& value, std::set<T>& variable)
472  {
473  return readContainer<T>(value, std::insert_iterator<std::set<T> >(variable, variable.end()));
474  }
475 
476  template <typename T> bool Parms::readValue(const QString& value, T& variable)
477  {
478  QString val(value);
479  QTextStream iss(&val, QIODevice::ReadOnly);
480  iss >> variable;
481  return iss.status() == QTextStream::Ok;
482  }
483 
484  template <typename T> bool Parms::operator()(const QString& section, const QString& key, T& value)
485  {
486  QStringList values;
487  if(!extractValues(section, key, values))
488  return false;
489 
490  if((values.size() > 1) && (VerboseLevel > 1)) {
491  Information::err << "Parms::operator():Warning multiple value for key [" << section << "]" << key
492  << ", last one used.\n" << flush;
493  }
494 
495  if(!readValue(values.back(), value)) {
496  if(VerboseLevel > 0) {
497  Information::err << "Parms::operator():Error getting value for key [" << section << "]" << key << " value "
498  << values.back() << "\n" << flush;
499  }
500  return false;
501  }
502  return true;
503  }
504 
505  template <typename T> bool Parms::operator()(const QString& section, const QString& key, T& value, const T& def)
506  {
507  bool found = true;
508  CheckExist = false;
509  if(!(*this)(section, key, value)) {
510  found = false;
511  if(VerboseLevel > 2) {
512  Information::err << "Parms::operator()::Info key [" << section << "]" << key
513  << " not found, using default value" << endl;
514  }
515  value = def;
516  }
517  CheckExist = true;
518  return found;
519  }
520 
521  template <typename Container> bool Parms::all(const QString& section, const QString& key, Container& value)
522  {
523  bool valid = true;
524  typedef typename Container::value_type T;
525  CheckExist = false;
526  QStringList values;
527  if(!extractValues(section, key, values))
528  return false;
529  value.clear();
530  std::insert_iterator<Container> it(value, value.end());
531  forall(const QString& val, values) {
532  T single_value;
533  if(readValue(val, single_value)) {
534  *it++ = (const T&)single_value;
535  } else {
536  if(VerboseLevel > 2) {
537  Information::err << "Parms::all:Error reading key [" << section << "]" << key << " with value " << val
538  << "\n" << flush;
539  }
540  valid = false;
541  }
542  }
543  CheckExist = true;
544  return valid;
545  }
546 
547  template <typename T, typename Container> bool Parms::all(const QString& section, std::map<QString, Container>& result)
548  {
549  bool valid = true;
550  CheckExist = false;
551  typedef std::map<QString, QStringList>::value_type value_type;
552  result.clear();
553  int pos = section.size() + 1;
554  forall(const value_type& pair, Parameters) {
555  QString sec(pair.first.mid(0, pos - 1));
556  if(sec != section)
557  continue;
558  QString key(pair.first.mid(pos));
559  Container& value = result[key];
560  value.clear();
561  forall(const QString& val, pair.second) {
562  T single_value;
563  if(readValue(val, single_value)) {
564  value.push_back(single_value);
565  } else {
566  if(VerboseLevel > 2) {
567  Information::err << "Parms::all:Error reading key [" << section << "]" << key
568  << " with value " << val << endl;
569  }
570  valid = false;
571  }
572  }
573  }
574  CheckExist = true;
575  return valid;
576  }
577 }}
578 #endif
This file contains the defines the forall loops.
bool isLoaded() const
Returns true if the parameter object has been correctly loaded.
Definition: Parms.hpp:131
bool empty() const
Returns true if the parameter file was empty (e.g.
Definition: Parms.hpp:138
bool operator()(const QString &section, const QString &key, T &value)
This operator retrieve a single parameter.
Definition: Parms.hpp:484
void verboseLevel(int vl)
Change the verbosity level.
Definition: Parms.hpp:124
A utility class to parse L-Studio like parameter files.
Definition: Parms.hpp:105
bool all(const QString &section, const QString &key, Container &value)
This operator retrieves all parameters with same [section]key.
Definition: Parms.hpp:521