1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtCore module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #ifndef QSCOPEDPOINTER_H
|
---|
43 | #define QSCOPEDPOINTER_H
|
---|
44 |
|
---|
45 | #include <QtCore/qglobal.h>
|
---|
46 |
|
---|
47 | QT_BEGIN_HEADER
|
---|
48 | QT_BEGIN_NAMESPACE
|
---|
49 | QT_MODULE(Core)
|
---|
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 secion 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 secion 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) qFree(pointer); }
|
---|
84 | };
|
---|
85 |
|
---|
86 | template <typename T, typename Cleanup = QScopedPointerDeleter<T> >
|
---|
87 | class QScopedPointer
|
---|
88 | {
|
---|
89 | #ifndef Q_CC_NOKIAX86
|
---|
90 | typedef T *QScopedPointer:: *RestrictedBool;
|
---|
91 | #endif
|
---|
92 | public:
|
---|
93 | explicit inline QScopedPointer(T *p = 0) : d(p)
|
---|
94 | {
|
---|
95 | }
|
---|
96 |
|
---|
97 | inline ~QScopedPointer()
|
---|
98 | {
|
---|
99 | T *oldD = this->d;
|
---|
100 | Cleanup::cleanup(oldD);
|
---|
101 | this->d = 0;
|
---|
102 | }
|
---|
103 |
|
---|
104 | inline T &operator*() const
|
---|
105 | {
|
---|
106 | Q_ASSERT(d);
|
---|
107 | return *d;
|
---|
108 | }
|
---|
109 |
|
---|
110 | inline T *operator->() const
|
---|
111 | {
|
---|
112 | Q_ASSERT(d);
|
---|
113 | return d;
|
---|
114 | }
|
---|
115 |
|
---|
116 | inline bool operator!() const
|
---|
117 | {
|
---|
118 | return !d;
|
---|
119 | }
|
---|
120 |
|
---|
121 | #if defined(Q_CC_NOKIAX86) || defined(Q_QDOC)
|
---|
122 | inline operator bool() const
|
---|
123 | {
|
---|
124 | return isNull() ? 0 : &QScopedPointer::d;
|
---|
125 | }
|
---|
126 | #else
|
---|
127 | inline operator RestrictedBool() const
|
---|
128 | {
|
---|
129 | return isNull() ? 0 : &QScopedPointer::d;
|
---|
130 | }
|
---|
131 | #endif
|
---|
132 |
|
---|
133 | inline T *data() const
|
---|
134 | {
|
---|
135 | return d;
|
---|
136 | }
|
---|
137 |
|
---|
138 | inline bool isNull() const
|
---|
139 | {
|
---|
140 | return !d;
|
---|
141 | }
|
---|
142 |
|
---|
143 | inline void reset(T *other = 0)
|
---|
144 | {
|
---|
145 | if (d == other)
|
---|
146 | return;
|
---|
147 | T *oldD = d;
|
---|
148 | d = other;
|
---|
149 | Cleanup::cleanup(oldD);
|
---|
150 | }
|
---|
151 |
|
---|
152 | inline T *take()
|
---|
153 | {
|
---|
154 | T *oldD = d;
|
---|
155 | d = 0;
|
---|
156 | return oldD;
|
---|
157 | }
|
---|
158 |
|
---|
159 | inline void swap(QScopedPointer<T, Cleanup> &other)
|
---|
160 | {
|
---|
161 | qSwap(d, other.d);
|
---|
162 | }
|
---|
163 |
|
---|
164 | typedef T *pointer;
|
---|
165 |
|
---|
166 | protected:
|
---|
167 | T *d;
|
---|
168 |
|
---|
169 | private:
|
---|
170 | Q_DISABLE_COPY(QScopedPointer)
|
---|
171 | };
|
---|
172 |
|
---|
173 | template <class T, class Cleanup>
|
---|
174 | inline bool operator==(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs)
|
---|
175 | {
|
---|
176 | return lhs.data() == rhs.data();
|
---|
177 | }
|
---|
178 |
|
---|
179 | template <class T, class Cleanup>
|
---|
180 | inline bool operator!=(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs)
|
---|
181 | {
|
---|
182 | return lhs.data() != rhs.data();
|
---|
183 | }
|
---|
184 |
|
---|
185 | template <class T, class Cleanup>
|
---|
186 | Q_INLINE_TEMPLATE void qSwap(QScopedPointer<T, Cleanup> &p1, QScopedPointer<T, Cleanup> &p2)
|
---|
187 | { p1.swap(p2); }
|
---|
188 |
|
---|
189 | template <typename T, typename Cleanup = QScopedPointerArrayDeleter<T> >
|
---|
190 | class QScopedArrayPointer : public QScopedPointer<T, Cleanup>
|
---|
191 | {
|
---|
192 | public:
|
---|
193 | explicit inline QScopedArrayPointer(T *p = 0)
|
---|
194 | : QScopedPointer<T, Cleanup>(p)
|
---|
195 | {
|
---|
196 | }
|
---|
197 |
|
---|
198 | inline T &operator[](int i)
|
---|
199 | {
|
---|
200 | return this->d[i];
|
---|
201 | }
|
---|
202 |
|
---|
203 | inline const T &operator[](int i) const
|
---|
204 | {
|
---|
205 | return this->d[i];
|
---|
206 | }
|
---|
207 |
|
---|
208 | private:
|
---|
209 | Q_DISABLE_COPY(QScopedArrayPointer)
|
---|
210 | };
|
---|
211 |
|
---|
212 | QT_END_NAMESPACE
|
---|
213 | QT_END_HEADER
|
---|
214 |
|
---|
215 | #endif // QSCOPEDPOINTER_H
|
---|