Utils/RestrictedInt: export Base and Tag template types
[senf.git] / senf / Utils / RestrictedInt.hh
1 // $Id$
2 //
3 // Copyright (C) 2010
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief RestrictedInt public header */
25
26 #ifndef HH_SENF_senf_Utils_RestrictedInt_
27 #define HH_SENF_senf_Utils_RestrictedInt_ 1
28
29 // Custom includes
30 #include <iostream>
31 #include <boost/operators.hpp>
32 #include <senf/Utils/safe_bool.hh>
33
34 //#include "RestrictedInt.mpp"
35 //-/////////////////////////////////////////////////////////////////////////////////////////////////
36
37 namespace senf {
38
39     // Really ... the correct spelling is 'euclidean' as in the current boost
40     // Versions but older versions only have this spelling so we can' t use
41     // the correct class name ...
42
43     template <class Base, class Tag>
44     class RestrictedInt
45         : public boost::ordered_euclidian_ring_operators< RestrictedInt<Base,Tag>,
46                  boost::unit_steppable< RestrictedInt<Base,Tag>,
47                  boost::shiftable< RestrictedInt<Base,Tag>,
48                  boost::bitwise1< RestrictedInt<Base,Tag>,
49                  senf::comparable_safe_bool< RestrictedInt<Base,Tag> > > > > >
50     {
51     public:
52         typedef Base base_type;
53         typedef Tag tag_type;
54
55         explicit RestrictedInt(Base value) : value_ (value) {}
56         RestrictedInt() : value_ () {}
57
58 #       define BinaryOp(op)                                             \
59             RestrictedInt & operator op ## = (RestrictedInt other)      \
60                 { value_ op ## = other.value_; return *this; }
61 #       define IncDecOp(op)                                             \
62             RestrictedInt & operator op ()                              \
63                 { op value_; return *this; }
64 #       define PrefixOp(op)                                             \
65             RestrictedInt const operator op () const                    \
66                 { return RestrictedInt(op value_); }
67 #       define CompareOp(op)                                            \
68             bool operator op (RestrictedInt other) const                \
69                 { return value_ op other.value_; }
70
71         BinaryOp(+)
72         BinaryOp(-)
73         BinaryOp(*)
74         BinaryOp(/)
75         BinaryOp(%)
76         BinaryOp(>>)
77         BinaryOp(<<)
78         BinaryOp(&)
79         BinaryOp(|)
80         BinaryOp(^)
81
82         IncDecOp(++)
83         IncDecOp(--)
84
85         PrefixOp(~)
86         PrefixOp(-)
87
88         CompareOp(<)
89         CompareOp(==)
90
91 #       undef CompareOp
92 #       undef PrefixOp
93 #       undef PostfixOp
94 #       undef BinaryOp
95
96         Base value() const
97             { return value_; }
98
99         bool boolean_test() const
100             { return value_; }
101
102     private:
103         Base value_;
104     };
105
106     template <class Base, class Tag>
107     std::ostream & operator<<(std::ostream & os, RestrictedInt<Base, Tag> value)
108         { os << value.value(); return os; }
109
110     template <class Base, class Tag>
111     std::istream & operator>>(std::istream & is, RestrictedInt<Base, Tag> & value)
112         { Base v; is >> v; value = RestrictedInt<Base,Tag>(v); return is; }
113
114 }
115
116 //-/////////////////////////////////////////////////////////////////////////////////////////////////
117 //#include "RestrictedInt.cci"
118 //#include "RestrictedInt.ct"
119 //#include "RestrictedInt.cti"
120 #endif
121
122 \f
123 // Local Variables:
124 // mode: c++
125 // fill-column: 100
126 // comment-column: 40
127 // c-file-style: "senf"
128 // indent-tabs-mode: nil
129 // ispell-local-dictionary: "american"
130 // compile-command: "scons -u test"
131 // End: