MorphoGraphX
Parms.hpp
Go to the documentation of this file.
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2015 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 #ifndef __PARMS_HPP__
12 #define __PARMS_HPP__
13 
19 #include <Config.hpp>
20 
21 #include <Forall.hpp>
22 #include <Information.hpp>
23 
24 #include <iostream>
25 #include <iterator>
26 #include <list>
27 #include <map>
28 #include <QString>
29 #include <QStringList>
30 #include <QTextStream>
31 #include <set>
32 #include <sstream>
33 #include <string>
34 #include <vector>
35 
36 mgx_EXPORT QTextStream& operator>>(QTextStream& ss, bool b);
37 
38 namespace mgx { namespace util {
115  class mgx_EXPORT Parms {
116  public:
120  explicit Parms(int verboseLevel = 1);
121 
125  Parms(const QString& parmFile, int verboseLevel = 1);
126 
127  ~Parms();
128 
134  void verboseLevel(int vl) {
135  VerboseLevel = (vl < 0) ? 0 : vl;
136  }
137 
141  bool isLoaded() const {
142  return loaded;
143  }
144 
148  bool empty() const {
149  return Parameters.empty();
150  }
151 
169  template <typename T> bool operator()(const QString& section, const QString& key, T& value);
170 
178  bool operator()(const QString& section, const QString& key, bool& value);
184  bool operator()(const QString& section, const QString& key, int& value);
190  bool operator()(const QString& section, const QString& key, float& value);
196  bool operator()(const QString& section, const QString& key, double& value);
205  bool operator()(const QString& section, const QString& key, std::string& value);
206 
215  bool operator()(const QString& section, const QString& key, QString& value);
216 
222  template <typename T> bool operator()(const QString& section, const QString& key, T& value, const T& def);
223 
238  template <typename Container> bool all(const QString& section, const QString& key, Container& value);
239 
245  bool all(const QString& section, const QString& key, std::vector<bool>& value);
251  bool all(const QString& section, const QString& key, std::vector<int>& value);
257  bool all(const QString& section, const QString& key, std::vector<float>& value);
263  bool all(const QString& section, const QString& key, std::vector<double>& value);
269  bool all(const QString& section, const QString& key, std::vector<std::string>& value);
275  bool all(const QString& section, const QString& key, std::vector<QString>& value);
281  bool all(const QString& section, const QString& key, QStringList& value);
282 
297  template <typename T, typename Container> bool all(const QString& section, std::map<QString, Container>& values);
298 
304  bool all(const QString& section, std::map<QString, std::vector<bool> >& value);
310  bool all(const QString& section, std::map<QString, std::vector<int> >& value);
316  bool all(const QString& section, std::map<QString, std::vector<float> >& value);
322  bool all(const QString& section, std::map<QString, std::vector<double> >& value);
328  bool all(const QString& section, std::map<QString, std::vector<std::string> >& value);
334  bool all(const QString& section, std::map<QString, std::vector<QString> >& value);
340  bool all(const QString& section, std::map<QString, QStringList>& value);
341 
342  private:
346  void init();
347 
353  bool extractValues(const QString& section, const QString& key, QStringList& values);
354 
363  bool readValue(const QString& value, bool& variable);
371  bool readValue(const QString& value, std::string& variable);
379  bool readValue(const QString& value, QString& variable);
388  template <typename T> bool readValue(const QString& value, std::vector<T>& variable);
397  template <typename T> bool readValue(const QString& value, std::list<T>& variable);
406  template <typename T> bool readValue(const QString& value, std::set<T>& variable);
415  template <typename T> bool readValue(const QString& value, T& variable);
416 
417  template <typename T, typename InsertIterator> bool readContainer(const QString& value, InsertIterator container);
418 
422  QString ParmFileName;
423 
429  bool check(QString& key);
430 
435  std::map<QString, QStringList> Parameters;
436 
440  QString Section;
441 
445  int VerboseLevel;
446 
451  bool CheckExist;
452 
456  bool loaded;
457  };
458 
459  template <typename T, typename InsertIterator> bool Parms::readContainer(const QString& value, InsertIterator it)
460  {
461  QString val(value);
462  QTextStream iss(&val, QIODevice::ReadOnly);
463  while(!iss.atEnd() and iss.status() == QTextStream::Ok) {
464  T v;
465  iss >> v;
466  *it++ = v;
467  }
468  return true;
469  }
470 
471  template <typename T> bool Parms::readValue(const QString& value, std::vector<T>& variable)
472  {
473  return readContainer<T>(value, std::back_insert_iterator<std::vector<T> >(variable));
474  }
475 
476  template <typename T> bool Parms::readValue(const QString& value, std::list<T>& variable)
477  {
478  return readContainer<T>(value, std::back_insert_iterator<std::list<T> >(variable));
479  }
480 
481  template <typename T> bool Parms::readValue(const QString& value, std::set<T>& variable)
482  {
483  return readContainer<T>(value, std::insert_iterator<std::set<T> >(variable, variable.end()));
484  }
485 
486  template <typename T> bool Parms::readValue(const QString& value, T& variable)
487  {
488  QString val(value);
489  QTextStream iss(&val, QIODevice::ReadOnly);
490  iss >> variable;
491  return iss.status() == QTextStream::Ok;
492  }
493 
494  template <typename T> bool Parms::operator()(const QString& section, const QString& key, T& value)
495  {
496  QStringList values;
497  if(!extractValues(section, key, values))
498  return false;
499 
500  if((values.size() > 1) && (VerboseLevel > 1)) {
501  Information::err << "Parms::operator():Warning multiple value for key [" << section << "]" << key
502  << ", last one used.\n" << flush;
503  }
504 
505  if(!readValue(values.back(), value)) {
506  if(VerboseLevel > 0) {
507  Information::err << "Parms::operator():Error getting value for key [" << section << "]" << key << " value "
508  << values.back() << "\n" << flush;
509  }
510  return false;
511  }
512  return true;
513  }
514 
515  template <typename T> bool Parms::operator()(const QString& section, const QString& key, T& value, const T& def)
516  {
517  bool found = true;
518  CheckExist = false;
519  if(!(*this)(section, key, value)) {
520  found = false;
521  if(VerboseLevel > 2) {
522  Information::err << "Parms::operator()::Info key [" << section << "]" << key
523  << " not found, using default value" << endl;
524  }
525  value = def;
526  }
527  CheckExist = true;
528  return found;
529  }
530 
531  template <typename Container> bool Parms::all(const QString& section, const QString& key, Container& value)
532  {
533  bool valid = true;
534  typedef typename Container::value_type T;
535  CheckExist = false;
536  QStringList values;
537  if(!extractValues(section, key, values))
538  return false;
539  value.clear();
540  std::insert_iterator<Container> it(value, value.end());
541  forall(const QString& val, values) {
542  T single_value;
543  if(readValue(val, single_value)) {
544  *it++ = (const T&)single_value;
545  } else {
546  if(VerboseLevel > 2) {
547  Information::err << "Parms::all:Error reading key [" << section << "]" << key << " with value " << val
548  << "\n" << flush;
549  }
550  valid = false;
551  }
552  }
553  CheckExist = true;
554  return valid;
555  }
556 
557  template <typename T, typename Container> bool Parms::all(const QString& section, std::map<QString, Container>& result)
558  {
559  bool valid = true;
560  CheckExist = false;
561  typedef std::map<QString, QStringList>::value_type value_type;
562  result.clear();
563  int pos = section.size() + 1;
564  forall(const value_type& pair, Parameters) {
565  QString sec(pair.first.mid(0, pos - 1));
566  if(sec != section)
567  continue;
568  QString key(pair.first.mid(pos));
569  Container& value = result[key];
570  value.clear();
571  forall(const QString& val, pair.second) {
572  T single_value;
573  if(readValue(val, single_value)) {
574  value.push_back(single_value);
575  } else {
576  if(VerboseLevel > 2) {
577  Information::err << "Parms::all:Error reading key [" << section << "]" << key
578  << " with value " << val << endl;
579  }
580  valid = false;
581  }
582  }
583  }
584  CheckExist = true;
585  return valid;
586  }
587 }}
588 #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:141
bool empty() const
Returns true if the parameter file was empty (e.g.
Definition: Parms.hpp:148
bool operator()(const QString &section, const QString &key, T &value)
This operator retrieve a single parameter.
Definition: Parms.hpp:494
void verboseLevel(int vl)
Change the verbosity level.
Definition: Parms.hpp:134
This namespace contains all the API of MorphoGraphX.
Definition: Assert.hpp:26
A utility class to parse L-Studio like parameter files.
Definition: Parms.hpp:115
bool all(const QString &section, const QString &key, Container &value)
This operator retrieves all parameters with same [section]key.
Definition: Parms.hpp:531