Qt 6.x
The Qt SDK
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
22template <typename F> static QVariant::PrivateShared *
23customConstructShared(size_t size, size_t align, F &&construct)
24{
25 struct Deleter {
26 void operator()(QVariant::PrivateShared *p) const
28 };
29
30 // this is exception-safe
31 std::unique_ptr<QVariant::PrivateShared, Deleter> ptr;
33 construct(ptr->data());
34 return ptr.release();
35}
36
38{
39 return int(((quintptr(ps) + sizeof(PrivateShared) + align - 1) & ~(align - 1)) - quintptr(ps));
40}
41
42inline size_t QVariant::PrivateShared::computeAllocationSize(size_t size, size_t align)
43{
44 size += sizeof(PrivateShared);
45 if (align > sizeof(PrivateShared)) {
46 // The alignment is larger than the alignment we can guarantee for the pointer
47 // directly following PrivateShared, so we need to allocate some additional
48 // memory to be able to fit the object into the available memory with suitable
49 // alignment.
50 size += align - sizeof(PrivateShared);
51 }
52 return size;
53}
54
56{
57 size = computeAllocationSize(size, align);
58 void *data = operator new(size);
59 auto *ps = new (data) QVariant::PrivateShared();
60 ps->offset = computeOffset(ps, align);
61 return ps;
62}
63
65{
66 p->~PrivateShared();
67 operator delete(p);
68}
69
71 : is_shared(false), is_null(false), packedType(quintptr(iface) >> 2)
72{
73 Q_ASSERT((quintptr(iface) & 0x3) == 0);
74}
75
76template <typename T> inline
77QVariant::Private::Private(std::piecewise_construct_t, const T &t)
78 : is_shared(!CanUseInternalSpace<T>), is_null(std::is_same_v<T, std::nullptr_t>)
79{
80 // confirm noexceptness
81 static constexpr bool isNothrowQVariantConstructible = noexcept(QVariant(t));
82 static constexpr bool isNothrowCopyConstructible = std::is_nothrow_copy_constructible_v<T>;
83 static constexpr bool isNothrowCopyAssignable = std::is_nothrow_copy_assignable_v<T>;
84
85 const QtPrivate::QMetaTypeInterface *iface = QtPrivate::qMetaTypeInterfaceForType<T>();
86 Q_ASSERT((quintptr(iface) & 0x3) == 0);
87 packedType = quintptr(iface) >> 2;
88
89 if constexpr (CanUseInternalSpace<T>) {
90 static_assert(isNothrowQVariantConstructible == isNothrowCopyConstructible);
91 static_assert(isNothrowQVariantConstructible == isNothrowCopyAssignable);
92 new (data.data) T(t);
93 } else {
94 static_assert(!isNothrowQVariantConstructible); // we allocate memory, even if T doesn't
95 data.shared = customConstructShared(sizeof(T), alignof(T), [=](void *where) {
96 new (where) T(t);
97 });
98 }
99}
100
102
103#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:217
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:72
static QT_BEGIN_NAMESPACE QVariant::PrivateShared * customConstructShared(size_t size, size_t align, F &&construct)
Definition qvariant_p.h:23
static void free(PrivateShared *p)
Definition qvariant_p.h:64
static PrivateShared * create(size_t size, size_t align)
Definition qvariant_p.h:55
static size_t computeAllocationSize(size_t size, size_t align)
Definition qvariant_p.h:42
static int computeOffset(PrivateShared *ps, size_t align)
Definition qvariant_p.h:37
quintptr packedType
Definition qvariant.h:117
constexpr Private() noexcept
Definition qvariant.h:119