diff options
Diffstat (limited to 'src/corelib/kernel/qvariant_p.h')
-rw-r--r-- | src/corelib/kernel/qvariant_p.h | 133 |
1 files changed, 83 insertions, 50 deletions
diff --git a/src/corelib/kernel/qvariant_p.h b/src/corelib/kernel/qvariant_p.h index ef6042d745..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,22 +15,92 @@ // We mean it. // -#include <QtCore/qglobal.h> -#include <QtCore/qvariant.h> -#include <QtCore/private/qmetatype_p.h> +#include "qvariant.h" QT_BEGIN_NAMESPACE -template <class T> -inline void v_construct(QVariant::Private *x, const T &t) +inline auto customConstructSharedImpl(size_t size, size_t align) { - if constexpr (QVariant::Private::CanUseInternalSpace<T>) { - new (&x->data) T(t); - x->is_shared = false; + struct Deleter { + void operator()(QVariant::PrivateShared *p) const + { QVariant::PrivateShared::free(p); } + }; + + // this is exception-safe + std::unique_ptr<QVariant::PrivateShared, Deleter> ptr; + ptr.reset(QVariant::PrivateShared::create(size, align)); + return ptr; +} + +template <typename F> static QVariant::PrivateShared * +customConstructShared(size_t size, size_t align, F &&construct) +{ + auto ptr = customConstructSharedImpl(size, align); + construct(ptr->data()); + return ptr.release(); +} + +inline int QVariant::PrivateShared::computeOffset(PrivateShared *ps, size_t align) +{ + return int(((quintptr(ps) + sizeof(PrivateShared) + align - 1) & ~(align - 1)) - quintptr(ps)); +} + +inline size_t QVariant::PrivateShared::computeAllocationSize(size_t size, size_t align) +{ + 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; +} + +inline QVariant::PrivateShared *QVariant::PrivateShared::create(size_t size, size_t align) +{ + size = computeAllocationSize(size, align); + void *data = operator new(size); + auto *ps = new (data) QVariant::PrivateShared(); + ps->offset = computeOffset(ps, align); + return ps; +} + +inline void QVariant::PrivateShared::free(PrivateShared *p) +{ + p->~PrivateShared(); + operator delete(p); +} + +inline QVariant::Private::Private(const QtPrivate::QMetaTypeInterface *iface) noexcept + : is_shared(false), is_null(false), packedType(quintptr(iface) >> 2) +{ + Q_ASSERT((quintptr(iface) & 0x3) == 0); +} + +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>) +{ + // 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>; + + const QtPrivate::QMetaTypeInterface *iface = QtPrivate::qMetaTypeInterfaceForType<T>(); + Q_ASSERT((quintptr(iface) & 0x3) == 0); + packedType = quintptr(iface) >> 2; + + if constexpr (CanUseInternalSpace<T>) { + static_assert(isNothrowQVariantConstructible == isNothrowCopyConstructible); + static_assert(isNothrowQVariantConstructible == isNothrowCopyAssignable); + new (data.data) T(t); } else { - x->data.shared = QVariant::PrivateShared::create(QtPrivate::qMetaTypeInterfaceForType<T>()); - new (x->data.shared->data()) T(t); - x->is_shared = true; + static_assert(!isNothrowQVariantConstructible); // we allocate memory, even if T doesn't + data.shared = customConstructShared(sizeof(T), alignof(T), [=](void *where) { + new (where) T(t); + }); } } |