aboutsummaryrefslogtreecommitdiff
path: root/lib/events/event.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/events/event.h')
-rw-r--r--lib/events/event.h52
1 files changed, 43 insertions, 9 deletions
diff --git a/lib/events/event.h b/lib/events/event.h
index 04384aa7..8fed0082 100644
--- a/lib/events/event.h
+++ b/lib/events/event.h
@@ -68,33 +68,67 @@ namespace QMatrixClient
{ ContentKey, content } };
}
- // === Event factory ===
+ // === Event types and event types registry ===
using event_type_t = size_t;
using event_mtype_t = const char*;
- template <typename EventT>
- struct EventTypeTraits
+ class EventTypeRegistry
{
- static const event_type_t id;
+ public:
+ ~EventTypeRegistry() = default;
+
+ static event_type_t initializeTypeId(event_mtype_t matrixTypeId);
+
+ template <typename EventT>
+ static inline event_type_t initializeTypeId()
+ {
+ return initializeTypeId(EventT::matrixTypeId());
+ }
+
+ static event_mtype_t getMatrixType(event_type_t typeId)
+ {
+ return typeId < get().eventTypes.size()
+ ? get().eventTypes[typeId] : "";
+ }
+
+ private:
+ EventTypeRegistry() = default;
+ Q_DISABLE_COPY(EventTypeRegistry)
+ DISABLE_MOVE(EventTypeRegistry)
+
+ static EventTypeRegistry& get()
+ {
+ static EventTypeRegistry etr;
+ return etr;
+ }
+
+ std::vector<event_mtype_t> eventTypes;
};
template <>
- struct EventTypeTraits<void>
+ inline event_type_t EventTypeRegistry::initializeTypeId<void>()
{
- static constexpr event_type_t id = 0;
- };
+ return initializeTypeId("");
+ }
- event_type_t nextTypeId();
+ template <typename EventT>
+ struct EventTypeTraits
+ {
+ static const event_type_t id;
+ };
template <typename EventT>
- const event_type_t EventTypeTraits<EventT>::id = nextTypeId();
+ const event_type_t EventTypeTraits<EventT>::id =
+ EventTypeRegistry::initializeTypeId<EventT>();
template <typename EventT>
inline event_type_t typeId() { return EventTypeTraits<std::decay_t<EventT>>::id; }
inline event_type_t unknownEventTypeId() { return typeId<void>(); }
+ // === EventFactory ===
+
template <typename EventT, typename... ArgTs>
inline event_ptr_tt<EventT> makeEvent(ArgTs&&... args)
{