5#ifndef USER_MODE_TOOLS_H
6#define USER_MODE_TOOLS_H
14#include <etl/bitset.h>
20#error "File requires -DLMBD_CPP17 to explicitly enables C++17 features"
26template<
typename Mode>
static constexpr bool is_mode = std::is_base_of_v<BasicMode, Mode>;
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;
47template<
typename>
static constexpr auto stateTyOfImpl(...) -> NoState;
50template<
typename Mode>
using StateTyOf =
decltype(stateTyOfImpl<Mode>(0));
62enum class NoStoreHere : uint16_t
68template<
typename Mode,
69 typename EnumTy =
typename Mode::Store,
71 typename RetTy = std::conditional_t<isDefault, NoStoreHere, EnumTy>>
72static constexpr auto storeEnumOfImpl(
int) -> RetTy;
75template<
typename>
static constexpr auto storeEnumOfImpl(...) -> NoStoreHere;
78template<
typename Mode>
using StoreEnumOf =
decltype(storeEnumOfImpl<Mode>(0));
88template<
class CbTy, uint8_t... Indexes>
89static constexpr void LMBD_INLINE unroll_impl(CbTy&& cb, std::integer_sequence<uint8_t, Indexes...>)
91 (cb(std::integral_constant<uint8_t, Indexes> {}), ...);
95template<u
int8_t N,
class CbTy>
static constexpr void LMBD_INLINE unroll(CbTy&& cb)
97 constexpr auto Indexes = std::make_integer_sequence<uint8_t, N>();
99 [&](
auto I) LMBD_INLINE {
100 return cb(std::integral_constant<uint8_t,
decltype(I)::value> {});
110template<
typename TupleTy, u
int8_t TupleSz = std::tuple_size_v<TupleTy>>
struct forEach
113 template<
template<
class>
class QueryStruct,
bool hasError =
false>
static constexpr bool any()
116 if constexpr (!hasError)
118 unroll<TupleSz>([&](
auto Idx) {
119 acc |= QueryStruct<std::tuple_element_t<Idx, TupleTy>>::value;
126 template<
template<
class>
class QueryStruct,
bool hasError =
false>
static constexpr bool all()
129 if constexpr (!hasError)
131 unroll<TupleSz>([&](
auto Idx) {
132 acc &= QueryStruct<std::tuple_element_t<Idx, TupleTy>>::value;
143 template<
template<
class>
class QueryStruct,
bool hasError =
false>
static constexpr auto asTable()
146 etl::bitset<TupleSz> acc {};
147 if constexpr (!hasError)
149 unroll<TupleSz>([&](
auto Idx) {
150 constexpr size_t I =
decltype(Idx)::value;
151 acc[I] = QueryStruct<std::tuple_element_t<Idx, TupleTy>>::value;
158 template<
template<
class>
class QueryStruct,
bool hasError =
false>
static constexpr size_t getMaxTableSize()
161 if constexpr (!hasError)
163 unroll<TupleSz>([&](
auto Idx) {
164 size_t sz = QueryStruct<std::tuple_element_t<Idx, TupleTy>>::value.size();
173 template<
template<
class>
class QueryStruct,
size_t MaxSz,
bool hasError =
false>
static constexpr auto asTable2D()
176 std::array<etl::bitset<MaxSz>, TupleSz> acc {};
177 if constexpr (!hasError)
179 unroll<TupleSz>([&](
auto Idx) {
180 constexpr size_t I =
decltype(Idx)::value;
182 auto& remote = QueryStruct<std::tuple_element_t<Idx, TupleTy>>::value;
183 for (
size_t J = 0; J < remote.size() && J < MaxSz; ++J)
185 acc[I][J] = remote[J];
194template<
typename TupleTy,
bool hasError = false>
struct anyOf
199 static constexpr bool value = Ty::hasSunsetAnimation;
205 static constexpr bool value = Ty::hasSunriseAnimation;
211 static constexpr bool value = Ty::hasBrightCallback;
217 static constexpr bool value = Ty::hasCustomRamp;
223 static constexpr bool value = Ty::hasButtonCustomUI;
229 static constexpr bool value = Ty::hasSystemCallbacks;
235 static constexpr bool value = Ty::requireUserThread;
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>();
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>();
258template<
typename TupleTy,
bool hasError = false>
struct asTableFor
263 static constexpr auto value = Ty::everySunsetCallback;
269 static constexpr auto value = Ty::everySunriseCallback;
275 static constexpr auto value = Ty::everyBrightCallback;
281 static constexpr auto value = Ty::everyCustomRamp;
287 static constexpr auto value = Ty::everyButtonCustomUI;
293 static constexpr auto value = Ty::everySystemCallbacks;
299 static constexpr auto value = Ty::everyRequireUserThread;
302 static constexpr size_t maxTableSz = forEach<TupleTy>::template getMaxTableSize<QHasBright, hasError>();
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>();
323template<
typename TupleTy,
bool hasError = false>
struct allOf
328 static constexpr bool value = is_mode<Ty>;
334 static constexpr bool value = std::is_default_constructible_v<StateTyOf<Ty>>;
338 static constexpr bool inheritsFromBasicMode = forEach<TupleTy>::template all<QIsMode, hasError>();
340 static constexpr bool stateDefaultConstructible = forEach<TupleTy>::template all<QStateOk, hasError>();
348template<
typename... Modes>
using StateTyFor = std::tuple<std::optional<StateTyOf<Modes>>...>;
351template<
typename... Modes>
static constexpr auto stateTyFromImpl(std::tuple<Modes...>*) -> StateTyFor<Modes...>;
354template<
typename AsTuple>
using StateTyFrom =
decltype(stateTyFromImpl((AsTuple*)0));
360template<
typename Item,
typename... Items>
static constexpr bool belongsToImpl(std::tuple<Items...>*)
362 return (std::is_same_v<Item, Items> || ...);
366template<
typename Mode,
typename AllModes>
static constexpr bool ModeBelongsTo = belongsToImpl<Mode>((AllModes*)0);
369template<
typename Group,
typename AllGroups>
static constexpr bool GroupBelongsTo = belongsToImpl<Group>((AllGroups*)0);
375template<
typename Mode,
typename AllGroups>
static constexpr int groupIdFromImpl()
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;
387 if constexpr (std::is_same_v<Mode, GroupHere>)
393 else if constexpr (ModeBelongsTo<Mode, AllModesHere>)
401template<
typename Mode,
typename AllGroups,
int groupId>
static constexpr int modeIdFromImpl()
403 if constexpr (groupId < 0)
409 using GroupHere = std::tuple_element_t<groupId, AllGroups>;
410 using AllModesHere =
typename GroupHere::AllModesTy;
413 if (std::is_same_v<Mode, GroupHere>)
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>;
427 if constexpr (std::is_same_v<Mode, ModeHere>)
438template<
typename Mode,
typename AllGroups>
static constexpr int GroupIdFrom = groupIdFromImpl<Mode, AllGroups>();
441template<
typename Mode,
typename AllGroups,
int GroupId = GroupIdFrom<Mode, AllGroups>>
442static constexpr int ModeIdFrom = modeIdFromImpl<Mode, AllGroups, GroupId>();
448template<
typename Mode,
typename AllGroups>
static constexpr bool testIfModeExistsImpl()
450 constexpr uint8_t NbGroups {std::tuple_size_v<AllGroups>};
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>;
463template<
typename Mode,
typename AllGroups>
static constexpr bool ModeExists = testIfModeExistsImpl<Mode, AllGroups>();
466template<
typename Ctx>
using LocalStoreOf =
typename std::remove_cv_t<std::remove_reference_t<Ctx>>::LocalStore;
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>,
476 LMBD_INLINE bit_cast(To& dst,
const From& src)
noexcept
478 std::memcpy(&dst, &src, NBytes);
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
Definition: tools.hpp:326
Definition: tools.hpp:332
Definition: tools.hpp:215
Definition: tools.hpp:209
Definition: tools.hpp:203
Definition: tools.hpp:197
Definition: tools.hpp:227
Definition: tools.hpp:233
Definition: tools.hpp:279
Definition: tools.hpp:273
Definition: tools.hpp:267
Definition: tools.hpp:261
Definition: tools.hpp:291
Definition: tools.hpp:297