diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-07-08 12:07:13 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-07-10 15:07:12 +0200 |
commit | bbfecdee1e2952c401e9c5ac6e16adc2428fb2dc (patch) | |
tree | 4b877d63b50606930bbac7416b3d164ae69cb70a /src/corelib/kernel/qpropertyprivate.h | |
parent | be1ce6b26909256e62c2b39e4de9f8f79d070937 (diff) |
Significantly improve performance of binding evaluation
Avoid any QVariant or type dependent code in the cpp files.
Instead, let the binding wrapper determine if the value
has changed and return true/false accordingly.
This required also some reworking of the guard mechanism
for notified properties, where the guard function wrapper
now calls first the binding evaluation function and then
passes the result to the guard.
Change-Id: I350d07a508ccc0c5db7054a0efa4f270b6a78ec3
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/corelib/kernel/qpropertyprivate.h')
-rw-r--r-- | src/corelib/kernel/qpropertyprivate.h | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/src/corelib/kernel/qpropertyprivate.h b/src/corelib/kernel/qpropertyprivate.h index 145f2c66b9..91d9204996 100644 --- a/src/corelib/kernel/qpropertyprivate.h +++ b/src/corelib/kernel/qpropertyprivate.h @@ -61,9 +61,17 @@ class QUntypedPropertyBinding; class QPropertyBindingPrivate; using QPropertyBindingPrivatePtr = QExplicitlySharedDataPointer<QPropertyBindingPrivate>; struct QPropertyBasePointer; +class QMetaType; namespace QtPrivate { +// writes binding result into dataPtr +using QPropertyBindingFunction = std::function<bool(const QMetaType &metaType, void *dataPtr)>; + +using QPropertyGuardFunction = bool(*)(const QMetaType &, void *dataPtr, + QPropertyBindingFunction, void *owner); +using QPropertyObserverCallback = void (*)(void *, void *); + class Q_CORE_EXPORT QPropertyBase { // Mutable because the address of the observer of the currently evaluating binding is stored here, for @@ -84,8 +92,8 @@ public: QUntypedPropertyBinding setBinding(const QUntypedPropertyBinding &newBinding, void *propertyDataPtr, void *staticObserver = nullptr, - void (*staticObserverCallback)(void *, void *) = nullptr, - bool (*guardCallback)(void *, void *) = nullptr); + QPropertyObserverCallback staticObserverCallback = nullptr, + QPropertyGuardFunction guardCallback = nullptr); QPropertyBindingPrivate *binding(); void evaluateIfDirty(); @@ -211,6 +219,38 @@ private: quintptr *d = nullptr; }; +namespace detail { + template <typename F> + struct ExtractClassFromFunctionPointer; + + template<typename T, typename C> + struct ExtractClassFromFunctionPointer<T C::*> { using Class = C; }; +} + +// type erased guard functions, casts its arguments to the correct types +template<typename T, typename Class, auto Guard, bool = std::is_same_v<decltype(Guard), std::nullptr_t>> +struct QPropertyGuardFunctionHelper +{ + static constexpr QPropertyGuardFunction guard = nullptr; +}; +template<typename T, typename Class, auto Guard> +struct QPropertyGuardFunctionHelper<T, Class, Guard, false> +{ + static auto guard(const QMetaType &metaType, void *dataPtr, + QPropertyBindingFunction eval, void *owner) -> bool + { + T t; + eval(metaType, &t); + if (!(static_cast<Class *>(owner)->*Guard)(t)) + return false; + T *data = static_cast<T *>(dataPtr); + if (*data == t) + return false; + *data = std::move(t); + return true; + }; +}; + } // namespace QtPrivate QT_END_NAMESPACE |