diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2017-11-16 14:10:08 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2017-11-16 14:10:08 +0900 |
commit | 491e392af73be3ebfd928e80efc0514fd43b8e87 (patch) | |
tree | b129a00585a19764ef4092fe381c56c4658cd478 /events | |
parent | f12e09ea1b45be1a96533aa83f6227940c70d548 (diff) | |
download | libquotient-491e392af73be3ebfd928e80efc0514fd43b8e87.tar.gz libquotient-491e392af73be3ebfd928e80efc0514fd43b8e87.zip |
Simplify code that loads events from JSON arrays
Diffstat (limited to 'events')
-rw-r--r-- | events/event.h | 41 |
1 files changed, 23 insertions, 18 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 |