diff options
Diffstat (limited to 'events')
-rw-r--r-- | events/event.cpp | 23 | ||||
-rw-r--r-- | events/event.h | 54 |
2 files changed, 48 insertions, 29 deletions
diff --git a/events/event.cpp b/events/event.cpp index 1e2d89f2..01f473ce 100644 --- a/events/event.cpp +++ b/events/event.cpp @@ -42,6 +42,8 @@ Event::Event(Type type, const QJsonObject& rep) } } +Event::~Event() = default; + QByteArray Event::originalJson() const { return QJsonDocument(_originalJson).toJson(); @@ -72,14 +74,15 @@ inline BaseEventT* makeIfMatches(const QJsonObject& o, const QString& selector) return makeIfMatches<BaseEventT, EventTs...>(o, selector); } -Event* Event::fromJson(const QJsonObject& obj) +template <> +EventPtr QMatrixClient::makeEvent<Event>(const QJsonObject& obj) { // Check more specific event types first - if (auto e = RoomEvent::fromJson(obj)) - return e; + if (auto e = makeEvent<RoomEvent>(obj)) + return EventPtr(move(e)); - return makeIfMatches<Event, - TypingEvent, ReceiptEvent>(obj, obj["type"].toString()); + return EventPtr { makeIfMatches<Event, + TypingEvent, ReceiptEvent>(obj, obj["type"].toString()) }; } RoomEvent::RoomEvent(Event::Type type) : Event(type) { } @@ -119,8 +122,7 @@ RoomEvent::RoomEvent(Type type, const QJsonObject& rep) qCDebug(EVENTS) << "Event transactionId:" << _txnId; } -RoomEvent::~RoomEvent() -{ /* Let QScopedPointer<RedactionEvent> do its job */ } +RoomEvent::~RoomEvent() = default; // Let the smart pointer do its job QString RoomEvent::redactionReason() const { @@ -133,11 +135,12 @@ void RoomEvent::addId(const QString& id) _id = id; } -RoomEvent* RoomEvent::fromJson(const QJsonObject& obj) +template <> +RoomEventPtr QMatrixClient::makeEvent<RoomEvent>(const QJsonObject& obj) { - return makeIfMatches<RoomEvent, + return RoomEventPtr { makeIfMatches<RoomEvent, RoomMessageEvent, RoomNameEvent, RoomAliasesEvent, RoomCanonicalAliasEvent, RoomMemberEvent, RoomTopicEvent, RoomAvatarEvent, EncryptionEvent, RedactionEvent> - (obj, obj["type"].toString()); + (obj, obj["type"].toString()) }; } diff --git a/events/event.h b/events/event.h index bd33bb50..2b18bb46 100644 --- a/events/event.h +++ b/events/event.h @@ -25,8 +25,21 @@ #include "util.h" +#include <memory> + namespace QMatrixClient { + template <typename EventT> + using event_ptr_tt = std::unique_ptr<EventT>; + + /** Create an event with proper type from a JSON object + * Use this factory template to detect the type from the JSON object + * contents (the detected event type should derive from the template + * parameter type) and create an event object of that type. + */ + template <typename EventT> + event_ptr_tt<EventT> makeEvent(const QJsonObject& obj); + class Event { Q_GADGET @@ -64,12 +77,6 @@ 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: const QJsonObject contentJson() const; @@ -82,6 +89,10 @@ namespace QMatrixClient Q_PROPERTY(QJsonObject contentJson READ contentJson CONSTANT) }; using EventType = Event::Type; + using EventPtr = event_ptr_tt<Event>; + + template <> + EventPtr makeEvent<Event>(const QJsonObject& obj); /** * \brief A vector of pointers to events with deserialisation capabilities @@ -94,7 +105,7 @@ namespace QMatrixClient * \tparam EventT base type of all events in the vector */ template <typename EventT> - class EventsBatch : public std::vector<EventT*> + class EventsBatch : public std::vector<event_ptr_tt<EventT>> { public: /** @@ -120,8 +131,10 @@ namespace QMatrixClient for (auto objValue: objs) { const auto o = objValue.toObject(); - auto e = EventT::fromJson(o); - this->push_back(e ? e : new EventT(EventType::Unknown, o)); + auto e { makeEvent<EventT>(o) }; + if (!e) + e.reset(new EventT(EventType::Unknown, o)); + this->emplace_back(std::move(e)); } } }; @@ -151,10 +164,10 @@ namespace QMatrixClient const QDateTime& timestamp() const { return _serverTimestamp; } const QString& roomId() const { return _roomId; } const QString& senderId() const { return _senderId; } - bool isRedacted() const { return redactedBecause(); } - RedactionEvent* redactedBecause() const + bool isRedacted() const { return bool(_redactedBecause); } + const RedactionEvent* redactedBecause() const { - return _redactedBecause.data(); + return _redactedBecause.get(); } QString redactionReason() const; const QString& transactionId() const { return _txnId; } @@ -180,18 +193,19 @@ namespace QMatrixClient */ void addId(const QString& id); - // "Static override" of the one in Event - static RoomEvent* fromJson(const QJsonObject& obj); - private: QString _id; QString _roomId; QString _senderId; QDateTime _serverTimestamp; - QScopedPointer<RedactionEvent> _redactedBecause; + event_ptr_tt<RedactionEvent> _redactedBecause; QString _txnId; }; using RoomEvents = EventsBatch<RoomEvent>; + using RoomEventPtr = event_ptr_tt<RoomEvent>; + + template <> + RoomEventPtr makeEvent<RoomEvent>(const QJsonObject& obj); /** * Conceptually similar to QStringView (but much more primitive), it's a @@ -199,10 +213,10 @@ namespace QMatrixClient * referring to the beginning and the end of a range in a RoomEvents * container. */ - struct RoomEventsView + struct RoomEventsRange { - RoomEvents::const_iterator from; - RoomEvents::const_iterator to; + RoomEvents::iterator from; + RoomEvents::iterator to; RoomEvents::size_type size() const { @@ -212,6 +226,8 @@ namespace QMatrixClient bool empty() const { return from == to; } RoomEvents::const_iterator begin() const { return from; } RoomEvents::const_iterator end() const { return to; } + RoomEvents::iterator begin() { return from; } + RoomEvents::iterator end() { return to; } }; template <typename ContentT> |