UraniumCompute 0.1.0
A GPU accelerated parallel task scheduler
Flags.h
1#pragma once
2#include <UnCompute/Base/Base.h>
3
4namespace UN
5{
7#define UN_BIT(n) (1 << (n))
8
10 template<class TEnum>
11 inline constexpr std::enable_if_t<std::is_enum_v<TEnum>, std::underlying_type_t<TEnum>> un_enum_cast(TEnum value)
12 {
13 return static_cast<std::underlying_type_t<TEnum>>(value);
14 }
15
19#define UN_ENUM_OPERATORS(Name) \
20 inline constexpr Name operator|(Name a, Name b) \
21 { \
22 return static_cast<Name>(un_enum_cast(a) | un_enum_cast(b)); \
23 } \
24 inline constexpr Name& operator|=(Name& a, Name b) \
25 { \
26 return a = a | b; \
27 } \
28 inline constexpr Name operator&(Name a, Name b) \
29 { \
30 return static_cast<Name>(un_enum_cast(a) & un_enum_cast(b)); \
31 } \
32 inline constexpr Name& operator&=(Name& a, Name b) \
33 { \
34 return a = a & b; \
35 } \
36 inline constexpr Name operator^(Name a, Name b) \
37 { \
38 return static_cast<Name>(un_enum_cast(a) ^ un_enum_cast(b)); \
39 } \
40 inline constexpr Name& operator^=(Name& a, Name b) \
41 { \
42 return a = a ^ b; \
43 } \
44 inline constexpr Name operator~(Name a) \
45 { \
46 return static_cast<Name>(~un_enum_cast(a)); \
47 }
48
50 namespace Internal
51 {
52 template<class T>
53 inline constexpr bool IsAllowedAsFlags = std::is_enum_v<T> | std::is_integral_v<T>;
54 }
55
57 template<class TFlags>
58 inline constexpr std::enable_if_t<Internal::IsAllowedAsFlags<TFlags>, bool> AnyFlagsActive(TFlags source, TFlags test)
59 {
60 return (source & test) != static_cast<TFlags>(0);
61 }
62
64 template<class TFlags>
65 inline constexpr std::enable_if_t<Internal::IsAllowedAsFlags<TFlags>, bool> AllFlagsActive(TFlags source, TFlags test)
66 {
67 return (source & test) == test;
68 }
69
71 template<class TFlags>
72 inline constexpr std::enable_if_t<Internal::IsAllowedAsFlags<TFlags>, TFlags> RemoveFlags(TFlags source, TFlags remove)
73 {
74 return source & ~remove;
75 }
76} // namespace UN