C++QEDCore  2.100.2 (v2 Milestone 10 Development branch)
a framework for simulating open quantum dynamics – core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Pars.h
Go to the documentation of this file.
1 // Copyright András Vukics 2006–2014. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.txt)
3 // -*- C++ -*-
4 #ifndef CPPQEDCORE_UTILS_PARS_H_INCLUDED
5 #define CPPQEDCORE_UTILS_PARS_H_INCLUDED
6 
7 #include "ParsFwd.h"
8 
9 #include "BooleanNegatedProxy.h"
10 #include "Exception.h"
11 
12 #include <boost/ptr_container/ptr_list.hpp>
13 
14 #include <string>
15 #include <sstream>
16 
17 
19 namespace parameters {
20 
21 const size_t maxTypeLabelLength=24;
22 
23 
25 
26 void update(ParameterTable& p,
27  int argc,
28  char* argv[],
29  const std::string& prefix="--"
30  );
31 
32 
34 //
35 // Pars Exceptions
36 //
38 
39 
41 {
42 public:
43  virtual ~Exception() {}
44 };
45 
46 
47 class NamedException : public Exception
48 {
49 public:
50  NamedException(const std::string& name);
51 
52  const std::string& getName() const {return name_;}
53 
54 private:
55  const std::string name_;
56 };
57 
58 
61 {
62 public:
63  UnrecognisedParameterException(const std::string& name) : NamedException(name) {}
64 };
65 
66 
69 {
70 public:
71  AttemptedRecreationOfParameterException(const std::string& name);
72 };
73 
74 
75 
77 
82 {
83 public:
84  ParameterBase(const std::string& s,
85  const std::string& d
86  ) : s_(s), d_(d) {}
87  virtual ~ParameterBase() {}
88 
89  const std::string& getS() const {return s_;}
90  const std::string& getD() const {return d_;}
91 
92  void print(size_t smw, size_t tmw, size_t dmw) const {print_v(smw,tmw,dmw);}
93  void read(std::istream& is) {read_v(is);}
94 
95 protected:
96  virtual void read_v(std::istream&) = 0;
97 
98 private:
99  virtual void print_v(size_t, size_t, size_t) const = 0;
100 
101  const std::string s_; // as appearing in command line
102  const std::string d_; // short description
103 
104 };
105 
106 
108 
109 template<typename T>
110 class Parameter : public ParameterBase
111 {
112 public:
113  Parameter(const std::string& s,
114  const std::string& d,
115  const T& v
116  ) : ParameterBase(s,d), v_(v) {}
117 
118  const T& get() const {return v_;}
119  T& get() {return const_cast<T&>(static_cast<const Parameter*>(this)->get());}
120 
121 protected:
122  void read_v(std::istream& is) {is>>v_;}
123 
124 private:
125  void print_v(size_t smw, size_t tmw, size_t dmw) const;
126 
127  T v_;
128 
129 };
130 
131 
133 
135 {
136 public:
137  typedef std::shared_ptr<std::string> ParsedCommandLine;
138 
139  ParameterTable() : table_(), smwidth_(0), tmwidth_(6), dmwidth_(0), stream_(), parsedCommandLine_(std::make_shared<std::string>("")) {} // tmwidth_ cf bool!
140 
142 
143  const ParameterBase& operator[](const std::string& ) const;
144  ParameterBase& operator[](const std::string& s) {return const_cast<ParameterBase&>(static_cast<const ParameterTable&>(*this)[s]);}
145 
146 
148 
149 
175  template<typename T> T& add(const std::string& s,
176  const std::string& d,
177  const T& v
178  );
179 
180 
182 
187  bool& add(const std::string& s, const std::string& d, bool v);
188 
190 
191  template<typename T> T& addMod(const std::string& s, const std::string& mod, const std::string& d, const T& v)
192  {
193  return add(s+mod,d,v);
194  }
195 
197  ParameterTable& addTitle(const std::string& s, const std::string& mod="");
199 
201 
202  void printList() const;
203 
205  std::stringstream& getStream() {return stream_;}
206 
208  const ParsedCommandLine getParsedCommandLine( ) const {return parsedCommandLine_;}
209 
211  void setParsedCommandLine(const std::string& s) { *parsedCommandLine_=s;}
212 
213 private:
214  typedef boost::ptr_list<ParameterBase> Impl;
215 
216  Impl table_;
217 
218  size_t smwidth_; // maximal width of s_ entries
219  size_t tmwidth_; // maximal width of typeIDs
220  size_t dmwidth_; // maximal width of d_ entries
221 
222  std::stringstream stream_;
223 
224  ParsedCommandLine parsedCommandLine_;
225 
226 };
227 
228 
229 } // parameters
230 
231 
232 #endif // CPPQEDCORE_UTILS_PARS_H_INCLUDED
The class that is (meant to be, at least) the base of all exceptions in the framework.
Definition: Exception.h:18
The parameter-bundle.
Definition: FormDouble.h:129
const ParsedCommandLine getParsedCommandLine() const
Getter for the full command line set by the update() function.
Definition: Pars.h:208
Template containing value for the given parameter.
Definition: Pars.h:110
ParameterTable & addTitle(const std::string &s, const std::string &mod="")
This adds a dummy “parameter” whose only effect is to cause a newline and a title to be printed for...
The parameter table according to which the command line will be parsed by update() ...
Definition: Pars.h:134
T & add(const std::string &s, const std::string &d, const T &v)
generic version
ParameterBase & operator[](const std::string &s)
” non-const version
Definition: Pars.h:144
T & addMod(const std::string &s, const std::string &mod, const std::string &d, const T &v)
Adds the parameter with a modifier suffix – useful when many parameters are distinguished only by su...
Definition: Pars.h:191
const ParameterBase & operator[](const std::string &) const
Subscription by parameter name.
Parameter(const std::string &s, const std::string &d, const T &v)
Definition: Pars.h:113
Defines class of the same name.
Common, template-parameter independent base class of Parameter, to be stored in a pointer-container i...
Definition: Pars.h:81
void setParsedCommandLine(const std::string &s)
Setter for the full command line used by the update() function.
Definition: Pars.h:211
std::stringstream & getStream()
The stream whereon parameter i/o occurs (an intermediary stringstream) is exposed in order that i/o m...
Definition: Pars.h:205
void printList() const
Print a full list of Parameters in help with name, typeID, description, and (default) value...
std::shared_ptr< std::string > ParsedCommandLine
Type for passing the parsed command line throughout the framework.
Definition: Pars.h:137
ParameterBase(const std::string &s, const std::string &d)
Definition: Pars.h:84
Defines tentative base classes for the exception classes of the framework.
Thrown if a given Parameter name is attempted to be used more than once when pushing to ParameterTabl...
Definition: Pars.h:68
Thrown if a certain Parameter name is not found in ParameterTable when subscripting.
Definition: Pars.h:60