HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_EnumHelper.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_EnumHelper.h
7  *
8  * COMMENTS:
9  * Simple system to enable bitflag operators on a strong enum.
10  */
11 
12 #ifndef __UT_ENUMHELPER_H__
13 #define __UT_ENUMHELPER_H__
14 
15 #include <type_traits>
16 
17 #define UT_STATIC_CHECK_ENUM(name) \
18  static_assert(std::is_enum<name>::value, "Enum type must be used.");
19 
20 template <typename T>
22 {
23  static constexpr bool enable = false;
24 };
25 
26 template <typename T>
27 typename std::enable_if<UT_EnableBitMask<T>::enable, T>::type
28 operator|(T lhs, T rhs)
29 {
31  using U = typename std::underlying_type<T>::type;
32  return static_cast<T>(static_cast<U>(lhs) | static_cast<U>(rhs));
33 }
34 
35 template <typename T>
36 typename std::enable_if<UT_EnableBitMask<T>::enable, T&>::type
37 operator|=(T& lhs, T rhs)
38 {
40  using U = typename std::underlying_type<T>::type;
41  lhs = static_cast<T>(static_cast<U>(lhs) | static_cast<U>(rhs));
42  return lhs;
43 }
44 
45 template <typename T>
46 typename std::enable_if<UT_EnableBitMask<T>::enable, T>::type operator&(
47  T lhs,
48  T rhs)
49 {
51  using U = typename std::underlying_type<T>::type;
52  return static_cast<T>(static_cast<U>(lhs) & static_cast<U>(rhs));
53 }
54 
55 template <typename T>
56 typename std::enable_if<UT_EnableBitMask<T>::enable, T&>::type
57 operator&=(T& lhs, T rhs)
58 {
60  using U = typename std::underlying_type<T>::type;
61  lhs = static_cast<T>(static_cast<U>(lhs) & static_cast<U>(rhs));
62  return lhs;
63 }
64 
65 template <typename T>
66 typename std::enable_if<UT_EnableBitMask<T>::enable, T>::type
67 operator^(T lhs, T rhs)
68 {
70  using U = typename std::underlying_type<T>::type;
71  return static_cast<T>(static_cast<U>(lhs) ^ static_cast<U>(rhs));
72 }
73 
74 template <typename T>
75 typename std::enable_if<UT_EnableBitMask<T>::enable, T&>::type
76 operator^=(T& lhs, T rhs)
77 {
79  using U = typename std::underlying_type<T>::type;
80  lhs = static_cast<T>(static_cast<U>(lhs) ^ static_cast<U>(rhs));
81  return lhs;
82 }
83 
84 template <typename T>
85 typename std::enable_if<UT_EnableBitMask<T>::enable, T>::type
86 operator~(T lhs)
87 {
89  using U = typename std::underlying_type<T>::type;
90  return static_cast<T>(~static_cast<U>(lhs));
91 }
92 
93 // Enable all bit flag operators on strong enum type
94 #define UT_ENABLE_ENUM_BIT_FLAGS(name) \
95  template <> \
96  struct UT_EnableBitMask<name> \
97  { \
98  static constexpr bool enable = true; \
99  };
100 
101 #endif // __UT_ENUMHELPER_H__
102 
static constexpr bool enable
Definition: UT_EnumHelper.h:23
std::enable_if< UT_EnableBitMask< T >::enable, T & >::type operator&=(T &lhs, T rhs)
Definition: UT_EnumHelper.h:57
std::enable_if< UT_EnableBitMask< T >::enable, T >::type operator&(T lhs, T rhs)
Definition: UT_EnumHelper.h:46
std::enable_if< UT_EnableBitMask< T >::enable, T >::type operator^(T lhs, T rhs)
Definition: UT_EnumHelper.h:67
std::enable_if< UT_EnableBitMask< T >::enable, T & >::type operator^=(T &lhs, T rhs)
Definition: UT_EnumHelper.h:76
std::enable_if< UT_EnableBitMask< T >::enable, T >::type operator~(T lhs)
Definition: UT_EnumHelper.h:86
#define UT_STATIC_CHECK_ENUM(name)
Definition: UT_EnumHelper.h:17
type
Definition: core.h:1059
std::enable_if< UT_EnableBitMask< T >::enable, T >::type operator|(T lhs, T rhs)
Definition: UT_EnumHelper.h:28
std::enable_if< UT_EnableBitMask< T >::enable, T & >::type operator|=(T &lhs, T rhs)
Definition: UT_EnumHelper.h:37