summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qproperty.h
diff options
context:
space:
mode:
authorFabian Kosmale <fabian.kosmale@qt.io>2020-07-15 14:12:58 +0200
committerLars Knoll <lars.knoll@qt.io>2020-09-02 22:44:27 +0200
commit733d890430542e907b9014a2cf73d63edf931245 (patch)
tree470dd4321b748c0de57b182617950321b7f22731 /src/corelib/kernel/qproperty.h
parent52bbb19fa4dc7647a0cad35a69dcf09437386080 (diff)
Add operator-> and operator*() to QProperty
Enable the arrow operator for all types that could have members, so that one can e.g. write myStringProperty->size() instead of having to use the less convenient myStringProperty.value().size(). Also cleaned up the rvalue ref overloads to be disabled for basic types. For those we now also return by value, for more complex types we return a const reference. Change-Id: If6a75898dc0a097f57052488f0af0cd7166b3393 Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'src/corelib/kernel/qproperty.h')
-rw-r--r--src/corelib/kernel/qproperty.h42
1 files changed, 30 insertions, 12 deletions
diff --git a/src/corelib/kernel/qproperty.h b/src/corelib/kernel/qproperty.h
index acaf74f9af..ff61596fb6 100644
--- a/src/corelib/kernel/qproperty.h
+++ b/src/corelib/kernel/qproperty.h
@@ -192,8 +192,15 @@ struct QPropertyBasePointer;
template <typename T>
class QProperty
{
+ class DisableRValueRefs {};
+ static constexpr bool UseReferences = !(std::is_arithmetic_v<T> || std::is_enum_v<T> || std::is_pointer_v<T>);
+
public:
using value_type = T;
+ using parameter_type = std::conditional_t<UseReferences, const T &, T>;
+ using rvalue_ref = typename std::conditional_t<UseReferences, T &&, DisableRValueRefs>;
+ using arrow_operator_result = std::conditional_t<std::is_pointer_v<T>, const T &,
+ std::conditional_t<QTypeTraits::is_dereferenceable_v<T>, const T &, void>>;
QProperty() = default;
explicit QProperty(const T &initialValue) : d(initialValue) {}
@@ -218,7 +225,7 @@ public:
#endif
~QProperty() = default;
- T value() const
+ parameter_type value() const
{
if (d.priv.hasBinding())
d.priv.evaluateIfDirty();
@@ -226,32 +233,49 @@ public:
return d.getValue();
}
- operator T() const
+ arrow_operator_result operator->() const
+ {
+ if constexpr (QTypeTraits::is_dereferenceable_v<T>) {
+ return value();
+ } else if constexpr (std::is_pointer_v<T>) {
+ value();
+ return this->val;
+ } else {
+ return;
+ }
+ }
+
+ parameter_type operator*() const
{
return value();
}
- void setValue(T &&newValue)
+ operator parameter_type() const
+ {
+ return value();
+ }
+
+ void setValue(rvalue_ref newValue)
{
d.priv.removeBinding();
if (d.setValueAndReturnTrueIfChanged(std::move(newValue)))
notify();
}
- void setValue(const T &newValue)
+ void setValue(parameter_type newValue)
{
d.priv.removeBinding();
if (d.setValueAndReturnTrueIfChanged(newValue))
notify();
}
- QProperty<T> &operator=(T &&newValue)
+ QProperty<T> &operator=(rvalue_ref newValue)
{
setValue(std::move(newValue));
return *this;
}
- QProperty<T> &operator=(const T &newValue)
+ QProperty<T> &operator=(parameter_type newValue)
{
setValue(newValue);
return *this;
@@ -263,12 +287,6 @@ public:
return *this;
}
- QProperty<T> &operator=(QPropertyBinding<T> &&newBinding)
- {
- setBinding(std::move(newBinding));
- return *this;
- }
-
QPropertyBinding<T> setBinding(const QPropertyBinding<T> &newBinding)
{
QPropertyBinding<T> oldBinding(d.priv.setBinding(newBinding, &d));