MorphoGraphX  2.0-1-227
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
39 {
116  class mgx_EXPORT Parms
117  {
118  public:
122  explicit Parms(int verboseLevel = 1);
123 
127  Parms(const QString& parmFile, int verboseLevel = 1);
128 
129  ~Parms();
130 
136  void verboseLevel(int vl) {
137  VerboseLevel = (vl < 0) ? 0 : vl;
138  }
139 
143  bool isLoaded() const {
144  return loaded;
145  }
146 
150  bool empty() const {
151  return Parameters.empty();
152  }
153 
171  template <typename T> bool operator()(const QString& section, const QString& key, T& value) const;
172 
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;
208 
217  bool operator()(const QString& section, const QString& key, QString& value) const;
218 
224  template <typename T> bool operator()(const QString& section, const QString& key, T& value, const T& def);
225 
240  template <typename Container> bool all(const QString& section, const QString& key, Container& value);
241 
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);
284 
299  template <typename T, typename Container> bool all(const QString& section, std::map<QString, Container>& values);
300 
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);
343 
344  private:
348  void init();
349 
355  bool extractValues(const QString& section, const QString& key, QStringList& values) const;
356 
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;
418 
419  template <typename T, typename InsertIterator> bool readContainer(const QString& value, InsertIterator container) const;
420 
424  QString ParmFileName;
425 
431  bool check(QString& key) const;
432 
437  std::map<QString, QStringList> Parameters;
438 
442  QString Section;
443 
447  int VerboseLevel;
448 
453  bool CheckExist;
454 
458  bool loaded;
459  };
460 
461  template <typename T, typename InsertIterator> bool Parms::readContainer(const QString& value, InsertIterator it) const
462  {
463  QString val(value);
464  QTextStream iss(&val, QIODevice::ReadOnly);
465  while(!iss.atEnd() and iss.status() == QTextStream::Ok) {
466  T v;
467  iss >> v;
468  *it++ = v;
469  }
470  return true;
471  }
472 
473  template <typename T> bool Parms::readValue(const QString& value, std::vector<T>& variable) const
474  {
475  return readContainer<T>(value, std::back_insert_iterator<std::vector<T> >(variable));
476  }
477 
478  template <typename T> bool Parms::readValue(const QString& value, std::list<T>& variable) const
479  {
480  return readContainer<T>(value, std::back_insert_iterator<std::list<T> >(variable));
481  }
482 
483  template <typename T> bool Parms::readValue(const QString& value, std::set<T>& variable) const
484  {
485  return readContainer<T>(value, std::insert_iterator<std::set<T> >(variable, variable.end()));
486  }
487 
488  template <typename T> bool Parms::readValue(const QString& value, T& variable) const
489  {
490  QString val(value);
491  QTextStream iss(&val, QIODevice::ReadOnly);
492  iss >> variable;
493  return iss.status() == QTextStream::Ok;
494  }
495 
496  template <typename T> bool Parms::operator()(const QString& section, const QString& key, T& value) const
497  {
498  QStringList values;
499  if(!extractValues(section, key, values))
500  return false;
501 
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;
505  }
506 
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;
511  }
512  return false;
513  }
514  return true;
515  }
516 
517  template <typename T> bool Parms::operator()(const QString& section, const QString& key, T& value, const T& def)
518  {
519  bool found = true;
520  CheckExist = false;
521  if(!(*this)(section, key, value)) {
522  found = false;
523  if(VerboseLevel > 2) {
524  Information::err << "Parms::operator()::Info key [" << section << "]" << key
525  << " not found, using default value" << endl;
526  }
527  value = def;
528  }
529  CheckExist = true;
530  return found;
531  }
532 
533  template <typename Container> bool Parms::all(const QString& section, const QString& key, Container& value)
534  {
535  bool valid = true;
536  typedef typename Container::value_type T;
537  CheckExist = false;
538  QStringList values;
539  if(!extractValues(section, key, values))
540  return false;
541  value.clear();
542  std::insert_iterator<Container> it(value, value.end());
543  forall(const QString& val, values) {
544  T single_value;
545  if(readValue(val, single_value)) {
546  *it++ = (const T&)single_value;
547  } else {
548  if(VerboseLevel > 2) {
549  Information::err << "Parms::all:Error reading key [" << section << "]" << key << " with value " << val
550  << "\n" << flush;
551  }
552  valid = false;
553  }
554  }
555  CheckExist = true;
556  return valid;
557  }
558 
559  template <typename T, typename Container>
560  bool Parms::all(const QString& section, std::map<QString, Container>& result)
561  {
562  bool valid = true;
563  CheckExist = false;
564  typedef std::map<QString, QStringList>::value_type value_type;
565  result.clear();
566  int pos = section.size() + 1;
567  forall(const value_type& pair, Parameters) {
568  QString sec(pair.first.mid(0, pos - 1));
569  if(sec != section)
570  continue;
571  QString key(pair.first.mid(pos));
572  Container& value = result[key];
573  value.clear();
574  forall(const QString& val, pair.second) {
575  T single_value;
576  if(readValue(val, single_value)) {
577  value.push_back(single_value);
578  } else {
579  if(VerboseLevel > 2) {
580  Information::err << "Parms::all:Error reading key [" << section << "]" << key
581  << " with value " << val << endl;
582  }
583  valid = false;
584  }
585  }
586  }
587  CheckExist = true;
588  return valid;
589  }
590 }
591 #endif
Information.hpp
mgx::Parms::verboseLevel
void verboseLevel(int vl)
Change the verbosity level.
Definition: Parms.hpp:136
forall
#define forall
Definition: Forall.hpp:22
mgx::Information::err
mgx_EXPORT QTextStream err
mgx::Information::init
mgx_EXPORT void init(QMainWindow *wnd)
mgx
Distributed matrix library.
Definition: Assert.hpp:26
mgx::Parms::isLoaded
bool isLoaded() const
Returns true if the parameter object has been correctly loaded.
Definition: Parms.hpp:143
mgx::Parms::all
bool all(const QString &section, const QString &key, Container &value)
This operator retrieves all parameters with same [section]key.
Definition: Parms.hpp:533
operator>>
mgx_EXPORT QTextStream & operator>>(QTextStream &ss, bool b)
mgx::Parms::operator()
bool operator()(const QString &section, const QString &key, T &value) const
This operator retrieve a single parameter.
Definition: Parms.hpp:496
mgx::Parms::empty
bool empty() const
Returns true if the parameter file was empty (e.g.
Definition: Parms.hpp:150
mgx::Parms
A utility class to parse L-Studio like parameter files.
Definition: Parms.hpp:116
Forall.hpp
mgx::map
CU_HOST_DEVICE Vector< dim, T > map(const T &(*fct)(const T1 &), const Vector< dim, T1 > &v)
Definition: Vector.hpp:1380