WeakPtr.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (C) 2013 Google, Inc. 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
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef WTF_WeakPtr_h
  26. #define WTF_WeakPtr_h
  27. #include <wtf/Noncopyable.h>
  28. #include <wtf/PassRefPtr.h>
  29. #include <wtf/RefPtr.h>
  30. #include <wtf/ThreadSafeRefCounted.h>
  31. #include <wtf/Threading.h>
  32. namespace WTF {
  33. template<typename T>
  34. class WeakReference : public ThreadSafeRefCounted<WeakReference<T> > {
  35. WTF_MAKE_NONCOPYABLE(WeakReference<T>);
  36. WTF_MAKE_FAST_ALLOCATED;
  37. public:
  38. static PassRefPtr<WeakReference<T> > create(T* ptr) { return adoptRef(new WeakReference(ptr)); }
  39. static PassRefPtr<WeakReference<T> > createUnbound() { return adoptRef(new WeakReference()); }
  40. T* get() const
  41. {
  42. ASSERT(m_boundThread == currentThread());
  43. return m_ptr;
  44. }
  45. void clear()
  46. {
  47. ASSERT(m_boundThread == currentThread());
  48. m_ptr = 0;
  49. }
  50. void bindTo(T* ptr)
  51. {
  52. ASSERT(!m_ptr);
  53. #ifndef NDEBUG
  54. m_boundThread = currentThread();
  55. #endif
  56. m_ptr = ptr;
  57. }
  58. private:
  59. WeakReference() : m_ptr(0) { }
  60. explicit WeakReference(T* ptr)
  61. : m_ptr(ptr)
  62. #ifndef NDEBUG
  63. , m_boundThread(currentThread())
  64. #endif
  65. {
  66. }
  67. T* m_ptr;
  68. #ifndef NDEBUG
  69. ThreadIdentifier m_boundThread;
  70. #endif
  71. };
  72. template<typename T>
  73. class WeakPtr {
  74. WTF_MAKE_FAST_ALLOCATED;
  75. public:
  76. WeakPtr() { }
  77. WeakPtr(PassRefPtr<WeakReference<T> > ref) : m_ref(ref) { }
  78. T* get() const { return m_ref->get(); }
  79. private:
  80. RefPtr<WeakReference<T> > m_ref;
  81. };
  82. template<typename T>
  83. class WeakPtrFactory {
  84. WTF_MAKE_NONCOPYABLE(WeakPtrFactory<T>);
  85. WTF_MAKE_FAST_ALLOCATED;
  86. public:
  87. explicit WeakPtrFactory(T* ptr) : m_ref(WeakReference<T>::create(ptr)) { }
  88. WeakPtrFactory(PassRefPtr<WeakReference<T> > ref, T* ptr)
  89. : m_ref(ref)
  90. {
  91. m_ref->bindTo(ptr);
  92. }
  93. ~WeakPtrFactory() { m_ref->clear(); }
  94. // We should consider having createWeakPtr populate m_ref the first time createWeakPtr is called.
  95. WeakPtr<T> createWeakPtr() { return WeakPtr<T>(m_ref); }
  96. void revokeAll()
  97. {
  98. T* ptr = m_ref->get();
  99. m_ref->clear();
  100. // We create a new WeakReference so that future calls to createWeakPtr() create nonzero WeakPtrs.
  101. m_ref = WeakReference<T>::create(ptr);
  102. }
  103. private:
  104. RefPtr<WeakReference<T> > m_ref;
  105. };
  106. } // namespace WTF
  107. using WTF::WeakPtr;
  108. using WTF::WeakPtrFactory;
  109. using WTF::WeakReference;
  110. #endif