aboutsummaryrefslogtreecommitdiff
path: root/events/event.cpp
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2017-12-14 18:41:55 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2017-12-14 18:41:55 +0900
commitc5f480355c7d4c000a4ee73fd7f8107a9a9340c2 (patch)
treed7a8f51e75230aa48cd243ef40b17ced70c0216b /events/event.cpp
parentbb14969dfa5e9e8a26a005a7f804f21a62460f68 (diff)
downloadlibquotient-c5f480355c7d4c000a4ee73fd7f8107a9a9340c2.tar.gz
libquotient-c5f480355c7d4c000a4ee73fd7f8107a9a9340c2.zip
Move all internal event pointers to std::unique_ptr<>
This causes the following changes along the way: - Owning<> template is decommissioned. - event.h has been rearranged, and Event/RoomEvent::fromJson static methods have been replaced with an external makeEvent<> function template. A side effect of that is that one cannot use a factory with a type other than the one it's defined for (i.e. you cannot call makeEvent<TypingEvent>) but that feature has been out of use for long anyway. - Room::doAddNewMessageEvents() and Room::doAddHistoricalMessageEvents() have been removed, giving place to Room::onAddNewTimelineEvents() and Room::onAddHistoricalTimelineEvents(). The most important difference is that all code that must be executed now resides in addNewMessageEvents() (it moved from Room to Room::Private) and classes inheriting from Room are not obliged to call the overridden function from the overriding function (they can do it but those functions have empty bodies in Room). This was a long overdue change, and owning pointers simply mandated it. Room::onAddNewTimelineEvents/onAddHistoricalTimelineEvents should not do anything with the passed range in terms of ownership, it's just a way to allow the derived class to update his data in due course. - Room::Private::dropDuplicateEvents() and Room::Private::insertEvents(), notably, have been updated to work with owning pointers. insertEvents() move()s pointers to the timeline, while dropDuplicateEvents uses remove_if instead of stable_partition and doesn't explicitly delete event objects. Also, a bugfix: Event accidentally had not virtual destructor for quite a long time. According to the standard, deleting an object through a pointer to a base class without a virtual destructor leads to UB. So the fact that libqmatrixclient clients even worked all these months is mere coincidence and compiler authors good will :-D
Diffstat (limited to 'events/event.cpp')
-rw-r--r--events/event.cpp23
1 files changed, 13 insertions, 10 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()) };
}