diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2017-05-18 03:37:06 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2017-05-22 09:39:56 +0900 |
commit | bb26ca86ad350f2562b51284e7c631b1e4f77106 (patch) | |
tree | 8fe867870d8e8a8488ece979634035109e39e5eb /events/event.cpp | |
parent | ced7a66686596e74a1f25b5d9634b9b562870943 (diff) | |
download | libquotient-bb26ca86ad350f2562b51284e7c631b1e4f77106.tar.gz libquotient-bb26ca86ad350f2562b51284e7c631b1e4f77106.zip |
util.h: lookup() uses forwarding refs; added Dispatch/dispatch and REGISTER_ENUM
The Dispatch<> template and dispatch(), a facility function for it, simplify dispatching to functions that have different signatures that still can be converted to the same std::function<> type. The case in point is in event.cpp; Event::fromJson calls make() that always returns the type we need; however, once we have several possible base types (Event, RoomEvent, StateEvent), we'd have to either write a specific make() incarnation for each of them, or mess with function return type conversions. Dispatch<> helps to keep the code clean.
REGISTER_ENUM is a cross-Qt versions approach to dumping enumeration values to qDebug() and the likes.
Diffstat (limited to 'events/event.cpp')
-rw-r--r-- | events/event.cpp | 27 |
1 files changed, 13 insertions, 14 deletions
diff --git a/events/event.cpp b/events/event.cpp index 07649b02..5df816fe 100644 --- a/events/event.cpp +++ b/events/event.cpp @@ -87,27 +87,26 @@ QString Event::originalJson() const return d->originalJson; } -template <typename T> -Event* make(const QJsonObject& obj) +template <typename EventT> +EventT* make(const QJsonObject& obj) { - return T::fromJson(obj); + return EventT::fromJson(obj); } Event* Event::fromJson(const QJsonObject& obj) { - auto delegate = lookup(obj.value("type").toString(), - "m.room.message", make<RoomMessageEvent>, - "m.room.name", make<RoomNameEvent>, - "m.room.aliases", make<RoomAliasesEvent>, - "m.room.canonical_alias", make<RoomCanonicalAliasEvent>, - "m.room.member", make<RoomMemberEvent>, - "m.room.topic", make<RoomTopicEvent>, - "m.typing", make<TypingEvent>, - "m.receipt", make<ReceiptEvent>, + return dispatch<Event*>(obj).to(obj["type"].toString(), + "m.room.message", &make<RoomMessageEvent>, + "m.room.name", &make<RoomNameEvent>, + "m.room.aliases", &make<RoomAliasesEvent>, + "m.room.canonical_alias", &make<RoomCanonicalAliasEvent>, + "m.room.member", &make<RoomMemberEvent>, + "m.room.topic", &make<RoomTopicEvent>, + "m.typing", &make<TypingEvent>, + "m.receipt", &make<ReceiptEvent>, /* Insert new event types BEFORE this line */ - make<UnknownEvent> + &make<UnknownEvent> ); - return delegate(obj); } bool Event::parseJson(const QJsonObject& obj) |