You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

277 lines
5.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. // scoped_ptr_malloc added in by Google. When one of
  16. // these goes out of scope, instead of doing a delete or delete[], it
  17. // calls free(). scoped_ptr_malloc<char> is likely to see much more
  18. // use than any other specializations.
  19. // release() added in by Google. Use this to conditionally
  20. // transfer ownership of a heap-allocated object to the caller, usually on
  21. // method success.
  22. #ifndef TALK_BASE_SCOPED_PTR_H__
  23. #define TALK_BASE_SCOPED_PTR_H__
  24. #include <cstddef> // for std::ptrdiff_t
  25. #include <stdlib.h> // for free() decl
  26. #include "talk/base/common.h" // for ASSERT
  27. #ifdef _WIN32
  28. namespace std { using ::ptrdiff_t; };
  29. #endif // _WIN32
  30. namespace talk_base {
  31. template <typename T>
  32. class scoped_ptr {
  33. private:
  34. T* ptr;
  35. scoped_ptr(scoped_ptr const &);
  36. scoped_ptr & operator=(scoped_ptr const &);
  37. public:
  38. typedef T element_type;
  39. explicit scoped_ptr(T* p = NULL): ptr(p) {}
  40. ~scoped_ptr() {
  41. typedef char type_must_be_complete[sizeof(T)];
  42. delete ptr;
  43. }
  44. void reset(T* p = NULL) {
  45. typedef char type_must_be_complete[sizeof(T)];
  46. if (ptr != p) {
  47. T* obj = ptr;
  48. ptr = p;
  49. // Delete last, in case obj destructor indirectly results in ~scoped_ptr
  50. delete obj;
  51. }
  52. }
  53. T& operator*() const {
  54. ASSERT(ptr != NULL);
  55. return *ptr;
  56. }
  57. T* operator->() const {
  58. ASSERT(ptr != NULL);
  59. return ptr;
  60. }
  61. T* get() const {
  62. return ptr;
  63. }
  64. void swap(scoped_ptr & b) {
  65. T* tmp = b.ptr;
  66. b.ptr = ptr;
  67. ptr = tmp;
  68. }
  69. T* release() {
  70. T* tmp = ptr;
  71. ptr = NULL;
  72. return tmp;
  73. }
  74. T** accept() {
  75. if (ptr) {
  76. delete ptr;
  77. ptr = NULL;
  78. }
  79. return &ptr;
  80. }
  81. T** use() {
  82. return &ptr;
  83. }
  84. // Allow scoped_ptr<T> to be used in boolean expressions, but not
  85. // implicitly convertible to a real bool (which is dangerous).
  86. // Borrowed from chromium's scoped_ptr implementation.
  87. typedef T* scoped_ptr::*Testable;
  88. operator Testable() const { return ptr ? &scoped_ptr::ptr : NULL; }
  89. };
  90. template<typename T> inline
  91. void swap(scoped_ptr<T>& a, scoped_ptr<T>& b) {
  92. a.swap(b);
  93. }
  94. // scoped_array extends scoped_ptr to arrays. Deletion of the array pointed to
  95. // is guaranteed, either on destruction of the scoped_array or via an explicit
  96. // reset(). Use shared_array or std::vector if your needs are more complex.
  97. template<typename T>
  98. class scoped_array {
  99. private:
  100. T* ptr;
  101. scoped_array(scoped_array const &);
  102. scoped_array & operator=(scoped_array const &);
  103. public:
  104. typedef T element_type;
  105. explicit scoped_array(T* p = NULL) : ptr(p) {}
  106. ~scoped_array() {
  107. typedef char type_must_be_complete[sizeof(T)];
  108. delete[] ptr;
  109. }
  110. void reset(T* p = NULL) {
  111. typedef char type_must_be_complete[sizeof(T)];
  112. if (ptr != p) {
  113. T* arr = ptr;
  114. ptr = p;
  115. // Delete last, in case arr destructor indirectly results in ~scoped_array
  116. delete [] arr;
  117. }
  118. }
  119. T& operator[](std::ptrdiff_t i) const {
  120. ASSERT(ptr != NULL);
  121. ASSERT(i >= 0);
  122. return ptr[i];
  123. }
  124. T* get() const {
  125. return ptr;
  126. }
  127. void swap(scoped_array & b) {
  128. T* tmp = b.ptr;
  129. b.ptr = ptr;
  130. ptr = tmp;
  131. }
  132. T* release() {
  133. T* tmp = ptr;
  134. ptr = NULL;
  135. return tmp;
  136. }
  137. T** accept() {
  138. if (ptr) {
  139. delete [] ptr;
  140. ptr = NULL;
  141. }
  142. return &ptr;
  143. }
  144. // Allow scoped_array<T> to be used in boolean expressions, but not
  145. // implicitly convertible to a real bool (which is dangerous).
  146. // Borrowed from chromium's scoped_array implementation.
  147. typedef T* scoped_array::*Testable;
  148. operator Testable() const { return ptr ? &scoped_array::ptr : NULL; }
  149. };
  150. template<class T> inline
  151. void swap(scoped_array<T>& a, scoped_array<T>& b) {
  152. a.swap(b);
  153. }
  154. // scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a
  155. // second template argument, the function used to free the object.
  156. template<typename T, void (*FF)(T*) = free> class scoped_ptr_malloc {
  157. private:
  158. T* ptr;
  159. scoped_ptr_malloc(scoped_ptr_malloc const &);
  160. scoped_ptr_malloc & operator=(scoped_ptr_malloc const &);
  161. public:
  162. typedef T element_type;
  163. explicit scoped_ptr_malloc(T* p = 0): ptr(p) {}
  164. ~scoped_ptr_malloc() {
  165. FF(ptr);
  166. }
  167. void reset(T* p = 0) {
  168. if (ptr != p) {
  169. FF(ptr);
  170. ptr = p;
  171. }
  172. }
  173. T& operator*() const {
  174. ASSERT(ptr != 0);
  175. return *ptr;
  176. }
  177. T* operator->() const {
  178. ASSERT(ptr != 0);
  179. return ptr;
  180. }
  181. T* get() const {
  182. return ptr;
  183. }
  184. void swap(scoped_ptr_malloc & b) {
  185. T* tmp = b.ptr;
  186. b.ptr = ptr;
  187. ptr = tmp;
  188. }
  189. T* release() {
  190. T* tmp = ptr;
  191. ptr = 0;
  192. return tmp;
  193. }
  194. T** accept() {
  195. if (ptr) {
  196. FF(ptr);
  197. ptr = 0;
  198. }
  199. return &ptr;
  200. }
  201. // Allow scoped_ptr_malloc<T> to be used in boolean expressions, but not
  202. // implicitly convertible to a real bool (which is dangerous).
  203. // Borrowed from chromium's scoped_ptr_malloc implementation.
  204. typedef T* scoped_ptr_malloc::*Testable;
  205. operator Testable() const { return ptr ? &scoped_ptr_malloc::ptr : NULL; }
  206. };
  207. template<typename T, void (*FF)(T*)> inline
  208. void swap(scoped_ptr_malloc<T,FF>& a, scoped_ptr_malloc<T,FF>& b) {
  209. a.swap(b);
  210. }
  211. } // namespace talk_base
  212. #endif // #ifndef TALK_BASE_SCOPED_PTR_H__