llpointer.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /**
  2. * @file llpointer.h
  3. * @brief A reference-counted pointer for objects derived from LLRefCount
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewergpl$
  6. *
  7. * Copyright (c) 2010, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LLPOINTER_H
  33. #define LLPOINTER_H
  34. #include "llpreprocessor.h"
  35. #include "stdtypes.h"
  36. //----------------------------------------------------------------------------
  37. // NOTE: LLPointer<LLFoo> x = new LLFoo(); MAY NOT BE THREAD SAFE if
  38. // LLFoo::LLFoo() does anything like put itself in an update queue. The queue
  39. // may get accessed before it gets assigned to x. The correct implementation
  40. // is:
  41. // LLPointer<LLFoo> x = new LLFoo; // constructor does not do anything
  42. // // interesting
  43. // x->instantiate(); // does stuff like place x into an update queue
  44. //----------------------------------------------------------------------------
  45. // This is to avoid inlining a llwarns...
  46. LL_NO_INLINE void warnUnreferenceDidAssignment();
  47. // Note: relies on Type having ref() and unref() methods
  48. template <class Type> class LLPointer
  49. {
  50. public:
  51. LL_INLINE LLPointer() noexcept
  52. : mPointer(NULL)
  53. {
  54. }
  55. LL_INLINE LLPointer(Type* ptr) noexcept
  56. : mPointer(ptr)
  57. {
  58. ref();
  59. }
  60. LL_INLINE LLPointer(const LLPointer<Type>& ptr) noexcept
  61. : mPointer(ptr.mPointer)
  62. {
  63. ref();
  64. }
  65. // This C++11 move constructor saves us an unref() on the moved pointer
  66. // and consequently a corresponding ref() on the constructed pointer.
  67. // Note: LLPointer::unref() still happens in ~LLPointer() of the moved
  68. // pointer, but it is not propagated down to the unref() method of the
  69. // object we point at, since mPointer is NULL when ~LLPointer() is called.
  70. LL_INLINE LLPointer(LLPointer<Type>&& ptr) noexcept
  71. : mPointer(ptr.mPointer)
  72. {
  73. ptr.mPointer = NULL;
  74. }
  75. // Support conversion up the type hierarchy. See Item 45 in Effective C++,
  76. // 3rd Ed.
  77. template<typename Subclass>
  78. LL_INLINE LLPointer(const LLPointer<Subclass>& ptr) noexcept
  79. : mPointer(ptr.get())
  80. {
  81. ref();
  82. }
  83. LL_INLINE ~LLPointer()
  84. {
  85. unref();
  86. }
  87. LL_INLINE Type* get() const { return mPointer; }
  88. LL_INLINE const Type* operator->() const { return mPointer; }
  89. LL_INLINE Type* operator->() { return mPointer; }
  90. LL_INLINE const Type& operator*() const { return *mPointer; }
  91. LL_INLINE Type& operator*() { return *mPointer; }
  92. LL_INLINE operator bool() const { return mPointer != NULL; }
  93. LL_INLINE bool operator!() const { return mPointer == NULL; }
  94. LL_INLINE bool isNull() const { return mPointer == NULL; }
  95. LL_INLINE bool notNull() const { return mPointer != NULL; }
  96. LL_INLINE operator Type*() const { return mPointer; }
  97. LL_INLINE bool operator!=(Type* ptr) const { return mPointer != ptr; }
  98. LL_INLINE bool operator==(Type* ptr) const { return mPointer == ptr; }
  99. LL_INLINE bool operator==(const LLPointer<Type>& ptr) const { return mPointer == ptr.mPointer; }
  100. LL_INLINE bool operator<(const LLPointer<Type>& ptr) const { return mPointer < ptr.mPointer; }
  101. LL_INLINE bool operator>(const LLPointer<Type>& ptr) const { return mPointer > ptr.mPointer; }
  102. LL_INLINE LLPointer<Type>& operator=(Type* ptr)
  103. {
  104. if (mPointer != ptr)
  105. {
  106. unref();
  107. mPointer = ptr;
  108. ref();
  109. }
  110. return *this;
  111. }
  112. LL_INLINE LLPointer<Type>& operator=(const LLPointer<Type>& ptr)
  113. {
  114. if (mPointer != ptr.mPointer)
  115. {
  116. unref();
  117. mPointer = ptr.mPointer;
  118. ref();
  119. }
  120. return *this;
  121. }
  122. // C++11 move assigment (saves a ref() in the assigned pointer and an
  123. // unref() in the moved pointer destructor).
  124. LL_INLINE LLPointer<Type>& operator=(LLPointer<Type>&& ptr) noexcept
  125. {
  126. if (mPointer != ptr.mPointer)
  127. {
  128. unref();
  129. mPointer = ptr.mPointer;
  130. ptr.mPointer = NULL;
  131. }
  132. return *this;
  133. }
  134. // Support assignment up the type hierarchy. See Item 45 in Effective C++,
  135. // 3rd Ed.
  136. template<typename Subclass>
  137. LL_INLINE LLPointer<Type>& operator=(const LLPointer<Subclass>& ptr)
  138. {
  139. if (mPointer != ptr.get())
  140. {
  141. unref();
  142. mPointer = ptr.get();
  143. ref();
  144. }
  145. return *this;
  146. }
  147. protected:
  148. LL_INLINE void ref() noexcept
  149. {
  150. if (mPointer)
  151. {
  152. mPointer->ref();
  153. }
  154. }
  155. LL_INLINE void unref()
  156. {
  157. if (mPointer)
  158. {
  159. Type* tempp = mPointer;
  160. mPointer = NULL;
  161. tempp->unref();
  162. if (mPointer)
  163. {
  164. warnUnreferenceDidAssignment();
  165. #if 0 // Attempting to work around a strange crash seen (by one user,
  166. // under Windows), in LLSpatialBridge::cleanupReferences()...
  167. // At worst, this change should result in a memory leak. HB
  168. unref();
  169. #else
  170. mPointer = NULL;
  171. #endif
  172. }
  173. }
  174. }
  175. protected:
  176. Type* mPointer;
  177. };
  178. // std::hash implementation
  179. namespace std
  180. {
  181. template <typename Subclass> struct hash<LLPointer<Subclass>>
  182. {
  183. LL_INLINE size_t operator()(const LLPointer<Subclass>& p) const noexcept
  184. {
  185. return (size_t)p.get();
  186. }
  187. };
  188. }
  189. // For use with boost::unordered_map and boost::unordered_set
  190. template <typename Subclass>
  191. LL_INLINE size_t hash_value(const LLPointer<Subclass>& p) noexcept
  192. {
  193. return (size_t)p.get();
  194. }
  195. #endif