diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-08-06 12:02:45 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-09-02 22:44:26 +0200 |
commit | 80745bfffe55effcea466faeaad47c7aa2569374 (patch) | |
tree | e7374bdb24300cd4db244bc887715b0aa47d470b | |
parent | 00e181fdae8fa29f56248989348c2d650752bf69 (diff) |
Generalize some methods taking a QProperty<>
Generalize some methods taking a QProperty<T>,
so that they can work with other types that
implement the QProperty interface as well.
This removes some duplication between QProperty and
QNotifiedProperty. It also makes it possible to
create private property classes that store their
data in a different place.
Change-Id: I4b1ae8589cb9a76be59e63206044dcf2244163c2
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
-rw-r--r-- | src/corelib/kernel/qproperty.cpp | 12 | ||||
-rw-r--r-- | src/corelib/kernel/qproperty.h | 46 | ||||
-rw-r--r-- | src/corelib/kernel/qproperty_p.h | 2 |
3 files changed, 23 insertions, 37 deletions
diff --git a/src/corelib/kernel/qproperty.cpp b/src/corelib/kernel/qproperty.cpp index 6336bd5c58..a0cdda4200 100644 --- a/src/corelib/kernel/qproperty.cpp +++ b/src/corelib/kernel/qproperty.cpp @@ -818,6 +818,12 @@ QString QPropertyBindingError::description() const */ /*! + \fn template <typename T> QtPrivate::QPropertyBase &QProperty<T>::propertyBase() const + \internal +*/ + + +/*! \class QNotifiedProperty \inmodule QtCore \brief The QNotifiedProperty class is a template class that enables automatic property bindings @@ -1042,6 +1048,12 @@ QString QPropertyBindingError::description() const */ /*! + \fn template <typename T> QtPrivate::QPropertyBase &QNotifiedProperty<T, Callback>::propertyBase() const + \internal +*/ + + +/*! \class QPropertyChangeHandler \inmodule QtCore \brief The QPropertyChangeHandler class controls the lifecycle of change callback installed on a QProperty. diff --git a/src/corelib/kernel/qproperty.h b/src/corelib/kernel/qproperty.h index 1c2aede856..c6be89e169 100644 --- a/src/corelib/kernel/qproperty.h +++ b/src/corelib/kernel/qproperty.h @@ -82,10 +82,6 @@ struct Q_CORE_EXPORT QPropertyBindingSourceLocation }; template <typename Functor> class QPropertyChangeHandler; - -template <typename T> class QProperty; -template <typename T, auto callbackMember, auto guardCallback> class QNotifiedProperty; - class QPropertyBindingErrorPrivate; class Q_CORE_EXPORT QPropertyBindingError @@ -171,13 +167,9 @@ public: : QUntypedPropertyBinding(QMetaType::fromType<PropertyType>(), BindingAdaptor<Functor>{std::forward<Functor>(f)}, location) {} - QPropertyBinding(const QProperty<PropertyType> &property) - : QUntypedPropertyBinding(property.d.priv.binding()) - {} - - template<auto notifier, auto guard> - QPropertyBinding(const QNotifiedProperty<PropertyType, notifier, guard> &property) - : QUntypedPropertyBinding(property.d.priv.binding()) + template<typename Property, typename = std::void_t<decltype(&Property::propertyBase)>> + QPropertyBinding(const Property &property) + : QUntypedPropertyBinding(property.propertyBase().binding()) {} // Internal @@ -331,6 +323,7 @@ public: template<typename Functor> QPropertyChangeHandler<Functor> subscribe(Functor f); + const QtPrivate::QPropertyBase &propertyBase() const { return d.priv; } private: void notify() { @@ -339,9 +332,6 @@ private: Q_DISABLE_COPY(QProperty) - friend struct QPropertyBasePointer; - friend class QPropertyBinding<T>; - friend class QPropertyObserver; // Mutable because querying for the value may require evalating the binding expression, calling // non-const functions on QPropertyBase. mutable QtPrivate::QPropertyValueStorage<T> d; @@ -539,6 +529,7 @@ public: template<typename Functor> QPropertyChangeHandler<Functor> subscribe(Functor f); + const QtPrivate::QPropertyBase &propertyBase() const { return d.priv; } private: void notify(Class *owner, T *oldValue=nullptr) { @@ -553,8 +544,6 @@ private: Q_DISABLE_COPY_MOVE(QNotifiedProperty) - friend class QPropertyBinding<T>; - friend class QPropertyObserver; // Mutable because querying for the value may require evalating the binding expression, calling // non-const functions on QPropertyBase. mutable QtPrivate::QPropertyValueStorage<T> d; @@ -578,13 +567,9 @@ public: QPropertyObserver &operator=(QPropertyObserver &&other); ~QPropertyObserver(); - template <typename PropertyType> - void setSource(const QProperty<PropertyType> &property) - { setSource(property.d.priv); } - - template <typename PropertyType, auto notifier, auto guard> - void setSource(const QNotifiedProperty<PropertyType, notifier, guard> &property) - { setSource(property.d.priv); } + template<typename Property, typename = std::enable_if_t<std::is_same_v<decltype(std::declval<Property>().propertyBase()), QtPrivate::QPropertyBase &>>> + void setSource(const Property &property) + { setSource(property.propertyBase()); } protected: QPropertyObserver(void (*callback)(QPropertyObserver*, void *)); @@ -632,19 +617,8 @@ public: { } - template <typename PropertyType> - QPropertyChangeHandler(const QProperty<PropertyType> &property, Functor handler) - : QPropertyObserver([](QPropertyObserver *self, void *) { - auto This = static_cast<QPropertyChangeHandler<Functor>*>(self); - This->m_handler(); - }) - , m_handler(handler) - { - setSource(property); - } - - template <typename PropertyType, auto Callback, auto Guard> - QPropertyChangeHandler(const QNotifiedProperty<PropertyType, Callback, Guard> &property, Functor handler) + template<typename Property, typename = std::void_t<decltype(&Property::propertyBase)>> + QPropertyChangeHandler(const Property &property, Functor handler) : QPropertyObserver([](QPropertyObserver *self, void *) { auto This = static_cast<QPropertyChangeHandler<Functor>*>(self); This->m_handler(); diff --git a/src/corelib/kernel/qproperty_p.h b/src/corelib/kernel/qproperty_p.h index 1ddb3dd453..9c43c23959 100644 --- a/src/corelib/kernel/qproperty_p.h +++ b/src/corelib/kernel/qproperty_p.h @@ -90,7 +90,7 @@ struct Q_AUTOTEST_EXPORT QPropertyBasePointer template <typename T> static QPropertyBasePointer get(QProperty<T> &property) { - return QPropertyBasePointer{&property.d.priv}; + return QPropertyBasePointer{&property.propertyBase()}; } }; |