Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TBitFlags.h
Go to the documentation of this file.
1#pragma once
2
3#include "Utility/traits.h"
4
5#include <type_traits>
6#include <initializer_list>
7
8namespace ph
9{
10
15template<typename Value, typename Input = Value>
16class TBitFlags final
17{
18 static_assert(std::is_integral_v<Value>);
19 static_assert(std::is_integral_v<Input> || std::is_enum_v<Input>);
20
21 static_assert(sizeof(Value) >= sizeof(Input),
22 "Input type may overflow Value type.");
23
24private:
25 using FlagsSet = std::initializer_list<Input>;
26
27public:
30 constexpr TBitFlags();
31
34 explicit constexpr TBitFlags(const FlagsSet& flagsSet);
35
38 explicit constexpr TBitFlags(Input flagsSet);
39
42 template<typename OtherInput>
43 explicit constexpr TBitFlags(const TBitFlags<Value, OtherInput>& otherFlags);
44
48 constexpr TBitFlags& unionWith(const FlagsSet& flagsSet);
49 constexpr TBitFlags& unionWith(const TBitFlags& flags);
51
54 constexpr TBitFlags& intersectWith(const FlagsSet& flagsSet);
55
59 constexpr TBitFlags& turnOn(const FlagsSet& flagsSet);
60 constexpr TBitFlags& turnOff(const FlagsSet& flagsSet);
62
65 constexpr bool hasNone(const FlagsSet& flagsSet) const;
66
69 constexpr bool hasAny(const FlagsSet& flagsSet) const;
70
73 constexpr bool hasAll(const FlagsSet& flagsSet) const;
74
77 constexpr bool hasExactly(const FlagsSet& flagsSet) const;
78
81 constexpr bool hasNone(Input flagsSet) const;
82
85 constexpr bool hasAny(Input flagsSet) const;
86
89 constexpr bool hasAll(Input flagsSet) const;
90
93 constexpr bool hasExactly(Input flagsSet) const;
94
99 constexpr bool has(Input singleFlag) const;
100
105 constexpr bool hasNo(Input singleFlag) const;
106
109 constexpr bool isEmpty() const;
110
111 constexpr bool isEqual(const TBitFlags& other) const;
112
115 constexpr Value get() const;
116
117 // TODO: method for clear all flags
118
119 constexpr TBitFlags& set(const FlagsSet& flagsSet);
120 constexpr TBitFlags& set(Input flagsSet);
121
125 constexpr Input getEnum() const
126 requires CEnum<Input> && (sizeof(Input) >= sizeof(Value))
127 {
128 return static_cast<Input>(m_bits);
129 }
130
131private:
132 Value m_bits;
133
134 static constexpr Value collectFlags(const FlagsSet& flagsSet);
135};
136
139template<CEnum EnumType>
141
142}// end namespace ph
143
146#define PH_DEFINE_INLINE_ENUM_FLAG_OPERATORS(EnumType)\
147 static_assert(::ph::CEnum<EnumType>, #EnumType " must be an enum type");\
148 \
149 inline constexpr EnumType operator | (const EnumType lhs, const EnumType rhs)\
150 {\
151 return ::ph::TEnumFlags<EnumType>({lhs, rhs}).getEnum();\
152 }\
153 \
154 inline constexpr EnumType operator & (const EnumType lhs, const EnumType rhs)\
155 {\
156 return ::ph::TEnumFlags<EnumType>({lhs}).intersectWith({rhs}).getEnum();\
157 }
158
159#include "Utility/TBitFlags.ipp"
Manipulate a value type where each bit is a binary flag.
Definition TBitFlags.h:17
constexpr bool hasExactly(const FlagsSet &flagsSet) const
Checks whether this instance contains exactly the specified flags. No more, no less.
Definition TBitFlags.ipp:99
constexpr bool has(Input singleFlag) const
Checks whether this single flag is fully contained.
Definition TBitFlags.ipp:133
constexpr bool isEmpty() const
Checks whether this instance contains no flags.
Definition TBitFlags.ipp:145
constexpr TBitFlags & set(const FlagsSet &flagsSet)
Definition TBitFlags.ipp:54
constexpr TBitFlags & intersectWith(const FlagsSet &flagsSet)
Intersects this instance with the specified flags.
Definition TBitFlags.ipp:46
constexpr TBitFlags & turnOff(const FlagsSet &flagsSet)
Definition TBitFlags.ipp:73
constexpr bool hasAny(const FlagsSet &flagsSet) const
Checks whether this instance contains at least one of the specified flags.
Definition TBitFlags.ipp:87
constexpr bool hasNo(Input singleFlag) const
Checks whether this single flag is fully absent.
Definition TBitFlags.ipp:139
constexpr bool hasNone(const FlagsSet &flagsSet) const
Checks whether this instance contains no specified flags.
Definition TBitFlags.ipp:81
constexpr TBitFlags()
Creates an instance with no flags.
Definition TBitFlags.ipp:9
constexpr Value get() const
Get the value representing current flags.
Definition TBitFlags.ipp:157
constexpr bool hasAll(const FlagsSet &flagsSet) const
Checks whether this instance contains all of the specified flags.
Definition TBitFlags.ipp:93
constexpr TBitFlags & turnOn(const FlagsSet &flagsSet)
Enable/disable specified flags.
Definition TBitFlags.ipp:67
constexpr TBitFlags & unionWith(const FlagsSet &flagsSet)
Unions specified flags into this instance.
Definition TBitFlags.ipp:30
constexpr Input getEnum() const
Get the enum representing current flags. This method is only defined for enum flags.
Definition TBitFlags.h:125
constexpr bool isEqual(const TBitFlags &other) const
Definition TBitFlags.ipp:151
Whether the type is a scoped/unscoped enum.
Definition traits.h:98
class ph::math::TBvhSimdComputingContext sizeof(float32) *BATCH_SIZE >
Definition TBvhSimdComputingContext.h:341
The root for all renderer implementations.
Definition EEngineProject.h:6