Adobe Source Libraries 1.49.0
A collection of C++ libraries.
Loading...
Searching...
No Matches
enum_ops.hpp
Go to the documentation of this file.
1/*
2 Copyright 2013 Adobe
3 Distributed under the Boost Software License, Version 1.0.
4 (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5*/
6/**************************************************************************************************/
7
8#ifndef ADOBE_ENUM_OPS_HPP
9#define ADOBE_ENUM_OPS_HPP
10
11/**************************************************************************************************/
12
13#include <type_traits>
14
15/**************************************************************************************************/
16
55
56/**************************************************************************************************/
57
58namespace adobe {
59
60/**************************************************************************************************/
61
62auto adobe_enable_bitmask_enum(...) -> std::false_type;
63auto adobe_enable_arithmetic_enum(...) -> std::false_type;
64
65/**************************************************************************************************/
66
67namespace implementation {
68
69/**************************************************************************************************/
70
71#if !defined(ADOBE_NO_DOCUMENTATION)
72template <typename T>
73constexpr bool has_enabled_bitmask = decltype(adobe_enable_bitmask_enum(std::declval<T>()))::value;
74template <typename T>
75constexpr bool has_enabled_arithmetic =
76 decltype(adobe_enable_arithmetic_enum(std::declval<T>()))::value;
77#endif
78
79/**************************************************************************************************/
80
81} // namespace implementation
82
83/**************************************************************************************************/
84
85} // namespace adobe
86
87/**************************************************************************************************/
88
89// this exist to mantain backwards compatability with the old ops
90#define ADOBE_DEFINE_BITSET_OPS(EnumType) \
91 constexpr auto adobe_enable_bitmask_enum(EnumType)->std::true_type;
92
93
94template <typename T>
95constexpr auto operator&(const T lhs, const T rhs)
96 -> std::enable_if_t<adobe::implementation::has_enabled_bitmask<T>, T> {
97 using underlying = std::underlying_type_t<T>;
98 return static_cast<T>(static_cast<underlying>(lhs) & static_cast<underlying>(rhs)); // NOLINT
99}
100
101template <typename T>
102constexpr auto operator~(const T a)
103 -> std::enable_if_t<adobe::implementation::has_enabled_bitmask<T>, T> {
104 using underlying = std::underlying_type_t<T>;
105 return static_cast<T>(~static_cast<underlying>(a)); // NOLINT
106}
107
108template <typename T>
109constexpr auto operator|(const T lhs, const T rhs)
110 -> std::enable_if_t<adobe::implementation::has_enabled_bitmask<T>, T> {
111 using underlying = std::underlying_type_t<T>;
112 return static_cast<T>(static_cast<underlying>(lhs) | static_cast<underlying>(rhs)); // NOLINT
113}
114
115template <typename T>
116constexpr auto operator^(const T lhs, const T rhs)
117 -> std::enable_if_t<adobe::implementation::has_enabled_bitmask<T>, T> {
118 using underlying = std::underlying_type_t<T>;
119 return static_cast<T>(static_cast<underlying>(lhs) ^ static_cast<underlying>(rhs)); // NOLINT
120}
121
122template <typename T>
123constexpr auto operator^=(T& lhs, const T rhs)
124 -> std::enable_if_t<adobe::implementation::has_enabled_bitmask<T>, T> {
125 return lhs = lhs ^ rhs;
126}
127
128template <typename T>
129constexpr auto operator&=(T& lhs, const T rhs)
130 -> std::enable_if_t<adobe::implementation::has_enabled_bitmask<T>, T> {
131 return lhs = lhs & rhs;
132}
133
134template <typename T>
135constexpr auto operator|=(T& lhs, const T rhs)
136 -> std::enable_if_t<adobe::implementation::has_enabled_bitmask<T>, T> {
137 return lhs = lhs | rhs;
138}
139
140// this exist to mantain backwards compatability with the old ops
141#define ADOBE_DEFINE_ARITHMETIC_OPS(EnumType) \
142 constexpr auto adobe_enable_arithmetic_enum(EnumType)->std::true_type;
143template <typename T>
144constexpr auto operator+(const T a)
145 -> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
146 using underlying = std::underlying_type_t<T>;
147 return static_cast<T>(+static_cast<underlying>(a)); // NOLINT
148}
149
150template <typename T>
151constexpr auto operator-(const T a)
152 -> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
153 using underlying = std::underlying_type_t<T>;
154 return static_cast<T>(-static_cast<underlying>(a)); // NOLINT
155}
156
157template <typename T>
158constexpr auto operator+(const T lhs, const T rhs)
159 -> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
160 using underlying = std::underlying_type_t<T>;
161 return static_cast<T>(static_cast<underlying>(lhs) + static_cast<underlying>(rhs)); // NOLINT
162}
163
164template <typename T>
165constexpr auto operator-(const T lhs, const T rhs)
166 -> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
167 using underlying = std::underlying_type_t<T>;
168 return static_cast<T>(static_cast<underlying>(lhs) - static_cast<underlying>(rhs)); // NOLINT
169}
170
171template <typename T>
172constexpr auto operator*(const T lhs, const T rhs)
173 -> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
174 using underlying = std::underlying_type_t<T>;
175 return static_cast<T>(static_cast<underlying>(lhs) * static_cast<underlying>(rhs)); // NOLINT
176}
177
178template <typename T>
179constexpr auto operator/(const T lhs, const T rhs)
180 -> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
181 using underlying = std::underlying_type_t<T>;
182 return static_cast<T>(static_cast<underlying>(lhs) / static_cast<underlying>(rhs)); // NOLINT
183}
184
185template <typename T>
186constexpr auto operator%(const T lhs, const T rhs)
187 -> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
188 using underlying = std::underlying_type_t<T>;
189 return static_cast<T>(static_cast<underlying>(lhs) % static_cast<underlying>(rhs)); // NOLINT
190}
191
192template <typename T>
193constexpr auto operator+=(T& lhs, const T rhs)
194 -> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
195 return lhs = lhs + rhs;
196}
197
198template <typename T>
199constexpr auto operator-=(T& lhs, const T rhs)
200 -> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
201 return lhs = lhs - rhs;
202}
203
204template <typename T>
205constexpr auto operator*=(T& lhs, const T rhs)
206 -> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
207 return lhs = lhs * rhs;
208}
209
210template <typename T>
211constexpr auto operator/=(T& lhs, const T rhs)
212 -> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
213 return lhs = lhs / rhs;
214}
215
216template <typename T>
217constexpr auto operator%=(T& lhs, const T rhs)
218 -> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
219 return lhs = lhs % rhs;
220}
221
222template <typename T>
223constexpr auto operator++(T& lhs)
224 -> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
225 return lhs += static_cast<T>(1); // NOLINT
226}
227
228template <typename T>
229constexpr auto operator++(T& lhs, int)
230 -> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
231 T result = lhs;
232 lhs += static_cast<T>(1); // NOLINT
233 return result;
234}
235
236template <typename T>
237constexpr auto operator--(T& lhs)
238 -> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
239 return lhs -= static_cast<T>(1); // NOLINT
240}
241
242template <typename T>
243constexpr auto operator--(T& lhs, int)
244 -> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
245 T result = lhs;
246 lhs -= static_cast<T>(1); // NOLINT
247 return result;
248}
249
250/**************************************************************************************************/
251
252#endif
253
254/**************************************************************************************************/
constexpr auto operator&(const T lhs, const T rhs) -> std::enable_if_t< adobe::implementation::has_enabled_bitmask< T >, T >
Definition enum_ops.hpp:95
constexpr auto operator+(const T a) -> std::enable_if_t< adobe::implementation::has_enabled_arithmetic< T >, T >
Definition enum_ops.hpp:144
constexpr auto operator%=(T &lhs, const T rhs) -> std::enable_if_t< adobe::implementation::has_enabled_arithmetic< T >, T >
Definition enum_ops.hpp:217
constexpr auto operator/(const T lhs, const T rhs) -> std::enable_if_t< adobe::implementation::has_enabled_arithmetic< T >, T >
Definition enum_ops.hpp:179
constexpr auto operator++(T &lhs) -> std::enable_if_t< adobe::implementation::has_enabled_arithmetic< T >, T >
Definition enum_ops.hpp:223
constexpr auto operator&=(T &lhs, const T rhs) -> std::enable_if_t< adobe::implementation::has_enabled_bitmask< T >, T >
Definition enum_ops.hpp:129
constexpr auto operator/=(T &lhs, const T rhs) -> std::enable_if_t< adobe::implementation::has_enabled_arithmetic< T >, T >
Definition enum_ops.hpp:211
constexpr auto operator+=(T &lhs, const T rhs) -> std::enable_if_t< adobe::implementation::has_enabled_arithmetic< T >, T >
Definition enum_ops.hpp:193
constexpr auto operator*=(T &lhs, const T rhs) -> std::enable_if_t< adobe::implementation::has_enabled_arithmetic< T >, T >
Definition enum_ops.hpp:205
constexpr auto operator^=(T &lhs, const T rhs) -> std::enable_if_t< adobe::implementation::has_enabled_bitmask< T >, T >
Definition enum_ops.hpp:123
constexpr auto operator*(const T lhs, const T rhs) -> std::enable_if_t< adobe::implementation::has_enabled_arithmetic< T >, T >
Definition enum_ops.hpp:172
constexpr auto operator--(T &lhs) -> std::enable_if_t< adobe::implementation::has_enabled_arithmetic< T >, T >
Definition enum_ops.hpp:237
constexpr auto operator-(const T a) -> std::enable_if_t< adobe::implementation::has_enabled_arithmetic< T >, T >
Definition enum_ops.hpp:151
constexpr auto operator^(const T lhs, const T rhs) -> std::enable_if_t< adobe::implementation::has_enabled_bitmask< T >, T >
Definition enum_ops.hpp:116
constexpr auto operator%(const T lhs, const T rhs) -> std::enable_if_t< adobe::implementation::has_enabled_arithmetic< T >, T >
Definition enum_ops.hpp:186
constexpr auto operator~(const T a) -> std::enable_if_t< adobe::implementation::has_enabled_bitmask< T >, T >
Definition enum_ops.hpp:102
constexpr auto operator|(const T lhs, const T rhs) -> std::enable_if_t< adobe::implementation::has_enabled_bitmask< T >, T >
Definition enum_ops.hpp:109
constexpr auto operator|=(T &lhs, const T rhs) -> std::enable_if_t< adobe::implementation::has_enabled_bitmask< T >, T >
Definition enum_ops.hpp:135
constexpr auto operator-=(T &lhs, const T rhs) -> std::enable_if_t< adobe::implementation::has_enabled_arithmetic< T >, T >
Definition enum_ops.hpp:199
auto adobe_enable_bitmask_enum(...) -> std::false_type
auto adobe_enable_arithmetic_enum(...) -> std::false_type