diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-07-11 18:14:39 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-07-11 18:14:39 +0900 |
commit | f7f42ab0f406b462e3e94bf4e806b9de22339c43 (patch) | |
tree | da512f7c6849c13d751fb9124d190819432fff7a /lib | |
parent | c76150cf77933ef83a7820fc53c22034873675b6 (diff) | |
download | libquotient-f7f42ab0f406b462e3e94bf4e806b9de22339c43.tar.gz libquotient-f7f42ab0f406b462e3e94bf4e806b9de22339c43.zip |
event.h: protect from readding factory methods
Diffstat (limited to 'lib')
-rw-r--r-- | lib/events/event.h | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/lib/events/event.h b/lib/events/event.h index 2a03f77a..a205b1b4 100644 --- a/lib/events/event.h +++ b/lib/events/event.h @@ -140,9 +140,10 @@ namespace QMatrixClient { public: template <typename FnT> - static void addMethod(FnT&& method) + static auto addMethod(FnT&& method) { factories().emplace_back(std::forward<FnT>(method)); + return 0; } /** Chain two type factories @@ -156,8 +157,7 @@ namespace QMatrixClient template <typename EventT> static auto chainFactory() { - addMethod(&EventT::factory_t::make); - return 0; + return addMethod(&EventT::factory_t::make); } static event_ptr_tt<BaseEventT> make(const QJsonObject& json, @@ -190,9 +190,10 @@ namespace QMatrixClient * \sa loadEvent, Event::type */ template <typename EventT> - inline void setupFactory() + inline auto setupFactory() { - EventT::factory_t::addMethod( + qDebug(EVENTS) << "Adding factory method for" << EventT::matrixTypeId(); + return EventT::factory_t::addMethod( [] (const QJsonObject& json, const QString& jsonMatrixType) { return EventT::matrixTypeId() == jsonMatrixType @@ -200,6 +201,13 @@ namespace QMatrixClient }); } + template <typename EventT> + inline auto registerEventType() + { + static const auto _ = setupFactory<EventT>(); + return _; + } + // === Event === class Event @@ -264,7 +272,7 @@ namespace QMatrixClient #define REGISTER_EVENT_TYPE(_Type) \ namespace { \ [[gnu::unused]] \ - static const auto _factoryAdded##_Type = ( setupFactory<_Type>(), 0); \ + static const auto _factoryAdded##_Type = registerEventType<_Type>(); \ } \ // End of macro |