Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
tools.hpp
Go to the documentation of this file.
1
5#ifndef USER_MODE_TOOLS_H
6#define USER_MODE_TOOLS_H
7
8#include <cstdint>
9#include <cstring>
10#include <utility>
11#include <optional>
12#include <tuple>
13
14#include <etl/bitset.h>
15
18
19#ifndef LMBD_CPP17
20#error "File requires -DLMBD_CPP17 to explicitly enables C++17 features"
21#endif
22
23namespace lampda::modes {
24
26template<typename Mode> static constexpr bool is_mode = std::is_base_of_v<BasicMode, Mode>;
27
28//
29// StateTyOf
30// - returns StateTy if defined
31// - or else return EmptyState
32//
33
35struct NoState
36{
37};
38
40template<typename Mode,
41 typename StateTy = typename Mode::StateTy,
42 bool isDefault = std::is_same_v<StateTy, BasicMode::StateTy> || (sizeof(StateTy) == 0),
43 typename RetTy = std::conditional_t<isDefault, NoState, StateTy>>
44static constexpr auto stateTyOfImpl(int) -> RetTy;
45
47template<typename> static constexpr auto stateTyOfImpl(...) -> NoState;
48
50template<typename Mode> using StateTyOf = decltype(stateTyOfImpl<Mode>(0));
51
52//
53// Store
54//
55
57struct NoStoreId
58{
59};
60
62enum class NoStoreHere : uint16_t
63{
64 noKeyDefined // placeholder (should not be used)
65};
66
68template<typename Mode,
69 typename EnumTy = typename Mode::Store,
70 bool isDefault = (Mode::storeId == BasicMode::storeId),
71 typename RetTy = std::conditional_t<isDefault, NoStoreHere, EnumTy>>
72static constexpr auto storeEnumOfImpl(int) -> RetTy;
73
75template<typename> static constexpr auto storeEnumOfImpl(...) -> NoStoreHere;
76
78template<typename Mode> using StoreEnumOf = decltype(storeEnumOfImpl<Mode>(0));
79
81namespace details {
82
83//
84// unroll
85//
86
88template<class CbTy, uint8_t... Indexes>
89static constexpr void LMBD_INLINE unroll_impl(CbTy&& cb, std::integer_sequence<uint8_t, Indexes...>)
90{
91 (cb(std::integral_constant<uint8_t, Indexes> {}), ...);
92}
93
95template<uint8_t N, class CbTy> static constexpr void LMBD_INLINE unroll(CbTy&& cb)
96{
97 constexpr auto Indexes = std::make_integer_sequence<uint8_t, N>();
98 unroll_impl(
99 [&](auto I) LMBD_INLINE {
100 return cb(std::integral_constant<uint8_t, decltype(I)::value> {});
101 },
102 Indexes);
103}
104
105//
106// forEach & anyOf & allOf
107//
108
110template<typename TupleTy, uint8_t TupleSz = std::tuple_size_v<TupleTy>> struct forEach
111{
113 template<template<class> class QueryStruct, bool hasError = false> static constexpr bool any()
114 {
115 bool acc = false;
116 if constexpr (!hasError)
117 {
118 unroll<TupleSz>([&](auto Idx) {
119 acc |= QueryStruct<std::tuple_element_t<Idx, TupleTy>>::value;
120 });
121 }
122 return acc;
123 }
124
126 template<template<class> class QueryStruct, bool hasError = false> static constexpr bool all()
127 {
128 bool acc = true;
129 if constexpr (!hasError)
130 {
131 unroll<TupleSz>([&](auto Idx) {
132 acc &= QueryStruct<std::tuple_element_t<Idx, TupleTy>>::value;
133 });
134 }
135 else
136 {
137 return false;
138 }
139 return acc;
140 }
141
143 template<template<class> class QueryStruct, bool hasError = false> static constexpr auto asTable()
144 {
145 // collect all values as table
146 etl::bitset<TupleSz> acc {};
147 if constexpr (!hasError)
148 {
149 unroll<TupleSz>([&](auto Idx) {
150 constexpr size_t I = decltype(Idx)::value;
151 acc[I] = QueryStruct<std::tuple_element_t<Idx, TupleTy>>::value;
152 });
153 }
154 return acc;
155 }
156
158 template<template<class> class QueryStruct, bool hasError = false> static constexpr size_t getMaxTableSize()
159 {
160 size_t acc = 0;
161 if constexpr (!hasError)
162 {
163 unroll<TupleSz>([&](auto Idx) {
164 size_t sz = QueryStruct<std::tuple_element_t<Idx, TupleTy>>::value.size();
165 if (sz > acc)
166 acc = sz;
167 });
168 }
169 return acc;
170 }
171
173 template<template<class> class QueryStruct, size_t MaxSz, bool hasError = false> static constexpr auto asTable2D()
174 {
175 // collect all values as table
176 std::array<etl::bitset<MaxSz>, TupleSz> acc {};
177 if constexpr (!hasError)
178 {
179 unroll<TupleSz>([&](auto Idx) {
180 constexpr size_t I = decltype(Idx)::value;
181
182 auto& remote = QueryStruct<std::tuple_element_t<Idx, TupleTy>>::value;
183 for (size_t J = 0; J < remote.size() && J < MaxSz; ++J)
184 {
185 acc[I][J] = remote[J];
186 }
187 });
188 }
189 return acc;
190 }
191};
192
194template<typename TupleTy, bool hasError = false> struct anyOf
195{
196 template<typename Ty> struct QHasSunset
197 {
199 static constexpr bool value = Ty::hasSunsetAnimation;
200 };
201
202 template<typename Ty> struct QHasSunrise
203 {
205 static constexpr bool value = Ty::hasSunriseAnimation;
206 };
207
208 template<typename Ty> struct QHasBright
209 {
211 static constexpr bool value = Ty::hasBrightCallback;
212 };
213
214 template<typename Ty> struct QCustomRamp
215 {
217 static constexpr bool value = Ty::hasCustomRamp;
218 };
219
220 template<typename Ty> struct QButtonUI
221 {
223 static constexpr bool value = Ty::hasButtonCustomUI;
224 };
225
226 template<typename Ty> struct QHasSystem
227 {
229 static constexpr bool value = Ty::hasSystemCallbacks;
230 };
231
232 template<typename Ty> struct QUserThread
233 {
235 static constexpr bool value = Ty::requireUserThread;
236 };
237
238 // booleans we need
239 static constexpr bool hasSunsetAnimation = forEach<TupleTy>::template any<QHasSunset, hasError>();
240 static constexpr bool hasSunriseAnimation = forEach<TupleTy>::template any<QHasSunrise, hasError>();
241 static constexpr bool hasBrightCallback = forEach<TupleTy>::template any<QHasBright, hasError>();
242 static constexpr bool hasCustomRamp = forEach<TupleTy>::template any<QCustomRamp, hasError>();
243 static constexpr bool hasButtonCustomUI = forEach<TupleTy>::template any<QButtonUI, hasError>();
244 static constexpr bool hasSystemCallbacks = forEach<TupleTy>::template any<QHasSystem, hasError>();
245 static constexpr bool requireUserThread = forEach<TupleTy>::template any<QUserThread, hasError>();
246
247 // as table
248 static constexpr auto everySunsetCallback = forEach<TupleTy>::template asTable<QHasSunset, hasError>();
249 static constexpr auto everySunriseCallback = forEach<TupleTy>::template asTable<QHasSunrise, hasError>();
250 static constexpr auto everyBrightCallback = forEach<TupleTy>::template asTable<QHasBright, hasError>();
251 static constexpr auto everyCustomRamp = forEach<TupleTy>::template asTable<QCustomRamp, hasError>();
252 static constexpr auto everyButtonCustomUI = forEach<TupleTy>::template asTable<QButtonUI, hasError>();
253 static constexpr auto everySystemCallbacks = forEach<TupleTy>::template asTable<QHasSystem, hasError>();
254 static constexpr auto everyRequireUserThread = forEach<TupleTy>::template asTable<QUserThread, hasError>();
255};
256
258template<typename TupleTy, bool hasError = false> struct asTableFor
259{
260 template<typename Ty> struct QHasSunset
261 {
263 static constexpr auto value = Ty::everySunsetCallback;
264 };
265
266 template<typename Ty> struct QHasSunrise
267 {
269 static constexpr auto value = Ty::everySunriseCallback;
270 };
271
272 template<typename Ty> struct QHasBright
273 {
275 static constexpr auto value = Ty::everyBrightCallback;
276 };
277
278 template<typename Ty> struct QCustomRamp
279 {
281 static constexpr auto value = Ty::everyCustomRamp;
282 };
283
284 template<typename Ty> struct QButtonUI
285 {
287 static constexpr auto value = Ty::everyButtonCustomUI;
288 };
289
290 template<typename Ty> struct QHasSystem
291 {
293 static constexpr auto value = Ty::everySystemCallbacks;
294 };
295
296 template<typename Ty> struct QUserThread
297 {
299 static constexpr auto value = Ty::everyRequireUserThread;
300 };
301
302 static constexpr size_t maxTableSz = forEach<TupleTy>::template getMaxTableSize<QHasBright, hasError>();
303
305 static constexpr auto everySunsetCallback = forEach<TupleTy>::template asTable2D<QHasSunset, maxTableSz, hasError>();
307 static constexpr auto everySunriseCallback =
308 forEach<TupleTy>::template asTable2D<QHasSunrise, maxTableSz, hasError>();
310 static constexpr auto everyBrightCallback = forEach<TupleTy>::template asTable2D<QHasBright, maxTableSz, hasError>();
312 static constexpr auto everyCustomRamp = forEach<TupleTy>::template asTable2D<QCustomRamp, maxTableSz, hasError>();
314 static constexpr auto everyButtonCustomUI = forEach<TupleTy>::template asTable2D<QButtonUI, maxTableSz, hasError>();
316 static constexpr auto everySystemCallbacks = forEach<TupleTy>::template asTable2D<QHasSystem, maxTableSz, hasError>();
318 static constexpr auto everyRequireUserThread =
319 forEach<TupleTy>::template asTable2D<QUserThread, maxTableSz, hasError>();
320};
321
323template<typename TupleTy, bool hasError = false> struct allOf
324{
325 template<typename Ty> struct QIsMode
326 {
328 static constexpr bool value = is_mode<Ty>;
329 };
330
331 template<typename Ty> struct QStateOk
332 {
334 static constexpr bool value = std::is_default_constructible_v<StateTyOf<Ty>>;
335 };
336
338 static constexpr bool inheritsFromBasicMode = forEach<TupleTy>::template all<QIsMode, hasError>();
340 static constexpr bool stateDefaultConstructible = forEach<TupleTy>::template all<QStateOk, hasError>();
341};
342
343//
344// StateTyFor & StateTyFrom
345//
346
348template<typename... Modes> using StateTyFor = std::tuple<std::optional<StateTyOf<Modes>>...>;
349
351template<typename... Modes> static constexpr auto stateTyFromImpl(std::tuple<Modes...>*) -> StateTyFor<Modes...>;
352
354template<typename AsTuple> using StateTyFrom = decltype(stateTyFromImpl((AsTuple*)0));
355
356//
357// ModeBelongsTo & GroupBelongsTo
358//
359
360template<typename Item, typename... Items> static constexpr bool belongsToImpl(std::tuple<Items...>*)
361{
362 return (std::is_same_v<Item, Items> || ...);
363};
364
366template<typename Mode, typename AllModes> static constexpr bool ModeBelongsTo = belongsToImpl<Mode>((AllModes*)0);
367
369template<typename Group, typename AllGroups> static constexpr bool GroupBelongsTo = belongsToImpl<Group>((AllGroups*)0);
370
371//
372// GroupIdFrom & ModeIdFrom
373//
374
375template<typename Mode, typename AllGroups> static constexpr int groupIdFromImpl()
376{
377 int acc = -1;
378 constexpr uint8_t N = std::tuple_size_v<AllGroups>;
379 unroll<N>([&](auto Idx) {
380 constexpr uint8_t groupId = decltype(Idx)::value;
381 using GroupHere = std::tuple_element_t<groupId, AllGroups>;
382 using AllModesHere = typename GroupHere::AllModesTy;
383 if (acc != -1)
384 return;
385
386 // make GroupIdFrom<Group, AllGroups> return the proper groupId
387 if constexpr (std::is_same_v<Mode, GroupHere>)
388 {
389 acc = groupId;
390
391 // save groupId if Mode was found in Group
392 }
393 else if constexpr (ModeBelongsTo<Mode, AllModesHere>)
394 {
395 acc = groupId;
396 }
397 });
398 return acc;
399}
400
401template<typename Mode, typename AllGroups, int groupId> static constexpr int modeIdFromImpl()
402{
403 if constexpr (groupId < 0)
404 {
405 return -1;
406 }
407 else
408 {
409 using GroupHere = std::tuple_element_t<groupId, AllGroups>;
410 using AllModesHere = typename GroupHere::AllModesTy;
411
412 // early exit for calls to ModeIdFrom<Group, AllGroups, groupId>
413 if (std::is_same_v<Mode, GroupHere>)
414 {
415 return -1;
416 }
417
418 int acc = -1;
419 constexpr uint8_t N = std::tuple_size_v<AllModesHere>;
420 unroll<N>([&](auto Idx) {
421 constexpr uint8_t modeId = decltype(Idx)::value;
422 using ModeHere = std::tuple_element_t<modeId, AllModesHere>;
423 if (acc != -1)
424 return;
425
426 // save modeId if Mode found at that position in Group
427 if constexpr (std::is_same_v<Mode, ModeHere>)
428 {
429 acc = modeId;
430 }
431 });
432
433 return acc;
434 }
435}
436
438template<typename Mode, typename AllGroups> static constexpr int GroupIdFrom = groupIdFromImpl<Mode, AllGroups>();
439
441template<typename Mode, typename AllGroups, int GroupId = GroupIdFrom<Mode, AllGroups>>
442static constexpr int ModeIdFrom = modeIdFromImpl<Mode, AllGroups, GroupId>();
443
444//
445// ModeExists
446//
447
448template<typename Mode, typename AllGroups> static constexpr bool testIfModeExistsImpl()
449{
450 constexpr uint8_t NbGroups {std::tuple_size_v<AllGroups>};
451
452 bool acc = false;
453 unroll<NbGroups>([&](auto Idx) {
454 using GroupHere = std::tuple_element_t<Idx, AllGroups>;
455 using AllModes = typename GroupHere::AllModesTy;
456 acc |= ModeBelongsTo<Mode, AllModes>;
457 });
458
459 return acc;
460}
461
463template<typename Mode, typename AllGroups> static constexpr bool ModeExists = testIfModeExistsImpl<Mode, AllGroups>();
464
466template<typename Ctx> using LocalStoreOf = typename std::remove_cv_t<std::remove_reference_t<Ctx>>::LocalStore;
467
472template<size_t NBytes, class To, class From>
473static inline std::enable_if_t<NBytes <= sizeof(To) && NBytes <= sizeof(From) && std::is_trivially_copyable_v<To> &&
474 std::is_trivially_copyable_v<From>,
475 void>
476 LMBD_INLINE bit_cast(To& dst, const From& src) noexcept
477{
478 std::memcpy(&dst, &src, NBytes);
479}
480
481} // namespace details
482
483} // namespace lampda::modes
484
485#endif
Contains shorthand macro definitions.
Basic interface types to implement custom user modes.
Contains basic interface types to implement custom user modes.
Definition: control_fixed_modes.hpp:14
static constexpr uint32_t storeId
Store identifier for persistent storage (optional)
Definition: mode_type.hpp:286