diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2016-08-31 18:27:43 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2016-09-07 09:05:30 +0900 |
commit | 6aee89052be72b57fe4d93f5576bdf99293f5953 (patch) | |
tree | 75e8f5ea0d874b256af97578d19e0f2cf5ab393d /events | |
parent | ae309030049098c2e831991b0317214e3b4de598 (diff) | |
download | libquotient-6aee89052be72b57fe4d93f5576bdf99293f5953.tar.gz libquotient-6aee89052be72b57fe4d93f5576bdf99293f5953.zip |
Event::fromJson(): made the code more compact (correctly this time)
That might be not as efficient as a solution on variadic templates; but
arguably easier to understand. Doesn't have the typo bug the previous version of this commit had.
Diffstat (limited to 'events')
-rw-r--r-- | events/event.cpp | 55 |
1 files changed, 24 insertions, 31 deletions
diff --git a/events/event.cpp b/events/event.cpp index 0c7700ee..c7a94e9e 100644 --- a/events/event.cpp +++ b/events/event.cpp @@ -82,40 +82,33 @@ QString Event::originalJson() const return d->originalJson; } +template <typename T> +Event* make(const QJsonObject& obj) +{ + return T::fromJson(obj); +} + Event* Event::fromJson(const QJsonObject& obj) { - //qDebug() << obj.value("type").toString(); - if( obj.value("type").toString() == "m.room.message" ) - { - return RoomMessageEvent::fromJson(obj); - } - if( obj.value("type").toString() == "m.room.name" ) - { - return RoomNameEvent::fromJson(obj); - } - if( obj.value("type").toString() == "m.room.aliases" ) - { - return RoomAliasesEvent::fromJson(obj); - } - if( obj.value("type").toString() == "m.room.canonical_alias" ) - { - return RoomCanonicalAliasEvent::fromJson(obj); - } - if( obj.value("type").toString() == "m.room.member" ) - { - return RoomMemberEvent::fromJson(obj); - } - if( obj.value("type").toString() == "m.room.topic" ) - { - return RoomTopicEvent::fromJson(obj); - } - if( obj.value("type").toString() == "m.typing" ) - { - return TypingEvent::fromJson(obj); - } - if( obj.value("type").toString() == "m.receipt" ) + struct Factory { + QString type; + Event* (*make)(const QJsonObject& obj); + }; + const Factory evTypes[] { + { "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 types before this line + }; + for (auto e: evTypes) { - return ReceiptEvent::fromJson(obj); + if (obj["type"].toString() == e.type) + return e.make(obj); } return UnknownEvent::fromJson(obj); } |