OmniSciDB  d2f719934e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EnumBitmaskOps.h
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <type_traits>
20 
41 template <typename T>
43  static constexpr bool enable = false;
44 };
45 
46 template <typename T>
47 typename std::enable_if_t<EnableBitmaskOps<T>::enable, T> constexpr operator&(T lhs,
48  T rhs) {
49  using type = typename std::underlying_type_t<T>;
50  return static_cast<T>(static_cast<type>(lhs) & static_cast<type>(rhs));
51 }
52 template <typename T>
53 typename std::enable_if_t<EnableBitmaskOps<T>::enable, T> constexpr operator|(T lhs,
54  T rhs) {
55  using type = typename std::underlying_type_t<T>;
56  return static_cast<T>(static_cast<type>(lhs) | static_cast<type>(rhs));
57 }
58 template <typename T>
59 typename std::enable_if_t<EnableBitmaskOps<T>::enable, T> constexpr operator~(T t) {
60  return static_cast<T>(~static_cast<std::underlying_type_t<T>>(t));
61 }
62 template <typename T>
63 typename std::enable_if_t<EnableBitmaskOps<T>::enable, T> constexpr operator|=(T& lhs,
64  T rhs) {
65  lhs = lhs | rhs;
66  return lhs;
67 }
68 template <typename T>
69 typename std::enable_if_t<EnableBitmaskOps<T>::enable, T> constexpr operator&=(T& lhs,
70  T rhs) {
71  lhs = lhs & rhs;
72  return lhs;
73 }
74 
75 template <typename T>
76 typename std::enable_if_t<EnableBitmaskOps<T>::enable, bool> constexpr any_bits_set(T t) {
77  using type = typename std::underlying_type_t<T>;
78  constexpr type zero{};
79  return static_cast<type>(t) != zero;
80 }
81 
82 #define ENABLE_BITMASK_OPS(x) \
83  template <> \
84  struct EnableBitmaskOps<x> { \
85  static constexpr bool enable = true; \
86  };
std::enable_if_t< EnableBitmaskOps< T >::enable, T > constexpr operator|(T lhs, T rhs)
std::enable_if_t< EnableBitmaskOps< T >::enable, T > constexpr operator|=(T &lhs, T rhs)
std::enable_if_t< EnableBitmaskOps< T >::enable, bool > constexpr any_bits_set(T t)
std::enable_if_t< EnableBitmaskOps< T >::enable, T > constexpr operator&=(T &lhs, T rhs)
std::enable_if_t< EnableBitmaskOps< T >::enable, T > constexpr operator&(T lhs, T rhs)
char * t
std::enable_if_t< EnableBitmaskOps< T >::enable, T > constexpr operator~(T t)
static constexpr bool enable