| 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | #ifndef SkEnumOperators_DEFINED |
| 8 | #define SkEnumOperators_DEFINED |
| 9 | |
| 10 | #include <type_traits> |
| 11 | |
| 12 | namespace skstd { |
| 13 | template <typename T> struct is_bitmask_enum : std::false_type {}; |
| 14 | |
| 15 | template <typename E> |
| 16 | typename std::enable_if<skstd::is_bitmask_enum<E>::value, bool>::type constexpr Any(E e) { |
| 17 | return static_cast<typename std::underlying_type<E>::type>(e) != 0; |
| 18 | } |
| 19 | } // namespace skstd |
| 20 | |
| 21 | template <typename E> |
| 22 | typename std::enable_if<skstd::is_bitmask_enum<E>::value, E>::type constexpr operator|(E l, E r) { |
| 23 | using U = typename std::underlying_type<E>::type; |
| 24 | return static_cast<E>(static_cast<U>(l) | static_cast<U>(r)); |
| 25 | } |
| 26 | |
| 27 | template <typename E> |
| 28 | typename std::enable_if<skstd::is_bitmask_enum<E>::value, E&>::type constexpr operator|=(E& l, E r) { |
| 29 | return l = l | r; |
| 30 | } |
| 31 | |
| 32 | template <typename E> |
| 33 | typename std::enable_if<skstd::is_bitmask_enum<E>::value, E>::type constexpr operator&(E l, E r) { |
| 34 | using U = typename std::underlying_type<E>::type; |
| 35 | return static_cast<E>(static_cast<U>(l) & static_cast<U>(r)); |
| 36 | } |
| 37 | |
| 38 | template <typename E> |
| 39 | typename std::enable_if<skstd::is_bitmask_enum<E>::value, E&>::type constexpr operator&=(E& l, E r) { |
| 40 | return l = l & r; |
| 41 | } |
| 42 | |
| 43 | template <typename E> |
| 44 | typename std::enable_if<skstd::is_bitmask_enum<E>::value, E>::type constexpr operator~(E e) { |
| 45 | return static_cast<E>(~static_cast<typename std::underlying_type<E>::type>(e)); |
| 46 | } |
| 47 | |
| 48 | #endif // SkEnumOperators_DEFINED |
| 49 | |