diff options
Diffstat (limited to 'src/corelib/kernel/qvariant_p.h')
-rw-r--r-- | src/corelib/kernel/qvariant_p.h | 413 |
1 files changed, 59 insertions, 354 deletions
diff --git a/src/corelib/kernel/qvariant_p.h b/src/corelib/kernel/qvariant_p.h index 8739cb5173..d2a7390938 100644 --- a/src/corelib/kernel/qvariant_p.h +++ b/src/corelib/kernel/qvariant_p.h @@ -1,42 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Copyright (C) 2016 Intel Corporation. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #ifndef QVARIANT_P_H #define QVARIANT_P_H @@ -52,353 +15,95 @@ // We mean it. // -#include <QtCore/qglobal.h> -#include <QtCore/qvariant.h> -#include <QtCore/private/qmetatype_p.h> -#include <QtCore/qdebug.h> - -#include "qmetatypeswitcher_p.h" +#include "qvariant.h" QT_BEGIN_NAMESPACE -template<typename T> -struct QVariantIntegrator -{ - static const bool CanUseInternalSpace = sizeof(T) <= sizeof(QVariant::Private::Data) - && ((QTypeInfoQuery<T>::isRelocatable) || std::is_enum<T>::value); - typedef std::integral_constant<bool, CanUseInternalSpace> CanUseInternalSpace_t; -}; -static_assert(QVariantIntegrator<double>::CanUseInternalSpace); -static_assert(QVariantIntegrator<long int>::CanUseInternalSpace); -static_assert(QVariantIntegrator<qulonglong>::CanUseInternalSpace); - -#ifdef Q_CC_SUN // Sun CC picks the wrong overload, so introduce awful hack - -// takes a type, returns the internal void* pointer cast -// to a pointer of the input type -template <typename T> -inline T *v_cast(const QVariant::Private *nd, T * = 0) +inline auto customConstructSharedImpl(size_t size, size_t align) { - QVariant::Private *d = const_cast<QVariant::Private *>(nd); - return !QVariantIntegrator<T>::CanUseInternalSpace - ? static_cast<T *>(d->data.shared->ptr) - : static_cast<T *>(static_cast<void *>(&d->data.c)); -} - -#else // every other compiler in this world + struct Deleter { + void operator()(QVariant::PrivateShared *p) const + { QVariant::PrivateShared::free(p); } + }; -template <typename T> -inline const T *v_cast(const QVariant::Private *d, T * = nullptr) -{ - return !QVariantIntegrator<T>::CanUseInternalSpace - ? static_cast<const T *>(d->data.shared->ptr) - : static_cast<const T *>(static_cast<const void *>(&d->data.c)); + // this is exception-safe + std::unique_ptr<QVariant::PrivateShared, Deleter> ptr; + ptr.reset(QVariant::PrivateShared::create(size, align)); + return ptr; } -template <typename T> -inline T *v_cast(QVariant::Private *d, T * = nullptr) +template <typename F> static QVariant::PrivateShared * +customConstructShared(size_t size, size_t align, F &&construct) { - return !QVariantIntegrator<T>::CanUseInternalSpace - ? static_cast<T *>(d->data.shared->ptr) - : static_cast<T *>(static_cast<void *>(&d->data.c)); + auto ptr = customConstructSharedImpl(size, align); + construct(ptr->data()); + return ptr.release(); } -#endif - -enum QVariantConstructionFlags : uint { - Default = 0x0, - PointerType = 0x1, - ShouldDeleteVariantData = 0x2 // only used in Q*Iterable -}; - -//a simple template that avoids to allocate 2 memory chunks when creating a QVariant -template <class T> class QVariantPrivateSharedEx : public QVariant::PrivateShared -{ -public: - QVariantPrivateSharedEx() : QVariant::PrivateShared(&m_t), m_t() { } - QVariantPrivateSharedEx(const T&t) : QVariant::PrivateShared(&m_t), m_t(t) { } - -private: - T m_t; -}; - -template <class T> -inline void v_construct_helper(QVariant::Private *x, const T &t, std::true_type) +inline int QVariant::PrivateShared::computeOffset(PrivateShared *ps, size_t align) { - new (&x->data) T(t); - x->is_shared = false; + return int(((quintptr(ps) + sizeof(PrivateShared) + align - 1) & ~(align - 1)) - quintptr(ps)); } -template <class T> -inline void v_construct_helper(QVariant::Private *x, const T &t, std::false_type) +inline size_t QVariant::PrivateShared::computeAllocationSize(size_t size, size_t align) { - x->data.shared = new QVariantPrivateSharedEx<T>(t); - x->is_shared = true; + size += sizeof(PrivateShared); + if (align > sizeof(PrivateShared)) { + // The alignment is larger than the alignment we can guarantee for the pointer + // directly following PrivateShared, so we need to allocate some additional + // memory to be able to fit the object into the available memory with suitable + // alignment. + size += align - sizeof(PrivateShared); + } + return size; } -template <class T> -inline void v_construct_helper(QVariant::Private *x, std::true_type) +inline QVariant::PrivateShared *QVariant::PrivateShared::create(size_t size, size_t align) { - new (&x->data) T(); - x->is_shared = false; + size = computeAllocationSize(size, align); + void *data = operator new(size); + auto *ps = new (data) QVariant::PrivateShared(); + ps->offset = computeOffset(ps, align); + return ps; } -template <class T> -inline void v_construct_helper(QVariant::Private *x, std::false_type) +inline void QVariant::PrivateShared::free(PrivateShared *p) { - x->data.shared = new QVariantPrivateSharedEx<T>; - x->is_shared = true; + p->~PrivateShared(); + operator delete(p); } -template <class T> -inline void v_construct(QVariant::Private *x, const T &t) +inline QVariant::Private::Private(const QtPrivate::QMetaTypeInterface *iface) noexcept + : is_shared(false), is_null(false), packedType(quintptr(iface) >> 2) { - // dispatch - v_construct_helper(x, t, typename QVariantIntegrator<T>::CanUseInternalSpace_t()); + Q_ASSERT((quintptr(iface) & 0x3) == 0); } -// constructs a new variant if copy is 0, otherwise copy-constructs -template <class T> -inline void v_construct(QVariant::Private *x, const void *copy, T * = nullptr) +template <typename T> inline +QVariant::Private::Private(std::piecewise_construct_t, const T &t) + : is_shared(!CanUseInternalSpace<T>), is_null(std::is_same_v<T, std::nullptr_t>) { - if (copy) - v_construct<T>(x, *static_cast<const T *>(copy)); - else - v_construct_helper<T>(x, typename QVariantIntegrator<T>::CanUseInternalSpace_t()); -} + // confirm noexceptness + static constexpr bool isNothrowQVariantConstructible = noexcept(QVariant(t)); + static constexpr bool isNothrowCopyConstructible = std::is_nothrow_copy_constructible_v<T>; + static constexpr bool isNothrowCopyAssignable = std::is_nothrow_copy_assignable_v<T>; -// deletes the internal structures -template <class T> -inline void v_clear(QVariant::Private *d, T* = nullptr) -{ + const QtPrivate::QMetaTypeInterface *iface = QtPrivate::qMetaTypeInterfaceForType<T>(); + Q_ASSERT((quintptr(iface) & 0x3) == 0); + packedType = quintptr(iface) >> 2; - if (!QVariantIntegrator<T>::CanUseInternalSpace) { - //now we need to cast - //because QVariant::PrivateShared doesn't have a virtual destructor - delete static_cast<QVariantPrivateSharedEx<T>*>(d->data.shared); + if constexpr (CanUseInternalSpace<T>) { + static_assert(isNothrowQVariantConstructible == isNothrowCopyConstructible); + static_assert(isNothrowQVariantConstructible == isNothrowCopyAssignable); + new (data.data) T(t); } else { - v_cast<T>(d)->~T(); - } - -} - -template <typename T> -struct PrimitiveIsNull -{ -public: - static bool isNull(const QVariant::Private *d) - { - return d->is_null; - } -}; - -template <typename T> -struct PrimitiveIsNull<T*> -{ -public: - static bool isNull(const QVariant::Private *d) - { - return d->is_null || d->data.ptr == nullptr; - } -}; - -template <> -struct PrimitiveIsNull<std::nullptr_t> -{ -public: - static bool isNull(const QVariant::Private *) - { - return true; - } -}; - -template<class Filter> -class QVariantComparator { - template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted> - struct FilteredComparator { - static bool compare(const QVariant::Private *a, const QVariant::Private *b) - { - return *v_cast<T>(a) == *v_cast<T>(b); - } - }; - template<typename T> - struct FilteredComparator<T, /* IsAcceptedType = */ false> { - static bool compare(const QVariant::Private *, const QVariant::Private *) - { - // It is not possible to construct a QVariant containing not fully defined type - Q_ASSERT(false); - return false; - } - }; -public: - QVariantComparator(const QVariant::Private *a, const QVariant::Private *b) - : m_a(a), m_b(b) - { - Q_ASSERT(a->type() == b->type()); + static_assert(!isNothrowQVariantConstructible); // we allocate memory, even if T doesn't + data.shared = customConstructShared(sizeof(T), alignof(T), [=](void *where) { + new (where) T(t); + }); } - - template<typename T> - bool delegate(const T*) - { - return FilteredComparator<T>::compare(m_a, m_b); - } - - bool delegate(const void*) { Q_ASSERT(false); return true; } - bool delegate(const QMetaTypeSwitcher::UnknownType*) - { - return true; // for historical reason invalid variant == invalid variant - } - bool delegate(const QMetaTypeSwitcher::NotBuiltinType*) { return false; } -protected: - const QVariant::Private *m_a; - const QVariant::Private *m_b; -}; - - -Q_CORE_EXPORT const QVariant::Handler *qcoreVariantHandler(); - -template<class Filter> -class QVariantIsNull -{ - /// \internal - /// This class checks if a type T has method called isNull. Result is kept in the Value property - /// TODO Can we somehow generalize it? A macro version? - template<typename T> - class HasIsNullMethod { - struct Yes { char unused[1]; }; - struct No { char unused[2]; }; - static_assert(sizeof(Yes) != sizeof(No)); - - template<class C> static decltype(static_cast<const C*>(nullptr)->isNull(), Yes()) test(int); - template<class C> static No test(...); - public: - static const bool Value = (sizeof(test<T>(0)) == sizeof(Yes)); - }; - - // TODO This part should go to autotests during HasIsNullMethod generalization. - static_assert(!HasIsNullMethod<bool>::Value); - struct SelfTest1 { bool isNull() const; }; - static_assert(HasIsNullMethod<SelfTest1>::Value); - struct SelfTest2 {}; - static_assert(!HasIsNullMethod<SelfTest2>::Value); - struct SelfTest3 : public SelfTest1 {}; - static_assert(HasIsNullMethod<SelfTest3>::Value); - struct SelfTestFinal1 final { bool isNull() const; }; - static_assert(HasIsNullMethod<SelfTestFinal1>::Value); - struct SelfTestFinal2 final {}; - static_assert(!HasIsNullMethod<SelfTestFinal2>::Value); - struct SelfTestFinal3 final : public SelfTest1 {}; - static_assert(HasIsNullMethod<SelfTestFinal3>::Value); - - template<typename T, bool HasIsNull = HasIsNullMethod<T>::Value> - struct CallFilteredIsNull - { - static bool isNull(const QVariant::Private *d) - { - return v_cast<T>(d)->isNull(); - } - }; - template<typename T> - struct CallFilteredIsNull<T, /* HasIsNull = */ false> - { - static bool isNull(const QVariant::Private *d) - { - return PrimitiveIsNull<T>::isNull(d); - } - }; - - template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted> - struct CallIsNull - { - static bool isNull(const QVariant::Private *d) - { - return CallFilteredIsNull<T>::isNull(d); - } - }; - template<typename T> - struct CallIsNull<T, /* IsAcceptedType = */ false> - { - static bool isNull(const QVariant::Private *d) - { - return CallFilteredIsNull<T, false>::isNull(d); - } - }; - -public: - QVariantIsNull(const QVariant::Private *d) - : m_d(d) - {} - template<typename T> - bool delegate(const T*) - { - return CallIsNull<T>::isNull(m_d); - } - // we need that as sizof(void) is undefined and it is needed in HasIsNullMethod - bool delegate(const void *) { Q_ASSERT(false); return m_d->is_null; } - bool delegate(const QMetaTypeSwitcher::UnknownType *) { return m_d->is_null; } - bool delegate(const QMetaTypeSwitcher::NotBuiltinType *) - { - // QVariantIsNull is used only for built-in types - Q_ASSERT(false); - return m_d->is_null; - } -protected: - const QVariant::Private *m_d; -}; - -namespace QVariantPrivate { -Q_CORE_EXPORT void registerHandler(const int /* Modules::Names */ name, const QVariant::Handler *handler); } -#if !defined(QT_NO_DEBUG_STREAM) -template<class Filter> -class QVariantDebugStream -{ - template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted> - struct Filtered { - Filtered(QDebug dbg, QVariant::Private *d) - { - dbg.nospace() << *v_cast<T>(d); - } - }; - template<typename T> - struct Filtered<T, /* IsAcceptedType = */ false> { - Filtered(QDebug /* dbg */, QVariant::Private *) - { - // It is not possible to construct not acccepted type, QVariantConstructor creates an invalid variant for them - Q_ASSERT(false); - } - }; - -public: - QVariantDebugStream(QDebug dbg, QVariant::Private *d) - : m_debugStream(dbg) - , m_d(d) - {} - - template<typename T> - void delegate(const T*) - { - Filtered<T> streamIt(m_debugStream, m_d); - Q_UNUSED(streamIt); - } - - void delegate(const QMetaTypeSwitcher::NotBuiltinType*) - { - // QVariantDebugStream class is used only for a built-in type - Q_ASSERT(false); - } - void delegate(const QMetaTypeSwitcher::UnknownType*) - { - m_debugStream.nospace() << "QVariant::Invalid"; - } - void delegate(const void*) { Q_ASSERT(false); } -private: - QDebug m_debugStream; - QVariant::Private *m_d; -}; -#endif - QT_END_NAMESPACE #endif // QVARIANT_P_H |