From f7f42ab0f406b462e3e94bf4e806b9de22339c43 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Wed, 11 Jul 2018 18:14:39 +0900 Subject: event.h: protect from readding factory methods --- lib/events/event.h | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'lib/events/event.h') 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 - static void addMethod(FnT&& method) + static auto addMethod(FnT&& method) { factories().emplace_back(std::forward(method)); + return 0; } /** Chain two type factories @@ -156,8 +157,7 @@ namespace QMatrixClient template static auto chainFactory() { - addMethod(&EventT::factory_t::make); - return 0; + return addMethod(&EventT::factory_t::make); } static event_ptr_tt make(const QJsonObject& json, @@ -190,9 +190,10 @@ namespace QMatrixClient * \sa loadEvent, Event::type */ template - 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 + inline auto registerEventType() + { + static const auto _ = setupFactory(); + 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 -- cgit v1.2.3