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
12namespace skstd {
13template <typename T> struct is_bitmask_enum : std::false_type {};
14
15template <typename E>
16typename 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
21template <typename E>
22typename 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
27template <typename E>
28typename std::enable_if<skstd::is_bitmask_enum<E>::value, E&>::type constexpr operator|=(E& l, E r) {
29 return l = l | r;
30}
31
32template <typename E>
33typename 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
38template <typename E>
39typename std::enable_if<skstd::is_bitmask_enum<E>::value, E&>::type constexpr operator&=(E& l, E r) {
40 return l = l & r;
41}
42
43template <typename E>
44typename 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