| 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 sknonstd { |
| 13 | template <typename T> struct is_bitmask_enum : std::false_type {}; |
| 14 | |
| 15 | template <typename E> |
| 16 | std::enable_if_t<sknonstd::is_bitmask_enum<E>::value, bool> constexpr Any(E e) { |
| 17 | return static_cast<std::underlying_type_t<E>>(e) != 0; |
| 18 | } |
| 19 | } // namespace sknonstd |
| 20 | |
| 21 | template <typename E> |
| 22 | std::enable_if_t<sknonstd::is_bitmask_enum<E>::value, E> constexpr operator|(E l, E r) { |
| 23 | using U = std::underlying_type_t<E>; |
| 24 | return static_cast<E>(static_cast<U>(l) | static_cast<U>(r)); |
| 25 | } |
| 26 | |
| 27 | template <typename E> |
| 28 | std::enable_if_t<sknonstd::is_bitmask_enum<E>::value, E&> constexpr operator|=(E& l, E r) { |
| 29 | return l = l | r; |
| 30 | } |
| 31 | |
| 32 | template <typename E> |
| 33 | std::enable_if_t<sknonstd::is_bitmask_enum<E>::value, E> constexpr operator&(E l, E r) { |
| 34 | using U = std::underlying_type_t<E>; |
| 35 | return static_cast<E>(static_cast<U>(l) & static_cast<U>(r)); |
| 36 | } |
| 37 | |
| 38 | template <typename E> |
| 39 | std::enable_if_t<sknonstd::is_bitmask_enum<E>::value, E&> constexpr operator&=(E& l, E r) { |
| 40 | return l = l & r; |
| 41 | } |
| 42 | |
| 43 | template <typename E> |
| 44 | std::enable_if_t<sknonstd::is_bitmask_enum<E>::value, E> constexpr operator^(E l, E r) { |
| 45 | using U = std::underlying_type_t<E>; |
| 46 | return static_cast<E>(static_cast<U>(l) ^ static_cast<U>(r)); |
| 47 | } |
| 48 | |
| 49 | template <typename E> |
| 50 | std::enable_if_t<sknonstd::is_bitmask_enum<E>::value, E&> constexpr operator^=(E& l, E r) { |
| 51 | return l = l ^ r; |
| 52 | } |
| 53 | |
| 54 | template <typename E> |
| 55 | std::enable_if_t<sknonstd::is_bitmask_enum<E>::value, E> constexpr operator~(E e) { |
| 56 | return static_cast<E>(~static_cast<std::underlying_type_t<E>>(e)); |
| 57 | } |
| 58 | |
| 59 | #endif // SkEnumOperators_DEFINED |
| 60 | |