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.

142 lines
4.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, 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. /*
  28. * linked_ptr - simple reference linked pointer
  29. * (like reference counting, just using a linked list of the references
  30. * instead of their count.)
  31. *
  32. * The implementation stores three pointers for every linked_ptr, but
  33. * does not allocate anything on the free store.
  34. */
  35. #ifndef TALK_BASE_LINKED_PTR_H__
  36. #define TALK_BASE_LINKED_PTR_H__
  37. namespace talk_base {
  38. /* For ANSI-challenged compilers, you may want to #define
  39. * NO_MEMBER_TEMPLATES, explicit or mutable */
  40. #define NO_MEMBER_TEMPLATES
  41. template <class X> class linked_ptr
  42. {
  43. public:
  44. #ifndef NO_MEMBER_TEMPLATES
  45. # define TEMPLATE_FUNCTION template <class Y>
  46. TEMPLATE_FUNCTION friend class linked_ptr<Y>;
  47. #else
  48. # define TEMPLATE_FUNCTION
  49. typedef X Y;
  50. #endif
  51. typedef X element_type;
  52. explicit linked_ptr(X* p = 0) throw()
  53. : itsPtr(p) {itsPrev = itsNext = this;}
  54. ~linked_ptr()
  55. {release();}
  56. linked_ptr(const linked_ptr& r) throw()
  57. {acquire(r);}
  58. linked_ptr& operator=(const linked_ptr& r)
  59. {
  60. if (this != &r) {
  61. release();
  62. acquire(r);
  63. }
  64. return *this;
  65. }
  66. #ifndef NO_MEMBER_TEMPLATES
  67. template <class Y> friend class linked_ptr<Y>;
  68. template <class Y> linked_ptr(const linked_ptr<Y>& r) throw()
  69. {acquire(r);}
  70. template <class Y> linked_ptr& operator=(const linked_ptr<Y>& r)
  71. {
  72. if (this != &r) {
  73. release();
  74. acquire(r);
  75. }
  76. return *this;
  77. }
  78. #endif // NO_MEMBER_TEMPLATES
  79. X& operator*() const throw() {return *itsPtr;}
  80. X* operator->() const throw() {return itsPtr;}
  81. X* get() const throw() {return itsPtr;}
  82. bool unique() const throw() {return itsPrev ? itsPrev==this : true;}
  83. private:
  84. X* itsPtr;
  85. mutable const linked_ptr* itsPrev;
  86. mutable const linked_ptr* itsNext;
  87. void acquire(const linked_ptr& r) throw()
  88. { // insert this to the list
  89. itsPtr = r.itsPtr;
  90. itsNext = r.itsNext;
  91. itsNext->itsPrev = this;
  92. itsPrev = &r;
  93. #ifndef mutable
  94. r.itsNext = this;
  95. #else // for ANSI-challenged compilers
  96. (const_cast<linked_ptr<X>*>(&r))->itsNext = this;
  97. #endif
  98. }
  99. #ifndef NO_MEMBER_TEMPLATES
  100. template <class Y> void acquire(const linked_ptr<Y>& r) throw()
  101. { // insert this to the list
  102. itsPtr = r.itsPtr;
  103. itsNext = r.itsNext;
  104. itsNext->itsPrev = this;
  105. itsPrev = &r;
  106. #ifndef mutable
  107. r.itsNext = this;
  108. #else // for ANSI-challenged compilers
  109. (const_cast<linked_ptr<X>*>(&r))->itsNext = this;
  110. #endif
  111. }
  112. #endif // NO_MEMBER_TEMPLATES
  113. void release()
  114. { // erase this from the list, delete if unique
  115. if (unique()) delete itsPtr;
  116. else {
  117. itsPrev->itsNext = itsNext;
  118. itsNext->itsPrev = itsPrev;
  119. itsPrev = itsNext = 0;
  120. }
  121. itsPtr = 0;
  122. }
  123. };
  124. } // namespace talk_base
  125. #endif // TALK_BASE_LINKED_PTR_H__