aboutsummaryrefslogtreecommitdiff
path: root/lib/events
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2018-05-01 21:01:33 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2018-05-01 21:01:33 +0900
commit1e42eca5c3d864750609eacd4996794c901c6c37 (patch)
tree88a2ca014bfd38bdbd2c30cd3918fc49dd7a7255 /lib/events
parent38934c2310b426be640988dc10f48de88a3d92bc (diff)
parent2a341e30ef2db74b331a8870ceb2f182af68f194 (diff)
downloadlibquotient-1e42eca5c3d864750609eacd4996794c901c6c37.tar.gz
libquotient-1e42eca5c3d864750609eacd4996794c901c6c37.zip
Merge branch 'master' into kitsune-gtad
Diffstat (limited to 'lib/events')
-rw-r--r--lib/events/event.cpp20
-rw-r--r--lib/events/event.h51
-rw-r--r--lib/events/redactionevent.h2
3 files changed, 46 insertions, 27 deletions
diff --git a/lib/events/event.cpp b/lib/events/event.cpp
index 8ddf3945..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) { }
@@ -98,10 +99,6 @@ RoomEvent::RoomEvent(Event::Type type) : Event(type) { }
RoomEvent::RoomEvent(Type type, const QJsonObject& rep)
: Event(type, rep)
, _id(rep["event_id"].toString())
-// , _roomId(rep["room_id"].toString())
-// , _senderId(rep["sender"].toString())
-// , _serverTimestamp(
-// QMatrixClient::fromJson<QDateTime>(rep["origin_server_ts"]))
{
// if (_id.isEmpty())
// {
@@ -122,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 d614115a..396406f1 100644
--- a/lib/events/event.h
+++ b/lib/events/event.h
@@ -18,22 +18,40 @@
#pragma once
-#include <QtCore/QString>
-#include <QtCore/QDateTime>
-#include <QtCore/QJsonObject>
-#include <QtCore/QJsonArray>
-
+#include "converters.h"
#include "util.h"
-#include <memory>
-
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);
}
@@ -100,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;
}
@@ -110,6 +128,14 @@ namespace QMatrixClient
EventPtr doMakeEvent<Event>(const QJsonObject& obj);
}
+ template <> struct FromJson<EventPtr>
+ {
+ EventPtr operator()(const QJsonValue& jv) const
+ {
+ return makeEvent<Event>(jv.toObject());
+ }
+ };
+
/**
* \brief A vector of pointers to events with deserialisation capabilities
*
@@ -165,16 +191,16 @@ namespace QMatrixClient
// constructors and destructors
explicit RoomEvent(Type type);
RoomEvent(Type type, const QJsonObject& rep);
- ~RoomEvent();
+ ~RoomEvent() override;
QString id() const { return _id; }
QDateTime timestamp() const;
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; }
@@ -202,9 +228,6 @@ namespace QMatrixClient
private:
QString _id;
-// QString _roomId;
-// QString _senderId;
-// QDateTime _serverTimestamp;
event_ptr_tt<RedactionEvent> _redactedBecause;
QString _txnId;
};
diff --git a/lib/events/redactionevent.h b/lib/events/redactionevent.h
index fa6902ab..829b9085 100644
--- a/lib/events/redactionevent.h
+++ b/lib/events/redactionevent.h
@@ -27,7 +27,7 @@ namespace QMatrixClient
public:
static constexpr const char* const TypeId = "m.room.redaction";
- RedactionEvent(const QJsonObject& obj)
+ explicit RedactionEvent(const QJsonObject& obj)
: RoomEvent(Type::Redaction, obj)
, _redactedEvent(obj.value("redacts").toString())
, _reason(contentJson().value("reason").toString())