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
qlazilyallocated_p.h
Go to the documentation of this file.
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// Qt-Security score:significant
4
5#ifndef QLAZILYALLOCATED_P_H
6#define QLAZILYALLOCATED_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 <QtCore/private/qglobal_p.h>
20#include <QtCore/qtaggedpointer.h>
21
22QT_BEGIN_NAMESPACE
23
24template<typename T, typename Tag = typename QtPrivate::TagInfo<T>::TagType>
25class QLazilyAllocated {
26public:
27 inline QLazilyAllocated();
28 inline ~QLazilyAllocated();
29
30 inline bool isAllocated() const;
31
32 inline T *operator->() const;
33
34 inline T &value();
35 inline const T &value() const;
36
37 inline Tag tag() const;
38 inline void setTag(Tag t);
39private:
40 mutable QTaggedPointer<T, Tag> d;
41};
42
43template<typename T, typename Tag>
44QLazilyAllocated<T, Tag>::QLazilyAllocated()
45{
46}
47
48template<typename T, typename Tag>
49QLazilyAllocated<T, Tag>::~QLazilyAllocated()
50{
51 delete d.data();
52}
53
54template<typename T, typename Tag>
55bool QLazilyAllocated<T, Tag>::isAllocated() const
56{
57 return !d.isNull();
58}
59
60template<typename T, typename Tag>
61T &QLazilyAllocated<T, Tag>::value()
62{
63 if (d.isNull()) d = new T;
64 return *d;
65}
66
67template<typename T, typename Tag>
68const T &QLazilyAllocated<T, Tag>::value() const
69{
70 if (d.isNull()) d = new T;
71 return *d;
72}
73
74template<typename T, typename Tag>
75T *QLazilyAllocated<T, Tag>::operator->() const
76{
77 return d.data();
78}
79
80template<typename T, typename Tag>
81Tag QLazilyAllocated<T, Tag>::tag() const
82{
83 return d.tag();
84}
85
86template<typename T, typename Tag>
87void QLazilyAllocated<T, Tag>::setTag(Tag t)
88{
89 d.setTag(t);
90}
91
92QT_END_NAMESPACE
93
94#endif // QLAZILYALLOCATED_P_H