diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2017-12-14 18:41:55 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2017-12-14 18:41:55 +0900 |
commit | c5f480355c7d4c000a4ee73fd7f8107a9a9340c2 (patch) | |
tree | d7a8f51e75230aa48cd243ef40b17ced70c0216b /room.h | |
parent | bb14969dfa5e9e8a26a005a7f804f21a62460f68 (diff) | |
download | libquotient-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 'room.h')
-rw-r--r-- | room.h | 36 |
1 files changed, 18 insertions, 18 deletions
@@ -45,25 +45,25 @@ namespace QMatrixClient { public: // For compatibility with Qt containers, even though we use - // a std:: container now + // a std:: container now for the room timeline using index_t = int; - TimelineItem(RoomEvent* e, index_t number) : evt(e), idx(number) { } + TimelineItem(RoomEventPtr&& e, index_t number) + : evt(move(e)), idx(number) { } RoomEvent* event() const { return evt.get(); } - RoomEvent* operator->() const { return event(); } //< Synonym for event()-> + RoomEvent* operator->() const { return evt.operator->(); } index_t index() const { return idx; } // Used for event redaction - RoomEvent* replaceEvent(RoomEvent* other) + RoomEventPtr replaceEvent(RoomEventPtr&& other) { - auto* old = evt.release(); - evt.reset(other); - return old; + evt.swap(other); + return move(other); } private: - std::unique_ptr<RoomEvent> evt; + RoomEventPtr evt; index_t idx; }; inline QDebug& operator<<(QDebug& d, const TimelineItem& ti) @@ -88,6 +88,7 @@ namespace QMatrixClient public: using Timeline = std::deque<TimelineItem>; using rev_iter_t = Timeline::const_reverse_iterator; + using timeline_iter_t = Timeline::const_iterator; Room(Connection* connection, QString id, JoinState initialJoinState); ~Room() override; @@ -188,8 +189,8 @@ namespace QMatrixClient void markAllMessagesAsRead(); signals: - void aboutToAddHistoricalMessages(RoomEventsView events); - void aboutToAddNewMessages(RoomEventsView events); + void aboutToAddHistoricalMessages(RoomEventsRange events); + void aboutToAddNewMessages(RoomEventsRange events); void addedMessages(); /** @@ -212,21 +213,20 @@ namespace QMatrixClient void lastReadEventChanged(User* user); void readMarkerMoved(); void unreadMessagesChanged(Room* room); - void replacedEvent(RoomEvent* before, RoomEvent* after); + void replacedEvent(const RoomEvent* newEvent, + const RoomEvent* oldEvent); protected: - virtual void doAddNewMessageEvents(RoomEventsView events); - virtual void doAddHistoricalMessageEvents(RoomEventsView events); virtual void processStateEvents(const RoomEvents& events); - virtual void processEphemeralEvent(Event* event); - virtual void onRedaction(RoomEvent*, TimelineItem&) { } + virtual void processEphemeralEvent(EventPtr event); + virtual void onAddNewTimelineEvents(timeline_iter_t from) { } + virtual void onAddHistoricalTimelineEvents(rev_iter_t from) { } + virtual void onRedaction(const RoomEvent* prevEvent, + const RoomEvent* after) { } private: class Private; Private* d; - - void addNewMessageEvents(RoomEvents events); - void addHistoricalMessageEvents(RoomEvents events); }; class MemberSorter |