Tapkee
parameter.hpp
Go to the documentation of this file.
1 
29 #ifndef STICHWORT_PARAMETER_H_
30 #define STICHWORT_PARAMETER_H_
31 
33 
34 #include <sstream>
35 #include <vector>
36 #include <map>
37 #include <iostream>
38 #include <list>
39 
40 namespace stichwort
41 {
42 
43 class ParametersSet;
44 class CheckedParameter;
45 
46 class Parameter
47 {
48  friend class CheckedParameter;
49 
50  typedef std::string ParameterName;
51 
52 private:
53 
54  template <typename T>
55  Parameter(const ParameterName& pname, const T& value) :
56  valid(true), invalidity_reasons(),
57  parameter_name(pname), keeper(stichwort_internal::ValueKeeper(value))
58  {
59  }
60 
61 public:
62 
63  template <typename T>
64  static Parameter create(const std::string& name, const T& value)
65  {
66  return Parameter(name, value);
67  }
68 
69  Parameter() :
70  valid(true), invalidity_reasons(),
71  parameter_name("unknown"), keeper(stichwort_internal::ValueKeeper())
72  {
73  }
74 
75  Parameter(const Parameter& p) :
78  {
79  }
80 
82  {
83  }
84 
85  template <typename T>
86  inline Parameter withDefault(T value)
87  {
88  if (!isInitialized())
89  {
91  }
92  return *this;
93  }
94 
95  template <typename T>
96  inline operator T()
97  {
98  if (!valid)
99  {
101  }
102  try
103  {
104  return getValue<T>();
105  }
106  catch (const missed_parameter_error&)
107  {
108  throw missed_parameter_error(parameter_name + " is missed");
109  }
110  }
111 
112  operator ParametersSet();
113 
114  template <typename T>
115  bool is(T v)
116  {
117  if (!isTypeCorrect<T>())
118  return false;
119  T kv = keeper.getValue<T>();
120  if (v == kv)
121  return true;
122  return false;
123  }
124 
125  template <typename T>
126  bool operator==(T v) const
127  {
128  return is<T>(v);
129  }
130 
132 
133  bool isInitialized() const
134  {
135  return keeper.isInitialized();
136  }
137 
138  template <template<class> class F, class Q>
139  inline bool isCondition(F<Q> cond) const
140  {
141  return keeper.isCondition(cond);
142  }
143 
145  {
146  return parameter_name;
147  }
148 
149  std::string repr() const
150  {
151  return keeper.repr();
152  }
153 
155 
156 private:
157 
158  template <typename T>
159  inline T getValue() const
160  {
161  return keeper.getValue<T>();
162  }
163 
164  template <typename T>
165  inline bool isTypeCorrect() const
166  {
167  return keeper.isTypeCorrect<T>();
168  }
169 
170  inline void invalidate(const std::string& reason)
171  {
172  if (valid)
173  valid = false;
174 
175  invalidity_reasons += reason;
176  }
177 
178 private:
179 
180  bool valid;
181  std::string invalidity_reasons;
184 
185 };
186 
188 {
189 
190 public:
191 
193  {
194  }
195 
196  inline operator const Parameter&()
197  {
198  return parameter;
199  }
200 
201  template <typename T>
202  bool is(T v)
203  {
204  return parameter.is<T>(v);
205  }
206 
207  template <template<class> class F, class Q>
208  inline Parameter& satisfies(const F<Q>& cond) const
209  {
210  if (!parameter.isCondition(cond))
211  parameter.invalidate(cond.failureMessage(parameter));
212 
213  return parameter;
214  }
215 
216  template <typename T>
217  bool operator==(T v)
218  {
219  return is<T>(v);
220  }
221 
222 private:
223 
225 
226 };
227 
229 {
230  return CheckedParameter(*this);
231 }
232 
234 {
235 public:
236 
237  typedef std::map<std::string, Parameter> ParametersMap;
238  typedef std::list<std::string> DuplicatesList;
239 
241  {
242  }
243  ParametersSet(const ParametersSet& other) : pmap(other.pmap), dups(other.dups)
244  {
245  }
247  {
248  this->pmap = other.pmap;
249  this->dups = other.dups;
250  return *this;
251  }
252  void check()
253  {
254  if (!dups.empty())
255  {
256  std::stringstream ss;
257  ss << "The following parameters are set more than once: ";
258  for (DuplicatesList::const_iterator iter=dups.begin(); iter!=dups.end(); ++iter)
259  ss << *iter << " ";
260 
261  throw multiple_parameter_error(ss.str());
262  }
263  }
264  void add(const Parameter& p)
265  {
266  if (pmap.count(p.name()))
267  dups.push_back(p.name());
268 
269  pmap[p.name()] = p;
270  }
271  bool contains(const std::string& name) const
272  {
273  return pmap.count(name) > 0;
274  }
275  void merge(const ParametersSet& pg)
276  {
277  typedef ParametersMap::const_iterator MapIter;
278  for (MapIter iter = pg.pmap.begin(); iter!=pg.pmap.end(); ++iter)
279  {
280  if (!pmap.count(iter->first))
281  {
282  pmap[iter->first] = iter->second;
283  }
284  }
285  }
286  Parameter operator[](const std::string& name) const
287  {
288  ParametersMap::const_iterator it = pmap.find(name);
289  if (it != pmap.end())
290  {
291  return it->second;
292  }
293  else
294  {
295  throw missed_parameter_error(name + " is missed");
296  }
297  }
299  {
300  add(p);
301  return *this;
302  }
303 
304 private:
305 
308 };
309 
311 {
312  ParametersSet pg;
313  pg.add(*this);
314  pg.add(p);
315  return pg;
316 }
317 
318 Parameter::operator ParametersSet()
319 {
320  ParametersSet pg;
321  pg.add(*this);
322  return pg;
323 }
324 
325 
326 }
327 
328 #endif
void add(const Parameter &p)
Definition: parameter.hpp:264
Parameter withDefault(T value)
Definition: parameter.hpp:86
An exception type that is thrown in case of missed parameter, i.e. when some required parameter is no...
CheckedParameter checked()
Definition: parameter.hpp:228
The namespace that contains implementations for the keywords.
Parameter & satisfies(const F< Q > &cond) const
Definition: parameter.hpp:208
friend class CheckedParameter
Definition: parameter.hpp:48
ParametersSet(const ParametersSet &other)
Definition: parameter.hpp:243
ParametersSet & operator,(const Parameter &p)
Definition: parameter.hpp:298
static Parameter create(const std::string &name, const T &value)
Definition: parameter.hpp:64
bool contains(const std::string &name) const
Definition: parameter.hpp:271
bool operator==(T v) const
Definition: parameter.hpp:126
std::list< std::string > DuplicatesList
Definition: parameter.hpp:238
Parameter(const ParameterName &pname, const T &value)
Definition: parameter.hpp:55
ParameterName name() const
Definition: parameter.hpp:144
std::string ParameterName
Definition: parameter.hpp:50
ParametersSet & operator=(const ParametersSet &other)
Definition: parameter.hpp:246
Parameter operator[](const std::string &name) const
Definition: parameter.hpp:286
bool isTypeCorrect() const
Definition: parameter.hpp:165
void invalidate(const std::string &reason)
Definition: parameter.hpp:170
std::string invalidity_reasons
Definition: parameter.hpp:181
An exception type that is thrown when some parameter is passed more than once.
stichwort_internal::ValueKeeper keeper
Definition: parameter.hpp:183
bool isInitialized() const
Definition: parameter.hpp:133
Parameter(const Parameter &p)
Definition: parameter.hpp:75
std::map< std::string, Parameter > ParametersMap
Definition: parameter.hpp:237
bool isCondition(F< Q > cond) const
Definition: parameter.hpp:139
ParametersSet operator,(const Parameter &p)
Definition: parameter.hpp:310
void merge(const ParametersSet &pg)
Definition: parameter.hpp:275
An exception type that is thrown in case if wrong parameter value is passed.
std::string repr() const
Definition: parameter.hpp:149
CheckedParameter(Parameter &p)
Definition: parameter.hpp:192
stichwort::ParametersSet ParametersSet
Definition: defines.hpp:64
ParameterName parameter_name
Definition: parameter.hpp:182