Line data Source code
1 : /****************************************************************************
2 : **
3 : ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 : ** Contact: http://www.qt-project.org/legal
5 : **
6 : ** This file is part of the QtCore module of the Qt Toolkit.
7 : **
8 : ** $QT_BEGIN_LICENSE:LGPL$
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 Digia. For licensing terms and
14 : ** conditions see http://qt.digia.com/licensing. For further information
15 : ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 : ** Foundation and appearing in the file LICENSE.LGPL included in the
21 : ** packaging of this file. Please review the following information to
22 : ** ensure the GNU Lesser General Public License version 2.1 requirements
23 : ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 : **
25 : ** In addition, as a special exception, Digia gives you certain additional
26 : ** rights. These rights are described in the Digia Qt LGPL Exception
27 : ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 : **
29 : ** GNU General Public License Usage
30 : ** Alternatively, this file may be used under the terms of the GNU
31 : ** General Public License version 3.0 as published by the Free Software
32 : ** Foundation and appearing in the file LICENSE.GPL included in the
33 : ** packaging of this file. Please review the following information to
34 : ** ensure the GNU General Public License version 3.0 requirements will be
35 : ** met: http://www.gnu.org/copyleft/gpl.html.
36 : **
37 : **
38 : ** $QT_END_LICENSE$
39 : **
40 : ****************************************************************************/
41 :
42 : #ifndef QSCOPEDPOINTER_H
43 : #define QSCOPEDPOINTER_H
44 :
45 : #include <QtCore/qglobal.h>
46 :
47 : #include <stdlib.h>
48 :
49 : QT_BEGIN_NAMESPACE
50 :
51 : template <typename T>
52 : struct QScopedPointerDeleter
53 : {
54 : static inline void cleanup(T *pointer)
55 : {
56 : // Enforce a complete type.
57 : // If you get a compile error here, read the section on forward declared
58 : // classes in the QScopedPointer documentation.
59 : typedef char IsIncompleteType[ sizeof(T) ? 1 : -1 ];
60 : (void) sizeof(IsIncompleteType);
61 :
62 : delete pointer;
63 : }
64 : };
65 :
66 : template <typename T>
67 : struct QScopedPointerArrayDeleter
68 : {
69 : static inline void cleanup(T *pointer)
70 : {
71 : // Enforce a complete type.
72 : // If you get a compile error here, read the section on forward declared
73 : // classes in the QScopedPointer documentation.
74 : typedef char IsIncompleteType[ sizeof(T) ? 1 : -1 ];
75 : (void) sizeof(IsIncompleteType);
76 :
77 : delete [] pointer;
78 : }
79 : };
80 :
81 : struct QScopedPointerPodDeleter
82 : {
83 : static inline void cleanup(void *pointer) { if (pointer) free(pointer); }
84 : };
85 :
86 : #ifndef QT_NO_QOBJECT
87 : template <typename T>
88 : struct QScopedPointerObjectDeleteLater
89 : {
90 : static inline void cleanup(T *pointer) { if (pointer) pointer->deleteLater(); }
91 : };
92 :
93 : class QObject;
94 : typedef QScopedPointerObjectDeleteLater<QObject> QScopedPointerDeleteLater;
95 : #endif
96 :
97 : template <typename T, typename Cleanup = QScopedPointerDeleter<T> >
98 : class QScopedPointer
99 : {
100 : typedef T *QScopedPointer:: *RestrictedBool;
101 : public:
102 : explicit inline QScopedPointer(T *p = 0) : d(p)
103 : {
104 : }
105 :
106 : inline ~QScopedPointer()
107 : {
108 : T *oldD = this->d;
109 : Cleanup::cleanup(oldD);
110 : }
111 :
112 : inline T &operator*() const
113 : {
114 : Q_ASSERT(d);
115 : return *d;
116 : }
117 :
118 161 : inline T *operator->() const
119 : {
120 161 : Q_ASSERT(d);
121 161 : return d;
122 : }
123 :
124 : inline bool operator!() const
125 : {
126 : return !d;
127 : }
128 :
129 : #if defined(Q_QDOC)
130 : inline operator bool() const
131 : {
132 : return isNull() ? 0 : &QScopedPointer::d;
133 : }
134 : #else
135 : inline operator RestrictedBool() const
136 : {
137 : return isNull() ? 0 : &QScopedPointer::d;
138 : }
139 : #endif
140 :
141 : inline T *data() const
142 : {
143 : return d;
144 : }
145 :
146 : inline bool isNull() const
147 : {
148 : return !d;
149 : }
150 :
151 : inline void reset(T *other = 0)
152 : {
153 : if (d == other)
154 : return;
155 : T *oldD = d;
156 : d = other;
157 : Cleanup::cleanup(oldD);
158 : }
159 :
160 : inline T *take()
161 : {
162 : T *oldD = d;
163 : d = 0;
164 : return oldD;
165 : }
166 :
167 : inline void swap(QScopedPointer<T, Cleanup> &other)
168 : {
169 : qSwap(d, other.d);
170 : }
171 :
172 : typedef T *pointer;
173 :
174 : protected:
175 : T *d;
176 :
177 : private:
178 : Q_DISABLE_COPY(QScopedPointer)
179 : };
180 :
181 : template <class T, class Cleanup>
182 : inline bool operator==(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs)
183 : {
184 : return lhs.data() == rhs.data();
185 : }
186 :
187 : template <class T, class Cleanup>
188 : inline bool operator!=(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs)
189 : {
190 : return lhs.data() != rhs.data();
191 : }
192 :
193 : template <class T, class Cleanup>
194 : Q_INLINE_TEMPLATE void qSwap(QScopedPointer<T, Cleanup> &p1, QScopedPointer<T, Cleanup> &p2)
195 : { p1.swap(p2); }
196 :
197 : QT_END_NAMESPACE
198 : namespace std {
199 : template <class T, class Cleanup>
200 : Q_INLINE_TEMPLATE void swap(QT_PREPEND_NAMESPACE(QScopedPointer)<T, Cleanup> &p1, QT_PREPEND_NAMESPACE(QScopedPointer)<T, Cleanup> &p2)
201 : { p1.swap(p2); }
202 : }
203 : QT_BEGIN_NAMESPACE
204 :
205 :
206 :
207 : namespace QtPrivate {
208 : template <typename X, typename Y> struct QScopedArrayEnsureSameType;
209 : template <typename X> struct QScopedArrayEnsureSameType<X,X> { typedef X* Type; };
210 : template <typename X> struct QScopedArrayEnsureSameType<const X, X> { typedef X* Type; };
211 : }
212 :
213 : template <typename T, typename Cleanup = QScopedPointerArrayDeleter<T> >
214 : class QScopedArrayPointer : public QScopedPointer<T, Cleanup>
215 : {
216 : public:
217 : inline QScopedArrayPointer() : QScopedPointer<T, Cleanup>(0) {}
218 :
219 : template <typename D>
220 : explicit inline QScopedArrayPointer(D *p, typename QtPrivate::QScopedArrayEnsureSameType<T,D>::Type = 0)
221 : : QScopedPointer<T, Cleanup>(p)
222 : {
223 : }
224 :
225 : inline T &operator[](int i)
226 : {
227 : return this->d[i];
228 : }
229 :
230 : inline const T &operator[](int i) const
231 : {
232 : return this->d[i];
233 : }
234 :
235 : private:
236 : explicit inline QScopedArrayPointer(void *) {
237 : // Enforce the same type.
238 :
239 : // If you get a compile error here, make sure you declare
240 : // QScopedArrayPointer with the same template type as you pass to the
241 : // constructor. See also the QScopedPointer documentation.
242 :
243 : // Storing a scalar array as a pointer to a different type is not
244 : // allowed and results in undefined behavior.
245 : }
246 :
247 : Q_DISABLE_COPY(QScopedArrayPointer)
248 : };
249 :
250 : QT_END_NAMESPACE
251 :
252 : #endif // QSCOPEDPOINTER_H
|