Add QVariant::Private::storage(), get() and typeId()

Adds convenient access to the data from the Private pointer.
data() determines the storage location at run time, get()
at compile time. internalStorage() can be used if we're accessing
one of multiple types, but know that the type is stored internally.

typeId() is an optimization as it allows retrieving the type id
of the metatype without atomic refcounting operations (which
type().id() would be doing).

Change-Id: I39a508c530a1588053248607c8932e501fd474dc
Reviewed-by: Maurice Kalinowski <maurice.kalinowski@qt.io>
This commit is contained in:
Lars Knoll 2020-07-12 21:07:22 +02:00
parent 73fd7f2efc
commit d1c3f81126

View File

@ -493,10 +493,29 @@ class Q_CORE_EXPORT QVariant
quintptr packedType : sizeof(QMetaType) * 8 - 2; quintptr packedType : sizeof(QMetaType) * 8 - 2;
quintptr is_shared : 1; quintptr is_shared : 1;
quintptr is_null : 1; quintptr is_null : 1;
template<typename T>
static constexpr bool CanUseInternalSpace = sizeof(T) <= sizeof(QVariant::Private::Data);
const void *storage() const
{ return is_shared ? data.shared->data() : &data; }
const void *internalStorage() const
{ Q_ASSERT(is_shared); return &data; }
// determine internal storage at compile time
template<typename T>
const T &get() const
{ return *static_cast<const T *>(CanUseInternalSpace<T> ? &data : data.shared->data()); }
inline QMetaType type() const inline QMetaType type() const
{ {
return QMetaType(reinterpret_cast<QtPrivate::QMetaTypeInterface *>(packedType << 2)); return QMetaType(reinterpret_cast<QtPrivate::QMetaTypeInterface *>(packedType << 2));
} }
inline int typeId() const
{
return type().id();
}
}; };
public: public:
inline bool operator==(const QVariant &v) const inline bool operator==(const QVariant &v) const