RestrictedInt.hh
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #ifndef HH_SENF_Utils_RestrictedInt_
18 #define HH_SENF_Utils_RestrictedInt_ 1
19 
20 // Custom includes
21 #include <boost/operators.hpp>
22 #include <senf/Utils/safe_bool.hh>
23 
24 //#include "RestrictedInt.mpp"
25 //-/////////////////////////////////////////////////////////////////////////////////////////////////
26 
27 namespace senf {
28 
29  // Really ... the correct spelling is 'euclidean' as in the current boost
30  // Versions but older versions only have this spelling so we can' t use
31  // the correct class name ...
32 
33  template <class Base, class Tag>
35  : public boost::ordered_euclidian_ring_operators< RestrictedInt<Base,Tag>,
36  boost::unit_steppable< RestrictedInt<Base,Tag>,
37  boost::shiftable< RestrictedInt<Base,Tag>,
38  boost::bitwise1< RestrictedInt<Base,Tag>,
39  senf::comparable_safe_bool<RestrictedInt<Base,Tag> > > > > >
40  {
41  public:
42  typedef Base base_type;
43  typedef Tag tag_type;
44 
45  explicit RestrictedInt(Base value) : value_ (value) {}
46  RestrictedInt() : value_ () {}
47 
48 # define BinaryOp(op) \
49  RestrictedInt & operator op ## = (RestrictedInt other) \
50  { value_ op ## = other.value_; return *this; }
51 # define IncDecOp(op) \
52  RestrictedInt & operator op () \
53  { op value_; return *this; }
54 # define PrefixOp(op) \
55  RestrictedInt const operator op () const \
56  { return RestrictedInt(op value_); }
57 # define CompareOp(op) \
58  bool operator op (RestrictedInt other) const \
59  { return value_ op other.value_; }
60 
61  BinaryOp(+)
62  BinaryOp(-)
63  BinaryOp(*)
64  BinaryOp(/)
65  BinaryOp(%)
66  BinaryOp(>>)
67  BinaryOp(<<)
68  BinaryOp(&)
69  BinaryOp(|)
70  BinaryOp(^)
71 
72  IncDecOp(++)
73  IncDecOp(--)
74 
75  PrefixOp(~)
76  PrefixOp(-)
77 
78  CompareOp(<)
79  CompareOp(==)
80 
81 # undef CompareOp
82 # undef PrefixOp
83 # undef PostfixOp
84 # undef BinaryOp
85 
86  Base value() const
87  { return value_; }
88 
89  bool boolean_test() const
90  { return value_; }
91 
92  private:
93  Base value_;
94  };
95 
96 
97  template <class Base, class Tag, typename OtherType>
99  {
100  return RestrictedInt<Base, Tag>( a * b.value());
101  }
102 
103  template <class Base, class Tag, typename OtherType>
105  {
106  return RestrictedInt<Base, Tag>( a.value() * b);
107  }
108 
109  template <class Base, class Tag, typename OtherType>
111  {
112  return RestrictedInt<Base, Tag>( a / b.value());
113  }
114 
115  template <class Base, class Tag, typename OtherType>
117  {
118  return RestrictedInt<Base, Tag>( a.value() / b);
119  }
120 
121  template <class Base, class Tag>
122  std::ostream & operator<<(std::ostream & os, RestrictedInt<Base, Tag> value)
123  {
124  os << value.value();
125  return os;
126  }
127 
128  template <class Base, class Tag>
129  std::istream & operator>>(std::istream & is, RestrictedInt<Base, Tag> & value)
130  {
131  Base v; is >> v; value = RestrictedInt<Base,Tag>(v);
132  return is;
133  }
134 
135 }
136 
137 //-/////////////////////////////////////////////////////////////////////////////////////////////////
138 //#include "RestrictedInt.cci"
139 //#include "RestrictedInt.ct"
140 //#include "RestrictedInt.cti"
141 #endif
142 
143 
144 // Local Variables:
145 // mode: c++
146 // fill-column: 100
147 // comment-column: 40
148 // c-file-style: "senf"
149 // indent-tabs-mode: nil
150 // ispell-local-dictionary: "american"
151 // compile-command: "scons -u test"
152 // End:
RestrictedInt< Base, Tag > operator*(OtherType a, RestrictedInt< Base, Tag > b)
RestrictedInt(Base value)
#define IncDecOp(op)
#define CompareOp(op)
RestrictedInt< Base, Tag > operator/(OtherType a, RestrictedInt< Base, Tag > b)
BinaryOp(+) BinaryOp(-) BinaryOp(*) BinaryOp(/) BinaryOp(%) BinaryOp(>>) BinaryOp(<<) BinaryOp(&) BinaryOp(|) BinaryOp(^) IncDecOp(++) IncDecOp(--) PrefixOp(~) PrefixOp(-) CompareOp(<) CompareOp(
safe_bool public header
std::istream & operator>>(std::istream &is, RestrictedInt< Base, Tag > &value)
#define PrefixOp(op)