diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-07-01 22:48:38 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-07-04 09:07:32 +0900 |
commit | f1ffe1e7a3e81c07a07a8416ce307e4413ec8fbc (patch) | |
tree | f2435183d11a4cea52a7532eb9ff3d4d837e1d22 /lib/events/accountdataevents.h | |
parent | d5397fe5ae2ca34d5cfb11394dac17728a2b50ce (diff) | |
download | libquotient-f1ffe1e7a3e81c07a07a8416ce307e4413ec8fbc.tar.gz libquotient-f1ffe1e7a3e81c07a07a8416ce307e4413ec8fbc.zip |
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.
Diffstat (limited to 'lib/events/accountdataevents.h')
-rw-r--r-- | lib/events/accountdataevents.h | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/lib/events/accountdataevents.h b/lib/events/accountdataevents.h index 11667172..6d53d2aa 100644 --- a/lib/events/accountdataevents.h +++ b/lib/events/accountdataevents.h @@ -32,7 +32,7 @@ namespace QMatrixClient { TagRecord (QString order = {}) : order(std::move(order)) { } explicit TagRecord(const QJsonValue& jv) - : order(jv.toObject().value("order").toString()) + : order(jv.toObject().value("order"_ls).toString()) { } QString order; @@ -50,28 +50,29 @@ namespace QMatrixClient using TagsMap = QHash<QString, TagRecord>; -#define DEFINE_SIMPLE_EVENT(_Name, _TypeId, _EnumType, _ContentType, _ContentKey) \ +#define DEFINE_SIMPLE_EVENT(_Name, _TypeId, _ContentType, _ContentKey) \ class _Name : public Event \ { \ public: \ - static constexpr const char* typeId() { return _TypeId; } \ - explicit _Name(const QJsonObject& obj) \ - : Event((_EnumType), obj) \ - , _content(contentJson(), QStringLiteral(#_ContentKey)) \ + using content_type = _ContentType; \ + DEFINE_EVENT_TYPEID(_TypeId, _Name) \ + explicit _Name(QJsonObject obj) \ + : Event(typeId(), std::move(obj)) \ { } \ - template <typename... Ts> \ - explicit _Name(Ts&&... contentArgs) \ - : Event(_EnumType) \ - , _content(QStringLiteral(#_ContentKey), \ - std::forward<Ts>(contentArgs)...) \ + explicit _Name(_ContentType content) \ + : Event(typeId(), matrixTypeId(), \ + QJsonObject { { QStringLiteral(#_ContentKey), \ + toJson(std::move(content)) } }) \ { } \ - const _ContentType& _ContentKey() const { return _content.value; } \ - QJsonObject toJson() const { return _content.toJson(); } \ - protected: \ - EventContent::SimpleContent<_ContentType> _content; \ - }; + auto _ContentKey() const \ + { return fromJson<content_type>(contentJson()[#_ContentKey]); } \ + }; // End of macro + + DEFINE_SIMPLE_EVENT(TagEvent, "m.tag", TagsMap, tags) + DEFINE_SIMPLE_EVENT(ReadMarkerEvent, "m.fully_read", QString, event_id) + DEFINE_SIMPLE_EVENT(IgnoredUsersEvent, "m.ignored_user_list", + QSet<QString>, ignored_users) - DEFINE_SIMPLE_EVENT(TagEvent, "m.tag", EventType::Tag, TagsMap, tags) - DEFINE_SIMPLE_EVENT(ReadMarkerEvent, "m.fully_read", EventType::ReadMarker, - QString, event_id) + DEFINE_EVENTTYPE_ALIAS(Tag, TagEvent) + DEFINE_EVENTTYPE_ALIAS(ReadMarker, ReadMarkerEvent) } |