/thirdparty/breakpad/processor/scoped_ptr.h

http://github.com/tomahawk-player/tomahawk · C++ Header · 335 lines · 205 code · 77 blank · 53 comment · 23 complexity · 1e832ce7e48877faeffca14a92f9bad7 MD5 · raw file

  1. // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
  2. // Copyright (c) 2001, 2002 Peter Dimov
  3. //
  4. // Permission to copy, use, modify, sell and distribute this software
  5. // is granted provided this copyright notice appears in all copies.
  6. // This software is provided "as is" without express or implied
  7. // warranty, and with no claim as to its suitability for any purpose.
  8. //
  9. // See http://www.boost.org/libs/smart_ptr/scoped_ptr.htm for documentation.
  10. //
  11. // scoped_ptr mimics a built-in pointer except that it guarantees deletion
  12. // of the object pointed to, either on destruction of the scoped_ptr or via
  13. // an explicit reset(). scoped_ptr is a simple solution for simple needs;
  14. // use shared_ptr or std::auto_ptr if your needs are more complex.
  15. // *** NOTE ***
  16. // If your scoped_ptr is a class member of class FOO pointing to a
  17. // forward declared type BAR (as shown below), then you MUST use a non-inlined
  18. // version of the destructor. The destructor of a scoped_ptr (called from
  19. // FOO's destructor) must have a complete definition of BAR in order to
  20. // destroy it. Example:
  21. //
  22. // -- foo.h --
  23. // class BAR;
  24. //
  25. // class FOO {
  26. // public:
  27. // FOO();
  28. // ~FOO(); // Required for sources that instantiate class FOO to compile!
  29. //
  30. // private:
  31. // scoped_ptr<BAR> bar_;
  32. // };
  33. //
  34. // -- foo.cc --
  35. // #include "foo.h"
  36. // FOO::~FOO() {} // Empty, but must be non-inlined to FOO's class definition.
  37. // scoped_ptr_malloc added by Google
  38. // When one of these goes out of scope, instead of doing a delete or
  39. // delete[], it calls free(). scoped_ptr_malloc<char> is likely to see
  40. // much more use than any other specializations.
  41. // release() added by Google
  42. // Use this to conditionally transfer ownership of a heap-allocated object
  43. // to the caller, usually on method success.
  44. #ifndef PROCESSOR_SCOPED_PTR_H__
  45. #define PROCESSOR_SCOPED_PTR_H__
  46. #include <cstddef> // for std::ptrdiff_t
  47. #include <assert.h> // for assert
  48. #include <stdlib.h> // for free() decl
  49. namespace google_breakpad {
  50. template <typename T>
  51. class scoped_ptr {
  52. private:
  53. T* ptr;
  54. scoped_ptr(scoped_ptr const &);
  55. scoped_ptr & operator=(scoped_ptr const &);
  56. public:
  57. typedef T element_type;
  58. explicit scoped_ptr(T* p = 0): ptr(p) {}
  59. ~scoped_ptr() {
  60. typedef char type_must_be_complete[sizeof(T)];
  61. delete ptr;
  62. }
  63. void reset(T* p = 0) {
  64. typedef char type_must_be_complete[sizeof(T)];
  65. if (ptr != p) {
  66. delete ptr;
  67. ptr = p;
  68. }
  69. }
  70. T& operator*() const {
  71. assert(ptr != 0);
  72. return *ptr;
  73. }
  74. T* operator->() const {
  75. assert(ptr != 0);
  76. return ptr;
  77. }
  78. bool operator==(T* p) const {
  79. return ptr == p;
  80. }
  81. bool operator!=(T* p) const {
  82. return ptr != p;
  83. }
  84. T* get() const {
  85. return ptr;
  86. }
  87. void swap(scoped_ptr & b) {
  88. T* tmp = b.ptr;
  89. b.ptr = ptr;
  90. ptr = tmp;
  91. }
  92. T* release() {
  93. T* tmp = ptr;
  94. ptr = 0;
  95. return tmp;
  96. }
  97. private:
  98. // no reason to use these: each scoped_ptr should have its own object
  99. template <typename U> bool operator==(scoped_ptr<U> const& p) const;
  100. template <typename U> bool operator!=(scoped_ptr<U> const& p) const;
  101. };
  102. template<typename T> inline
  103. void swap(scoped_ptr<T>& a, scoped_ptr<T>& b) {
  104. a.swap(b);
  105. }
  106. template<typename T> inline
  107. bool operator==(T* p, const scoped_ptr<T>& b) {
  108. return p == b.get();
  109. }
  110. template<typename T> inline
  111. bool operator!=(T* p, const scoped_ptr<T>& b) {
  112. return p != b.get();
  113. }
  114. // scoped_array extends scoped_ptr to arrays. Deletion of the array pointed to
  115. // is guaranteed, either on destruction of the scoped_array or via an explicit
  116. // reset(). Use shared_array or std::vector if your needs are more complex.
  117. template<typename T>
  118. class scoped_array {
  119. private:
  120. T* ptr;
  121. scoped_array(scoped_array const &);
  122. scoped_array & operator=(scoped_array const &);
  123. public:
  124. typedef T element_type;
  125. explicit scoped_array(T* p = 0) : ptr(p) {}
  126. ~scoped_array() {
  127. typedef char type_must_be_complete[sizeof(T)];
  128. delete[] ptr;
  129. }
  130. void reset(T* p = 0) {
  131. typedef char type_must_be_complete[sizeof(T)];
  132. if (ptr != p) {
  133. delete [] ptr;
  134. ptr = p;
  135. }
  136. }
  137. T& operator[](std::ptrdiff_t i) const {
  138. assert(ptr != 0);
  139. assert(i >= 0);
  140. return ptr[i];
  141. }
  142. bool operator==(T* p) const {
  143. return ptr == p;
  144. }
  145. bool operator!=(T* p) const {
  146. return ptr != p;
  147. }
  148. T* get() const {
  149. return ptr;
  150. }
  151. void swap(scoped_array & b) {
  152. T* tmp = b.ptr;
  153. b.ptr = ptr;
  154. ptr = tmp;
  155. }
  156. T* release() {
  157. T* tmp = ptr;
  158. ptr = 0;
  159. return tmp;
  160. }
  161. private:
  162. // no reason to use these: each scoped_array should have its own object
  163. template <typename U> bool operator==(scoped_array<U> const& p) const;
  164. template <typename U> bool operator!=(scoped_array<U> const& p) const;
  165. };
  166. template<class T> inline
  167. void swap(scoped_array<T>& a, scoped_array<T>& b) {
  168. a.swap(b);
  169. }
  170. template<typename T> inline
  171. bool operator==(T* p, const scoped_array<T>& b) {
  172. return p == b.get();
  173. }
  174. template<typename T> inline
  175. bool operator!=(T* p, const scoped_array<T>& b) {
  176. return p != b.get();
  177. }
  178. // This class wraps the c library function free() in a class that can be
  179. // passed as a template argument to scoped_ptr_malloc below.
  180. class ScopedPtrMallocFree {
  181. public:
  182. inline void operator()(void* x) const {
  183. free(x);
  184. }
  185. };
  186. // scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a
  187. // second template argument, the functor used to free the object.
  188. template<typename T, typename FreeProc = ScopedPtrMallocFree>
  189. class scoped_ptr_malloc {
  190. private:
  191. T* ptr;
  192. scoped_ptr_malloc(scoped_ptr_malloc const &);
  193. scoped_ptr_malloc & operator=(scoped_ptr_malloc const &);
  194. public:
  195. typedef T element_type;
  196. explicit scoped_ptr_malloc(T* p = 0): ptr(p) {}
  197. ~scoped_ptr_malloc() {
  198. typedef char type_must_be_complete[sizeof(T)];
  199. free_((void*) ptr);
  200. }
  201. void reset(T* p = 0) {
  202. typedef char type_must_be_complete[sizeof(T)];
  203. if (ptr != p) {
  204. free_((void*) ptr);
  205. ptr = p;
  206. }
  207. }
  208. T& operator*() const {
  209. assert(ptr != 0);
  210. return *ptr;
  211. }
  212. T* operator->() const {
  213. assert(ptr != 0);
  214. return ptr;
  215. }
  216. bool operator==(T* p) const {
  217. return ptr == p;
  218. }
  219. bool operator!=(T* p) const {
  220. return ptr != p;
  221. }
  222. T* get() const {
  223. return ptr;
  224. }
  225. void swap(scoped_ptr_malloc & b) {
  226. T* tmp = b.ptr;
  227. b.ptr = ptr;
  228. ptr = tmp;
  229. }
  230. T* release() {
  231. T* tmp = ptr;
  232. ptr = 0;
  233. return tmp;
  234. }
  235. private:
  236. // no reason to use these: each scoped_ptr_malloc should have its own object
  237. template <typename U, typename GP>
  238. bool operator==(scoped_ptr_malloc<U, GP> const& p) const;
  239. template <typename U, typename GP>
  240. bool operator!=(scoped_ptr_malloc<U, GP> const& p) const;
  241. static FreeProc const free_;
  242. };
  243. template<typename T, typename FP>
  244. FP const scoped_ptr_malloc<T,FP>::free_ = FP();
  245. template<typename T, typename FP> inline
  246. void swap(scoped_ptr_malloc<T,FP>& a, scoped_ptr_malloc<T,FP>& b) {
  247. a.swap(b);
  248. }
  249. template<typename T, typename FP> inline
  250. bool operator==(T* p, const scoped_ptr_malloc<T,FP>& b) {
  251. return p == b.get();
  252. }
  253. template<typename T, typename FP> inline
  254. bool operator!=(T* p, const scoped_ptr_malloc<T,FP>& b) {
  255. return p != b.get();
  256. }
  257. } // namespace google_breakpad
  258. #endif // PROCESSOR_SCOPED_PTR_H__