param_utils.h
1 /*
2  * This file is part of SPORE.
3  *
4  * Copyright (C) 2016, the SPORE team (see AUTHORS).
5  *
6  * SPORE is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * SPORE is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with SPORE. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * For more information see: https://github.com/IGITUGraz/spore-nest-module
20  *
21  * File: param_utils.h
22  * Author: kappel
23  *
24  * Created on March 31, 2017, 6:10 PM
25  */
26 
27 #ifndef PARAM_UTILS_H
28 #define PARAM_UTILS_H
29 
30 #include <string>
31 #include <sstream>
32 #include <exception>
33 
34 #include "nest.h"
35 #include "kernel_manager.h"
36 #include "exceptions.h"
37 #include "dictdatum.h"
38 #include "logging.h"
39 
40 
41 namespace spore
42 {
43 
47 namespace pc
48 {
52 template < typename T >
53  class Con
54 {
55 public:
56  virtual void apply( T const&, const Name& name ) const = 0;
57 };
58 
62 template < typename T >
63  class CMin : public Con < T >
64 {
65 public:
66  CMin( T min_value )
67  : min_value_( min_value )
68  {
69  }
70 
71  void apply( T const& val, const Name& name ) const
72  {
73  if ( val < min_value_ )
74  {
75  std::stringstream strm;
76  strm << "Parameter '" << name << "' must not be smaller than " << T(min_value_) << " but is " << T(val);
77  LOG( nest::M_ERROR, "CMin::apply()", strm.str() );
78  throw nest::BadProperty("Parameter out of range. See LOG file for details.");
79  }
80  }
81 
82 private:
83  T min_value_;
84 };
85 
89 template < typename T >
90  class CMax : public Con < T >
91 {
92 public:
93  CMax( T max_value )
94  : max_value_( max_value )
95  {
96  }
97 
98  void apply( T const& val, const Name& name ) const
99  {
100  if ( val > max_value_ )
101  {
102  std::stringstream strm;
103  strm << "Parameter '" << name << "' must not be bigger than " << T(max_value_) << " but is " << T(val);
104  LOG( nest::M_ERROR, "CMax::apply()", strm.str() );
105  throw nest::BadProperty("Parameter out of range. See LOG file for details.");
106  }
107  }
108 
109 private:
110  T max_value_;
111 };
112 
116 template < typename T >
117  class CBigger : public Con < T >
118 {
119 public:
120  CBigger( T min_value )
121  : min_value_( min_value )
122  {
123  }
124 
125  void apply( T const& val, const Name& name ) const
126  {
127  if ( val <= min_value_ )
128  {
129  std::stringstream strm;
130  strm << "Parameter '" << name << "' must be strictly bigger than " << T(min_value_) << " but is " << T(val);
131  LOG( nest::M_ERROR, "CBigger::apply()", strm.str() );
132  throw nest::BadProperty("Parameter out of range. See LOG file for details.");
133  }
134  }
135 
136 private:
137  T min_value_;
138 };
139 
143 template < typename T >
144  class CSmaller : public Con < T >
145 {
146 public:
147  CSmaller( T min_value )
148  : max_value_( min_value )
149  {
150  }
151 
152  void apply( T const& val, const Name& name ) const
153  {
154  if ( val >= max_value_ )
155  {
156  std::stringstream strm;
157  strm << "Parameter '" << name << "' must be strictly smaller than " << T(max_value_)
158  << " but is " << T(val);
159  LOG( nest::M_ERROR, "CSmaller::apply()", strm.str() );
160  throw nest::BadProperty("Parameter out of range. See LOG file for details.");
161  }
162  }
163 
164 private:
165  T max_value_;
166 };
167 
168 typedef CMin< double > MinD;
169 typedef CMin< long > MinL;
171 typedef CMin< int > MinI;
173 typedef CMax< double > MaxD;
174 typedef CMax< long > MaxL;
176 typedef CMax< int > MaxI;
178 typedef CBigger< double > BiggerD;
179 typedef CBigger< long > BiggerL;
181 typedef CBigger< int > BiggerI;
184 typedef CSmaller< long > SmallerL;
186 typedef CSmaller< int > SmallerI;
188 }
189 
194 {
195 public:
196  SetDefault();
197 
204  template < typename T >
205  void parameter( T& val, const Name& name, T default_val )
206  {
207  val = default_val;
208  }
209 
217  template < typename T >
218  void parameter( T& val, const Name& name, T default_val, const pc::Con< T >& con1 )
219  {
220  con1.apply(default_val, name);
221  val = default_val;
222  }
223 
231  template < typename T >
232  void parameter( T& val, const Name& name, T default_val, const pc::Con< T >& con1,
233  const pc::Con< T >& con2 )
234  {
235  con1.apply(default_val, name);
236  con2.apply(default_val, name);
237  val = default_val;
238  }
239 
247  template < typename T >
248  void parameter( T& val, const Name& name, T default_val, const pc::Con< T >& con1,
249  const pc::Con< T >& con2, const pc::Con< T >& con3 )
250  {
251  con1.apply(default_val, name);
252  con2.apply(default_val, name);
253  con3.apply(default_val, name);
254  val = default_val;
255  }
256 };
257 
262 {
263 public:
264  GetStatus(DictionaryDatum& d);
265 
272  template < typename T >
273  void parameter( const T& val, const Name& name, T )
274  {
275  def< T >(d_, name, val);
276  }
277 
285  template < typename T >
286  void parameter( const T& val, const Name& name, T, const pc::Con< T >& )
287  {
288  def< T >(d_, name, val);
289  }
290 
298  template < typename T >
299  void parameter( const T& val, const Name& name, T, const pc::Con< T >&, const pc::Con< T >& )
300  {
301  def< T >(d_, name, val);
302  }
303 
311  template < typename T >
312  void parameter( const T& val, const Name& name, T, const pc::Con< T >&, const pc::Con< T >&,
313  const pc::Con< T >& )
314  {
315  def< T >(d_, name, val);
316  }
317 
318 private:
319  DictionaryDatum& d_;
320 };
321 
326 {
327 public:
328  CheckParameters(const DictionaryDatum& d);
329 
336  template < typename T >
337  void parameter( const T&, const Name&, T )
338  {
339  }
340 
348  template < typename T >
349  void parameter( const T&, const Name& name, T, const pc::Con< T >& con1 )
350  {
351  T val;
352  if (updateValue< T >(d_, name, val))
353  {
354  con1.apply(val, name);
355  }
356  }
357 
365  template < typename T >
366  void parameter( const T&, const Name& name, T, const pc::Con< T >& con1,
367  const pc::Con< T >& con2 )
368  {
369  T val;
370  if (updateValue< T >(d_, name, val))
371  {
372  con1.apply(val, name);
373  con2.apply(val, name);
374  }
375  }
376 
384  template < typename T >
385  void parameter( const T&, const Name& name, T, const pc::Con< T >& con1,
386  const pc::Con< T >& con2, const pc::Con< T >& con3 )
387  {
388  T val;
389  if (updateValue< T >(d_, name, val))
390  {
391  con1.apply(val, name);
392  con2.apply(val, name);
393  con3.apply(val, name);
394  }
395  }
396 
397 private:
398  const DictionaryDatum& d_;
399 };
400 
405 {
406 public:
407  SetStatus(const DictionaryDatum& d);
408 
415  template < typename T >
416  void parameter( T& val, const Name& name, T )
417  {
418  updateValue< T >(d_, name, val);
419  }
420 
428  template < typename T >
429  void parameter( T& val, const Name& name, T, const pc::Con< T >& )
430  {
431  updateValue< T >(d_, name, val);
432  }
433 
441  template < typename T >
442  void parameter( T& val, const Name& name, T, const pc::Con< T >&, const pc::Con< T >& )
443  {
444  updateValue< T >(d_, name, val);
445  }
446 
454  template < typename T >
455  void parameter( T& val, const Name& name, T, const pc::Con< T >&, const pc::Con< T >&,
456  const pc::Con< T >& )
457  {
458  updateValue< T >(d_, name, val);
459  }
460 
461 private:
462  const DictionaryDatum& d_;
463 };
464 
465 }
466 
467 #endif
void parameter(T &val, const Name &name, T, const pc::Con< T > &, const pc::Con< T > &, const pc::Con< T > &)
Define parameter with 3 constraints.
Definition: param_utils.h:455
void parameter(T &val, const Name &name, T default_val, const pc::Con< T > &con1, const pc::Con< T > &con2)
Define parameter with 2 constraints.
Definition: param_utils.h:232
void parameter(T &val, const Name &name, T)
Define parameter without constraint.
Definition: param_utils.h:416
void parameter(T &val, const Name &name, T, const pc::Con< T > &)
Define parameter with 1 constraint.
Definition: param_utils.h:429
void parameter(const T &val, const Name &name, T, const pc::Con< T > &)
Define parameter with 1 constraint.
Definition: param_utils.h:286
void parameter(const T &val, const Name &name, T, const pc::Con< T > &, const pc::Con< T > &)
Define parameter with 2 constraints.
Definition: param_utils.h:299
void parameter(const T &, const Name &name, T, const pc::Con< T > &con1, const pc::Con< T > &con2, const pc::Con< T > &con3)
Define parameter with 3 constraints.
Definition: param_utils.h:385
void parameter(const T &, const Name &name, T, const pc::Con< T > &con1)
Define parameter with 1 constraint.
Definition: param_utils.h:349
Maximum condition. Parameter must be smaller or equal to given value.
Definition: param_utils.h:90
void parameter(const T &val, const Name &name, T, const pc::Con< T > &, const pc::Con< T > &, const pc::Con< T > &)
Define parameter with 3 constraints.
Definition: param_utils.h:312
Minimum condition. Parameter must be larger or equal to given value.
Definition: param_utils.h:63
void parameter(const T &, const Name &, T)
Define parameter without constraint.
Definition: param_utils.h:337
"Bigger than" condition. Parameter must be larger or equal to given value.
Definition: param_utils.h:117
Generic class to check parameters.
Definition: param_utils.h:325
void parameter(const T &val, const Name &name, T)
Define parameter without constraint.
Definition: param_utils.h:273
void parameter(T &val, const Name &name, T, const pc::Con< T > &, const pc::Con< T > &)
Define parameter with 2 constraints.
Definition: param_utils.h:442
void parameter(T &val, const Name &name, T default_val)
Define parameter without constraint.
Definition: param_utils.h:205
Generic parameter setter class.
Definition: param_utils.h:404
Base class to all conditions that can be imposed onto parameters.
Definition: param_utils.h:53
"Smaller than" condition. Parameter must be larger or equal to given value.
Definition: param_utils.h:144
void parameter(const T &, const Name &name, T, const pc::Con< T > &con1, const pc::Con< T > &con2)
Define parameter with 2 constraints.
Definition: param_utils.h:366
Generic parameter getter class.
Definition: param_utils.h:261
Global namespace holding all classes of the SPORE NEST module.
Definition: circular_buffer.h:31
void parameter(T &val, const Name &name, T default_val, const pc::Con< T > &con1, const pc::Con< T > &con2, const pc::Con< T > &con3)
Define parameter with 3 constraints.
Definition: param_utils.h:248
void parameter(T &val, const Name &name, T default_val, const pc::Con< T > &con1)
Define parameter with 1 constraint.
Definition: param_utils.h:218
Generic class to set up the default parameters.
Definition: param_utils.h:193