summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qvariant_p.h
blob: d2a739093869cd77a1ae1d607c0c84ccf27a1052 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QVARIANT_P_H
#define QVARIANT_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "qvariant.h"

QT_BEGIN_NAMESPACE

inline auto customConstructSharedImpl(size_t size, size_t align)
{
    struct Deleter {
        void operator()(QVariant::PrivateShared *p) const
        { QVariant::PrivateShared::free(p); }
    };

    // this is exception-safe
    std::unique_ptr<QVariant::PrivateShared, Deleter> ptr;
    ptr.reset(QVariant::PrivateShared::create(size, align));
    return ptr;
}

template <typename F> static QVariant::PrivateShared *
customConstructShared(size_t size, size_t align, F &&construct)
{
    auto ptr = customConstructSharedImpl(size, align);
    construct(ptr->data());
    return ptr.release();
}

inline int QVariant::PrivateShared::computeOffset(PrivateShared *ps, size_t align)
{
    return int(((quintptr(ps) + sizeof(PrivateShared) + align - 1) & ~(align - 1)) - quintptr(ps));
}

inline size_t QVariant::PrivateShared::computeAllocationSize(size_t size, size_t align)
{
    size += sizeof(PrivateShared);
    if (align > sizeof(PrivateShared)) {
        // The alignment is larger than the alignment we can guarantee for the pointer
        // directly following PrivateShared, so we need to allocate some additional
        // memory to be able to fit the object into the available memory with suitable
        // alignment.
        size += align - sizeof(PrivateShared);
    }
    return size;
}

inline QVariant::PrivateShared *QVariant::PrivateShared::create(size_t size, size_t align)
{
    size = computeAllocationSize(size, align);
    void *data = operator new(size);
    auto *ps = new (data) QVariant::PrivateShared();
    ps->offset = computeOffset(ps, align);
    return ps;
}

inline void QVariant::PrivateShared::free(PrivateShared *p)
{
    p->~PrivateShared();
    operator delete(p);
}

inline QVariant::Private::Private(const QtPrivate::QMetaTypeInterface *iface) noexcept
    : is_shared(false), is_null(false), packedType(quintptr(iface) >> 2)
{
    Q_ASSERT((quintptr(iface) & 0x3) == 0);
}

template <typename T> inline
QVariant::Private::Private(std::piecewise_construct_t, const T &t)
    : is_shared(!CanUseInternalSpace<T>), is_null(std::is_same_v<T, std::nullptr_t>)
{
    // confirm noexceptness
    static constexpr bool isNothrowQVariantConstructible = noexcept(QVariant(t));
    static constexpr bool isNothrowCopyConstructible = std::is_nothrow_copy_constructible_v<T>;
    static constexpr bool isNothrowCopyAssignable = std::is_nothrow_copy_assignable_v<T>;

    const QtPrivate::QMetaTypeInterface *iface = QtPrivate::qMetaTypeInterfaceForType<T>();
    Q_ASSERT((quintptr(iface) & 0x3) == 0);
    packedType = quintptr(iface) >> 2;

    if constexpr (CanUseInternalSpace<T>) {
        static_assert(isNothrowQVariantConstructible == isNothrowCopyConstructible);
        static_assert(isNothrowQVariantConstructible == isNothrowCopyAssignable);
        new (data.data) T(t);
    } else {
        static_assert(!isNothrowQVariantConstructible); // we allocate memory, even if T doesn't
        data.shared = customConstructShared(sizeof(T), alignof(T), [=](void *where) {
            new (where) T(t);
        });
    }
}

QT_END_NAMESPACE

#endif // QVARIANT_P_H