Hermes
bitmask_operators.h
Go to the documentation of this file.
1 
32 #ifndef HERMES_COMMON_BITMASK_OPERATORS_H
33 #define HERMES_COMMON_BITMASK_OPERATORS_H
34 
35 #include <type_traits>
36 
37 namespace hermes {
38 
58 #define HERMES_ENABLE_BITMASK_OPERATORS(x) \
59 template<> \
60 struct EnableBitMaskOperators<x> \
61 { \
62  static const bool enable = true; \
63 }
64 
84 #define HERMES_MASK_BIT(MASK, BIT) (((MASK) & (BIT)) == (BIT))
85 
88 template<typename Enum>
90  static const bool enable = false;
91 };
92 
98 template<typename Enum>
100 typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type
101 operator|(Enum lhs, Enum rhs) noexcept {
103  using underlying = typename std::underlying_type<Enum>::type;
104  return static_cast<Enum> (
105  static_cast<underlying>(lhs) |
106  static_cast<underlying>(rhs)
107  );
108 }
109 
115 template<typename Enum>
117 typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type
118 operator&(Enum lhs, Enum rhs) {
120  using underlying = typename std::underlying_type<Enum>::type;
121  return static_cast<Enum> (
122  static_cast<underlying>(lhs) &
123  static_cast<underlying>(rhs)
124  );
125 }
126 
132 template<typename Enum>
134 typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type
135 operator^(Enum lhs, Enum rhs) {
136  using underlying = typename std::underlying_type<Enum>::type;
137  return static_cast<Enum> (
138  static_cast<underlying>(lhs) ^
139  static_cast<underlying>(rhs)
140  );
141 }
142 
147 template<typename Enum>
149 typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type
150 operator~(Enum rhs) {
152  using underlying = typename std::underlying_type<Enum>::type;
153  return static_cast<Enum> (
154  ~static_cast<underlying>(rhs)
155  );
156 }
157 
158 }
159 
160 #endif //HERMES_COMMON_BITMASK_OPERATORS_H
161 
HERMES_DEVICE_CALLABLE std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator^(Enum lhs, Enum rhs)
adds ^ operation support
Definition: bitmask_operators.h:135
HERMES_DEVICE_CALLABLE std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator&(Enum lhs, Enum rhs)
Definition: bitmask_operators.h:118
HERMES_DEVICE_CALLABLE std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator|(Enum lhs, Enum rhs) noexcept
Definition: bitmask_operators.h:101
HERMES_DEVICE_CALLABLE std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator~(Enum rhs)
adds ~ operation support
Definition: bitmask_operators.h:150
#define HERMES_DEVICE_CALLABLE
Specifies that the function can be called from both host and device sides.
Definition: defs.h:45
Wrapper struct to add bitwise operations to enum class.
Definition: bitmask_operators.h:89
static const bool enable
enable flag
Definition: bitmask_operators.h:90