2bd50403e7bfab5320bdf44b5b4a1db8567bb457
[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
28 /** \file
29     \brief RestrictedInt public header */
30
31 #ifndef HH_SENF_senf_Utils_RestrictedInt_
32 #define HH_SENF_senf_Utils_RestrictedInt_ 1
33
34 // Custom includes
35 #include <iostream>
36 #include <boost/operators.hpp>
37 #include <senf/Utils/safe_bool.hh>
38
39 //#include "RestrictedInt.mpp"
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41
42 namespace senf {
43
44     // Really ... the correct spelling is 'euclidean' as in the current boost
45     // Versions but older versions only have this spelling so we can' t use
46     // the correct class name ...
47
48     template <class Base, class Tag>
49     class RestrictedInt
50         : public boost::ordered_euclidian_ring_operators< RestrictedInt<Base,Tag>,
51                  boost::unit_steppable< RestrictedInt<Base,Tag>,
52                  boost::shiftable< RestrictedInt<Base,Tag>,
53                  boost::bitwise1< RestrictedInt<Base,Tag>,
54                  senf::comparable_safe_bool< RestrictedInt<Base,Tag> > > > > >
55     {
56     public:
57         typedef Base base_type;
58         typedef Tag tag_type;
59
60         explicit RestrictedInt(Base value) : value_ (value) {}
61         RestrictedInt() : value_ () {}
62
63 #       define BinaryOp(op)                                             \
64             RestrictedInt & operator op ## = (RestrictedInt other)      \
65                 { value_ op ## = other.value_; return *this; }
66 #       define IncDecOp(op)                                             \
67             RestrictedInt & operator op ()                              \
68                 { op value_; return *this; }
69 #       define PrefixOp(op)                                             \
70             RestrictedInt const operator op () const                    \
71                 { return RestrictedInt(op value_); }
72 #       define CompareOp(op)                                            \
73             bool operator op (RestrictedInt other) const                \
74                 { return value_ op other.value_; }
75
76         BinaryOp(+)
77         BinaryOp(-)
78         BinaryOp(*)
79         BinaryOp(/)
80         BinaryOp(%)
81         BinaryOp(>>)
82         BinaryOp(<<)
83         BinaryOp(&)
84         BinaryOp(|)
85         BinaryOp(^)
86
87         IncDecOp(++)
88         IncDecOp(--)
89
90         PrefixOp(~)
91         PrefixOp(-)
92
93         CompareOp(<)
94         CompareOp(==)
95
96 #       undef CompareOp
97 #       undef PrefixOp
98 #       undef PostfixOp
99 #       undef BinaryOp
100
101         Base value() const
102             { return value_; }
103
104         bool boolean_test() const
105             { return value_; }
106
107     private:
108         Base value_;
109     };
110
111     template <class Base, class Tag>
112     std::ostream & operator<<(std::ostream & os, RestrictedInt<Base, Tag> value)
113         { os << value.value(); return os; }
114
115     template <class Base, class Tag>
116     std::istream & operator>>(std::istream & is, RestrictedInt<Base, Tag> & value)
117         { Base v; is >> v; value = RestrictedInt<Base,Tag>(v); return is; }
118
119 }
120
121 //-/////////////////////////////////////////////////////////////////////////////////////////////////
122 //#include "RestrictedInt.cci"
123 //#include "RestrictedInt.ct"
124 //#include "RestrictedInt.cti"
125 #endif
126
127 \f
128 // Local Variables:
129 // mode: c++
130 // fill-column: 100
131 // comment-column: 40
132 // c-file-style: "senf"
133 // indent-tabs-mode: nil
134 // ispell-local-dictionary: "american"
135 // compile-command: "scons -u test"
136 // End: