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