Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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// Qt-Security score:critical reason:data-parser
4
5#ifndef QVARIANT_P_H
6#define QVARIANT_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include "qvariant.h"
20
22
23inline auto customConstructSharedImpl(size_t size, size_t align)
24{
25 struct Deleter {
26 void operator()(QVariant::PrivateShared *p) const
27 { QVariant::PrivateShared::free(p); }
28 };
29
30 // this is exception-safe
31 std::unique_ptr<QVariant::PrivateShared, Deleter> ptr;
32 ptr.reset(QVariant::PrivateShared::create(size, align));
33 return ptr;
34}
35
36template <typename F> static QVariant::PrivateShared *
37customConstructShared(size_t size, size_t align, F &&construct)
38{
39 auto ptr = customConstructSharedImpl(size, align);
40 construct(ptr->data());
41 return ptr.release();
42}
43
44inline int QVariant::PrivateShared::computeOffset(PrivateShared *ps, size_t align)
45{
46 return int(((quintptr(ps) + sizeof(PrivateShared) + align - 1) & ~(align - 1)) - quintptr(ps));
47}
48
49inline size_t QVariant::PrivateShared::computeAllocationSize(size_t size, size_t align)
50{
51 size += sizeof(PrivateShared);
52 if (align > sizeof(PrivateShared)) {
53 // The alignment is larger than the alignment we can guarantee for the pointer
54 // directly following PrivateShared, so we need to allocate some additional
55 // memory to be able to fit the object into the available memory with suitable
56 // alignment.
57 size += align - sizeof(PrivateShared);
58 }
59 return size;
60}
61
62inline QVariant::PrivateShared *QVariant::PrivateShared::create(size_t size, size_t align)
63{
64 size = computeAllocationSize(size, align);
65 void *data = operator new(size);
66 auto *ps = new (data) QVariant::PrivateShared();
67 ps->offset = computeOffset(ps, align);
68 return ps;
69}
70
71inline void QVariant::PrivateShared::free(PrivateShared *p)
72{
73 p->~PrivateShared();
74 operator delete(p);
75}
76
77inline QVariant::Private::Private(const QtPrivate::QMetaTypeInterface *iface) noexcept
78 : is_shared(false), is_null(false), packedType(quintptr(iface) >> 2)
79{
80 Q_ASSERT((quintptr(iface) & 0x3) == 0);
81}
82
83template <typename T> inline
84QVariant::Private::Private(std::piecewise_construct_t, const T &t)
85 : is_shared(!CanUseInternalSpace<T>), is_null(std::is_same_v<T, std::nullptr_t>)
86{
87 // confirm noexceptness
88 static constexpr bool isNothrowQVariantConstructible = noexcept(QVariant(t));
89 static constexpr bool isNothrowCopyConstructible = std::is_nothrow_copy_constructible_v<T>;
90 static constexpr bool isNothrowCopyAssignable = std::is_nothrow_copy_assignable_v<T>;
91
92 const QtPrivate::QMetaTypeInterface *iface = QtPrivate::qMetaTypeInterfaceForType<T>();
93 Q_ASSERT((quintptr(iface) & 0x3) == 0);
94 packedType = quintptr(iface) >> 2;
95
96 if constexpr (CanUseInternalSpace<T>) {
97 static_assert(isNothrowQVariantConstructible == isNothrowCopyConstructible);
98 static_assert(isNothrowQVariantConstructible == isNothrowCopyAssignable);
99 new (data.data) T(t);
100 } else {
101 static_assert(!isNothrowQVariantConstructible); // we allocate memory, even if T doesn't
102 data.shared = customConstructShared(sizeof(T), alignof(T), [=](void *where) {
103 new (where) T(t);
104 });
105 }
106}
107
108QT_END_NAMESPACE
109
110#endif // QVARIANT_P_H
\inmodule QtCore
Definition qmetatype.h:339
QDataStream & operator>>(QDataStream &s, QVariant &p)
\keyword 16-bit Floating Point Support\inmodule QtCore \inheaderfile QFloat16
Definition qfloat16.h:48
bool isDestructible(const QtPrivate::QMetaTypeInterface *iface) noexcept
bool isMoveConstructible(const QtPrivate::QMetaTypeInterface *iface) noexcept
bool isDefaultConstructible(const QtPrivate::QMetaTypeInterface *iface) noexcept
bool isCopyConstructible(const QtPrivate::QMetaTypeInterface *iface) noexcept
void copyConstruct(const QtPrivate::QMetaTypeInterface *iface, void *where, const void *copy)
void destruct(const QtPrivate::QMetaTypeInterface *iface, void *where)
constexpr int Qt6ToQt5GuiTypeDelta
static bool qIsFloatingPoint(uint tp)
static QPartialOrdering numericCompare(const QVariant::Private *d1, const QVariant::Private *d2)
constexpr int Qt5QQuaternion
static bool qIsNumericType(uint tp)
@ MapFromThreeCount
static bool canBeNumericallyCompared(const QtPrivate::QMetaTypeInterface *iface1, const QtPrivate::QMetaTypeInterface *iface2)
constexpr int Qt5KeySequence
static QPartialOrdering integralCompare(uint promotedType, const QVariant::Private *d1, const QVariant::Private *d2)
static const ushort mapIdFromQt3ToCurrent[MapFromThreeCount]
constexpr int Qt5LastCoreType
T qNumVariantToHelper(const QVariant::Private &d, bool *ok)
constexpr int Qt5FirstGuiType
static bool qvCanConvertMetaObject(QMetaType fromType, QMetaType toType)
constexpr int Qt5RegExp
constexpr int Qt5LastGuiType
static int numericTypePromotion(const QtPrivate::QMetaTypeInterface *iface1, const QtPrivate::QMetaTypeInterface *iface2)
constexpr int Qt5SizePolicy
constexpr int Qt5UserType
static QPartialOrdering pointerCompare(const QVariant::Private *d1, const QVariant::Private *d2)
QT_BEGIN_NAMESPACE auto customConstructSharedImpl(size_t size, size_t align)
Definition qvariant_p.h:23
static QVariant::PrivateShared * customConstructShared(size_t size, size_t align, F &&construct)
Definition qvariant_p.h:37