diff options
-rw-r--r-- | lib/events/event.cpp | 12 | ||||
-rw-r--r-- | lib/events/event.h | 52 |
2 files changed, 52 insertions, 12 deletions
diff --git a/lib/events/event.cpp b/lib/events/event.cpp index 44bf79a1..2cad1462 100644 --- a/lib/events/event.cpp +++ b/lib/events/event.cpp @@ -24,10 +24,16 @@ using namespace QMatrixClient; -event_type_t QMatrixClient::nextTypeId() +event_type_t EventTypeRegistry::initializeTypeId(event_mtype_t matrixTypeId) { - static event_type_t _id = EventTypeTraits<void>::id; - return ++_id; + const auto id = get().eventTypes.size(); + get().eventTypes.push_back(matrixTypeId); + if (strncmp(matrixTypeId, "", 1) == 0) + qDebug(EVENTS) << "Initialized unknown event type with id" << id; + else + qDebug(EVENTS) << "Initialized event type" << matrixTypeId + << "with id" << id; + return id; } Event::Event(Type type, const QJsonObject& json) 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) { |