Loading...
Searching...
No Matches
MultiCustomFlaggable.h
Go to the documentation of this file.
1// Copyright (c) 2025 Dave R. Smith.
2// Malena Framework — Licensed under PolyForm Noncommercial 1.0.0; commercial use requires a paid license. See LICENSE.
3
4#ifndef MALENA_MULTICUSTOMFLAGGABLE_H
5#define MALENA_MULTICUSTOMFLAGGABLE_H
6
8#include <unordered_map>
9#include <tuple>
10#include <type_traits>
13
14namespace ml
15{
16 // =========================================================================
17 // SingleFlaggable
18 // =========================================================================
19
36 template<typename Enum>
38 {
39 std::unordered_map<Enum, bool, EnumClassHash> _states;
40
41 public:
43 void enableFlag(Enum flag) { _states[flag] = true; }
44
46 void disableFlag(Enum flag) { _states[flag] = false; }
47
49 bool checkFlag(Enum flag) const { auto it = _states.find(flag); return it != _states.end() && it->second; }
50
52 void setFlag(Enum flag, bool value) { _states[flag] = value; }
53
55 void toggleFlag(Enum flag) { _states[flag] = !checkFlag(flag); }
56 };
57
58 // =========================================================================
59 // MultiCustomFlaggable
60 // =========================================================================
61
95 template<typename... Enums>
96 class MultiCustomFlaggable : public SingleFlaggable<Enums>...
97 {
98 public:
99 using SingleFlaggable<Enums>::enableFlag...;
100 using SingleFlaggable<Enums>::disableFlag...;
101 using SingleFlaggable<Enums>::checkFlag...;
102 using SingleFlaggable<Enums>::setFlag...;
103 using SingleFlaggable<Enums>::toggleFlag...;
104 };
105
114 template<>
116 {
117 public:
118 void enableFlag(...) {}
119 void disableFlag(...) {}
120 bool checkFlag(...) const { return false; }
121 void setFlag(...) {}
122 void toggleFlag(...) {}
123 };
124
125 // =========================================================================
126 // Trait manifest detection helpers
127 // =========================================================================
128
130
136 template<typename M, typename = void>
137 struct extract_ManifestFlags { using type = void; };
138
139 template<typename M>
140 struct extract_ManifestFlags<M, std::void_t<typename M::Flag>>
141 { using type = typename M::Flag; };
142
143 // ── Void filtering ────────────────────────────────────────────────────────
144
145 template<typename T, typename Acc>
146 struct AppendIfNotVoid { using type = Acc; };
147
148 template<typename T, typename... Acc>
149 struct AppendIfNotVoid<T, std::tuple<Acc...>>
150 {
151 using type = std::conditional_t<
152 std::is_void_v<T>,
153 std::tuple<Acc...>,
154 std::tuple<Acc..., T>
155 >;
156 };
157
158 template<typename... Ts>
159 struct FilterVoid;
160
161 template<>
162 struct FilterVoid<> { using type = std::tuple<>; };
163
164 template<typename T, typename... Rest>
165 struct FilterVoid<T, Rest...>
166 {
167 using type = typename AppendIfNotVoid<
168 T,
169 typename FilterVoid<Rest...>::type
170 >::type;
171 };
172
173 template<typename Tuple>
174 struct TupleToMultiFlaggable;
175
176 template<typename... Enums>
177 struct TupleToMultiFlaggable<std::tuple<Enums...>>
178 { using type = MultiCustomFlaggable<Enums...>; };
179
181
182 // =========================================================================
183 // GatherFlags
184 // =========================================================================
185
204 template<typename ComponentManifest, typename... Traits>
206 {
207 using AllFlagsTuple = typename FilterVoid<
208 typename extract_ManifestFlags<ComponentManifest>::type,
209 typename extract_ManifestFlags<
210 typename extract_TraitManifest<Traits>::type
211 >::type...
212 >::type;
213
214 using type = typename TupleToMultiFlaggable<AllFlagsTuple>::type;
215 };
216
217} // namespace ml
218
219#endif // MALENA_MULTICUSTOMFLAGGABLE_H
Aggregates flag stores for multiple enum types into one class.
Flag store for a single enum type.
void setFlag(Enum flag, bool value)
Set flag to an explicit value.
void toggleFlag(Enum flag)
Flip flag between true and false.
bool checkFlag(Enum flag) const
Return true if flag is set, false if unset or never written.
void disableFlag(Enum flag)
Set flag to false.
void enableFlag(Enum flag)
Set flag to true.
Definition Component.h:22
Collects all Flag enums from a component manifest and its traits.
typename TupleToMultiFlaggable< AllFlagsTuple >::type type
typename FilterVoid< typename extract_ManifestFlags< ComponentManifest >::type, typename extract_ManifestFlags< typename extract_TraitManifest< Traits >::type >::type... >::type AllFlagsTuple