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 | |
21 | QT_BEGIN_NAMESPACE |
22 | |
23 | template<typename T, typename Tag = typename QtPrivate::TagInfo<T>::TagType> |
24 | class QLazilyAllocated { |
25 | public: |
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); |
38 | private: |
39 | mutable QTaggedPointer<T, Tag> d; |
40 | }; |
41 | |
42 | template<typename T, typename Tag> |
43 | QLazilyAllocated<T, Tag>::QLazilyAllocated() |
44 | { |
45 | } |
46 | |
47 | template<typename T, typename Tag> |
48 | QLazilyAllocated<T, Tag>::~QLazilyAllocated() |
49 | { |
50 | delete d.data(); |
51 | } |
52 | |
53 | template<typename T, typename Tag> |
54 | bool QLazilyAllocated<T, Tag>::isAllocated() const |
55 | { |
56 | return !d.isNull(); |
57 | } |
58 | |
59 | template<typename T, typename Tag> |
60 | T &QLazilyAllocated<T, Tag>::value() |
61 | { |
62 | if (d.isNull()) d = new T; |
63 | return *d; |
64 | } |
65 | |
66 | template<typename T, typename Tag> |
67 | const T &QLazilyAllocated<T, Tag>::value() const |
68 | { |
69 | if (d.isNull()) d = new T; |
70 | return *d; |
71 | } |
72 | |
73 | template<typename T, typename Tag> |
74 | T *QLazilyAllocated<T, Tag>::operator->() const |
75 | { |
76 | return d.data(); |
77 | } |
78 | |
79 | template<typename T, typename Tag> |
80 | Tag QLazilyAllocated<T, Tag>::tag() const |
81 | { |
82 | return d.tag(); |
83 | } |
84 | |
85 | template<typename T, typename Tag> |
86 | void QLazilyAllocated<T, Tag>::setTag(Tag t) |
87 | { |
88 | d.setTag(t); |
89 | } |
90 | |
91 | QT_END_NAMESPACE |
92 | |
93 | #endif // QLAZILYALLOCATED_P_H |
94 | |