diff options
-rw-r--r-- | events/event.h | 41 | ||||
-rw-r--r-- | jobs/roommessagesjob.cpp | 4 | ||||
-rw-r--r-- | jobs/syncjob.h | 5 |
3 files changed, 27 insertions, 23 deletions
diff --git a/events/event.h b/events/event.h index c0c1b603..cc99b57b 100644 --- a/events/event.h +++ b/events/event.h @@ -63,6 +63,10 @@ namespace QMatrixClient // (and in most cases it will be a combination of other fields // instead of "content" field). + /** Create an event with proper type from a JSON object + * Use this factory to detect the type from the JSON object contents + * and create an event object of that type. + */ static Event* fromJson(const QJsonObject& obj); protected: @@ -77,26 +81,27 @@ namespace QMatrixClient Q_PROPERTY(QJsonObject contentJson READ contentJson CONSTANT) }; using EventType = Event::Type; - template <typename EventT> - using EventsBatch = std::vector<EventT*>; - using Events = EventsBatch<Event>; - template <typename BaseEventT = Event, - typename BatchT = EventsBatch<BaseEventT> > - inline BatchT makeEvents(const QJsonArray& objs) + template <typename EventT> + class EventsBatch : public std::vector<EventT*> { - BatchT evs; - // The below line accommodates the difference in size types of - // STL and Qt containers. - evs.reserve(static_cast<typename BatchT::size_type>(objs.size())); - for (auto objValue: objs) - { - const auto o = objValue.toObject(); - auto e = BaseEventT::fromJson(o); - evs.push_back(e ? e : new BaseEventT(EventType::Unknown, o)); - } - return evs; - } + public: + void fromJson(const QJsonObject& container, const QString& node) + { + const auto objs = container.value(node).toArray(); + using size_type = typename std::vector<EventT*>::size_type; + // The below line accommodates the difference in size types of + // STL and Qt containers. + this->reserve(static_cast<size_type>(objs.size())); + for (auto objValue: objs) + { + const auto o = objValue.toObject(); + auto e = EventT::fromJson(o); + this->push_back(e ? e : new EventT(EventType::Unknown, o)); + } + } + }; + using Events = EventsBatch<Event>; /** This class corresponds to m.room.* events */ class RoomEvent : public Event diff --git a/jobs/roommessagesjob.cpp b/jobs/roommessagesjob.cpp index 078c692a..c527cc71 100644 --- a/jobs/roommessagesjob.cpp +++ b/jobs/roommessagesjob.cpp @@ -58,8 +58,8 @@ QString RoomMessagesJob::end() const BaseJob::Status RoomMessagesJob::parseJson(const QJsonDocument& data) { - QJsonObject obj = data.object(); - d->events.assign(makeEvents<RoomEvent>(obj.value("chunk").toArray())); + const auto obj = data.object(); + d->events.fromJson(obj, "chunk"); d->end = obj.value("end").toString(); return Success; } diff --git a/jobs/syncjob.h b/jobs/syncjob.h index b1db914d..08bd773e 100644 --- a/jobs/syncjob.h +++ b/jobs/syncjob.h @@ -36,11 +36,10 @@ namespace QMatrixClient explicit Batch(QString k) : jsonKey(std::move(k)) { } void fromJson(const QJsonObject& roomContents) { - this->assign(makeEvents<EventT>( - roomContents[jsonKey].toObject()["events"].toArray())); + EventsBatch<EventT>::fromJson( + roomContents[jsonKey].toObject(), "events"); } - private: QString jsonKey; }; |