diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2020-09-10 18:30:22 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2020-09-19 11:14:36 +0200 |
commit | 217a25a6bf59bb9ce0845267f46244fedd8bcaaa (patch) | |
tree | ae54c75f951553001472d5d8bb0f54e5a4f9056c /src/corelib/kernel/qvariant.cpp | |
parent | 5c808073af5b8f1290602fcccf60666c9a3682f8 (diff) |
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 <lars.knoll@qt.io>
Diffstat (limited to 'src/corelib/kernel/qvariant.cpp')
-rw-r--r-- | src/corelib/kernel/qvariant.cpp | 27 |
1 files changed, 27 insertions, 0 deletions
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<typename T> 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<typename T> static QVariant QVariant::fromValue(const T &value) Returns a QVariant containing a copy of \a value. Behaves |