]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // boost/integer/cover_operators.hpp ----------------------------------------// |
2 | ||
3 | // Copyright Darin Adler 2000 | |
4 | // Copyright Beman Dawes 2008 | |
5 | ||
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) | |
8 | ||
9 | //----------------------------------------------------------------------------// | |
10 | ||
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. | |
14 | ||
15 | // Define BOOST_NO_IO_COVER_OPERATORS if I/O cover operations are not desired. | |
16 | ||
17 | //----------------------------------------------------------------------------// | |
18 | ||
19 | #ifndef BOOST_SPIRIT_INTEGER_COVER_OPERATORS_HPP | |
20 | #define BOOST_SPIRIT_INTEGER_COVER_OPERATORS_HPP | |
21 | ||
22 | #if defined(_MSC_VER) | |
23 | #pragma once | |
24 | #endif | |
25 | ||
26 | # ifndef BOOST_MINIMAL_INTEGER_COVER_OPERATORS | |
27 | # include <boost/operators.hpp> | |
28 | # endif | |
29 | ||
30 | #include <iosfwd> | |
31 | ||
32 | namespace boost { namespace spirit | |
33 | { | |
34 | namespace endian | |
35 | { | |
36 | ||
37 | // A class that adds integer operators to an integer cover class | |
38 | ||
39 | template <typename T, typename IntegerType> | |
40 | class cover_operators | |
41 | # ifndef BOOST_MINIMAL_INTEGER_COVER_OPERATORS | |
42 | : boost::operators<T> | |
43 | # endif | |
44 | { | |
45 | // The other operations take advantage of the type conversion that's | |
46 | // built into unary +. | |
47 | ||
48 | // Unary operations. | |
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; } | |
54 | ||
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; } | |
58 | # endif | |
59 | ||
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; } | |
71 | ||
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; } | |
75 | ||
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; } | |
80 | ||
81 | # ifdef BOOST_MINIMAL_INTEGER_COVER_OPERATORS | |
82 | friend T operator++(T& x, int) | |
83 | { | |
84 | T tmp(x); | |
85 | x += 1; | |
86 | return tmp; | |
87 | } | |
88 | friend T operator--(T& x, int) | |
89 | { | |
90 | T tmp(x); | |
91 | x -= 1; | |
92 | return tmp; | |
93 | } | |
94 | # endif | |
95 | ||
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. | |
99 | ||
100 | // Stream input and output. | |
101 | friend std::ostream& operator<<(std::ostream& s, const T& x) | |
102 | { return s << +x; } | |
103 | friend std::istream& operator>>(std::istream& s, T& x) | |
104 | { | |
105 | IntegerType i; | |
106 | if (s >> i) | |
107 | x = i; | |
108 | return s; | |
109 | } | |
110 | # endif | |
111 | }; | |
112 | } // namespace endian | |
113 | }} // namespace boost::spirit | |
114 | ||
115 | #endif // BOOST_SPIRIT_INTEGER_COVER_OPERATORS_HPP |