Skip to content

Commit

Permalink
Remove QPropertyMemberChangeHandler again
Browse files Browse the repository at this point in the history
Adding support for a static notifier within QProperty itself - through a
QProperty "sister" class - is more efficient in terms of memory
consumption and run-time performance.

The MemberChangeHandler permanently takes up at least three pointers,
while the notified properties only cost one pointer in the binding.

Change-Id: Ia1a8c2b66f1f3c2fe13ae0ad9f12cdb6bdcc35ef
Reviewed-by: Ulf Hermann <[email protected]>
  • Loading branch information
tronical authored and Inkane committed Jun 25, 2020
1 parent e18a060 commit b20c7df
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 43 deletions.
23 changes: 0 additions & 23 deletions src/corelib/kernel/qproperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -731,28 +731,6 @@ QPropertyChangeHandler<Functor> QNotifiedProperty<T, Callback, ValueGuard>::subs
return onValueChanged(f);
}

template <auto propertyMember, auto callbackMember>
struct QPropertyMemberChangeHandler;

template<typename Class, typename PropertyType, PropertyType Class::* PropertyMember, void(Class::*Callback)()>
struct QPropertyMemberChangeHandler<PropertyMember, Callback> : public QPropertyObserver
{
QPropertyMemberChangeHandler(Class *obj)
: QPropertyObserver(notify)
{
setSource(obj->*PropertyMember);
}

static void notify(QPropertyObserver *, void *propertyDataPtr)
{
// memberOffset is the offset of the QProperty<> member within the class. We get the absolute address
// of that member and subtracting the relative offset gives us the address of the class instance.
const size_t memberOffset = reinterpret_cast<size_t>(&(static_cast<Class *>(nullptr)->*PropertyMember));
Class *obj = reinterpret_cast<Class *>(reinterpret_cast<char *>(propertyDataPtr) - memberOffset);
(obj->*Callback)();
}
};

template<typename T>
class QPropertyAlias : public QPropertyObserver
{
Expand Down Expand Up @@ -892,7 +870,6 @@ class QPropertyAlias : public QPropertyObserver
return aliasedProperty<T>() != nullptr;
}
};

QT_END_NAMESPACE

#endif // QPROPERTY_H
20 changes: 0 additions & 20 deletions tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ private slots:
void settingPropertyValueDoesRemoveBinding();
void genericPropertyBinding();
void genericPropertyBindingBool();
void staticChangeHandler();
void setBindingFunctor();
void multipleObservers();
void propertyAlias();
Expand Down Expand Up @@ -661,25 +660,6 @@ void tst_QProperty::genericPropertyBindingBool()
QVERIFY(property.value());
}

struct ItemType
{
QProperty<int> x;
QVector<int> observedValues;
void xChanged() {
observedValues << x.value();
}
QPropertyMemberChangeHandler<&ItemType::x, &ItemType::xChanged> test{this};
};

void tst_QProperty::staticChangeHandler()
{
ItemType t;
t.x = 42;
t.x = 100;
QVector<int> values{42, 100};
QCOMPARE(t.observedValues, values);
}

void tst_QProperty::setBindingFunctor()
{
QProperty<int> property;
Expand Down

0 comments on commit b20c7df

Please sign in to comment.