From aacc4bcb4a487871daae6717f77605aaba444341 Mon Sep 17 00:00:00 2001 From: Marc Deop Date: Sat, 2 Mar 2019 12:26:57 +0100 Subject: style: apply .clang-format to all .cpp and .h files --- lib/events/event.h | 330 +++++++++++++++++++++++++---------------------------- 1 file changed, 158 insertions(+), 172 deletions(-) (limited to 'lib/events/event.h') diff --git a/lib/events/event.h b/lib/events/event.h index d7ac4292..a0f12b75 100644 --- a/lib/events/event.h +++ b/lib/events/event.h @@ -13,7 +13,7 @@ * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #pragma once @@ -25,12 +25,10 @@ #define USE_EVENTTYPE_ALIAS 1 #endif -namespace QMatrixClient -{ +namespace QMatrixClient { // === event_ptr_tt<> and type casting facilities === - template - using event_ptr_tt = std::unique_ptr; + template using event_ptr_tt = std::unique_ptr; template inline EventT* rawPtr(const event_ptr_tt& ptr) // unwrap @@ -80,50 +78,46 @@ namespace QMatrixClient class EventTypeRegistry { public: - ~EventTypeRegistry() = default; + ~EventTypeRegistry() = default; - static event_type_t initializeTypeId(event_mtype_t matrixTypeId); + static event_type_t initializeTypeId(event_mtype_t matrixTypeId); - template - static inline event_type_t initializeTypeId() - { - return initializeTypeId(EventT::matrixTypeId()); - } + template static inline event_type_t initializeTypeId() + { + return initializeTypeId(EventT::matrixTypeId()); + } - static QString getMatrixType(event_type_t typeId); + static QString getMatrixType(event_type_t typeId); private: - EventTypeRegistry() = default; - Q_DISABLE_COPY(EventTypeRegistry) - DISABLE_MOVE(EventTypeRegistry) + EventTypeRegistry() = default; + Q_DISABLE_COPY(EventTypeRegistry) + DISABLE_MOVE(EventTypeRegistry) - static EventTypeRegistry& get() - { - static EventTypeRegistry etr; - return etr; - } + static EventTypeRegistry& get() + { + static EventTypeRegistry etr; + return etr; + } - std::vector eventTypes; + std::vector eventTypes; }; - template <> - inline event_type_t EventTypeRegistry::initializeTypeId() + template <> inline event_type_t EventTypeRegistry::initializeTypeId() { return initializeTypeId(""); } - template - struct EventTypeTraits - { + template struct EventTypeTraits { static event_type_t id() { - static const auto id = EventTypeRegistry::initializeTypeId(); + static const auto id = + EventTypeRegistry::initializeTypeId(); return id; } }; - template - inline event_type_t typeId() + template inline event_type_t typeId() { return EventTypeTraits>::id(); } @@ -139,49 +133,45 @@ namespace QMatrixClient return std::make_unique(std::forward(args)...); } - template - class EventFactory + template class EventFactory { public: - template - static auto addMethod(FnT&& method) - { - factories().emplace_back(std::forward(method)); - return 0; - } - - /** Chain two type factories - * Adds the factory class of EventT2 (EventT2::factory_t) to - * the list in factory class of EventT1 (EventT1::factory_t) so - * that when EventT1::factory_t::make() is invoked, types of - * EventT2 factory are looked through as well. This is used - * to include RoomEvent types into the more general Event factory, - * and state event types into the RoomEvent factory. - */ - template - static auto chainFactory() - { - return addMethod(&EventT::factory_t::make); - } - - static event_ptr_tt make(const QJsonObject& json, - const QString& matrixType) - { - for (const auto& f: factories()) - if (auto e = f(json, matrixType)) - return e; - return nullptr; - } + template static auto addMethod(FnT&& method) + { + factories().emplace_back(std::forward(method)); + return 0; + } + + /** Chain two type factories + * Adds the factory class of EventT2 (EventT2::factory_t) to + * the list in factory class of EventT1 (EventT1::factory_t) so + * that when EventT1::factory_t::make() is invoked, types of + * EventT2 factory are looked through as well. This is used + * to include RoomEvent types into the more general Event factory, + * and state event types into the RoomEvent factory. + */ + template static auto chainFactory() + { + return addMethod(&EventT::factory_t::make); + } + + static event_ptr_tt make(const QJsonObject& json, + const QString& matrixType) + { + for (const auto& f : factories()) + if (auto e = f(json, matrixType)) + return e; + return nullptr; + } private: - static auto& factories() - { - using inner_factory_tt = - std::function(const QJsonObject&, - const QString&)>; - static std::vector _factories {}; - return _factories; - } + static auto& factories() + { + using inner_factory_tt = std::function( + const QJsonObject&, const QString&)>; + static std::vector _factories {}; + return _factories; + } }; /** Add a type to its default factory @@ -193,20 +183,18 @@ namespace QMatrixClient * \return the registered type id * \sa loadEvent, Event::type */ - template - inline auto setupFactory() + template inline auto setupFactory() { qDebug(EVENTS) << "Adding factory method for" << EventT::matrixTypeId(); return EventT::factory_t::addMethod( - [] (const QJsonObject& json, const QString& jsonMatrixType) - { - return EventT::matrixTypeId() == jsonMatrixType - ? makeEvent(json) : nullptr; - }); + [](const QJsonObject& json, const QString& jsonMatrixType) { + return EventT::matrixTypeId() == jsonMatrixType + ? makeEvent(json) + : nullptr; + }); } - template - inline auto registerEventType() + template inline auto registerEventType() { // Initialise exactly once, even if this function is called twice for // the same type (for whatever reason - you never know the ways of @@ -219,66 +207,64 @@ namespace QMatrixClient class Event { - Q_GADGET - Q_PROPERTY(Type type READ type CONSTANT) - Q_PROPERTY(QJsonObject contentJson READ contentJson CONSTANT) + Q_GADGET + Q_PROPERTY(Type type READ type CONSTANT) + Q_PROPERTY(QJsonObject contentJson READ contentJson CONSTANT) public: - using Type = event_type_t; - using factory_t = EventFactory; - - explicit Event(Type type, const QJsonObject& json); - explicit Event(Type type, event_mtype_t matrixType, - const QJsonObject& contentJson = {}); - Q_DISABLE_COPY(Event) - Event(Event&&) = default; - Event& operator=(Event&&) = delete; - virtual ~Event(); - - Type type() const { return _type; } - QString matrixType() const; - QByteArray originalJson() const; - QJsonObject originalJsonObject() const { return fullJson(); } - - const QJsonObject& fullJson() const { return _json; } - - // According to the CS API spec, every event also has - // a "content" object; but since its structure is different for - // different types, we're implementing it per-event type. - - const QJsonObject contentJson() const; - const QJsonObject unsignedJson() const; - - template - T content(const QString& key) const - { - return fromJson(contentJson()[key]); - } - - template - T content(const QLatin1String& key) const - { - return fromJson(contentJson()[key]); - } - - friend QDebug operator<<(QDebug dbg, const Event& e) - { - QDebugStateSaver _dss { dbg }; - dbg.noquote().nospace() - << e.matrixType() << '(' << e.type() << "): "; - e.dumpTo(dbg); - return dbg; - } - - virtual bool isStateEvent() const { return false; } - virtual bool isCallEvent() const { return false; } - virtual void dumpTo(QDebug dbg) const; + using Type = event_type_t; + using factory_t = EventFactory; + + explicit Event(Type type, const QJsonObject& json); + explicit Event(Type type, event_mtype_t matrixType, + const QJsonObject& contentJson = {}); + Q_DISABLE_COPY(Event) + Event(Event&&) = default; + Event& operator=(Event&&) = delete; + virtual ~Event(); + + Type type() const { return _type; } + QString matrixType() const; + QByteArray originalJson() const; + QJsonObject originalJsonObject() const { return fullJson(); } + + const QJsonObject& fullJson() const { return _json; } + + // According to the CS API spec, every event also has + // a "content" object; but since its structure is different for + // different types, we're implementing it per-event type. + + const QJsonObject contentJson() const; + const QJsonObject unsignedJson() const; + + template T content(const QString& key) const + { + return fromJson(contentJson()[key]); + } + + template T content(const QLatin1String& key) const + { + return fromJson(contentJson()[key]); + } + + friend QDebug operator<<(QDebug dbg, const Event& e) + { + QDebugStateSaver _dss { dbg }; + dbg.noquote().nospace() + << e.matrixType() << '(' << e.type() << "): "; + e.dumpTo(dbg); + return dbg; + } + + virtual bool isStateEvent() const { return false; } + virtual bool isCallEvent() const { return false; } + virtual void dumpTo(QDebug dbg) const; protected: - QJsonObject& editJson() { return _json; } + QJsonObject& editJson() { return _json; } private: - Type _type; - QJsonObject _json; + Type _type; + QJsonObject _json; }; using EventPtr = event_ptr_tt; @@ -290,24 +276,23 @@ namespace QMatrixClient // This macro should be used in a public section of an event class to // provide matrixTypeId() and typeId(). -#define DEFINE_EVENT_TYPEID(_Id, _Type) \ - static constexpr event_mtype_t matrixTypeId() { return _Id; } \ - static auto typeId() { return QMatrixClient::typeId<_Type>(); } \ +#define DEFINE_EVENT_TYPEID(_Id, _Type) \ + static constexpr event_mtype_t matrixTypeId() { return _Id; } \ + static auto typeId() { return QMatrixClient::typeId<_Type>(); } \ // End of macro // This macro should be put after an event class definition (in .h or .cpp) // to enable its deserialisation from a /sync and other // polymorphic event arrays -#define REGISTER_EVENT_TYPE(_Type) \ - namespace { \ - [[gnu::unused]] \ - static const auto _factoryAdded##_Type = registerEventType<_Type>(); \ - } \ +#define REGISTER_EVENT_TYPE(_Type) \ + namespace { \ + [[gnu::unused]] static const auto _factoryAdded##_Type = \ + registerEventType<_Type>(); \ + } \ // End of macro #ifdef USE_EVENTTYPE_ALIAS - namespace EventType - { + namespace EventType { inline event_type_t logEventType(event_type_t id, const char* idName) { qDebug(EVENTS) << "Using id" << id << "for" << idName; @@ -317,12 +302,12 @@ namespace QMatrixClient // This macro provides constants in EventType:: namespace for // back-compatibility with libQMatrixClient 0.3 event type system. -#define DEFINE_EVENTTYPE_ALIAS(_Id, _Type) \ - namespace EventType \ - { \ - [[deprecated("Use is<>(), eventCast<>() or visit<>()")]] \ - static const auto _Id = logEventType(typeId<_Type>(), #_Id); \ - } \ +#define DEFINE_EVENTTYPE_ALIAS(_Id, _Type) \ + namespace EventType { \ + [[deprecated( \ + "Use is<>(), eventCast<>() or visit<>()")]] static const auto \ + _Id = logEventType(typeId<_Type>(), #_Id); \ + } \ // End of macro #else #define DEFINE_EVENTTYPE_ALIAS(_Id, _Type) // Nothing @@ -330,46 +315,48 @@ namespace QMatrixClient // === is<>(), eventCast<>() and visit<>() === - template - inline bool is(const Event& e) { return e.type() == typeId(); } + template inline bool is(const Event& e) + { + return e.type() == typeId(); + } - inline bool isUnknown(const Event& e) { return e.type() == unknownEventTypeId(); } + inline bool isUnknown(const Event& e) + { + return e.type() == unknownEventTypeId(); + } template inline auto eventCast(const BasePtrT& eptr) - -> decltype(static_cast(&*eptr)) + -> decltype(static_cast(&*eptr)) { Q_ASSERT(eptr); - return is>(*eptr) - ? static_cast(&*eptr) : nullptr; + return is>(*eptr) ? static_cast(&*eptr) + : nullptr; } // A single generic catch-all visitor template inline auto visit(const BaseEventT& event, FnT&& visitor) - -> decltype(visitor(event)) + -> decltype(visitor(event)) { return visitor(event); } - template - constexpr auto is_event() + template constexpr auto is_event() { return std::is_base_of>::value; } - template - constexpr auto needs_cast() + template constexpr auto needs_cast() { return !std::is_convertible>::value; } // A single type-specific void visitor template - inline - std::enable_if_t< - is_event() && needs_cast() && - std::is_void>::value> + inline std::enable_if_t() + && needs_cast() + && std::is_void>::value> visit(const BaseEventT& event, FnT&& visitor) { using event_type = fn_arg_t; @@ -379,10 +366,9 @@ namespace QMatrixClient // A single type-specific non-void visitor with an optional default value template - inline - std::enable_if_t< - is_event() && needs_cast(), - fn_return_t> // non-voidness is guarded by defaultValue type + inline std::enable_if_t< + is_event() && needs_cast(), + fn_return_t> // non-voidness is guarded by defaultValue type visit(const BaseEventT& event, FnT&& visitor, fn_return_t&& defaultValue = {}) { @@ -393,9 +379,9 @@ namespace QMatrixClient } // A chain of 2 or more visitors - template - inline - std::enable_if_t(), fn_return_t> + template + inline std::enable_if_t(), fn_return_t> visit(const BaseEventT& event, FnT1&& visitor1, FnT2&& visitor2, FnTs&&... visitors) { @@ -405,6 +391,6 @@ namespace QMatrixClient return visit(event, std::forward(visitor2), std::forward(visitors)...); } -} // namespace QMatrixClient +} // namespace QMatrixClient Q_DECLARE_METATYPE(QMatrixClient::Event*) Q_DECLARE_METATYPE(const QMatrixClient::Event*) -- cgit v1.2.3 From 7374cef74ab92dd2165831c73db51c7dfa4a3511 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sat, 18 May 2019 21:48:04 +0900 Subject: event.h: add doc-comments; deprecate ptrCast() --- lib/events/event.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lib/events/event.h') diff --git a/lib/events/event.h b/lib/events/event.h index d7ac4292..b7bbd83e 100644 --- a/lib/events/event.h +++ b/lib/events/event.h @@ -32,19 +32,23 @@ namespace QMatrixClient template using event_ptr_tt = std::unique_ptr; + /// Unwrap a plain pointer from a smart pointer template - inline EventT* rawPtr(const event_ptr_tt& ptr) // unwrap + inline EventT* rawPtr(const event_ptr_tt& ptr) { return ptr.get(); } + /// Unwrap a plain pointer and downcast it to the specified type template inline TargetEventT* weakPtrCast(const event_ptr_tt& ptr) { return static_cast(rawPtr(ptr)); } + /// Re-wrap a smart pointer to base into a smart pointer to derived template + [[deprecated("Consider using eventCast() or visit() instead")]] inline event_ptr_tt ptrCast(event_ptr_tt&& ptr) { return unique_ptr_cast(ptr); -- cgit v1.2.3 From 5b8079ca3d35360b7d98814654f37885f21bcb7e Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sat, 6 Jul 2019 19:53:23 +0900 Subject: Fix clazy warnings --- lib/events/event.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/events/event.h') diff --git a/lib/events/event.h b/lib/events/event.h index b7bbd83e..5248472c 100644 --- a/lib/events/event.h +++ b/lib/events/event.h @@ -259,7 +259,7 @@ namespace QMatrixClient } template - T content(const QLatin1String& key) const + T content(QLatin1String key) const { return fromJson(contentJson()[key]); } -- cgit v1.2.3 From 01cce0e39d255cbcf39f6a1aa58c6d7ab1d995d1 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sat, 6 Jul 2019 19:58:29 +0900 Subject: Convenience: StateKeyKey, StateKeyKeyL, basicStateEventJson() --- lib/events/event.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lib/events/event.h') diff --git a/lib/events/event.h b/lib/events/event.h index 5248472c..b3a58806 100644 --- a/lib/events/event.h +++ b/lib/events/event.h @@ -60,14 +60,16 @@ namespace QMatrixClient static const auto ContentKey = QStringLiteral("content"); static const auto EventIdKey = QStringLiteral("event_id"); static const auto UnsignedKey = QStringLiteral("unsigned"); + static const auto StateKeyKey = QStringLiteral("state_key"); static const auto TypeKeyL = "type"_ls; static const auto ContentKeyL = "content"_ls; static const auto EventIdKeyL = "event_id"_ls; static const auto UnsignedKeyL = "unsigned"_ls; static const auto RedactedCauseKeyL = "redacted_because"_ls; static const auto PrevContentKeyL = "prev_content"_ls; + static const auto StateKeyKeyL = "state_key"_ls; - // Minimal correct Matrix event JSON + /// Make a minimal correct Matrix event JSON template inline QJsonObject basicEventJson(StrT matrixType, const QJsonObject& content) -- cgit v1.2.3 From 2737dc00334ad3a56c1b311435dbe84453ee389e Mon Sep 17 00:00:00 2001 From: Alexey Andreyev Date: Sun, 14 Jul 2019 03:54:19 +0300 Subject: E2EE: introduce EncryptedEvent --- lib/events/event.h | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/events/event.h') diff --git a/lib/events/event.h b/lib/events/event.h index b3a58806..6f28c4fa 100644 --- a/lib/events/event.h +++ b/lib/events/event.h @@ -62,6 +62,7 @@ namespace QMatrixClient static const auto UnsignedKey = QStringLiteral("unsigned"); static const auto StateKeyKey = QStringLiteral("state_key"); static const auto TypeKeyL = "type"_ls; + static const auto BodyKeyL = "body"_ls; static const auto ContentKeyL = "content"_ls; static const auto EventIdKeyL = "event_id"_ls; static const auto UnsignedKeyL = "unsigned"_ls; -- cgit v1.2.3 From c05ade838f0fce81f2bbe80a3295618a8a26ff52 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 2 Aug 2019 19:59:40 +0900 Subject: Apply the new brace wrapping to source files --- lib/events/event.h | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) (limited to 'lib/events/event.h') diff --git a/lib/events/event.h b/lib/events/event.h index d6525281..686bd8e0 100644 --- a/lib/events/event.h +++ b/lib/events/event.h @@ -25,8 +25,7 @@ # define USE_EVENTTYPE_ALIAS 1 #endif -namespace QMatrixClient -{ +namespace QMatrixClient { // === event_ptr_tt<> and type casting facilities === template @@ -85,8 +84,7 @@ inline QJsonObject basicEventJson(StrT matrixType, const QJsonObject& content) using event_type_t = size_t; using event_mtype_t = const char*; -class EventTypeRegistry -{ +class EventTypeRegistry { public: ~EventTypeRegistry() = default; @@ -121,8 +119,7 @@ inline event_type_t EventTypeRegistry::initializeTypeId() } template -struct EventTypeTraits -{ +struct EventTypeTraits { static event_type_t id() { static const auto id = EventTypeRegistry::initializeTypeId(); @@ -148,8 +145,7 @@ inline event_ptr_tt makeEvent(ArgTs&&... args) } template -class EventFactory -{ +class EventFactory { public: template static auto addMethod(FnT&& method) @@ -223,8 +219,7 @@ inline auto registerEventType() // === Event === -class Event -{ +class Event { Q_GADGET Q_PROPERTY(Type type READ type CONSTANT) Q_PROPERTY(QJsonObject contentJson READ contentJson CONSTANT) @@ -304,16 +299,14 @@ using Events = EventsArray; // to enable its deserialisation from a /sync and other // polymorphic event arrays #define REGISTER_EVENT_TYPE(_Type) \ - namespace \ - { \ + namespace { \ [[gnu::unused]] static const auto _factoryAdded##_Type = \ registerEventType<_Type>(); \ } \ // End of macro #ifdef USE_EVENTTYPE_ALIAS -namespace EventType -{ +namespace EventType { inline event_type_t logEventType(event_type_t id, const char* idName) { qDebug(EVENTS) << "Using id" << id << "for" << idName; @@ -324,8 +317,7 @@ namespace EventType // This macro provides constants in EventType:: namespace for // back-compatibility with libQMatrixClient 0.3 event type system. # define DEFINE_EVENTTYPE_ALIAS(_Id, _Type) \ - namespace EventType \ - { \ + namespace EventType { \ [[deprecated("Use is<>(), eventCast<>() or " \ "visit<>()")]] static const auto _Id = \ logEventType(typeId<_Type>(), #_Id); \ -- cgit v1.2.3 From 27ca32a1e5a56e09b9cc1d94224d2831004dcf3d Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 7 Jul 2019 19:32:34 +0900 Subject: Namespace: QMatrixClient -> Quotient (with back comp alias) --- lib/events/event.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'lib/events/event.h') diff --git a/lib/events/event.h b/lib/events/event.h index 686bd8e0..e96d6897 100644 --- a/lib/events/event.h +++ b/lib/events/event.h @@ -25,7 +25,7 @@ # define USE_EVENTTYPE_ALIAS 1 #endif -namespace QMatrixClient { +namespace Quotient { // === event_ptr_tt<> and type casting facilities === template @@ -292,7 +292,7 @@ using Events = EventsArray; // provide matrixTypeId() and typeId(). #define DEFINE_EVENT_TYPEID(_Id, _Type) \ static constexpr event_mtype_t matrixTypeId() { return _Id; } \ - static auto typeId() { return QMatrixClient::typeId<_Type>(); } \ + static auto typeId() { return Quotient::typeId<_Type>(); } \ // End of macro // This macro should be put after an event class definition (in .h or .cpp) @@ -406,6 +406,6 @@ visit(const BaseEventT& event, FnT1&& visitor1, FnT2&& visitor2, return visit(event, std::forward(visitor2), std::forward(visitors)...); } -} // namespace QMatrixClient -Q_DECLARE_METATYPE(QMatrixClient::Event*) -Q_DECLARE_METATYPE(const QMatrixClient::Event*) +} // namespace Quotient +Q_DECLARE_METATYPE(Quotient::Event*) +Q_DECLARE_METATYPE(const Quotient::Event*) -- cgit v1.2.3 From 9ce16ef3123d5e63b931cafefb7dba4e894fc82b Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 11 Aug 2019 13:18:40 +0900 Subject: Drop EventType namespace and DEFINE_EVENTTYPE_ALIAS macro They've been deprecated for almost a year by now. --- lib/events/event.h | 22 ---------------------- 1 file changed, 22 deletions(-) (limited to 'lib/events/event.h') diff --git a/lib/events/event.h b/lib/events/event.h index e96d6897..d9c8e088 100644 --- a/lib/events/event.h +++ b/lib/events/event.h @@ -305,28 +305,6 @@ using Events = EventsArray; } \ // End of macro -#ifdef USE_EVENTTYPE_ALIAS -namespace EventType { - inline event_type_t logEventType(event_type_t id, const char* idName) - { - qDebug(EVENTS) << "Using id" << id << "for" << idName; - return id; - } -} // namespace EventType - -// This macro provides constants in EventType:: namespace for -// back-compatibility with libQMatrixClient 0.3 event type system. -# define DEFINE_EVENTTYPE_ALIAS(_Id, _Type) \ - namespace EventType { \ - [[deprecated("Use is<>(), eventCast<>() or " \ - "visit<>()")]] static const auto _Id = \ - logEventType(typeId<_Type>(), #_Id); \ - } \ - // End of macro -#else -# define DEFINE_EVENTTYPE_ALIAS(_Id, _Type) // Nothing -#endif - // === is<>(), eventCast<>() and visit<>() === template -- cgit v1.2.3 From e9ae0d5eaac12a276ea352f1e633fb22cdd46828 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 11 Aug 2019 17:01:41 +0900 Subject: Now that we're C++17, remove old compatibility cruft --- lib/events/event.h | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'lib/events/event.h') diff --git a/lib/events/event.h b/lib/events/event.h index d9c8e088..25362786 100644 --- a/lib/events/event.h +++ b/lib/events/event.h @@ -47,11 +47,10 @@ inline TargetEventT* weakPtrCast(const event_ptr_tt& ptr) /// Re-wrap a smart pointer to base into a smart pointer to derived template -[[deprecated("Consider using eventCast() or visit() " - "instead")]] inline event_ptr_tt -ptrCast(event_ptr_tt&& ptr) +[[deprecated("Consider using eventCast() or visit() instead")]] +inline event_ptr_tt ptrCast(event_ptr_tt&& ptr) { - return unique_ptr_cast(ptr); + return std::unique_ptr(static_cast(ptr.release())); } // === Standard Matrix key names and basicEventJson() === @@ -290,19 +289,19 @@ using Events = EventsArray; // This macro should be used in a public section of an event class to // provide matrixTypeId() and typeId(). -#define DEFINE_EVENT_TYPEID(_Id, _Type) \ - static constexpr event_mtype_t matrixTypeId() { return _Id; } \ - static auto typeId() { return Quotient::typeId<_Type>(); } \ +#define DEFINE_EVENT_TYPEID(_Id, _Type) \ + static constexpr event_mtype_t matrixTypeId() { return _Id; } \ + static auto typeId() { return Quotient::typeId<_Type>(); } \ // End of macro // This macro should be put after an event class definition (in .h or .cpp) // to enable its deserialisation from a /sync and other // polymorphic event arrays -#define REGISTER_EVENT_TYPE(_Type) \ - namespace { \ - [[gnu::unused]] static const auto _factoryAdded##_Type = \ - registerEventType<_Type>(); \ - } \ +#define REGISTER_EVENT_TYPE(_Type) \ + namespace { \ + [[maybe_unused]] static const auto _factoryAdded##_Type = \ + registerEventType<_Type>(); \ + } \ // End of macro // === is<>(), eventCast<>() and visit<>() === -- cgit v1.2.3 From 72dd1eb7c1986c23a7630205e2f2a0280b7c2a2b Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 29 Sep 2019 18:03:31 +0900 Subject: Simplify visit() signature Checking that BaseEventT descends from Event is really extraneous. --- lib/events/event.h | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) (limited to 'lib/events/event.h') diff --git a/lib/events/event.h b/lib/events/event.h index 25362786..f985ae92 100644 --- a/lib/events/event.h +++ b/lib/events/event.h @@ -334,22 +334,18 @@ inline auto visit(const BaseEventT& event, FnT&& visitor) return visitor(event); } -template -constexpr auto is_event() -{ - return std::is_base_of>::value; -} - -template -constexpr auto needs_cast() -{ - return !std::is_convertible>::value; +namespace _impl { + template + constexpr auto needs_downcast() + { + return !std::is_convertible_v>; + } } // A single type-specific void visitor template -inline std::enable_if_t() && needs_cast() - && std::is_void>::value> +inline std::enable_if_t<_impl::needs_downcast() + && std::is_void_v>> visit(const BaseEventT& event, FnT&& visitor) { using event_type = fn_arg_t; @@ -358,10 +354,9 @@ visit(const BaseEventT& event, FnT&& visitor) } // A single type-specific non-void visitor with an optional default value +// non-voidness is guarded by defaultValue type template -inline std::enable_if_t() && needs_cast(), - fn_return_t> // non-voidness is guarded by - // defaultValue type +inline std::enable_if_t<_impl::needs_downcast(), fn_return_t> visit(const BaseEventT& event, FnT&& visitor, fn_return_t&& defaultValue = {}) { @@ -373,9 +368,8 @@ visit(const BaseEventT& event, FnT&& visitor, // A chain of 2 or more visitors template -inline std::enable_if_t(), fn_return_t> -visit(const BaseEventT& event, FnT1&& visitor1, FnT2&& visitor2, - FnTs&&... visitors) +inline fn_return_t visit(const BaseEventT& event, FnT1&& visitor1, + FnT2&& visitor2, FnTs&&... visitors) { using event_type1 = fn_arg_t; if (is>(event)) -- cgit v1.2.3 From ec4110c63443e29c78fdf0f72af08f5395ec48f7 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Thu, 26 Mar 2020 13:25:09 +0100 Subject: Refactoring around Connection::onSyncSuccess() The method grew large and a bit unwieldy over the years. --- lib/events/event.h | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'lib/events/event.h') diff --git a/lib/events/event.h b/lib/events/event.h index f985ae92..6c8961ad 100644 --- a/lib/events/event.h +++ b/lib/events/event.h @@ -377,6 +377,17 @@ inline fn_return_t visit(const BaseEventT& event, FnT1&& visitor1, return visit(event, std::forward(visitor2), std::forward(visitors)...); } + +// A facility overload that calls void-returning visit() on each event +// over a range of event pointers +template +inline auto visitEach(RangeT&& events, FnTs&&... visitors) + -> std::enable_if_t, Event>> +{ + for (auto&& evtPtr: events) + visit(*evtPtr, std::forward(visitors)...); +} } // namespace Quotient Q_DECLARE_METATYPE(Quotient::Event*) Q_DECLARE_METATYPE(const Quotient::Event*) -- cgit v1.2.3