aboutsummaryrefslogtreecommitdiff
path: root/lib/events
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2018-05-01 17:06:20 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2018-05-01 20:47:02 +0900
commitb657cd22aa64f24f2e0f9f31ef8a5d4e38a26a3a (patch)
tree5878e26a3719ace45d49ee08ccb2a99057426d1d /lib/events
parent5c61bb08e3fe87591884e0440a85d64482500199 (diff)
downloadlibquotient-b657cd22aa64f24f2e0f9f31ef8a5d4e38a26a3a.tar.gz
libquotient-b657cd22aa64f24f2e0f9f31ef8a5d4e38a26a3a.zip
Event and Room: further abstract event pointers
So that eventual switch from std::unique_ptr to some other pointer (as a case - QSharedPointer) would be as painless as possible.
Diffstat (limited to 'lib/events')
-rw-r--r--lib/events/event.cpp16
-rw-r--r--lib/events/event.h30
2 files changed, 35 insertions, 11 deletions
diff --git a/lib/events/event.cpp b/lib/events/event.cpp
index 31520fc9..193250de 100644
--- a/lib/events/event.cpp
+++ b/lib/events/event.cpp
@@ -67,16 +67,17 @@ const QJsonObject Event::contentJson() const
}
template <typename BaseEventT>
-inline BaseEventT* makeIfMatches(const QJsonObject&, const QString&)
+inline event_ptr_tt<BaseEventT> makeIfMatches(const QJsonObject&, const QString&)
{
return nullptr;
}
template <typename BaseEventT, typename EventT, typename... EventTs>
-inline BaseEventT* makeIfMatches(const QJsonObject& o, const QString& selector)
+inline event_ptr_tt<BaseEventT> makeIfMatches(const QJsonObject& o,
+ const QString& selector)
{
if (selector == EventT::TypeId)
- return new EventT(o);
+ return _impl::create<EventT>(o);
return makeIfMatches<BaseEventT, EventTs...>(o, selector);
}
@@ -86,11 +87,11 @@ EventPtr _impl::doMakeEvent<Event>(const QJsonObject& obj)
{
// Check more specific event types first
if (auto e = doMakeEvent<RoomEvent>(obj))
- return EventPtr(move(e));
+ return e;
- return EventPtr { makeIfMatches<Event,
+ return makeIfMatches<Event,
TypingEvent, ReceiptEvent, TagEvent, ReadMarkerEvent, DirectChatEvent>(
- obj, obj["type"].toString()) };
+ obj, obj["type"].toString());
}
RoomEvent::RoomEvent(Event::Type type) : Event(type) { }
@@ -118,8 +119,7 @@ RoomEvent::RoomEvent(Type type, const QJsonObject& rep)
auto redaction = unsignedData.value("redacted_because");
if (redaction.isObject())
{
- _redactedBecause =
- std::make_unique<RedactionEvent>(redaction.toObject());
+ _redactedBecause = _impl::create<RedactionEvent>(redaction.toObject());
return;
}
diff --git a/lib/events/event.h b/lib/events/event.h
index 4e2b1071..396406f1 100644
--- a/lib/events/event.h
+++ b/lib/events/event.h
@@ -26,8 +26,32 @@ namespace QMatrixClient
template <typename EventT>
using event_ptr_tt = std::unique_ptr<EventT>;
+ template <typename EventT>
+ inline EventT* rawPtr(const event_ptr_tt<EventT>& ptr)
+ {
+ return ptr.get();
+ }
+
+ template <typename TargetEventT, typename EventT>
+ inline TargetEventT* weakPtr(const event_ptr_tt<EventT>& ptr)
+ {
+ return static_cast<TargetEventT*>(rawPtr(ptr));
+ }
+
+ template <typename TargetT, typename SourceT>
+ inline event_ptr_tt<TargetT> ptrCast(event_ptr_tt<SourceT>&& ptr)
+ {
+ return unique_ptr_cast<TargetT>(ptr);
+ }
+
namespace _impl
{
+ template <typename EventT, typename... ArgTs>
+ inline event_ptr_tt<EventT> create(ArgTs&&... args)
+ {
+ return std::make_unique<EventT>(std::forward<ArgTs>(args)...);
+ }
+
template <typename EventT>
event_ptr_tt<EventT> doMakeEvent(const QJsonObject& obj);
}
@@ -94,7 +118,7 @@ namespace QMatrixClient
{
auto e = _impl::doMakeEvent<EventT>(obj);
if (!e)
- e = std::make_unique<EventT>(EventType::Unknown, obj);
+ e = _impl::create<EventT>(EventType::Unknown, obj);
return e;
}
@@ -174,9 +198,9 @@ namespace QMatrixClient
QString roomId() const;
QString senderId() const;
bool isRedacted() const { return bool(_redactedBecause); }
- const RedactionEvent* redactedBecause() const
+ const event_ptr_tt<RedactionEvent>& redactedBecause() const
{
- return _redactedBecause.get();
+ return _redactedBecause;
}
QString redactionReason() const;
const QString& transactionId() const { return _txnId; }