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 TT>
84QVariant::Private::Private(std::in_place_t, TT &&t)
85 : is_shared{!CanUseInternalSpace<q20::remove_cvref_t<TT>>},
86 is_null{std::is_null_pointer_v<std::remove_reference_t<TT>>}
87{
88 using T = q20::remove_cvref_t<TT>;
89 // confirm noexceptness
90 constexpr bool isNothrowQVariantConstructible = noexcept(QVariant{std::forward<TT>(t)});
91 constexpr bool isNothrowConstructible = std::is_nothrow_constructible_v<T, TT>;
92 constexpr bool isNothrowAssignable = std::is_nothrow_assignable_v<T&, TT>;
93
94 const QtPrivate::QMetaTypeInterface *iface = QtPrivate::qMetaTypeInterfaceForType<T>();
95 Q_ASSERT((quintptr(iface) & 0x3) == 0);
96 packedType = quintptr(iface) >> 2;
97
98 if constexpr (CanUseInternalSpace<T>) {
99 static_assert(isNothrowQVariantConstructible == isNothrowConstructible);
100 static_assert(isNothrowQVariantConstructible == isNothrowAssignable);
101 new (data.data) T(std::forward<TT>(t));
102 } else {
103 static_assert(!isNothrowQVariantConstructible); // we allocate memory, even if T doesn't
104 data.shared = customConstructShared(sizeof(T), alignof(T), [&t](void *where) {
105 new (where) T(std::forward<TT>(t));
106 });
107 }
108}
109
110QT_END_NAMESPACE
111
112#endif // QVARIANT_P_H
\inmodule QtCore
Definition qmetatype.h:383
QDataStream & operator>>(QDataStream &s, QVariant &p)
\keyword 16-bit Floating Point Support\inmodule QtCore \inheaderfile QFloat16
Definition qfloat16.h:57
Combined button and popup list for selecting options.
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)
QCborSimpleType
Definition qcborcommon.h:29
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
#define MAKE_CTOR_BY_VALUE(...)
#define MAKE_CTOR_BY_REF(...)
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