From 07ded4912f82008549b69b61a598ec7b0d2b3325 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Thu, 28 May 2020 17:10:10 +0200 Subject: Fix and compactify QNotifiedProperty The static observer can live in a union with the inline observers. We only need to take care of calling the ctors and dtors manually then. In order for any observers to be called in the presence of a static observer, the static observer has to be called after the other observers. Change-Id: I2f56fa64f3fe6fcd7f06cc403929362da7d86f65 Reviewed-by: Fabian Kosmale --- src/corelib/kernel/qproperty.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/corelib/kernel/qproperty.h') diff --git a/src/corelib/kernel/qproperty.h b/src/corelib/kernel/qproperty.h index e2a6172d84..f0156fac02 100644 --- a/src/corelib/kernel/qproperty.h +++ b/src/corelib/kernel/qproperty.h @@ -590,6 +590,17 @@ public: { setSource(property); } + + template + QPropertyChangeHandler(const QNotifiedProperty &property, Functor handler) + : QPropertyObserver([](QPropertyObserver *self, void *) { + auto This = static_cast*>(self); + This->m_handler(); + }) + , m_handler(handler) + { + setSource(property); + } }; template @@ -613,6 +624,27 @@ QPropertyChangeHandler QProperty::subscribe(Functor f) return onValueChanged(f); } +template +template +QPropertyChangeHandler QNotifiedProperty::onValueChanged(Functor f) +{ +#if defined(__cpp_lib_is_invocable) && (__cpp_lib_is_invocable >= 201703L) + static_assert(std::is_invocable_v, "Functor callback must be callable without any parameters"); +#endif + return QPropertyChangeHandler(*this, f); +} + +template +template +QPropertyChangeHandler QNotifiedProperty::subscribe(Functor f) +{ +#if defined(__cpp_lib_is_invocable) && (__cpp_lib_is_invocable >= 201703L) + static_assert(std::is_invocable_v, "Functor callback must be callable without any parameters"); +#endif + f(); + return onValueChanged(f); +} + template struct QPropertyMemberChangeHandler; -- cgit v1.2.3