diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-07-07 11:05:58 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-07-07 11:05:58 +0900 |
commit | bd8764cbbb0ec003549261586867a6ae6f46f5cd (patch) | |
tree | e751dde2d07a0675ce79cff0c6487db0098338df /lib/events | |
parent | 1742f6e0f0af6aa6deefe2cee4bc41877f4ac2ad (diff) | |
download | libquotient-bd8764cbbb0ec003549261586867a6ae6f46f5cd.tar.gz libquotient-bd8764cbbb0ec003549261586867a6ae6f46f5cd.zip |
Reinstate EventTypeRegistry, keeping EventTypeTraits<> around
You can now get a (much more readable) Matrix type (event_mtype_t) for the internal event type code (event_type_t) using EventTypeRegistry::getMatrixType().
Diffstat (limited to 'lib/events')
-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) { |