aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlcompiler/qdeferredpointer_p.h
blob: e4d5eb6df35c7e57e59ae6d4f320336f63f570b5 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#ifndef QDEFERREDPOINTER_P_H
#define QDEFERREDPOINTER_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 <qtqmlcompilerexports.h>

#include <QtCore/private/qglobal_p.h>
#include <QtCore/qsharedpointer.h>

QT_BEGIN_NAMESPACE

template<typename T>
class QDeferredSharedPointer;

template<typename T>
class QDeferredWeakPointer;

template<typename T>
class QDeferredFactory
{
public:
    bool isValid() const;

private:
    friend class QDeferredSharedPointer<const T>;
    friend class QDeferredWeakPointer<const T>;
    friend class QDeferredSharedPointer<T>;
    friend class QDeferredWeakPointer<T>;
    void populate(const QSharedPointer<T> &) const;
};

template<typename T>
class QDeferredSharedPointer
{
public:
    using Factory = QDeferredFactory<std::remove_const_t<T>>;

    Q_NODISCARD_CTOR QDeferredSharedPointer() = default;

    Q_NODISCARD_CTOR QDeferredSharedPointer(QSharedPointer<T> data)
        : m_data(std::move(data))
    {}

    Q_NODISCARD_CTOR QDeferredSharedPointer(QWeakPointer<T> data)
        : m_data(std::move(data))
    {}

    Q_NODISCARD_CTOR QDeferredSharedPointer(QSharedPointer<T> data, QSharedPointer<Factory> factory)
        : m_data(std::move(data)), m_factory(std::move(factory))
    {
        // You have to provide a valid pointer if you provide a factory. We cannot allocate the
        // pointer for you because then two copies of the same QDeferredSharedPointer will diverge
        // and lazy-load two separate data objects.
        Q_ASSERT(!m_data.isNull() || m_factory.isNull());
    }

    [[nodiscard]] operator QSharedPointer<T>() const
    {
        lazyLoad();
        return m_data;
    }

    operator QDeferredSharedPointer<const T>() const { return { m_data, m_factory }; }

    [[nodiscard]] T &operator*() const { return QSharedPointer<T>(*this).operator*(); }
    [[nodiscard]] T *operator->() const { return QSharedPointer<T>(*this).operator->(); }

    bool isNull() const
    {
        return m_data.isNull();
    }

    explicit operator bool() const noexcept { return !isNull(); }
    bool operator !() const noexcept { return isNull(); }

    [[nodiscard]] T *data() const { return QSharedPointer<T>(*this).data(); }
    [[nodiscard]] T *get() const { return data(); }

    friend size_t qHash(const QDeferredSharedPointer &ptr, size_t seed = 0)
    {
        // This is a hash of the pointer, not the data.
        return qHash(ptr.m_data, seed);
    }

    friend bool operator==(const QDeferredSharedPointer &a, const QDeferredSharedPointer &b)
    {
        // This is a comparison of the pointers, not their data. As we require the pointers to
        // be given in the ctor, we can do this.
        return a.m_data == b.m_data;
    }

    friend bool operator!=(const QDeferredSharedPointer &a, const QDeferredSharedPointer &b)
    {
        return !(a == b);
    }

    friend bool operator<(const QDeferredSharedPointer &a, const QDeferredSharedPointer &b)
    {
        return a.m_data < b.m_data;
    }

    friend bool operator<=(const QDeferredSharedPointer &a, const QDeferredSharedPointer &b)
    {
        return a.m_data <= b.m_data;
    }

    friend bool operator>(const QDeferredSharedPointer &a, const QDeferredSharedPointer &b)
    {
        return a.m_data > b.m_data;
    }

    friend bool operator>=(const QDeferredSharedPointer &a, const QDeferredSharedPointer &b)
    {
        return a.m_data >= b.m_data;
    }

    template <typename U>
    friend bool operator==(const QDeferredSharedPointer &a, const QSharedPointer<U> &b)
    {
        return a.m_data == b;
    }

    template <typename U>
    friend bool operator!=(const QDeferredSharedPointer &a, const QSharedPointer<U> &b)
    {
        return !(a == b);
    }

    template <typename U>
    friend bool operator==(const QSharedPointer<U> &a, const QDeferredSharedPointer &b)
    {
        return b == a;
    }

    template <typename U>
    friend bool operator!=(const QSharedPointer<U> &a, const QDeferredSharedPointer &b)
    {
        return b != a;
    }

    Factory *factory() const
    {
        return (m_factory && m_factory->isValid()) ? m_factory.data() : nullptr;
    }

private:
    friend class QDeferredWeakPointer<T>;

    void lazyLoad() const
    {
        if (Factory *f = factory()) {
            Factory localFactory;
            std::swap(localFactory, *f); // Swap before executing, to avoid recursion
            localFactory.populate(m_data.template constCast<std::remove_const_t<T>>());
        }
    }

    QSharedPointer<T> m_data;
    QSharedPointer<Factory> m_factory;
};

template<typename T>
class QDeferredWeakPointer
{
public:
    using Factory = QDeferredFactory<std::remove_const_t<T>>;

    Q_NODISCARD_CTOR QDeferredWeakPointer() = default;

    Q_NODISCARD_CTOR QDeferredWeakPointer(const QDeferredSharedPointer<T> &strong)
        : m_data(strong.m_data), m_factory(strong.m_factory)
    {
    }

    Q_NODISCARD_CTOR QDeferredWeakPointer(QWeakPointer<T> data, QWeakPointer<Factory> factory)
        : m_data(data), m_factory(factory)
    {}

    [[nodiscard]] operator QWeakPointer<T>() const
    {
        lazyLoad();
        return m_data;
    }

    [[nodiscard]] operator QDeferredSharedPointer<T>() const
    {
        return QDeferredSharedPointer<T>(m_data.toStrongRef(), m_factory.toStrongRef());
    }

    operator QDeferredWeakPointer<const T>() const { return {m_data, m_factory}; }

    [[nodiscard]] QSharedPointer<T> toStrongRef() const
    {
        return QWeakPointer<T>(*this).toStrongRef();
    }

    bool isNull() const { return m_data.isNull(); }

    explicit operator bool() const noexcept { return !isNull(); }
    bool operator !() const noexcept { return isNull(); }

    friend bool operator==(const QDeferredWeakPointer &a, const QDeferredWeakPointer &b)
    {
        return a.m_data == b.m_data;
    }

    friend bool operator!=(const QDeferredWeakPointer &a, const QDeferredWeakPointer &b)
    {
        return !(a == b);
    }

private:
    void lazyLoad() const
    {
        if (m_factory) {
            auto factory = m_factory.toStrongRef();
            if (factory->isValid()) {
                Factory localFactory;
                std::swap(localFactory, *factory); // Swap before executing, to avoid recursion
                localFactory.populate(
                        m_data.toStrongRef().template constCast<std::remove_const_t<T>>());
            }
        }
    }

    QWeakPointer<T> m_data;
    QWeakPointer<Factory> m_factory;
};


QT_END_NAMESPACE

#endif // QDEFERREDPOINTER_P_H