QBindable: Mark non-modifying methods as const

A few methods in QBindable which do not modify anything were not marked
as const so far. This adds the missing const, and a test to verify that
they work.

As all methods are fully inline, this does not cause any binary
compatibility issues.

Fixes: QTBUG-89508
Change-Id: If06d33bc405232887b8c371c268840ba34dbadf6
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Fabian Kosmale 2021-04-13 15:05:42 +02:00
parent 865f3c2a11
commit d5b79e876e
2 changed files with 26 additions and 5 deletions

View File

@ -577,7 +577,7 @@ public:
bool isBindable() const { return iface && iface->getBinding; }
bool isReadOnly() const { return !(iface && iface->setBinding && iface->setObserver); }
QUntypedPropertyBinding makeBinding(const QPropertyBindingSourceLocation &location = QT_PROPERTY_DEFAULT_BINDING_LOCATION)
QUntypedPropertyBinding makeBinding(const QPropertyBindingSourceLocation &location = QT_PROPERTY_DEFAULT_BINDING_LOCATION) const
{
return iface ? iface->makeBinding(data, location) : QUntypedPropertyBinding();
}
@ -597,14 +597,14 @@ public:
return binding;
}
void observe(QPropertyObserver *observer)
void observe(QPropertyObserver *observer) const
{
if (iface)
iface->setObserver(data, observer);
}
template<typename Functor>
QPropertyChangeHandler<Functor> onValueChanged(Functor f)
QPropertyChangeHandler<Functor> onValueChanged(Functor f) const
{
QPropertyChangeHandler<Functor> handler(f);
observe(&handler);
@ -612,7 +612,7 @@ public:
}
template<typename Functor>
QPropertyChangeHandler<Functor> subscribe(Functor f)
QPropertyChangeHandler<Functor> subscribe(Functor f) const
{
f();
return onValueChanged(f);
@ -658,7 +658,7 @@ public:
}
}
QPropertyBinding<T> makeBinding(const QPropertyBindingSourceLocation &location = QT_PROPERTY_DEFAULT_BINDING_LOCATION)
QPropertyBinding<T> makeBinding(const QPropertyBindingSourceLocation &location = QT_PROPERTY_DEFAULT_BINDING_LOCATION) const
{
return static_cast<QPropertyBinding<T> &&>(QUntypedBindable::makeBinding(location));
}

View File

@ -80,6 +80,7 @@ private slots:
void typeNoOperatorEqual();
void bindingValueReplacement();
void quntypedBindableApi();
void readonlyConstQBindable();
void testNewStuff();
void qobjectObservers();
@ -1030,6 +1031,26 @@ void tst_QProperty::quntypedBindableApi()
QVERIFY(propLess.takeBinding().isNull());
}
void tst_QProperty::readonlyConstQBindable()
{
QProperty<int> i {42};
const QBindable<int> bindableI(const_cast<const QProperty<int> *>(&i));
// check that read-only operations work with a const QBindable
QVERIFY(bindableI.isReadOnly());
QVERIFY(!bindableI.hasBinding());
// we can still create a binding to a read only bindable through the interface
QProperty<int> j;
j.setBinding(bindableI.makeBinding());
QCOMPARE(j.value(), bindableI.value());
int counter = 0;
auto observer = bindableI.subscribe([&](){++counter;});
QCOMPARE(counter, 1);
auto observer2 = bindableI.onValueChanged([&](){++counter;});
i = 0;
QCOMPARE(counter, 3);
}
class MyQObject : public QObject
{
Q_OBJECT