From 0fe83d59d76cd8f9c8f92d40cc58d9f5b082a84a Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Wed, 31 Aug 2016 18:27:43 +0900 Subject: Event::fromJson(): made the code more compact That might be not as efficient as a solution on variadic templates; but arguably easier to understand. --- events/event.cpp | 55 ++++++++++++++++++++++++------------------------------- 1 file changed, 24 insertions(+), 31 deletions(-) (limited to 'events') diff --git a/events/event.cpp b/events/event.cpp index d05d666f..1235780d 100644 --- a/events/event.cpp +++ b/events/event.cpp @@ -82,40 +82,33 @@ QString Event::originalJson() const return d->originalJson; } +template +Event* make(const QJsonObject& obj) +{ + return T::fromJson(obj); +} + Event* Event::fromJson(const QJsonObject& obj) { - //qDebug() << obj.value("type").toString(); - if( obj.value("type").toString() == "m.room.message" ) - { - return RoomMessageEvent::fromJson(obj); - } - if( obj.value("type").toString() == "m.room.name" ) - { - return RoomNameEvent::fromJson(obj); - } - if( obj.value("type").toString() == "m.room.aliases" ) - { - return RoomAliasesEvent::fromJson(obj); - } - if( obj.value("type").toString() == "m.room.canonical_alias" ) - { - return RoomCanonicalAliasEvent::fromJson(obj); - } - if( obj.value("type").toString() == "m.room.member" ) - { - return RoomMemberEvent::fromJson(obj); - } - if( obj.value("type").toString() == "m.room.topic" ) - { - return RoomTopicEvent::fromJson(obj); - } - if( obj.value("type").toString() == "m.typing" ) - { - return TypingEvent::fromJson(obj); - } - if( obj.value("type").toString() == "m.receipt" ) + struct Factory { + QString type; + Event* (*make)(const QJsonObject& obj); + }; + const Factory evTypes[] { + { "m.room.message", make }, + { "m.room.name", make }, + { "m.room.aliases", make }, + { "m.room.canonical_alias", make }, + { "m.room.member", make }, + { "m.room.topic", make }, + { "m.room.typing", make }, + { "m.room.receipt", make }, + // Insert new types before this line + }; + for (auto e: evTypes) { - return ReceiptEvent::fromJson(obj); + if (obj["type"].toString() == e.type) + return e.make(obj); } return UnknownEvent::fromJson(obj); } -- cgit v1.2.3 From b1b23e3500ff80d62b54153341e66fdfef4534db Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Wed, 31 Aug 2016 18:30:42 +0900 Subject: QList -> using Events=QVector --- events/event.cpp | 10 +++++----- events/event.h | 12 ++++++++++-- 2 files changed, 15 insertions(+), 7 deletions(-) (limited to 'events') diff --git a/events/event.cpp b/events/event.cpp index 1235780d..11ed1cc9 100644 --- a/events/event.cpp +++ b/events/event.cpp @@ -144,11 +144,11 @@ bool Event::parseJson(const QJsonObject& obj) return correct; } -QList QMatrixClient::eventListFromJson(const QJsonArray& json) +Events QMatrixClient::eventsFromJson(const QJsonArray& json) { - QList l; - l.reserve(json.size()); + Events evs; + evs.reserve(json.size()); for (auto event: json) - l.push_back(Event::fromJson(event.toObject())); - return l; + evs.push_back(Event::fromJson(event.toObject())); + return evs; } diff --git a/events/event.h b/events/event.h index 72d26208..d9316747 100644 --- a/events/event.h +++ b/events/event.h @@ -24,6 +24,7 @@ #include #include #include +#include class QJsonArray; @@ -38,7 +39,8 @@ namespace QMatrixClient class Event { public: - Event(EventType type); + explicit Event(EventType type); + Event(Event&) = delete; virtual ~Event(); EventType type() const; @@ -57,8 +59,9 @@ namespace QMatrixClient class Private; Private* d; }; + using Events = QVector; - QList eventListFromJson(const QJsonArray& contents); + Events eventsFromJson(const QJsonArray& contents); /** * Finds a place in the timeline where a new event/message could be inserted. @@ -71,6 +74,11 @@ namespace QMatrixClient { return std::lower_bound (timeline.begin(), timeline.end(), item, [](const typename ContT::value_type a, const ItemT * b) { + // FIXME: We should not order the message list by origin timestamp. + // Rather, an order of receiving should be used (which actually + // poses a question on whether this method is needed at all - + // or we'd just prepend and append, depending on whether we + // received something from /sync or from /messages. return a->timestamp() < b->timestamp(); } ); -- cgit v1.2.3 From 573e0e478c4a904ba69e6b523dead41eb28121c1 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Wed, 7 Sep 2016 08:14:29 +0900 Subject: Temporarily revert "Event::fromJson(): made the code more compact" as it breaks highlighting This reverts commit 0fe83d59d76cd8f9c8f92d40cc58d9f5b082a84a. --- events/event.cpp | 55 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 24 deletions(-) (limited to 'events') diff --git a/events/event.cpp b/events/event.cpp index 11ed1cc9..0c7700ee 100644 --- a/events/event.cpp +++ b/events/event.cpp @@ -82,33 +82,40 @@ QString Event::originalJson() const return d->originalJson; } -template -Event* make(const QJsonObject& obj) -{ - return T::fromJson(obj); -} - Event* Event::fromJson(const QJsonObject& obj) { - struct Factory { - QString type; - Event* (*make)(const QJsonObject& obj); - }; - const Factory evTypes[] { - { "m.room.message", make }, - { "m.room.name", make }, - { "m.room.aliases", make }, - { "m.room.canonical_alias", make }, - { "m.room.member", make }, - { "m.room.topic", make }, - { "m.room.typing", make }, - { "m.room.receipt", make }, - // Insert new types before this line - }; - for (auto e: evTypes) + //qDebug() << obj.value("type").toString(); + if( obj.value("type").toString() == "m.room.message" ) + { + return RoomMessageEvent::fromJson(obj); + } + if( obj.value("type").toString() == "m.room.name" ) + { + return RoomNameEvent::fromJson(obj); + } + if( obj.value("type").toString() == "m.room.aliases" ) + { + return RoomAliasesEvent::fromJson(obj); + } + if( obj.value("type").toString() == "m.room.canonical_alias" ) + { + return RoomCanonicalAliasEvent::fromJson(obj); + } + if( obj.value("type").toString() == "m.room.member" ) + { + return RoomMemberEvent::fromJson(obj); + } + if( obj.value("type").toString() == "m.room.topic" ) + { + return RoomTopicEvent::fromJson(obj); + } + if( obj.value("type").toString() == "m.typing" ) + { + return TypingEvent::fromJson(obj); + } + if( obj.value("type").toString() == "m.receipt" ) { - if (obj["type"].toString() == e.type) - return e.make(obj); + return ReceiptEvent::fromJson(obj); } return UnknownEvent::fromJson(obj); } -- cgit v1.2.3