aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2018-07-11 18:16:39 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2018-07-11 18:16:39 +0900
commit1ffb27f506ed1a4b3766768b872a86af7799a39e (patch)
treeb87c0b36d5da179bc41182598c1253986189de58
parentd714e47dd713d89d1581dbad1a8e751fcf5fa2e4 (diff)
downloadlibquotient-1ffb27f506ed1a4b3766768b872a86af7799a39e.tar.gz
libquotient-1ffb27f506ed1a4b3766768b872a86af7799a39e.zip
EventFactory::make() should return nullptr in case of failure
Otherwise factory chaining doesn't work right (an unknown event returned by a chained factory is treated as successful parsing).
-rw-r--r--lib/events/event.h2
-rw-r--r--lib/events/eventloader.h19
2 files changed, 16 insertions, 5 deletions
diff --git a/lib/events/event.h b/lib/events/event.h
index a205b1b4..8eb4ee88 100644
--- a/lib/events/event.h
+++ b/lib/events/event.h
@@ -166,7 +166,7 @@ namespace QMatrixClient
for (const auto& f: factories())
if (auto e = f(json, matrixType))
return e;
- return makeEvent<BaseEventT>(unknownEventTypeId(), json);
+ return nullptr;
}
private:
diff --git a/lib/events/eventloader.h b/lib/events/eventloader.h
index 5f1e3b57..ea261cac 100644
--- a/lib/events/eventloader.h
+++ b/lib/events/eventloader.h
@@ -22,6 +22,17 @@
#include "converters.h"
namespace QMatrixClient {
+ namespace _impl {
+ template <typename BaseEventT>
+ static inline auto loadEvent(const QJsonObject& json,
+ const QString& matrixType)
+ {
+ if (auto e = EventFactory<BaseEventT>::make(json, matrixType))
+ return e;
+ return makeEvent<BaseEventT>(unknownEventTypeId(), json);
+ }
+ }
+
/** Create an event with proper type from a JSON object
* Use this factory template to detect the type from the JSON object
* contents (the detected event type should derive from the template
@@ -30,8 +41,8 @@ namespace QMatrixClient {
template <typename BaseEventT>
inline event_ptr_tt<BaseEventT> loadEvent(const QJsonObject& fullJson)
{
- return EventFactory<BaseEventT>
- ::make(fullJson, fullJson[TypeKeyL].toString());
+ return _impl::loadEvent<BaseEventT>(fullJson,
+ fullJson[TypeKeyL].toString());
}
/** Create an event from a type string and content JSON
@@ -43,8 +54,8 @@ namespace QMatrixClient {
inline event_ptr_tt<BaseEventT> loadEvent(const QString& matrixType,
const QJsonObject& content)
{
- return EventFactory<BaseEventT>
- ::make(basicEventJson(matrixType, content), matrixType);
+ return _impl::loadEvent<BaseEventT>(basicEventJson(matrixType, content),
+ matrixType);
}
template <typename EventT> struct FromJson<event_ptr_tt<EventT>>