/thirdparty/breakpad/processor/linked_ptr.h

http://github.com/tomahawk-player/tomahawk · C Header · 193 lines · 98 code · 24 blank · 71 comment · 16 complexity · bcf0909d50e6c769364865c63dc3fda1 MD5 · raw file

  1. // Copyright (c) 2006, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // A "smart" pointer type with reference tracking. Every pointer to a
  30. // particular object is kept on a circular linked list. When the last pointer
  31. // to an object is destroyed or reassigned, the object is deleted.
  32. //
  33. // Used properly, this deletes the object when the last reference goes away.
  34. // There are several caveats:
  35. // - Like all reference counting schemes, cycles lead to leaks.
  36. // - Each smart pointer is actually two pointers (8 bytes instead of 4).
  37. // - Every time a pointer is assigned, the entire list of pointers to that
  38. // object is traversed. This class is therefore NOT SUITABLE when there
  39. // will often be more than two or three pointers to a particular object.
  40. // - References are only tracked as long as linked_ptr<> objects are copied.
  41. // If a linked_ptr<> is converted to a raw pointer and back, BAD THINGS
  42. // will happen (double deletion).
  43. //
  44. // A good use of this class is storing object references in STL containers.
  45. // You can safely put linked_ptr<> in a vector<>.
  46. // Other uses may not be as good.
  47. //
  48. // Note: If you use an incomplete type with linked_ptr<>, the class
  49. // *containing* linked_ptr<> must have a constructor and destructor (even
  50. // if they do nothing!).
  51. #ifndef PROCESSOR_LINKED_PTR_H__
  52. #define PROCESSOR_LINKED_PTR_H__
  53. namespace google_breakpad {
  54. // This is used internally by all instances of linked_ptr<>. It needs to be
  55. // a non-template class because different types of linked_ptr<> can refer to
  56. // the same object (linked_ptr<Superclass>(obj) vs linked_ptr<Subclass>(obj)).
  57. // So, it needs to be possible for different types of linked_ptr to participate
  58. // in the same circular linked list, so we need a single class type here.
  59. //
  60. // DO NOT USE THIS CLASS DIRECTLY YOURSELF. Use linked_ptr<T>.
  61. class linked_ptr_internal {
  62. public:
  63. // Create a new circle that includes only this instance.
  64. void join_new() {
  65. next_ = this;
  66. }
  67. // Join an existing circle.
  68. void join(linked_ptr_internal const* ptr) {
  69. linked_ptr_internal const* p = ptr;
  70. while (p->next_ != ptr) p = p->next_;
  71. p->next_ = this;
  72. next_ = ptr;
  73. }
  74. // Leave whatever circle we're part of. Returns true iff we were the
  75. // last member of the circle. Once this is done, you can join() another.
  76. bool depart() {
  77. if (next_ == this) return true;
  78. linked_ptr_internal const* p = next_;
  79. while (p->next_ != this) p = p->next_;
  80. p->next_ = next_;
  81. return false;
  82. }
  83. private:
  84. mutable linked_ptr_internal const* next_;
  85. };
  86. template <typename T>
  87. class linked_ptr {
  88. public:
  89. typedef T element_type;
  90. // Take over ownership of a raw pointer. This should happen as soon as
  91. // possible after the object is created.
  92. explicit linked_ptr(T* ptr = NULL) { capture(ptr); }
  93. ~linked_ptr() { depart(); }
  94. // Copy an existing linked_ptr<>, adding ourselves to the list of references.
  95. template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); }
  96. linked_ptr(linked_ptr const& ptr) { copy(&ptr); }
  97. // Assignment releases the old value and acquires the new.
  98. template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) {
  99. depart();
  100. copy(&ptr);
  101. return *this;
  102. }
  103. linked_ptr& operator=(linked_ptr const& ptr) {
  104. if (&ptr != this) {
  105. depart();
  106. copy(&ptr);
  107. }
  108. return *this;
  109. }
  110. // Smart pointer members.
  111. void reset(T* ptr = NULL) { depart(); capture(ptr); }
  112. T* get() const { return value_; }
  113. T* operator->() const { return value_; }
  114. T& operator*() const { return *value_; }
  115. // Release ownership of the pointed object and returns it.
  116. // Sole ownership by this linked_ptr object is required.
  117. T* release() {
  118. bool last = link_.depart();
  119. T* v = value_;
  120. value_ = NULL;
  121. return v;
  122. }
  123. bool operator==(T* p) const { return value_ == p; }
  124. bool operator!=(T* p) const { return value_ != p; }
  125. template <typename U>
  126. bool operator==(linked_ptr<U> const& ptr) const {
  127. return value_ == ptr.get();
  128. }
  129. template <typename U>
  130. bool operator!=(linked_ptr<U> const& ptr) const {
  131. return value_ != ptr.get();
  132. }
  133. private:
  134. template <typename U>
  135. friend class linked_ptr;
  136. T* value_;
  137. linked_ptr_internal link_;
  138. void depart() {
  139. if (link_.depart()) delete value_;
  140. }
  141. void capture(T* ptr) {
  142. value_ = ptr;
  143. link_.join_new();
  144. }
  145. template <typename U> void copy(linked_ptr<U> const* ptr) {
  146. value_ = ptr->get();
  147. if (value_)
  148. link_.join(&ptr->link_);
  149. else
  150. link_.join_new();
  151. }
  152. };
  153. template<typename T> inline
  154. bool operator==(T* ptr, const linked_ptr<T>& x) {
  155. return ptr == x.get();
  156. }
  157. template<typename T> inline
  158. bool operator!=(T* ptr, const linked_ptr<T>& x) {
  159. return ptr != x.get();
  160. }
  161. // A function to convert T* into linked_ptr<T>
  162. // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation
  163. // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
  164. template <typename T>
  165. linked_ptr<T> make_linked_ptr(T* ptr) {
  166. return linked_ptr<T>(ptr);
  167. }
  168. } // namespace google_breakpad
  169. #endif // PROCESSOR_LINKED_PTR_H__