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.

162 lines
4.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2011, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. // Originally these classes are from Chromium.
  28. // http://src.chromium.org/viewvc/chrome/trunk/src/base/memory/ref_counted.h?view=markup
  29. //
  30. // A smart pointer class for reference counted objects. Use this class instead
  31. // of calling AddRef and Release manually on a reference counted object to
  32. // avoid common memory leaks caused by forgetting to Release an object
  33. // reference. Sample usage:
  34. //
  35. // class MyFoo : public RefCounted<MyFoo> {
  36. // ...
  37. // };
  38. //
  39. // void some_function() {
  40. // scoped_refptr<MyFoo> foo = new MyFoo();
  41. // foo->Method(param);
  42. // // |foo| is released when this function returns
  43. // }
  44. //
  45. // void some_other_function() {
  46. // scoped_refptr<MyFoo> foo = new MyFoo();
  47. // ...
  48. // foo = NULL; // explicitly releases |foo|
  49. // ...
  50. // if (foo)
  51. // foo->Method(param);
  52. // }
  53. //
  54. // The above examples show how scoped_refptr<T> acts like a pointer to T.
  55. // Given two scoped_refptr<T> classes, it is also possible to exchange
  56. // references between the two objects, like so:
  57. //
  58. // {
  59. // scoped_refptr<MyFoo> a = new MyFoo();
  60. // scoped_refptr<MyFoo> b;
  61. //
  62. // b.swap(a);
  63. // // now, |b| references the MyFoo object, and |a| references NULL.
  64. // }
  65. //
  66. // To make both |a| and |b| in the above example reference the same MyFoo
  67. // object, simply use the assignment operator:
  68. //
  69. // {
  70. // scoped_refptr<MyFoo> a = new MyFoo();
  71. // scoped_refptr<MyFoo> b;
  72. //
  73. // b = a;
  74. // // now, |a| and |b| each own a reference to the same MyFoo object.
  75. // }
  76. //
  77. #ifndef TALK_BASE_SCOPED_REF_PTR_H_
  78. #define TALK_BASE_SCOPED_REF_PTR_H_
  79. namespace talk_base {
  80. template <class T>
  81. class scoped_refptr {
  82. public:
  83. scoped_refptr() : ptr_(NULL) {
  84. }
  85. scoped_refptr(T* p) : ptr_(p) {
  86. if (ptr_)
  87. ptr_->AddRef();
  88. }
  89. scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) {
  90. if (ptr_)
  91. ptr_->AddRef();
  92. }
  93. template <typename U>
  94. scoped_refptr(const scoped_refptr<U>& r) : ptr_(r.get()) {
  95. if (ptr_)
  96. ptr_->AddRef();
  97. }
  98. ~scoped_refptr() {
  99. if (ptr_)
  100. ptr_->Release();
  101. }
  102. T* get() const { return ptr_; }
  103. operator T*() const { return ptr_; }
  104. T* operator->() const { return ptr_; }
  105. // Release a pointer.
  106. // The return value is the current pointer held by this object.
  107. // If this object holds a NULL pointer, the return value is NULL.
  108. // After this operation, this object will hold a NULL pointer,
  109. // and will not own the object any more.
  110. T* release() {
  111. T* retVal = ptr_;
  112. ptr_ = NULL;
  113. return retVal;
  114. }
  115. scoped_refptr<T>& operator=(T* p) {
  116. // AddRef first so that self assignment should work
  117. if (p)
  118. p->AddRef();
  119. if (ptr_ )
  120. ptr_ ->Release();
  121. ptr_ = p;
  122. return *this;
  123. }
  124. scoped_refptr<T>& operator=(const scoped_refptr<T>& r) {
  125. return *this = r.ptr_;
  126. }
  127. template <typename U>
  128. scoped_refptr<T>& operator=(const scoped_refptr<U>& r) {
  129. return *this = r.get();
  130. }
  131. void swap(T** pp) {
  132. T* p = ptr_;
  133. ptr_ = *pp;
  134. *pp = p;
  135. }
  136. void swap(scoped_refptr<T>& r) {
  137. swap(&r.ptr_);
  138. }
  139. protected:
  140. T* ptr_;
  141. };
  142. } // namespace talk_base
  143. #endif // TALK_BASE_SCOPED_REF_PTR_H_