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