summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qvariant.h
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2020-09-10 18:30:22 +0200
committerUlf Hermann <ulf.hermann@qt.io>2020-09-19 11:14:36 +0200
commit217a25a6bf59bb9ce0845267f46244fedd8bcaaa (patch)
treeae54c75f951553001472d5d8bb0f54e5a4f9056c /src/corelib/kernel/qvariant.h
parent5c808073af5b8f1290602fcccf60666c9a3682f8 (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.h')
-rw-r--r--src/corelib/kernel/qvariant.h18
1 files changed, 17 insertions, 1 deletions
diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h
index 1825d4be88..83cb40a7c7 100644
--- a/src/corelib/kernel/qvariant.h
+++ b/src/corelib/kernel/qvariant.h
@@ -248,6 +248,9 @@ class Q_CORE_EXPORT QVariant
{ return QMetaType::canConvert(d.type(), targetType); }
bool convert(QMetaType type);
+ bool canView(QMetaType targetType) const
+ { return QMetaType::canView(d.type(), targetType); }
+
#if QT_DEPRECATED_SINCE(6, 0)
QT_DEPRECATED_VERSION_6_0
bool canConvert(int targetTypeId) const
@@ -364,6 +367,14 @@ class Q_CORE_EXPORT QVariant
{ return qvariant_cast<T>(*this); }
template<typename T>
+ inline T view()
+ {
+ T t{};
+ QMetaType::view(metaType(), data(), QMetaType::fromType<T>(), &t);
+ return t;
+ }
+
+ template<typename T>
static inline QVariant fromValue(const T &value)
{
return QVariant(QMetaType::fromType<T>(), std::addressof(value));
@@ -383,6 +394,10 @@ class Q_CORE_EXPORT QVariant
bool canConvert() const
{ return canConvert(QMetaType::fromType<T>()); }
+ template<typename T>
+ bool canView() const
+ { return canView(QMetaType::fromType<T>()); }
+
public:
struct PrivateShared
{
@@ -488,7 +503,8 @@ protected:
Private d;
void create(int type, const void *copy);
bool equals(const QVariant &other) const;
- bool convert(int t, void *ptr) const;
+ bool convert(int type, void *ptr) const;
+ bool view(int type, void *ptr);
private:
// force compile error, prevent QVariant(bool) to be called