08714ac896dcccdbb67157a16dc1e149bfd421dd
[senf.git] / senf / Utils / RestrictedInt.hh
1 // $Id$
2 //
3 // Copyright (C) 2010
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27 //   Philipp Batroff <philipp.batroff@fokus.fraunhofer.de>
28
29 /** \file
30     \brief RestrictedInt public header */
31
32 #ifndef HH_SENF_senf_Utils_RestrictedInt_
33 #define HH_SENF_senf_Utils_RestrictedInt_ 1
34
35 // Custom includes
36 #include <iostream>
37 #include <boost/operators.hpp>
38 #include <senf/Utils/safe_bool.hh>
39
40 //#include "RestrictedInt.mpp"
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 namespace senf {
44
45     // Really ... the correct spelling is 'euclidean' as in the current boost
46     // Versions but older versions only have this spelling so we can' t use
47     // the correct class name ...
48
49     template <class Base, class Tag>
50     class RestrictedInt
51         : public boost::ordered_euclidian_ring_operators< RestrictedInt<Base,Tag>,
52                  boost::unit_steppable< RestrictedInt<Base,Tag>,
53                  boost::shiftable< RestrictedInt<Base,Tag>,
54                  boost::bitwise1< RestrictedInt<Base,Tag>,
55                  senf::comparable_safe_bool< RestrictedInt<Base,Tag> > > > > >
56     {
57     public:
58         typedef Base base_type;
59         typedef Tag tag_type;
60
61         explicit RestrictedInt(Base value) : value_ (value) {}
62         RestrictedInt() : value_ () {}
63
64 #       define BinaryOp(op)                                             \
65             RestrictedInt & operator op ## = (RestrictedInt other)      \
66                 { value_ op ## = other.value_; return *this; }
67 #       define IncDecOp(op)                                             \
68             RestrictedInt & operator op ()                              \
69                 { op value_; return *this; }
70 #       define PrefixOp(op)                                             \
71             RestrictedInt const operator op () const                    \
72                 { return RestrictedInt(op value_); }
73 #       define CompareOp(op)                                            \
74             bool operator op (RestrictedInt other) const                \
75                 { return value_ op other.value_; }
76
77         BinaryOp(+)
78         BinaryOp(-)
79         BinaryOp(*)
80         BinaryOp(/)
81         BinaryOp(%)
82         BinaryOp(>>)
83         BinaryOp(<<)
84         BinaryOp(&)
85         BinaryOp(|)
86         BinaryOp(^)
87
88         IncDecOp(++)
89         IncDecOp(--)
90
91         PrefixOp(~)
92         PrefixOp(-)
93
94         CompareOp(<)
95         CompareOp(==)
96
97 #       undef CompareOp
98 #       undef PrefixOp
99 #       undef PostfixOp
100 #       undef BinaryOp
101
102         Base value() const
103             { return value_; }
104
105         bool boolean_test() const
106             { return value_; }
107
108         RestrictedInt<Base, Tag> operator*(int i) const
109             { return RestrictedInt<Base, Tag>(value_ * i); }
110
111         RestrictedInt<Base, Tag> operator/(int i) const
112             { return RestrictedInt<Base, Tag>(value_ / i); }
113
114         bool operator>(int i) const
115             { return value_ > i; }
116
117         bool operator>=(int i) const
118             { return value_ >= i; }
119
120         bool operator<(int i) const
121             { return value_ < i; }
122
123         bool operator<=(int i) const
124             { return value_ <= i; }
125
126         bool operator==(int i) const
127             { return value_ == i; }
128
129         bool operator!=(int i) const
130             { return value_ != i; }
131
132     private:
133         Base value_;
134     };
135
136     template <class Base, class Tag>
137     std::ostream & operator<<(std::ostream & os, RestrictedInt<Base, Tag> value)
138         { os << value.value(); return os; }
139
140     template <class Base, class Tag>
141     std::istream & operator>>(std::istream & is, RestrictedInt<Base, Tag> & value)
142         { Base v; is >> v; value = RestrictedInt<Base,Tag>(v); return is; }
143
144 }
145
146 //-/////////////////////////////////////////////////////////////////////////////////////////////////
147 //#include "RestrictedInt.cci"
148 //#include "RestrictedInt.ct"
149 //#include "RestrictedInt.cti"
150 #endif
151
152 \f
153 // Local Variables:
154 // mode: c++
155 // fill-column: 100
156 // comment-column: 40
157 // c-file-style: "senf"
158 // indent-tabs-mode: nil
159 // ispell-local-dictionary: "american"
160 // compile-command: "scons -u test"
161 // End: