Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qvariant_p.h
Go to the documentation of this file.
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
21
22inline auto customConstructSharedImpl(size_t size, size_t align)
23{
24 struct Deleter {
25 void operator()(QVariant::PrivateShared *p) const
27 };
28
29 // this is exception-safe
30 std::unique_ptr<QVariant::PrivateShared, Deleter> ptr;
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
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
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
71{
72 p->~PrivateShared();
73 operator delete(p);
74}
75
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
108
109#endif // QVARIANT_P_H
void * data()
Returns a pointer to the contained object as a generic void* that can be written to.
QVariant() noexcept
Constructs an invalid variant.
Definition qvariant.h:218
Combined button and popup list for selecting options.
static ControlElement< T > * ptr(QWidget *widget)
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLdouble GLdouble t
Definition qopenglext.h:243
GLfloat GLfloat p
[1]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
size_t quintptr
Definition qtypes.h:167
QT_BEGIN_NAMESPACE auto customConstructSharedImpl(size_t size, size_t align)
Definition qvariant_p.h:22
static QVariant::PrivateShared * customConstructShared(size_t size, size_t align, F &&construct)
Definition qvariant_p.h:36
static void free(PrivateShared *p)
Definition qvariant_p.h:70
static PrivateShared * create(size_t size, size_t align)
Definition qvariant_p.h:61
static size_t computeAllocationSize(size_t size, size_t align)
Definition qvariant_p.h:48
static int computeOffset(PrivateShared *ps, size_t align)
Definition qvariant_p.h:43
quintptr packedType
Definition qvariant.h:118
constexpr Private() noexcept
Definition qvariant.h:120