FastEngine 0.9.4
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_flag.hpp
1/*
2 * Copyright 2025 Guillaume Guillet
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#ifndef _FGE_C_FLAG_HPP_INCLUDED
18#define _FGE_C_FLAG_HPP_INCLUDED
19
20namespace fge
21{
22
31class BooleanFlag
32{
33public:
34 constexpr BooleanFlag(bool defaultValue = false) :
35 g_flag(defaultValue)
36 {}
37
46 constexpr bool check(bool input)
47 {
48 if (!this->g_flag)
49 {
50 this->g_flag = input;
51 return input;
52 }
53 if (!input)
54 {
55 this->g_flag = false;
56 }
57 return false;
58 }
59
65 constexpr void set(bool value) { this->g_flag = value; }
71 [[nodiscard]] constexpr bool get() const { return this->g_flag; }
72
79 constexpr bool operator=(bool value) { return this->g_flag = value; }
80
86 constexpr operator bool() const { return this->g_flag; }
87
88private:
89 bool g_flag;
90};
91
102template<typename EnumType>
103class EnumFlags
104{
105 static_assert(std::is_enum_v<EnumType>, "EnumFlags can only be used with enum types");
106
107public:
108 using Type = std::underlying_type_t<EnumType>;
109
110 constexpr EnumFlags(Type value = Type{0}) :
111 g_flags(static_cast<Type>(value))
112 {}
113
114 EnumFlags& set(Type flag)
115 {
116 this->g_flags |= static_cast<Type>(flag);
117 return *this;
118 }
119 EnumFlags& unset(Type flag)
120 {
121 this->g_flags &= ~static_cast<Type>(flag);
122 return *this;
123 }
124 EnumFlags& toggle(Type flag)
125 {
126 this->g_flags ^= static_cast<Type>(flag);
127 return *this;
128 }
129
130 [[nodiscard]] constexpr bool has(Type flag) const { return (this->g_flags & static_cast<Type>(flag)) == flag; }
131
132 [[nodiscard]] constexpr Type get() const { return this->g_flags; }
133 constexpr EnumFlags& operator=(Type value)
134 {
135 this->g_flags = value;
136 return *this;
137 }
138
139private:
140 Type g_flags;
141};
142
143template<typename EnumType>
144using EnumFlags_t = fge::EnumFlags<EnumType>::Type;
145
146} // namespace fge
147
148#endif // _FGE_C_FLAG_HPP_INCLUDED
constexpr bool operator=(bool value)
Manually set the flag value operator.
Definition C_flag.hpp:79
constexpr bool check(bool input)
Check the input and return the flag value.
Definition C_flag.hpp:46
constexpr void set(bool value)
Manually set the flag value.
Definition C_flag.hpp:65
constexpr bool get() const
Get the flag value.
Definition C_flag.hpp:71