From 643aa92da416ab7b25c8b406a90007e4e7ebbb41 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sat, 16 Dec 2017 19:33:49 +0900 Subject: Fix an assertion failure when redacting an unknown event Closes #135. --- events/event.h | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) (limited to 'events/event.h') diff --git a/events/event.h b/events/event.h index a5730f14..71f2f4b2 100644 --- a/events/event.h +++ b/events/event.h @@ -32,13 +32,11 @@ namespace QMatrixClient template using event_ptr_tt = std::unique_ptr; - /** 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 - event_ptr_tt makeEvent(const QJsonObject& obj); + namespace _impl + { + template + event_ptr_tt doMakeEvent(const QJsonObject& obj); + } class Event { @@ -92,8 +90,25 @@ namespace QMatrixClient using EventType = Event::Type; using EventPtr = event_ptr_tt; - template <> - EventPtr makeEvent(const QJsonObject& obj); + /** 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 + event_ptr_tt makeEvent(const QJsonObject& obj) + { + auto e = _impl::doMakeEvent(obj); + if (!e) + e.reset(new EventT(EventType::Unknown, obj)); + return e; + } + + namespace _impl + { + template <> + EventPtr doMakeEvent(const QJsonObject& obj); + } /** * \brief A vector of pointers to events with deserialisation capabilities @@ -130,13 +145,7 @@ namespace QMatrixClient // STL and Qt containers. this->reserve(static_cast(objs.size())); for (auto objValue: objs) - { - const auto o = objValue.toObject(); - auto&& e = makeEvent(o); - if (!e) - e.reset(new EventT(EventType::Unknown, o)); - this->emplace_back(std::move(e)); - } + this->emplace_back(makeEvent(objValue.toObject())); } }; using Events = EventsBatch; @@ -205,8 +214,11 @@ namespace QMatrixClient using RoomEvents = EventsBatch; using RoomEventPtr = event_ptr_tt; - template <> - RoomEventPtr makeEvent(const QJsonObject& obj); + namespace _impl + { + template <> + RoomEventPtr doMakeEvent(const QJsonObject& obj); + } /** * Conceptually similar to QStringView (but much more primitive), it's a -- cgit v1.2.3