di 0.1.0
Loading...
Searching...
No Matches
bitwise_enum.h
Go to the documentation of this file.
1#pragma once
2
3#include "di/meta/core.h"
4#include "di/meta/language.h"
6
7namespace di::concepts {
8template<typename T>
9concept BitwiseEnum = concepts::Enum<T> && requires(T& lvalue, T a) {
10 { ~a } -> di::SameAs<T>;
11 { a | a } -> di::SameAs<T>;
12 { a & a } -> di::SameAs<T>;
13 { a ^ a } -> di::SameAs<T>;
14 { lvalue |= a } -> di::SameAs<T&>;
15 { lvalue &= a } -> di::SameAs<T&>;
16 { lvalue ^= a } -> di::SameAs<T&>;
17};
18}
19
20#define DI_DEFINE_ENUM_BITWISE_OPERATIONS(Type) \
21 static_assert(::di::concepts::Enum<Type>); \
22 constexpr auto operator~(Type a)->Type { \
23 return static_cast<Type>(~::di::util::to_underlying(a)); \
24 } \
25 constexpr auto operator|(Type a, Type b)->Type { \
26 return static_cast<Type>(::di::util::to_underlying(a) | ::di::util::to_underlying(b)); \
27 } \
28 constexpr auto operator&(Type a, Type b)->Type { \
29 return static_cast<Type>(::di::util::to_underlying(a) & ::di::util::to_underlying(b)); \
30 } \
31 constexpr auto operator^(Type a, Type b)->Type { \
32 return static_cast<Type>(::di::util::to_underlying(a) ^ ::di::util::to_underlying(b)); \
33 } \
34 constexpr auto operator|=(Type& a, Type b)->Type& { \
35 return a = a | b; \
36 } \
37 constexpr auto operator&=(Type& a, Type b)->Type& { \
38 return a = a & b; \
39 } \
40 constexpr auto operator^=(Type& a, Type b)->Type& { \
41 return a = a ^ b; \
42 } \
43 constexpr auto operator!(Type a)->bool { \
44 return !::di::util::to_underlying(a); \
45 }
Definition bitwise_enum.h:9
Definition language.h:259
Definition any_storable.h:9