1 // boost/endian/detail/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 #ifndef BOOST_ENDIAN_COVER_OPERATORS_HPP
10 #define BOOST_ENDIAN_COVER_OPERATORS_HPP
13 # pragma warning(push)
14 # pragma warning(disable:4365) // conversion ... signed/unsigned mismatch
17 # ifndef BOOST_ENDIAN_MINIMAL_COVER_OPERATORS
18 # include <boost/operators.hpp>
21 #include <boost/config.hpp>
29 //--------------------------------------------------------------------------------------//
31 // A class that adds arithmetic operators to an arithmetic cover class
33 // Uses the curiously recurring template pattern (CRTP).
35 // If the class being covered has a non-explicit conversion to an integer type
36 // then a smaller number of cover operations are needed. Define the macro
37 // BOOST_ENDIAN_MINIMAL_COVER_OPERATORS to indicate this.
39 // Define BOOST_NO_IO_COVER_OPERATORS if I/O cover operations are not desired.
41 //--------------------------------------------------------------------------------------//
43 template <class D, // D is the CRTP derived type, i.e. the cover class
46 # ifndef BOOST_ENDIAN_MINIMAL_COVER_OPERATORS
50 // The other operations take advantage of the type conversion that's
51 // built into unary +.
54 friend ArithmeticT operator+(const D& x) BOOST_NOEXCEPT { return x; }
55 # ifndef BOOST_ENDIAN_MINIMAL_COVER_OPERATORS
56 friend ArithmeticT operator-(const D& x) BOOST_NOEXCEPT { return -+x; }
57 friend ArithmeticT operator~(const D& x) BOOST_NOEXCEPT { return ~+x; }
58 friend ArithmeticT operator!(const D& x) BOOST_NOEXCEPT { return !+x; }
60 // The basic ordering operations.
61 friend bool operator==(const D& x, ArithmeticT y) BOOST_NOEXCEPT { return +x == y; }
62 friend bool operator<(const D& x, ArithmeticT y) BOOST_NOEXCEPT { return +x < y; }
65 // The basic arithmetic operations.
66 friend D& operator+=(D& x, ArithmeticT y) BOOST_NOEXCEPT
67 { return x = static_cast<ArithmeticT>(+x + y); }
68 friend D& operator-=(D& x, ArithmeticT y) BOOST_NOEXCEPT
69 { return x = static_cast<ArithmeticT>(+x - y); }
70 friend D& operator*=(D& x, ArithmeticT y) BOOST_NOEXCEPT
71 { return x = static_cast<ArithmeticT>(+x * y); }
72 friend D& operator/=(D& x, ArithmeticT y) BOOST_NOEXCEPT
73 { return x = static_cast<ArithmeticT>(+x / y); }
74 friend D& operator%=(D& x, ArithmeticT y) BOOST_NOEXCEPT
75 { return x = static_cast<ArithmeticT>(+x % y); }
76 friend D& operator&=(D& x, ArithmeticT y) BOOST_NOEXCEPT
77 { return x = static_cast<ArithmeticT>(+x & y); }
78 friend D& operator|=(D& x, ArithmeticT y) BOOST_NOEXCEPT
79 { return x = static_cast<ArithmeticT>(+x | y); }
80 friend D& operator^=(D& x, ArithmeticT y) BOOST_NOEXCEPT
81 { return x = static_cast<ArithmeticT>(+x ^ y); }
82 friend D& operator<<=(D& x, ArithmeticT y) BOOST_NOEXCEPT
83 { return x = static_cast<ArithmeticT>(+x << y); }
84 friend D& operator>>=(D& x, ArithmeticT y) BOOST_NOEXCEPT
85 { return x = static_cast<ArithmeticT>(+x >> y); }
87 // A few binary arithmetic operations not covered by operators base class.
88 friend ArithmeticT operator<<(const D& x, ArithmeticT y) BOOST_NOEXCEPT
89 { return static_cast<ArithmeticT>(+x << y); }
90 friend ArithmeticT operator>>(const D& x, ArithmeticT y) BOOST_NOEXCEPT
91 { return static_cast<ArithmeticT>(+x >> y); }
93 // Auto-increment and auto-decrement can be defined in terms of the
94 // arithmetic operations.
95 friend D& operator++(D& x) BOOST_NOEXCEPT { return x += 1; }
96 friend D& operator--(D& x) BOOST_NOEXCEPT { return x -= 1; }
98 # ifdef BOOST_ENDIAN_MINIMAL_COVER_OPERATORS
99 friend D operator++(D& x, int) BOOST_NOEXCEPT
105 friend D operator--(D& x, int) BOOST_NOEXCEPT
113 # ifndef BOOST_NO_IO_COVER_OPERATORS
116 template <class charT, class traits>
117 friend std::basic_ostream<charT, traits>&
118 operator<<(std::basic_ostream<charT, traits>& os, const D& x)
124 template <class charT, class traits>
125 friend std::basic_istream<charT, traits>&
126 operator>>(std::basic_istream<charT, traits>& is, D& x)
135 } // namespace endian
138 #if defined(_MSC_VER)
139 # pragma warning(pop)
142 #endif // BOOST_ENDIAN_COVER_OPERATORS_HPP