1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#ifndef QQUICKDEFERREDPOINTER_P_P_H
38#define QQUICKDEFERREDPOINTER_P_P_H
39
40//
41// W A R N I N G
42// -------------
43//
44// This file is not part of the Qt API. It exists purely as an
45// implementation detail. This header file may change from version to
46// version without notice, or even be removed.
47//
48// We mean it.
49//
50
51#include <QtCore/qglobal.h>
52
53QT_BEGIN_NAMESPACE
54
55template<typename T>
56class QQuickDeferredPointer
57{
58public:
59 inline QQuickDeferredPointer();
60 inline QQuickDeferredPointer(T *);
61 inline QQuickDeferredPointer(const QQuickDeferredPointer<T> &o);
62
63 inline bool isNull() const;
64
65 inline bool wasExecuted() const;
66 inline void setExecuted();
67
68 inline bool isExecuting() const;
69 inline void setExecuting(bool);
70
71 inline operator T*() const;
72 inline operator bool() const;
73
74 inline T *data() const;
75 inline T *operator*() const;
76 inline T *operator->() const;
77
78 inline QQuickDeferredPointer<T> &operator=(T *);
79 inline QQuickDeferredPointer<T> &operator=(const QQuickDeferredPointer &o);
80
81private:
82 quintptr ptr_value = 0;
83
84 static const quintptr WasExecutedBit = 0x1;
85 static const quintptr IsExecutingBit = 0x2;
86 static const quintptr FlagsMask = WasExecutedBit | IsExecutingBit;
87};
88
89template<typename T>
90QQuickDeferredPointer<T>::QQuickDeferredPointer()
91{
92}
93
94template<typename T>
95QQuickDeferredPointer<T>::QQuickDeferredPointer(T *v)
96: ptr_value(quintptr(v))
97{
98 Q_ASSERT((ptr_value & FlagsMask) == 0);
99}
100
101template<typename T>
102QQuickDeferredPointer<T>::QQuickDeferredPointer(const QQuickDeferredPointer<T> &o)
103: ptr_value(o.ptr_value)
104{
105}
106
107template<typename T>
108bool QQuickDeferredPointer<T>::isNull() const
109{
110 return 0 == (ptr_value & (~FlagsMask));
111}
112
113template<typename T>
114bool QQuickDeferredPointer<T>::wasExecuted() const
115{
116 return ptr_value & WasExecutedBit;
117}
118
119template<typename T>
120void QQuickDeferredPointer<T>::setExecuted()
121{
122 ptr_value |= WasExecutedBit;
123}
124
125template<typename T>
126bool QQuickDeferredPointer<T>::isExecuting() const
127{
128 return ptr_value & IsExecutingBit;
129}
130
131template<typename T>
132void QQuickDeferredPointer<T>::setExecuting(bool b)
133{
134 if (b)
135 ptr_value |= IsExecutingBit;
136 else
137 ptr_value &= ~IsExecutingBit;
138}
139
140template<typename T>
141QQuickDeferredPointer<T>::operator T*() const
142{
143 return data();
144}
145
146template<typename T>
147QQuickDeferredPointer<T>::operator bool() const
148{
149 return !isNull();
150}
151
152template<typename T>
153T *QQuickDeferredPointer<T>::data() const
154{
155 return (T *)(ptr_value & ~FlagsMask);
156}
157
158template<typename T>
159T *QQuickDeferredPointer<T>::operator*() const
160{
161 return (T *)(ptr_value & ~FlagsMask);
162}
163
164template<typename T>
165T *QQuickDeferredPointer<T>::operator->() const
166{
167 return (T *)(ptr_value & ~FlagsMask);
168}
169
170template<typename T>
171QQuickDeferredPointer<T> &QQuickDeferredPointer<T>::operator=(T *o)
172{
173 Q_ASSERT((quintptr(o) & FlagsMask) == 0);
174
175 ptr_value = quintptr(o) | (ptr_value & FlagsMask);
176 return *this;
177}
178
179template<typename T>
180QQuickDeferredPointer<T> &QQuickDeferredPointer<T>::operator=(const QQuickDeferredPointer &o)
181{
182 ptr_value = o.ptr_value;
183 return *this;
184}
185
186QT_END_NAMESPACE
187
188#endif // QQUICKDEFERREDPOINTER_P_P_H
189