aboutsummaryrefslogtreecommitdiff
path: root/events/event.h
diff options
context:
space:
mode:
Diffstat (limited to 'events/event.h')
-rw-r--r--events/event.h53
1 files changed, 38 insertions, 15 deletions
diff --git a/events/event.h b/events/event.h
index c151ac0e..020ef54b 100644
--- a/events/event.h
+++ b/events/event.h
@@ -56,9 +56,6 @@ namespace QMatrixClient
static Event* fromJson(const QJsonObject& obj);
protected:
- static QDateTime toTimestamp(const QJsonValue& v);
- static QStringList toStringList(const QJsonValue& v);
-
const QJsonObject contentJson() const;
private:
@@ -74,25 +71,20 @@ namespace QMatrixClient
using EventsBatch = std::vector<EventT*>;
using Events = EventsBatch<Event>;
- template <typename BaseEventT>
- BaseEventT* makeEvent(const QJsonObject& obj)
- {
- if (auto e = BaseEventT::fromJson(obj))
- return e;
-
- return new BaseEventT(EventType::Unknown, obj);
- }
-
template <typename BaseEventT = Event,
typename BatchT = EventsBatch<BaseEventT> >
- BatchT makeEvents(const QJsonArray& objs)
+ inline BatchT makeEvents(const QJsonArray& objs)
{
BatchT evs;
// The below line accommodates the difference in size types of
// STL and Qt containers.
evs.reserve(static_cast<typename BatchT::size_type>(objs.size()));
- for (auto obj: objs)
- evs.push_back(makeEvent<BaseEventT>(obj.toObject()));
+ for (auto objValue: objs)
+ {
+ const auto o = objValue.toObject();
+ auto e = BaseEventT::fromJson(o);
+ evs.push_back(e ? e : new BaseEventT(EventType::Unknown, o));
+ }
return evs;
}
@@ -147,6 +139,37 @@ namespace QMatrixClient
QString _txnId;
};
using RoomEvents = EventsBatch<RoomEvent>;
+
+ template <typename ContentT>
+ class StateEvent: public RoomEvent
+ {
+ public:
+ using content_type = ContentT;
+
+ template <typename... ContentParamTs>
+ explicit StateEvent(Type type, const QJsonObject& obj,
+ ContentParamTs&&... contentParams)
+ : RoomEvent(type, obj)
+ , _content(contentJson(),
+ std::forward<ContentParamTs>(contentParams)...)
+ , _prev(new ContentT(obj["prev_content"].toObject(),
+ std::forward<ContentParamTs>(contentParams)...))
+ { }
+ template <typename... ContentParamTs>
+ explicit StateEvent(Type type, ContentParamTs&&... contentParams)
+ : RoomEvent(type)
+ , _content(std::forward<ContentParamTs>(contentParams)...)
+ { }
+
+ QJsonObject toJson() const { return _content.toJson(); }
+
+ ContentT content() const { return _content; }
+ ContentT* prev_content() const { return _prev.data(); }
+
+ protected:
+ ContentT _content;
+ QScopedPointer<ContentT> _prev;
+ };
} // namespace QMatrixClient
Q_DECLARE_OPAQUE_POINTER(QMatrixClient::Event*)
Q_DECLARE_METATYPE(QMatrixClient::Event*)