From 217a25a6bf59bb9ce0845267f46244fedd8bcaaa Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Thu, 10 Sep 2020 18:30:22 +0200 Subject: QMetaType: Allow registration of mutable views and register iterables In order to modify a container through an iterable, we need the original container to be mutable. The iterable, then, is not a conversion of the container, but rather a view on the container. The concept may be extended to other types. In order to facilitate this, provide a set of methods in QMetaType and QVariant similar to the convert family. The new methods are non-const and expect the original value to stay available during the life time of the view. Change-Id: I363621033f7fc600edcea2acb786820ccba49c86 Reviewed-by: Lars Knoll --- src/corelib/kernel/qvariant.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/corelib/kernel/qvariant.cpp') diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index 603c49ebfe..65cdfc8580 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -2068,6 +2068,13 @@ bool QVariant::convert(int type, void *ptr) const return QMetaType::convert(d.type(), constData(), QMetaType(type), ptr); } +/*! + \internal +*/ +bool QVariant::view(int type, void *ptr) +{ + return QMetaType::view(d.type(), data(), QMetaType(type), ptr); +} /*! \fn bool operator==(const QVariant &v1, const QVariant &v2) @@ -2457,6 +2464,18 @@ QDebug operator<<(QDebug dbg, const QVariant::Type p) \sa setValue(), fromValue(), canConvert(), Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE() */ +/*! \fn template T QVariant::view() + + Returns a mutable view of template type \c{T} on the stored value. + Call canView() to find out whether such a view is supported. + If no such view can be created, returns the stored value converted to the + template type \c{T}. Call canConvert() to find out whether a type can be + converted. If the value can neither be viewed nor converted, a + \l{default-constructed value} will be returned. + + \sa canView(), Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE() +*/ + /*! \fn bool QVariant::canConvert() const Returns \c true if the variant can be converted to the template type \c{T}, @@ -2473,6 +2492,14 @@ QDebug operator<<(QDebug dbg, const QVariant::Type p) \sa convert() */ +/*! \fn bool QVariant::canView() const + + Returns \c true if a mutable view of the template type \c{T} can be created on this variant, + otherwise \c false. + + \sa value() +*/ + /*! \fn template static QVariant QVariant::fromValue(const T &value) Returns a QVariant containing a copy of \a value. Behaves -- cgit v1.2.3