1// Copyright (C) 2020 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
4#ifndef QLAZILYALLOCATED_P_H
5#define QLAZILYALLOCATED_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/private/qglobal_p.h>
19#include <QtCore/qtaggedpointer.h>
20
21QT_BEGIN_NAMESPACE
22
23template<typename T, typename Tag = typename QtPrivate::TagInfo<T>::TagType>
24class QLazilyAllocated {
25public:
26 inline QLazilyAllocated();
27 inline ~QLazilyAllocated();
28
29 inline bool isAllocated() const;
30
31 inline T *operator->() const;
32
33 inline T &value();
34 inline const T &value() const;
35
36 inline Tag tag() const;
37 inline void setTag(Tag t);
38private:
39 mutable QTaggedPointer<T, Tag> d;
40};
41
42template<typename T, typename Tag>
43QLazilyAllocated<T, Tag>::QLazilyAllocated()
44{
45}
46
47template<typename T, typename Tag>
48QLazilyAllocated<T, Tag>::~QLazilyAllocated()
49{
50 delete d.data();
51}
52
53template<typename T, typename Tag>
54bool QLazilyAllocated<T, Tag>::isAllocated() const
55{
56 return !d.isNull();
57}
58
59template<typename T, typename Tag>
60T &QLazilyAllocated<T, Tag>::value()
61{
62 if (d.isNull()) d = new T;
63 return *d;
64}
65
66template<typename T, typename Tag>
67const T &QLazilyAllocated<T, Tag>::value() const
68{
69 if (d.isNull()) d = new T;
70 return *d;
71}
72
73template<typename T, typename Tag>
74T *QLazilyAllocated<T, Tag>::operator->() const
75{
76 return d.data();
77}
78
79template<typename T, typename Tag>
80Tag QLazilyAllocated<T, Tag>::tag() const
81{
82 return d.tag();
83}
84
85template<typename T, typename Tag>
86void QLazilyAllocated<T, Tag>::setTag(Tag t)
87{
88 d.setTag(t);
89}
90
91QT_END_NAMESPACE
92
93#endif // QLAZILYALLOCATED_P_H
94

source code of qtdeclarative/src/qml/qml/ftw/qlazilyallocated_p.h