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_SPIRIT_INTEGER_COVER_OPERATORS_HPP
20 #define BOOST_SPIRIT_INTEGER_COVER_OPERATORS_HPP
26 # ifndef BOOST_MINIMAL_INTEGER_COVER_OPERATORS
27 # include <boost/operators.hpp>
32 namespace boost { namespace spirit
37 // A class that adds integer operators to an integer cover class
39 template <typename T, typename IntegerType>
41 # ifndef BOOST_MINIMAL_INTEGER_COVER_OPERATORS
45 // The other operations take advantage of the type conversion that's
46 // built into unary +.
49 friend IntegerType operator+(const T& x) { return x; }
50 # ifndef BOOST_MINIMAL_INTEGER_COVER_OPERATORS
51 friend IntegerType operator-(const T& x) { return -+x; }
52 friend IntegerType operator~(const T& x) { return ~+x; }
53 friend IntegerType operator!(const T& x) { return !+x; }
55 // The basic ordering operations.
56 friend bool operator==(const T& x, IntegerType y) { return +x == y; }
57 friend bool operator<(const T& x, IntegerType y) { return +x < y; }
60 // The basic arithmetic operations.
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; }
67 friend T& operator|=(T& x, IntegerType y) { return x = +x | y; }
68 friend T& operator^=(T& x, IntegerType y) { return x = +x ^ y; }
69 friend T& operator<<=(T& x, IntegerType y) { return x = +x << y; }
70 friend T& operator>>=(T& x, IntegerType y) { return x = +x >> y; }
72 // A few binary arithmetic operations not covered by operators base class.
73 friend IntegerType operator<<(const T& x, IntegerType y) { return +x << y; }
74 friend IntegerType operator>>(const T& x, IntegerType y) { return +x >> y; }
76 // Auto-increment and auto-decrement can be defined in terms of the
77 // arithmetic operations.
78 friend T& operator++(T& x) { return x += 1; }
79 friend T& operator--(T& x) { return x -= 1; }
81 # ifdef BOOST_MINIMAL_INTEGER_COVER_OPERATORS
82 friend T operator++(T& x, int)
88 friend T operator--(T& x, int)
96 # ifndef BOOST_NO_IO_COVER_OPERATORS
97 // TODO: stream I/O needs to be templatized on the stream type, so will
98 // work with wide streams, etc.
100 // Stream input and output.
101 friend std::ostream& operator<<(std::ostream& s, const T& x)
103 friend std::istream& operator>>(std::istream& s, T& x)
112 } // namespace endian
113 }} // namespace boost::spirit
115 #endif // BOOST_SPIRIT_INTEGER_COVER_OPERATORS_HPP