ecto
parameters.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <ecto/abi.hpp>
4 #include <ecto/parameters.hpp>
5 
6 #include <stdexcept>
7 #include <exception>
8 #include <boost/format.hpp>
9 #include <boost/lexical_cast.hpp>
10 
11 namespace ecto
12 {
13  template<typename T>
14  bounded<T>::bounded(const T& value, const T& min, const T& max)
15  :
16  min(min),
17  max(max),
18  has_bounds(true)
19  {
20  *this = value;
21  }
22 
23  template<typename T>
25  :
26  value(value),
27  min(),
28  max(),
29  has_bounds(false)
30  {
31  }
32  template<typename T>
33  void
35  {
36  if (!check(value))
37  throw std::runtime_error(
38  "Bad bounds! " + boost::lexical_cast<std::string>(value) + " is not within: " + bounds());
39  this->value = value;
40  }
41 
42  template<typename T>
43  bounded<T>&
45  {
46  set(value);
47  return *this;
48  }
49 
50  template<typename T>
51  bool
52  bounded<T>::check(const T& value) const
53  {
54  if (!has_bounds)
55  return true;
56  if (min >= value || max <= value)
57  return false;
58  return true;
59  }
60 
61  template<typename T>
62  std::string
64  {
65  return boost::str(
66  boost::format("(%s,%s)") % boost::lexical_cast<std::string>(min) % boost::lexical_cast<std::string>(max));
67  }
68 
69  template<typename T>
71  {
72  return value;
73  }
74 }
bounded(const T &value, const T &min, const T &max)
Definition: parameters.hpp:14
T max
Definition: parameters.hpp:32
Definition: parameters.hpp:11
bounded & operator=(const T &value)
Definition: parameters.hpp:44
std::string bounds() const
Definition: parameters.hpp:63
Definition: parameters.hpp:13
bool has_bounds
Definition: parameters.hpp:33
T min
Definition: parameters.hpp:32
bool check(const T &value) const
Definition: parameters.hpp:52
T value
Definition: parameters.hpp:32
void set(const T &value)
Definition: parameters.hpp:34