From bb26ca86ad350f2562b51284e7c631b1e4f77106 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Thu, 18 May 2017 03:37:06 +0900 Subject: 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. --- events/event.cpp | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 'events/event.cpp') 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 -Event* make(const QJsonObject& obj) +template +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, - "m.room.name", make, - "m.room.aliases", make, - "m.room.canonical_alias", make, - "m.room.member", make, - "m.room.topic", make, - "m.typing", make, - "m.receipt", make, + return dispatch(obj).to(obj["type"].toString(), + "m.room.message", &make, + "m.room.name", &make, + "m.room.aliases", &make, + "m.room.canonical_alias", &make, + "m.room.member", &make, + "m.room.topic", &make, + "m.typing", &make, + "m.receipt", &make, /* Insert new event types BEFORE this line */ - make + &make ); - return delegate(obj); } bool Event::parseJson(const QJsonObject& obj) -- cgit v1.2.3