PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/qt-everywhere-opensource-src-4.8.2/src/corelib/tools/qscopedpointer.cpp

#
C++ | 282 lines | 3 code | 23 blank | 256 comment | 0 complexity | c0dc817459d307e080241f7490103ec1 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-4.0, LGPL-3.0, GPL-2.0, LGPL-2.0, LGPL-2.1, GPL-3.0
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the QtCore module of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include "qscopedpointer.h"
  42. QT_BEGIN_NAMESPACE
  43. /*!
  44. \class QScopedPointer
  45. \brief The QScopedPointer class stores a pointer to a dynamically allocated object, and deletes it upon destruction.
  46. \since 4.6
  47. \reentrant
  48. \ingroup misc
  49. Managing heap allocated objects manually is hard and error prone, with the
  50. common result that code leaks memory and is hard to maintain.
  51. QScopedPointer is a small utility class that heavily simplifies this by
  52. assigning stack-based memory ownership to heap allocations, more generally
  53. called resource acquisition is initialization(RAII).
  54. QScopedPointer guarantees that the object pointed to will get deleted when
  55. the current scope disappears.
  56. Consider this function which does heap allocations, and have various exit points:
  57. \snippet doc/src/snippets/code/src_corelib_tools_qscopedpointer.cpp 0
  58. It's encumbered by the manual delete calls. With QScopedPointer, the code
  59. can be simplified to:
  60. \snippet doc/src/snippets/code/src_corelib_tools_qscopedpointer.cpp 1
  61. The code the compiler generates for QScopedPointer is the same as when
  62. writing it manually. Code that makes use of \a delete are candidates for
  63. QScopedPointer usage (and if not, possibly another type of smart pointer
  64. such as QSharedPointer). QScopedPointer intentionally has no copy
  65. constructor or assignment operator, such that ownership and lifetime is
  66. clearly communicated.
  67. The const qualification on a regular C++ pointer can also be expressed with
  68. a QScopedPointer:
  69. \snippet doc/src/snippets/code/src_corelib_tools_qscopedpointer.cpp 2
  70. \section1 Custom cleanup handlers
  71. Arrays as well as pointers that have been allocated with \c malloc must
  72. not be deleted using \c delete. QScopedPointer's second template parameter
  73. can be used for custom cleanup handlers.
  74. The following custom cleanup handlers exist:
  75. \list
  76. \i QScopedPointerDeleter - the default, deletes the pointer using \c delete
  77. \i QScopedPointerArrayDeleter - deletes the pointer using \c{delete []}. Use
  78. this handler for pointers that were allocated with \c{new []}.
  79. \i QScopedPointerPodDeleter - deletes the pointer using \c{free()}. Use this
  80. handler for pointers that were allocated with \c{malloc()}.
  81. \endlist
  82. You can pass your own classes as handlers, provided that they have a public
  83. static function \c{void cleanup(T *pointer)}.
  84. \snippet doc/src/snippets/code/src_corelib_tools_qscopedpointer.cpp 5
  85. \section1 Forward Declared Pointers
  86. Classes that are forward declared can be used within QScopedPointer, as
  87. long as the destructor of the forward declared class is available whenever
  88. a QScopedPointer needs to clean up.
  89. Concretely, this means that all classes containing a QScopedPointer that
  90. points to a forward declared class must have non-inline constructors,
  91. destructors and assignment operators:
  92. \snippet doc/src/snippets/code/src_corelib_tools_qscopedpointer.cpp 4
  93. Otherwise, the compiler output a warning about not being able to destruct
  94. \c MyPrivateClass.
  95. \sa QSharedPointer
  96. */
  97. /*! \typedef QScopedPointer::pointer
  98. \internal
  99. */
  100. /*!
  101. \fn QScopedPointer::QScopedPointer(T *p = 0)
  102. Constructs this QScopedPointer instance and sets its pointer to \a p.
  103. */
  104. /*!
  105. \fn QScopedPointer::~QScopedPointer()
  106. Destroys this QScopedPointer object. Delete the object its pointer points
  107. to.
  108. */
  109. /*!
  110. \fn T *QScopedPointer::data() const
  111. Returns the value of the pointer referenced by this object. QScopedPointer
  112. still owns the object pointed to.
  113. */
  114. /*!
  115. \fn T &QScopedPointer::operator*() const
  116. Provides access to the scoped pointer's object.
  117. If the contained pointer is \c null, behavior is undefined.
  118. \sa isNull()
  119. */
  120. /*!
  121. \fn T *QScopedPointer::operator->() const
  122. Provides access to the scoped pointer's object.
  123. If the contained pointer is \c null, behavior is undefined.
  124. \sa isNull()
  125. */
  126. /*!
  127. \fn QScopedPointer::operator bool() const
  128. Returns \c true if this object is not \c null. This function is suitable
  129. for use in \tt if-constructs, like:
  130. \snippet doc/src/snippets/code/src_corelib_tools_qscopedpointer.cpp 3
  131. \sa isNull()
  132. */
  133. /*!
  134. \fn bool operator==(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs)
  135. Equality operator. Returns true if the scoped pointers
  136. \a lhs and \a rhs are pointing to the same object.
  137. Otherwise returns false.
  138. */
  139. /*!
  140. \fn bool operator!=(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs)
  141. Inequality operator. Returns true if the scoped pointers
  142. \a lhs and \a rhs are \e not pointing to the same object.
  143. Otherwise returns false.
  144. */
  145. /*!
  146. \fn bool QScopedPointer::isNull() const
  147. Returns \c true if this object is holding a pointer that is \c null.
  148. */
  149. /*!
  150. \fn void QScopedPointer::reset(T *other = 0)
  151. Deletes the existing object it is pointing to if any, and sets its pointer to
  152. \a other. QScopedPointer now owns \a other and will delete it in its
  153. destructor.
  154. */
  155. /*!
  156. \fn T *QScopedPointer::take()
  157. Returns the value of the pointer referenced by this object. The pointer of this
  158. QScopedPointer object will be reset to \c null.
  159. Callers of this function take ownership of the pointer.
  160. */
  161. /*! \fn bool QScopedPointer::operator!() const
  162. Returns \c true if the pointer referenced by this object is \c null, otherwise
  163. returns \c false.
  164. \sa isNull()
  165. */
  166. /*! \fn void QScopedPointer::swap(QScopedPointer<T, Cleanup> &other)
  167. Swap this pointer with \a other.
  168. */
  169. /*!
  170. \class QScopedArrayPointer
  171. \brief The QScopedArrayPointer class stores a pointer to a
  172. dynamically allocated array of objects, and deletes it upon
  173. destruction.
  174. \since 4.6
  175. \reentrant
  176. \ingroup misc
  177. A QScopedArrayPointer is a QScopedPointer that defaults to
  178. deleting the object it is pointing to with the delete[] operator. It
  179. also features operator[] for convenience, so we can write:
  180. \code
  181. void foo()
  182. {
  183. QScopedArrayPointer<int> i(new int[10]);
  184. i[2] = 42;
  185. ...
  186. return; // our integer array is now deleted using delete[]
  187. }
  188. \endcode
  189. */
  190. /*!
  191. \fn QScopedArrayPointer::QScopedArrayPointer()
  192. Constructs a QScopedArrayPointer instance.
  193. */
  194. /*!
  195. \fn T *QScopedArrayPointer::operator[](int i)
  196. Provides access to entry \a i of the scoped pointer's array of
  197. objects.
  198. If the contained pointer is \c null, behavior is undefined.
  199. \sa isNull()
  200. */
  201. /*!
  202. \fn T *QScopedArrayPointer::operator[](int i) const
  203. Provides access to entry \a i of the scoped pointer's array of
  204. objects.
  205. If the contained pointer is \c null, behavior is undefined.
  206. \sa isNull()
  207. */
  208. QT_END_NAMESPACE