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 if (align < sizeof(PrivateShared))
47 return sizeof(PrivateShared);
48 return int(((quintptr(ps) + sizeof(PrivateShared) + align - 1) & ~(align - 1)) - quintptr(ps));
49}
50
51inline size_t QVariant::PrivateShared::computeAllocationSize(size_t size, size_t align)
52{
53 size += sizeof(PrivateShared);
54 if (align > sizeof(PrivateShared)) {
55 // The alignment is larger than the alignment we can guarantee for the pointer
56 // directly following PrivateShared, so we need to allocate some additional
57 // memory to be able to fit the object into the available memory with suitable
58 // alignment.
59 size += align - sizeof(PrivateShared);
60 }
61 return size;
62}
63
64inline QVariant::PrivateShared *QVariant::PrivateShared::create(size_t size, size_t align)
65{
66 size = computeAllocationSize(size, align);
67 void *data = operator new(size);
68 auto *ps = new (data) QVariant::PrivateShared();
69 ps->offset = computeOffset(ps, align);
70 return ps;
71}
72
73inline void QVariant::PrivateShared::free(PrivateShared *p)
74{
75 p->~PrivateShared();
76 operator delete(p);
77}
78
79inline QVariant::Private::Private(const QtPrivate::QMetaTypeInterface *iface) noexcept
80 : is_shared(false), is_null(false), packedType(quintptr(iface) >> 2)
81{
82 Q_ASSERT((quintptr(iface) & 0x3) == 0);
83}
84
85template <typename TT>
86QVariant::Private::Private(std::in_place_t, TT &&t)
87 : is_shared{!CanUseInternalSpace<q20::remove_cvref_t<TT>>},
88 is_null{std::is_null_pointer_v<std::remove_reference_t<TT>>}
89{
90 using T = q20::remove_cvref_t<TT>;
91 // confirm noexceptness
92 constexpr bool isNothrowQVariantConstructible = noexcept(QVariant{std::forward<TT>(t)});
93 constexpr bool isNothrowConstructible = std::is_nothrow_constructible_v<T, TT>;
94 constexpr bool isNothrowAssignable = std::is_nothrow_assignable_v<T&, TT>;
95
96 const QtPrivate::QMetaTypeInterface *iface = QtPrivate::qMetaTypeInterfaceForType<T>();
97 Q_ASSERT((quintptr(iface) & 0x3) == 0);
98 packedType = quintptr(iface) >> 2;
99
100 if constexpr (CanUseInternalSpace<T>) {
101 static_assert(isNothrowQVariantConstructible == isNothrowConstructible);
102 static_assert(isNothrowQVariantConstructible == isNothrowAssignable);
103 new (data.data) T(std::forward<TT>(t));
104 } else {
105 static_assert(!isNothrowQVariantConstructible); // we allocate memory, even if T doesn't
106 data.shared = customConstructShared(sizeof(T), alignof(T), [&t](void *where) {
107 new (where) T(std::forward<TT>(t));
108 });
109 }
110}
111
112QT_END_NAMESPACE
113
114#endif // QVARIANT_P_H
\inmodule QtCore
Definition qmetatype.h:395
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
#define BFLOAT16_ENABLED
Definition qvariant.cpp:54
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