1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QVARIANT_P_H
5#define QVARIANT_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include "qvariant.h"
19
20QT_BEGIN_NAMESPACE
21
22inline auto customConstructSharedImpl(size_t size, size_t align)
23{
24 struct Deleter {
25 void operator()(QVariant::PrivateShared *p) const
26 { QVariant::PrivateShared::free(p); }
27 };
28
29 // this is exception-safe
30 std::unique_ptr<QVariant::PrivateShared, Deleter> ptr;
31 ptr.reset(p: QVariant::PrivateShared::create(size, align));
32 return ptr;
33}
34
35template <typename F> static QVariant::PrivateShared *
36customConstructShared(size_t size, size_t align, F &&construct)
37{
38 auto ptr = customConstructSharedImpl(size, align);
39 construct(ptr->data());
40 return ptr.release();
41}
42
43inline int QVariant::PrivateShared::computeOffset(PrivateShared *ps, size_t align)
44{
45 return int(((quintptr(ps) + sizeof(PrivateShared) + align - 1) & ~(align - 1)) - quintptr(ps));
46}
47
48inline size_t QVariant::PrivateShared::computeAllocationSize(size_t size, size_t align)
49{
50 size += sizeof(PrivateShared);
51 if (align > sizeof(PrivateShared)) {
52 // The alignment is larger than the alignment we can guarantee for the pointer
53 // directly following PrivateShared, so we need to allocate some additional
54 // memory to be able to fit the object into the available memory with suitable
55 // alignment.
56 size += align - sizeof(PrivateShared);
57 }
58 return size;
59}
60
61inline QVariant::PrivateShared *QVariant::PrivateShared::create(size_t size, size_t align)
62{
63 size = computeAllocationSize(size, align);
64 void *data = operator new(size);
65 auto *ps = new (data) QVariant::PrivateShared();
66 ps->offset = computeOffset(ps, align);
67 return ps;
68}
69
70inline void QVariant::PrivateShared::free(PrivateShared *p)
71{
72 p->~PrivateShared();
73 operator delete(p);
74}
75
76inline QVariant::Private::Private(const QtPrivate::QMetaTypeInterface *iface) noexcept
77 : is_shared(false), is_null(false), packedType(quintptr(iface) >> 2)
78{
79 Q_ASSERT((quintptr(iface) & 0x3) == 0);
80}
81
82template <typename T> inline
83QVariant::Private::Private(std::piecewise_construct_t, const T &t)
84 : is_shared(!CanUseInternalSpace<T>), is_null(std::is_same_v<T, std::nullptr_t>)
85{
86 // confirm noexceptness
87 static constexpr bool isNothrowQVariantConstructible = noexcept(QVariant(t));
88 static constexpr bool isNothrowCopyConstructible = std::is_nothrow_copy_constructible_v<T>;
89 static constexpr bool isNothrowCopyAssignable = std::is_nothrow_copy_assignable_v<T>;
90
91 const QtPrivate::QMetaTypeInterface *iface = QtPrivate::qMetaTypeInterfaceForType<T>();
92 Q_ASSERT((quintptr(iface) & 0x3) == 0);
93 packedType = quintptr(iface) >> 2;
94
95 if constexpr (CanUseInternalSpace<T>) {
96 static_assert(isNothrowQVariantConstructible == isNothrowCopyConstructible);
97 static_assert(isNothrowQVariantConstructible == isNothrowCopyAssignable);
98 new (data.data) T(t);
99 } else {
100 static_assert(!isNothrowQVariantConstructible); // we allocate memory, even if T doesn't
101 data.shared = customConstructShared(sizeof(T), alignof(T), [=](void *where) {
102 new (where) T(t);
103 });
104 }
105}
106
107QT_END_NAMESPACE
108
109#endif // QVARIANT_P_H
110

source code of qtbase/src/corelib/kernel/qvariant_p.h