fixes for ClockService::clock_type -> RestrictedInt change introduced by last commit
[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     private:
109         Base value_;
110     };
111
112
113     template <class Base, class Tag, typename OtherType>
114     RestrictedInt<Base, Tag> operator*(OtherType a, RestrictedInt<Base,Tag> b)
115     {
116         return RestrictedInt<Base, Tag>( a * b.value());
117     }
118
119     template <class Base, class Tag, typename OtherType>
120     RestrictedInt<Base, Tag> operator*(RestrictedInt<Base,Tag> a, OtherType b)
121     {
122         return RestrictedInt<Base, Tag>( a.value() * b);
123     }
124
125     template <class Base, class Tag, typename OtherType>
126     RestrictedInt<Base, Tag> operator/(OtherType a, RestrictedInt<Base,Tag> b)
127     {
128         return RestrictedInt<Base, Tag>( a / b.value());
129     }
130
131     template <class Base, class Tag, typename OtherType>
132     RestrictedInt<Base, Tag> operator/(RestrictedInt<Base,Tag> a, OtherType b)
133     {
134         return RestrictedInt<Base, Tag>( a.value() / b);
135     }
136
137     template <class Base, class Tag>
138     std::ostream & operator<<(std::ostream & os, RestrictedInt<Base, Tag> value)
139     {
140         os << value.value();
141         return os;
142     }
143
144     template <class Base, class Tag>
145     std::istream & operator>>(std::istream & is, RestrictedInt<Base, Tag> & value)
146     {
147         Base v; is >> v; value = RestrictedInt<Base,Tag>(v);
148         return is;
149     }
150
151 }
152
153 //-/////////////////////////////////////////////////////////////////////////////////////////////////
154 //#include "RestrictedInt.cci"
155 //#include "RestrictedInt.ct"
156 //#include "RestrictedInt.cti"
157 #endif
158
159 \f
160 // Local Variables:
161 // mode: c++
162 // fill-column: 100
163 // comment-column: 40
164 // c-file-style: "senf"
165 // indent-tabs-mode: nil
166 // ispell-local-dictionary: "american"
167 // compile-command: "scons -u test"
168 // End: