1 // boost/integer/cover_operators.hpp ----------------------------------------//
3 // Copyright Darin Adler 2000
4 // Copyright Beman Dawes 2008
6 // Distributed under the Boost Software License, Version 1.0. (See accompanying
7 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //----------------------------------------------------------------------------//
11 // If the class being covered has a non-explicit conversion to an integer type
12 // then a smaller number of cover operations are needed. Define the macro
13 // BOOST_MINIMAL_INTEGER_COVER_OPERATORS to indicate this.
15 // Define BOOST_NO_IO_COVER_OPERATORS if I/O cover operations are not desired.
17 //----------------------------------------------------------------------------//
19 #ifndef BOOST_INTEGER_COVER_OPERATORS_HPP
20 #define BOOST_INTEGER_COVER_OPERATORS_HPP
22 # ifndef BOOST_MINIMAL_INTEGER_COVER_OPERATORS
23 # include <boost/operators.hpp>
33 // A class that adds integer operators to an integer cover class
35 template <typename T
, typename IntegerType
>
37 # ifndef BOOST_MINIMAL_INTEGER_COVER_OPERATORS
41 // The other operations take advantage of the type conversion that's
42 // built into unary +.
45 friend IntegerType
operator+(const T
& x
) { return x
; }
46 # ifndef BOOST_MINIMAL_INTEGER_COVER_OPERATORS
47 friend IntegerType
operator-(const T
& x
) { return -+x
; }
48 friend IntegerType
operator~(const T
& x
) { return ~+x
; }
49 friend IntegerType
operator!(const T
& x
) { return !+x
; }
51 // The basic ordering operations.
52 friend bool operator==(const T
& x
, IntegerType y
) { return +x
== y
; }
53 friend bool operator<(const T
& x
, IntegerType y
) { return +x
< y
; }
56 // The basic arithmetic operations.
57 friend T
& operator+=(T
& x
, IntegerType y
) { return x
= +x
+ y
; }
58 friend T
& operator-=(T
& x
, IntegerType y
) { return x
= +x
- y
; }
59 friend T
& operator*=(T
& x
, IntegerType y
) { return x
= +x
* y
; }
60 friend T
& operator/=(T
& x
, IntegerType y
) { return x
= +x
/ y
; }
61 friend T
& operator%=(T
& x
, IntegerType y
) { return x
= +x
% y
; }
62 friend T
& operator&=(T
& x
, IntegerType y
) { return x
= +x
& y
; }
63 friend T
& operator|=(T
& x
, IntegerType y
) { return x
= +x
| y
; }
64 friend T
& operator^=(T
& x
, IntegerType y
) { return x
= +x
^ y
; }
65 friend T
& operator<<=(T
& x
, IntegerType y
) { return x
= +x
<< y
; }
66 friend T
& operator>>=(T
& x
, IntegerType y
) { return x
= +x
>> y
; }
68 // A few binary arithmetic operations not covered by operators base class.
69 friend IntegerType
operator<<(const T
& x
, IntegerType y
) { return +x
<< y
; }
70 friend IntegerType
operator>>(const T
& x
, IntegerType y
) { return +x
>> y
; }
72 // Auto-increment and auto-decrement can be defined in terms of the
73 // arithmetic operations.
74 friend T
& operator++(T
& x
) { return x
+= 1; }
75 friend T
& operator--(T
& x
) { return x
-= 1; }
77 # ifdef BOOST_MINIMAL_INTEGER_COVER_OPERATORS
78 friend T
operator++(T
& x
, int)
84 friend T
operator--(T
& x
, int)
92 # ifndef BOOST_NO_IO_COVER_OPERATORS
93 // TODO: stream I/O needs to be templatized on the stream type, so will
94 // work with wide streams, etc.
96 // Stream input and output.
97 friend std::ostream
& operator<<(std::ostream
& s
, const T
& x
)
99 friend std::istream
& operator>>(std::istream
& s
, T
& x
)
108 } // namespace integer
111 #endif // BOOST_INTEGER_COVER_OPERATORS_HPP