summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qproperty.h
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2020-05-28 17:10:10 +0200
committerUlf Hermann <ulf.hermann@qt.io>2020-06-03 18:53:40 +0200
commit07ded4912f82008549b69b61a598ec7b0d2b3325 (patch)
treec01e7856e7605152631ed9ea8f8c85a19002e3ea /src/corelib/kernel/qproperty.h
parent5a1680ef131d89190e745131c18b50a4c0871d18 (diff)
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 <fabian.kosmale@qt.io>
Diffstat (limited to 'src/corelib/kernel/qproperty.h')
-rw-r--r--src/corelib/kernel/qproperty.h32
1 files changed, 32 insertions, 0 deletions
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 <typename PropertyType, typename Class, void(Class::*Callback)()>
+ QPropertyChangeHandler(const QNotifiedProperty<PropertyType, Callback> &property, Functor handler)
+ : QPropertyObserver([](QPropertyObserver *self, void *) {
+ auto This = static_cast<QPropertyChangeHandler<Functor>*>(self);
+ This->m_handler();
+ })
+ , m_handler(handler)
+ {
+ setSource(property);
+ }
};
template <typename T>
@@ -613,6 +624,27 @@ QPropertyChangeHandler<Functor> QProperty<T>::subscribe(Functor f)
return onValueChanged(f);
}
+template <typename T, typename Class, void(Class::*Callback)()>
+template<typename Functor>
+QPropertyChangeHandler<Functor> QNotifiedProperty<T, Callback>::onValueChanged(Functor f)
+{
+#if defined(__cpp_lib_is_invocable) && (__cpp_lib_is_invocable >= 201703L)
+ static_assert(std::is_invocable_v<Functor>, "Functor callback must be callable without any parameters");
+#endif
+ return QPropertyChangeHandler<Functor>(*this, f);
+}
+
+template <typename T, typename Class, void(Class::*Callback)()>
+template<typename Functor>
+QPropertyChangeHandler<Functor> QNotifiedProperty<T, Callback>::subscribe(Functor f)
+{
+#if defined(__cpp_lib_is_invocable) && (__cpp_lib_is_invocable >= 201703L)
+ static_assert(std::is_invocable_v<Functor>, "Functor callback must be callable without any parameters");
+#endif
+ f();
+ return onValueChanged(f);
+}
+
template <auto propertyMember, auto callbackMember>
struct QPropertyMemberChangeHandler;