scopedptr.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/scopedptr.h
  3. // Purpose: interface of wxScopedPtr
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxScopedPtr
  9. This is a simple scoped smart pointer implementation that is similar to
  10. the Boost smart pointers (see http://www.boost.org) but rewritten
  11. to use macros instead.
  12. Since wxWidgets 2.9.0 there is also a templated version of this class
  13. with the same name. See wxScopedPtr<T>.
  14. A smart pointer holds a pointer to an object. The memory used by the object is
  15. deleted when the smart pointer goes out of scope. This class is different from
  16. the @c std::auto_ptr<> in so far as it doesn't provide copy constructor
  17. nor assignment operator. This limits what you can do with it but is much less
  18. surprising than the "destructive copy" behaviour of the standard class.
  19. @b Example:
  20. Below is an example of using a wxWidgets scoped smart pointer and pointer array.
  21. @code
  22. class MyClass{ ... };
  23. // declare a smart pointer to a MyClass called wxMyClassPtr
  24. wxDECLARE_SCOPED_PTR(MyClass, wxMyClassPtr)
  25. // declare a smart pointer to an array of chars
  26. wxDECLARE_SCOPED_ARRAY(char, wxCharArray)
  27. ...
  28. // define the first pointer class, must be complete
  29. wxDEFINE_SCOPED_PTR(MyClass, wxMyClassPtr)
  30. // define the second pointer class
  31. wxDEFINE_SCOPED_ARRAY(char, wxCharArray)
  32. // create an object with a new pointer to MyClass
  33. wxMyClassPtr theObj(new MyClass());
  34. // reset the pointer (deletes the previous one)
  35. theObj.reset(new MyClass());
  36. // access the pointer
  37. theObj->MyFunc();
  38. // create an object with a new array of chars
  39. wxCharArray theCharObj(new char[100]);
  40. // access the array
  41. theCharObj[0] = "!";
  42. @endcode
  43. @section scopedptr_newpointers Declaring new smart pointer types
  44. To declare the smart pointer class @c CLASSNAME containing pointer to
  45. a (possibly incomplete) type @c TYPE you should use
  46. @code
  47. wxDECLARE_SCOPED_PTR( TYPE, // type of the values
  48. CLASSNAME ); // name of the class
  49. @endcode
  50. And later, when @c TYPE is fully defined, you must also use
  51. @code
  52. wxDEFINE_SCOPED_PTR( TYPE, CLASSNAME );
  53. @endcode
  54. to implement the scoped pointer class.
  55. The first argument of these macro is the pointer type, the second is the name
  56. of the new smart pointer class being created. Below we will use wxScopedPtr
  57. to represent the scoped pointer class, but the user may create the class with
  58. any legal name.
  59. Alternatively, if you don't have to separate the point of declaration and
  60. definition of this class and if you accept the standard naming convention,
  61. that is that the scoped pointer for the class @c Foo is called @c FooPtr,
  62. you can use a single macro which replaces two macros above:
  63. @code
  64. wxDEFINE_SCOPED_PTR_TYPE( TYPE );
  65. @endcode
  66. Once again, in this cass @c CLASSNAME will be @c TYPEPtr.
  67. @library{wxbase}
  68. @category{smartpointers}
  69. @see wxScopedArray
  70. */
  71. class wxScopedPtr
  72. {
  73. public:
  74. /**
  75. Creates the smart pointer with the given pointer or none if @NULL.
  76. On compilers that support it, this uses the explicit keyword.
  77. */
  78. explicit wxScopedPtr(type* T = NULL);
  79. /**
  80. Destructor frees the pointer help by this object if it is not @NULL.
  81. */
  82. ~wxScopedPtr();
  83. /**
  84. This operator gets the pointer stored in the smart pointer or returns
  85. @NULL if there is none.
  86. */
  87. T* get() const;
  88. /**
  89. This operator works like the standard C++ pointer operator to return the object
  90. being pointed to by the pointer.
  91. If the internal pointer is @NULL this method will cause an assert in debug mode.
  92. */
  93. T& operator *() const;
  94. /**
  95. Smart pointer member access. Returns pointer to its object.
  96. If the internal pointer is @NULL this method will cause an assert in debug mode.
  97. */
  98. T* operator ->() const;
  99. /**
  100. Returns the currently hold pointer and resets the smart pointer object to
  101. @NULL.
  102. @remarks
  103. After a call to this function the caller is responsible for deleting the
  104. pointer.
  105. */
  106. T* release();
  107. /**
  108. Deletes the currently held pointer and sets it to @a p or to @NULL if no
  109. arguments are specified.
  110. @note
  111. This function does check to make sure that the pointer you are assigning
  112. is not the same pointer that is already stored.
  113. */
  114. reset(T* p = NULL);
  115. /**
  116. Swap the pointer inside the smart pointer with @a other. The pointer being
  117. swapped must be of the same type (hence the same class name).
  118. */
  119. swap(wxScopedPtr& other);
  120. };
  121. /**
  122. @class wxScopedTiedPtr
  123. This is a variation on the topic of wxScopedPtr. This class is also a smart pointer
  124. but in addition it "ties" the pointer value to another variable. In other words,
  125. during the life time of this class the value of that variable is set to be the same
  126. as the value of the pointer itself and it is reset to its old value when the object
  127. is destroyed. This class is especially useful when converting the existing code
  128. (which may already store the pointers value in some variable) to the smart pointers.
  129. @library{wxbase}
  130. @category{smartpointers}
  131. */
  132. class wxScopedTiedPtr : public wxScopedPtr
  133. {
  134. public:
  135. /**
  136. Constructor creates a smart pointer initialized with @a ptr and stores
  137. @a ptr in the location specified by @a ppTie which must not be @NULL.
  138. */
  139. wxScopedTiedPtr(T** ppTie, T* ptr);
  140. /**
  141. Destructor frees the pointer help by this object and restores the value
  142. stored at the tied location (as specified in the @ref wxScopedTiedPtr() constructor)
  143. to the old value.
  144. @warning
  145. This location may now contain an uninitialized value if it hadn't been
  146. initialized previously, in particular don't count on it magically being @NULL!
  147. */
  148. ~wxScopedTiedPtr();
  149. };
  150. /**
  151. A scoped pointer template class.
  152. It is the template version of the old-style @ref wxScopedPtr "scoped pointer macros".
  153. Notice that objects of this class intentionally cannot be copied.
  154. @library{wxbase}
  155. @category{smartpointers}
  156. @see wxSharedPtr<T>, wxWeakRef<T>
  157. */
  158. template<typename T>
  159. class wxScopedPtr<T>
  160. {
  161. public:
  162. /**
  163. Constructor takes ownership of the pointer.
  164. @param ptr
  165. Pointer allocated with @c new or @NULL.
  166. */
  167. wxScopedPtr(T* ptr = NULL);
  168. /**
  169. Destructor deletes the pointer.
  170. */
  171. ~wxScopedPtr();
  172. /**
  173. Returns pointer to object or @NULL.
  174. */
  175. T* get() const;
  176. /**
  177. Conversion to a boolean expression (in a variant which is not
  178. convertible to anything but a boolean expression).
  179. If this class contains a valid pointer it will return @true, if it contains
  180. a @NULL pointer it will return @false.
  181. */
  182. operator unspecified_bool_type() const;
  183. /**
  184. Returns a reference to the object.
  185. If the internal pointer is @NULL this method will cause an assert in debug mode.
  186. */
  187. T& operator*() const;
  188. /**
  189. Smart pointer member access. Returns pointer to object.
  190. If the internal pointer is @NULL this method will cause an assert in debug mode.
  191. */
  192. T* operator->() const;
  193. /**
  194. Releases the current pointer and returns it.
  195. @remarks
  196. Afterwards the caller is responsible for deleting
  197. the data contained in the scoped pointer before.
  198. */
  199. T* release();
  200. /**
  201. Reset pointer to the value of @a ptr.
  202. The previous pointer will be deleted.
  203. */
  204. void reset(T* ptr = NULL);
  205. /**
  206. Swaps pointers.
  207. */
  208. void swap(wxScopedPtr<T>& ot);
  209. };