1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtCore module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #ifndef QSCOPEDPOINTER_H
43 #define QSCOPEDPOINTER_H
45 #include <QtCore/qglobal.h>
52 struct QScopedPointerDeleter
54 static inline void cleanup(T
*pointer
)
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
);
67 struct QScopedPointerArrayDeleter
69 static inline void cleanup(T
*pointer
)
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
);
81 struct QScopedPointerPodDeleter
83 static inline void cleanup(void *pointer
) { if (pointer
) qFree(pointer
); }
86 template <typename T
, typename Cleanup
= QScopedPointerDeleter
<T
> >
90 typedef T
*QScopedPointer:: *RestrictedBool
;
93 explicit inline QScopedPointer(T
*p
= 0) : d(p
)
97 inline ~QScopedPointer()
100 Cleanup::cleanup(oldD
);
104 inline T
&operator*() const
110 inline T
*operator->() const
116 inline bool operator!() const
121 #if defined(Q_CC_NOKIAX86) || defined(Q_QDOC)
122 inline operator bool() const
124 return isNull() ? 0 : &QScopedPointer::d
;
127 inline operator RestrictedBool() const
129 return isNull() ? 0 : &QScopedPointer::d
;
133 inline T
*data() const
138 inline bool isNull() const
143 inline void reset(T
*other
= 0)
149 Cleanup::cleanup(oldD
);
159 inline void swap(QScopedPointer
<T
, Cleanup
> &other
)
170 Q_DISABLE_COPY(QScopedPointer
)
173 template <class T
, class Cleanup
>
174 inline bool operator==(const QScopedPointer
<T
, Cleanup
> &lhs
, const QScopedPointer
<T
, Cleanup
> &rhs
)
176 return lhs
.data() == rhs
.data();
179 template <class T
, class Cleanup
>
180 inline bool operator!=(const QScopedPointer
<T
, Cleanup
> &lhs
, const QScopedPointer
<T
, Cleanup
> &rhs
)
182 return lhs
.data() != rhs
.data();
185 template <class T
, class Cleanup
>
186 Q_INLINE_TEMPLATE
void qSwap(QScopedPointer
<T
, Cleanup
> &p1
, QScopedPointer
<T
, Cleanup
> &p2
)
189 template <typename T
, typename Cleanup
= QScopedPointerArrayDeleter
<T
> >
190 class QScopedArrayPointer
: public QScopedPointer
<T
, Cleanup
>
193 explicit inline QScopedArrayPointer(T
*p
= 0)
194 : QScopedPointer
<T
, Cleanup
>(p
)
198 inline T
&operator[](int i
)
203 inline const T
&operator[](int i
) const
209 Q_DISABLE_COPY(QScopedArrayPointer
)
212 /* Internal helper class - exposes the data through data_ptr (legacy from QShared).
213 Required for some internal Qt classes, do not use otherwise. */
214 template <typename T
, typename Cleanup
= QScopedPointerDeleter
<T
> >
215 class QCustomScopedPointer
: public QScopedPointer
<T
, Cleanup
>
218 explicit inline QCustomScopedPointer(T
*p
= 0)
219 : QScopedPointer
<T
, Cleanup
>(p
)
223 inline T
*&data_ptr()
229 Q_DISABLE_COPY(QCustomScopedPointer
)
232 /* Internal helper class - a handler for QShared* classes, to be used in QCustomScopedPointer */
233 template <typename T
>
234 class QScopedPointerSharedDeleter
237 static inline void cleanup(T
*d
)
239 if (d
&& !d
->ref
.deref())
245 This class is basically a scoped pointer pointing to a ref-counted object
247 template <typename T
>
248 class QScopedSharedPointer
: public QCustomScopedPointer
<T
, QScopedPointerSharedDeleter
<T
> >
251 explicit inline QScopedSharedPointer(T
*p
= 0)
252 : QCustomScopedPointer
<T
, QScopedPointerSharedDeleter
<T
> >(p
)
258 qAtomicDetach(this->d
);
261 inline void assign(T
*other
)
263 if (this->d
== other
)
269 QScopedPointerSharedDeleter
<T
>::cleanup(oldD
);
273 Q_DISABLE_COPY(QScopedSharedPointer
)
279 #endif // QSCOPEDPOINTER_H