llhandle.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * @file llhandle.h
  3. * @brief "Handle" to an object (usually a floater) whose lifetime you don't
  4. * control.
  5. *
  6. * $LicenseInfo:firstyear=2001&license=viewergpl$
  7. *
  8. * Copyright (c) 2001-2009, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #ifndef LLHANDLE_H
  34. #define LLHANDLE_H
  35. #include <type_traits>
  36. #include "llpointer.h"
  37. #include "llrefcount.h"
  38. // Helper object for LLHandle. Do not instantiate these directly. Used
  39. // exclusively by LLHandle.
  40. class LLTombStone : public LLRefCount
  41. {
  42. public:
  43. LLTombStone(void* target = NULL)
  44. : mTarget(target)
  45. {
  46. }
  47. LL_INLINE void setTarget(void* target) { mTarget = target; }
  48. LL_INLINE void* getTarget() const { return mTarget; }
  49. private:
  50. mutable void* mTarget;
  51. };
  52. // LLHandles are used to refer to objects whose lifetime you do not control or
  53. // influence. Calling get() on a handle will return a pointer to the referenced
  54. // object or NULL, if the object no longer exists. Note that during the
  55. // lifetime of the returned pointer, you are assuming that the object will not
  56. // be deleted by any action you perform, or any other thread, as normal when
  57. // using pointers, so avoid using that pointer outside of the local code block.
  58. //
  59. // https://wiki.lindenlab.com/mediawiki/index.php?title=LLHandle&oldid=79669
  60. //
  61. // The implementation is like some "weak pointer" implementations. When we
  62. // cannot control the lifespan of the referenced object of interest, we can
  63. // still instantiate a proxy object whose lifespan we DO control, and store in
  64. // the proxy object a dumb pointer to the actual target. Then we just have to
  65. // ensure that on destruction of the target object, the proxy's dumb pointer
  66. // is set NULL.
  67. //
  68. // LLTombStone is our proxy object. LLHandle contains an LLPointer to the
  69. // LLTombStone, so every copy of an LLHandle increments the LLTombStone's ref
  70. // count as usual.
  71. //
  72. // One copy of the LLHandle, specifically the LLRootHandle, must be stored in
  73. // the referenced object. Destroying the LLRootHandle is what NULLs the
  74. // proxy's target pointer.
  75. //
  76. // Minor optimization: we want LLHandle's mTombStone to always be a valid
  77. // LLPointer, saving some conditionals in dereferencing. That is the
  78. // getDefaultTombStone() mechanism. The default LLTombStone object's target
  79. // pointer is always NULL, so it is semantically identical to allowing
  80. // mTombStone to be invalid.
  81. template <typename T>
  82. class LLHandle
  83. {
  84. template <typename U> friend class LLHandle;
  85. template <typename U> friend class LLHandleProvider;
  86. public:
  87. LL_INLINE LLHandle()
  88. : mTombStone(getDefaultTombStone())
  89. {
  90. }
  91. template<typename U>
  92. LLHandle(const LLHandle<U>& other,
  93. typename std::enable_if<std::is_convertible<U*, T*>::value>::type* dummy = 0)
  94. : mTombStone(other.mTombStone)
  95. {
  96. }
  97. LL_INLINE bool isDead() const
  98. {
  99. return mTombStone->getTarget() == NULL;
  100. }
  101. LL_INLINE void markDead()
  102. {
  103. mTombStone = getDefaultTombStone();
  104. }
  105. LL_INLINE T* get() const
  106. {
  107. return reinterpret_cast<T*>(mTombStone->getTarget());
  108. }
  109. LL_INLINE friend bool operator==(const LLHandle<T>& lhs,
  110. const LLHandle<T>& rhs)
  111. {
  112. return lhs.mTombStone == rhs.mTombStone;
  113. }
  114. LL_INLINE friend bool operator!=(const LLHandle<T>& lhs,
  115. const LLHandle<T>& rhs)
  116. {
  117. return lhs.mTombStone != rhs.mTombStone;
  118. }
  119. LL_INLINE friend bool operator<(const LLHandle<T>& lhs,
  120. const LLHandle<T>& rhs)
  121. {
  122. return lhs.mTombStone < rhs.mTombStone;
  123. }
  124. LL_INLINE friend bool operator>(const LLHandle<T>& lhs,
  125. const LLHandle<T>& rhs)
  126. {
  127. return lhs.mTombStone > rhs.mTombStone;
  128. }
  129. private:
  130. static LLPointer<LLTombStone>& getDefaultTombStone()
  131. {
  132. static LLPointer<LLTombStone> sDefaultTombStone = new LLTombStone;
  133. return sDefaultTombStone;
  134. }
  135. protected:
  136. LLPointer<LLTombStone> mTombStone;
  137. };
  138. // LLRootHandle is a LLHandle which must be stored in the referenced object.
  139. // You can either store it directly and explicitly bind(this), or derive from
  140. // LLHandleProvider (q.v.) which automates that for you. The essential point is
  141. // that destroying the LLRootHandle (as a consequence of destroying the
  142. // referenced object) calls unbind(), setting the LLTombStone's target pointer
  143. // NULL.
  144. template <typename T>
  145. class LLRootHandle : public LLHandle<T>
  146. {
  147. public:
  148. typedef LLRootHandle<T> self_t;
  149. typedef LLHandle<T> base_t;
  150. LLRootHandle() = default;
  151. LL_INLINE LLRootHandle(T* object) { bind(object); }
  152. // Do not allow copying of root handles, since there should only be one
  153. LLRootHandle(const LLRootHandle&) = delete;
  154. LL_INLINE ~LLRootHandle() { unbind(); }
  155. #if 0 // This is redundant, since a LLRootHandle *is* an LLHandle
  156. LL_INLINE LLHandle<T> getHandle() { return LLHandle<T>(*this); }
  157. #endif
  158. void bind(T* object)
  159. {
  160. // Unbind existing tombstone
  161. if (LLHandle<T>::mTombStone.notNull())
  162. {
  163. if (LLHandle<T>::mTombStone->getTarget() == (void*)object)
  164. {
  165. return;
  166. }
  167. LLHandle<T>::mTombStone->setTarget(NULL);
  168. }
  169. // Tombstone reference counted, so no paired delete
  170. LLHandle<T>::mTombStone = new LLTombStone((void*)object);
  171. }
  172. LL_INLINE void unbind()
  173. {
  174. LLHandle<T>::mTombStone->setTarget(NULL);
  175. }
  176. };
  177. // Use this as a mixin for simple classes that need handles and when you do not
  178. // want handles at multiple points of the inheritance hierarchy.
  179. template <typename T>
  180. class LLHandleProvider
  181. {
  182. public:
  183. LL_INLINE LLHandle<T> getHandle() const
  184. {
  185. // Perform lazy binding to avoid small tombstone allocations for handle
  186. // providers whose handles are never referenced
  187. mHandle.bind(static_cast<T*>(const_cast<LLHandleProvider<T>*>(this)));
  188. return mHandle;
  189. }
  190. template <typename U>
  191. LLHandle<U> getDerivedHandle(typename std::enable_if<std::is_convertible<U*, T*>::value>::type* dummy = 0) const
  192. {
  193. LLHandle<U> downcast_handle;
  194. downcast_handle.mTombStone = getHandle().mTombStone;
  195. return downcast_handle;
  196. }
  197. protected:
  198. typedef LLHandle<T> handle_type_t;
  199. LL_INLINE LLHandleProvider()
  200. {
  201. // Provided here to enforce T deriving from LLHandleProvider<T>
  202. }
  203. private:
  204. mutable LLRootHandle<T> mHandle;
  205. };
  206. #endif // LLHANDLE_H