From f1ffe1e7a3e81c07a07a8416ce307e4413ec8fbc Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 1 Jul 2018 22:48:38 +0900 Subject: Event types system remade to be extensible There were two common points that had to be updated every time a new event is introduced: the EventType enumeration and one of 3 doMakeEvent<> specialisations. The new code has a template class, EventFactory<>, that uses a list of static factory methods to create events instead of typelists used in doMakeEvent<>(); the EventType enumeration is replaced with a namespace populated with constants as necessary. In general, EventType is considered a deprecated mechanism altogether; instead, a set of facilities is provided: is<>() to check if an event has a certain type (to replace comparison against an EventType value) and visit<>() to execute actions based on the event type (replacing switch statements over EventType values). Closes #129. --- lib/events/roommemberevent.h | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'lib/events/roommemberevent.h') diff --git a/lib/events/roommemberevent.h b/lib/events/roommemberevent.h index 8e0cc0a4..943d5ac6 100644 --- a/lib/events/roommemberevent.h +++ b/lib/events/roommemberevent.h @@ -22,8 +22,6 @@ #include "eventcontent.h" -#include - namespace QMatrixClient { class MemberEventContent: public EventContent::Base @@ -36,6 +34,9 @@ namespace QMatrixClient : membership(mt) { } explicit MemberEventContent(const QJsonObject& json); + explicit MemberEventContent(const QJsonValue& jv) + : MemberEventContent(jv.toObject()) + { } MembershipType membership; bool isDirect = false; @@ -52,23 +53,26 @@ namespace QMatrixClient { Q_GADGET public: - static constexpr const char* typeId() { return "m.room.member"; } + DEFINE_EVENT_TYPEID("m.room.member", RoomMemberEvent) using MembershipType = MemberEventContent::MembershipType; - explicit RoomMemberEvent(Type type, const QJsonObject& obj) - : StateEvent(type, obj) + explicit RoomMemberEvent(const QJsonObject& obj) + : StateEvent(typeId(), obj) { } RoomMemberEvent(MemberEventContent&& c) - : StateEvent(Type::RoomMember, c) + : StateEvent(typeId(), matrixTypeId(), c.toJson()) { } - explicit RoomMemberEvent(const QJsonObject& obj) - : RoomMemberEvent(Type::RoomMember, obj) + + // This is a special constructor enabling RoomMemberEvent to be + // a base class for more specific member events. + RoomMemberEvent(Type type, const QJsonObject& fullJson) + : StateEvent(type, fullJson) { } MembershipType membership() const { return content().membership; } QString userId() const - { return originalJsonObject().value("state_key").toString(); } + { return fullJson()["state_key"_ls].toString(); } bool isDirect() const { return content().isDirect; } QString displayName() const { return content().displayName; } QUrl avatarUrl() const { return content().avatarUrl; } @@ -76,4 +80,5 @@ namespace QMatrixClient private: REGISTER_ENUM(MembershipType) }; + DEFINE_EVENTTYPE_ALIAS(RoomMember, RoomMemberEvent) } // namespace QMatrixClient -- cgit v1.2.3 From ed467d27b07781fdd2f7ddef043568954ce50b69 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Mon, 2 Jul 2018 13:39:33 +0900 Subject: Events: use a template structure instead of template variables; rearrange code into blocks A template member variable in it seemed to cause internal compiler error in MSVC 2017, let alone MSVC 2015... --- lib/events/roommemberevent.h | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/events/roommemberevent.h') diff --git a/lib/events/roommemberevent.h b/lib/events/roommemberevent.h index 943d5ac6..1ecd63d1 100644 --- a/lib/events/roommemberevent.h +++ b/lib/events/roommemberevent.h @@ -80,5 +80,6 @@ namespace QMatrixClient private: REGISTER_ENUM(MembershipType) }; + REGISTER_EVENT_TYPE(RoomMemberEvent) DEFINE_EVENTTYPE_ALIAS(RoomMember, RoomMemberEvent) } // namespace QMatrixClient -- cgit v1.2.3 From 2cbb2a8c1be8bf08b1b835ffaa7bb0bee823e3a6 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Wed, 4 Jul 2018 19:46:08 +0900 Subject: Break down event.* into smaller files We now have event.*, roomevent.*, stateevent.* and eventloader.h. If you only use event leaf-classes (such as RoomMemberEvent) you shouldn't notice anything. --- lib/events/roommemberevent.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib/events/roommemberevent.h') diff --git a/lib/events/roommemberevent.h b/lib/events/roommemberevent.h index 1ecd63d1..f3e4f53a 100644 --- a/lib/events/roommemberevent.h +++ b/lib/events/roommemberevent.h @@ -18,8 +18,7 @@ #pragma once -#include "event.h" - +#include "stateevent.h" #include "eventcontent.h" namespace QMatrixClient -- cgit v1.2.3