aboutsummaryrefslogtreecommitdiff
path: root/events
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2017-05-18 03:37:06 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2017-05-22 09:39:56 +0900
commitbb26ca86ad350f2562b51284e7c631b1e4f77106 (patch)
tree8fe867870d8e8a8488ece979634035109e39e5eb /events
parentced7a66686596e74a1f25b5d9634b9b562870943 (diff)
downloadlibquotient-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')
-rw-r--r--events/event.cpp27
-rw-r--r--events/roommessageevent.cpp20
2 files changed, 23 insertions, 24 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)
diff --git a/events/roommessageevent.cpp b/events/roommessageevent.cpp
index 677bb79f..d5e960a1 100644
--- a/events/roommessageevent.cpp
+++ b/events/roommessageevent.cpp
@@ -120,17 +120,17 @@ RoomMessageEvent* RoomMessageEvent::fromJson(const QJsonObject& obj)
{
e->d->plainBody = content["body"].toString();
- auto delegate = lookup(content.value("msgtype").toString(),
- "m.text", make<MessageEventType::Text, TextContent>,
- "m.emote", make<MessageEventType::Emote, TextContent>,
- "m.notice", make<MessageEventType::Notice, TextContent>,
- "m.image", make<MessageEventType::Image, ImageContent>,
- "m.file", make<MessageEventType::File, FileContent>,
- "m.location", make<MessageEventType::Location, LocationContent>,
- "m.video", makeVideo,
- "m.audio", make<MessageEventType::Audio, AudioContent>,
+ auto delegate = lookup(content["msgtype"].toString(),
+ "m.text", &make<MessageEventType::Text, TextContent>,
+ "m.emote", &make<MessageEventType::Emote, TextContent>,
+ "m.notice", &make<MessageEventType::Notice, TextContent>,
+ "m.image", &make<MessageEventType::Image, ImageContent>,
+ "m.file", &make<MessageEventType::File, FileContent>,
+ "m.location", &make<MessageEventType::Location, LocationContent>,
+ "m.video", &makeVideo,
+ "m.audio", &make<MessageEventType::Audio, AudioContent>,
// Insert new message types before this line
- makeUnknown
+ &makeUnknown
);
std::tie(e->d->msgtype, e->d->content) = delegate(content);
}