6 #include "events/stateevent.h"
8 #include <QtCore/QHash>
18 template <
typename FnT,
class EvT = std::decay_t<fn_arg_t<FnT>>>
19 concept Keyed_State_Fn = EvT::needsStateKey;
21 template <
typename FnT,
class EvT = std::decay_t<fn_arg_t<FnT>>>
22 concept Keyless_State_Fn = !EvT::needsStateKey;
25 :
private QHash<StateEventKey,
const StateEvent*> {
28 const QHash<StateEventKey,
const StateEvent*>& events()
const
42 const StateEvent* get(
const QString& evtType,
43 const QString& stateKey = {})
const;
50 template <Keyed_State_Event EvT>
51 const EvT* get(
const QString& stateKey = {})
const
53 if (
const auto* evt = get(EvT::TypeId, stateKey)) {
54 Q_ASSERT(evt->matrixType() == EvT::TypeId
55 && evt->stateKey() == stateKey);
56 return eventCast<
const EvT>(evt);
66 template <Keyless_State_Event EvT>
67 const EvT* get()
const
69 if (
const auto* evt = get(EvT::TypeId)) {
70 Q_ASSERT(evt->matrixType() == EvT::TypeId);
71 return eventCast<
const EvT>(evt);
76 using QHash::contains;
78 bool contains(
const QString& evtType,
const QString& stateKey = {})
const;
80 template <Keyed_State_Event EvT>
81 bool contains(
const QString& stateKey = {})
const
83 return contains(EvT::TypeId, stateKey);
86 template <Keyless_State_Event EvT>
89 return contains(EvT::TypeId);
92 template <Keyed_State_Event EvT>
93 auto content(
const QString& stateKey,
94 typename EvT::content_type defaultValue = {})
const
98 if (
const auto evt = get<EvT>(stateKey))
99 return evt->content();
100 return std::move(defaultValue);
103 template <Keyless_State_Event EvT>
104 auto content(
typename EvT::content_type defaultValue = {})
const
107 if (
const auto evt = get<EvT>())
108 return evt->content();
117 Q_INVOKABLE QJsonObject contentJson(
const QString& evtType,
118 const QString& stateKey = {})
const;
124 const QVector<
const StateEvent*> eventsOfType(
const QString& evtType)
const;
132 template <
typename FnT>
133 auto query(
const QString& evtType,
const QString& stateKey, FnT&& fn)
const
135 return lift(std::forward<FnT>(fn), get(evtType, stateKey));
144 template <Keyed_State_Fn FnT>
145 auto query(
const QString& stateKey, FnT&& fn)
const
147 using EventT = std::decay_t<fn_arg_t<FnT>>;
148 return lift(std::forward<FnT>(fn), get<EventT>(stateKey));
157 template <Keyless_State_Fn FnT>
158 auto query(FnT&& fn)
const
160 using EventT = std::decay_t<fn_arg_t<FnT>>;
161 return lift(std::forward<FnT>(fn), get<EventT>());
171 template <
typename FnT,
typename FallbackT>
172 auto queryOr(
const QString& evtType,
const QString& stateKey, FnT&& fn,
173 FallbackT&& fallback)
const
175 return query(evtType, stateKey, std::forward<FnT>(fn))
176 .value_or(std::forward<FallbackT>(fallback));
186 template <
typename FnT,
typename FallbackT>
187 auto queryOr(
const QString& stateKey, FnT&& fn, FallbackT&& fallback)
const
189 return query(stateKey, std::forward<FnT>(fn))
190 .value_or(std::forward<FallbackT>(fallback));
200 template <
typename FnT,
typename FallbackT>
201 auto queryOr(FnT&& fn, FallbackT&& fallback)
const
203 return query(std::forward<FnT>(fn))
204 .value_or(std::forward<FallbackT>(fallback));
209 using QHash<StateEventKey,
const StateEvent*>::QHash;