| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google, Inc. All Rights Reserved. | 2 * Copyright (C) 2013 Google, Inc. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| 11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
| 12 * | 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #ifndef WTF_WeakPtr_h | 26 #ifndef WTF_WeakPtr_h |
| 27 #define WTF_WeakPtr_h | 27 #define WTF_WeakPtr_h |
| 28 | 28 |
| 29 #include "base/memory/weak_ptr.h" | |
| 30 #include "wtf/Noncopyable.h" | 29 #include "wtf/Noncopyable.h" |
| 30 #include "wtf/PassRefPtr.h" |
| 31 #include "wtf/RefPtr.h" |
| 32 #include "wtf/ThreadSafeRefCounted.h" |
| 33 #include "wtf/Threading.h" |
| 31 | 34 |
| 32 namespace WTF { | 35 namespace WTF { |
| 33 | 36 |
| 34 template<typename T> | 37 template<typename T> |
| 35 using WeakPtr = base::WeakPtr<T>; | 38 class WeakReference : public ThreadSafeRefCounted<WeakReference<T>> { |
| 39 WTF_MAKE_NONCOPYABLE(WeakReference<T>); |
| 40 USING_FAST_MALLOC(WeakReference); |
| 41 public: |
| 42 static PassRefPtr<WeakReference<T>> create(T* ptr) { return adoptRef(new Wea
kReference(ptr)); } |
| 43 static PassRefPtr<WeakReference<T>> createUnbound() { return adoptRef(new We
akReference()); } |
| 44 |
| 45 T* get() const |
| 46 { |
| 47 #if DCHECK_IS_ON() |
| 48 DCHECK_EQ(m_boundThread, currentThread()); |
| 49 #endif |
| 50 return m_ptr; |
| 51 } |
| 52 |
| 53 void clear() |
| 54 { |
| 55 #if DCHECK_IS_ON() |
| 56 DCHECK_EQ(m_boundThread, currentThread()); |
| 57 #endif |
| 58 m_ptr = 0; |
| 59 } |
| 60 |
| 61 void bindTo(T* ptr) |
| 62 { |
| 63 DCHECK(!m_ptr); |
| 64 #if DCHECK_IS_ON() |
| 65 m_boundThread = currentThread(); |
| 66 #endif |
| 67 m_ptr = ptr; |
| 68 } |
| 69 |
| 70 private: |
| 71 WeakReference() : m_ptr(0) { } |
| 72 |
| 73 explicit WeakReference(T* ptr) |
| 74 : m_ptr(ptr) |
| 75 #if DCHECK_IS_ON() |
| 76 , m_boundThread(currentThread()) |
| 77 #endif |
| 78 { |
| 79 } |
| 80 |
| 81 T* m_ptr; |
| 82 #if DCHECK_IS_ON() |
| 83 ThreadIdentifier m_boundThread; |
| 84 #endif |
| 85 }; |
| 86 |
| 87 template<typename T> |
| 88 class WeakPtr { |
| 89 USING_FAST_MALLOC(WeakPtr); |
| 90 public: |
| 91 WeakPtr() { } |
| 92 WeakPtr(std::nullptr_t) { } |
| 93 WeakPtr(PassRefPtr<WeakReference<T>> ref) : m_ref(ref) { } |
| 94 |
| 95 T* get() const { return m_ref ? m_ref->get() : 0; } |
| 96 void clear() { m_ref.clear(); } |
| 97 |
| 98 T& operator*() const |
| 99 { |
| 100 DCHECK(get()); |
| 101 return *get(); |
| 102 } |
| 103 |
| 104 T* operator->() const |
| 105 { |
| 106 DCHECK(get()); |
| 107 return get(); |
| 108 } |
| 109 |
| 110 explicit operator bool() const { return get(); } |
| 111 |
| 112 private: |
| 113 RefPtr<WeakReference<T>> m_ref; |
| 114 }; |
| 115 |
| 116 template<typename T, typename U> inline bool operator==(const WeakPtr<T>& a, con
st WeakPtr<U>& b) |
| 117 { |
| 118 return a.get() == b.get(); |
| 119 } |
| 120 |
| 121 template<typename T, typename U> inline bool operator!=(const WeakPtr<T>& a, con
st WeakPtr<U>& b) |
| 122 { |
| 123 return a.get() != b.get(); |
| 124 } |
| 36 | 125 |
| 37 template<typename T> | 126 template<typename T> |
| 38 class WeakPtrFactory { | 127 class WeakPtrFactory { |
| 39 WTF_MAKE_NONCOPYABLE(WeakPtrFactory<T>); | 128 WTF_MAKE_NONCOPYABLE(WeakPtrFactory<T>); |
| 40 USING_FAST_MALLOC(WeakPtrFactory); | 129 USING_FAST_MALLOC(WeakPtrFactory); |
| 41 public: | 130 public: |
| 42 explicit WeakPtrFactory(T* ptr) : m_factory(ptr) { } | 131 explicit WeakPtrFactory(T* ptr) : m_ref(WeakReference<T>::create(ptr)) { } |
| 43 | 132 |
| 44 WeakPtr<T> createWeakPtr() { return m_factory.GetWeakPtr(); } | 133 WeakPtrFactory(PassRefPtr<WeakReference<T>> ref, T* ptr) |
| 134 : m_ref(ref) |
| 135 { |
| 136 m_ref->bindTo(ptr); |
| 137 } |
| 138 |
| 139 ~WeakPtrFactory() { m_ref->clear(); } |
| 140 |
| 141 // We should consider having createWeakPtr populate m_ref the first time cre
ateWeakPtr is called. |
| 142 WeakPtr<T> createWeakPtr() { return WeakPtr<T>(m_ref); } |
| 45 | 143 |
| 46 void revokeAll() | 144 void revokeAll() |
| 47 { | 145 { |
| 48 m_factory.InvalidateWeakPtrs(); | 146 T* ptr = m_ref->get(); |
| 147 m_ref->clear(); |
| 148 // We create a new WeakReference so that future calls to createWeakPtr()
create nonzero WeakPtrs. |
| 149 m_ref = WeakReference<T>::create(ptr); |
| 49 } | 150 } |
| 50 | 151 |
| 51 bool hasWeakPtrs() const | 152 bool hasWeakPtrs() const |
| 52 { | 153 { |
| 53 return m_factory.HasWeakPtrs(); | 154 return m_ref->refCount() > 1; |
| 54 } | 155 } |
| 55 | 156 |
| 56 private: | 157 private: |
| 57 base::WeakPtrFactory<T> m_factory; | 158 RefPtr<WeakReference<T>> m_ref; |
| 58 }; | 159 }; |
| 59 | 160 |
| 60 } // namespace WTF | 161 } // namespace WTF |
| 61 | 162 |
| 62 using WTF::WeakPtr; | 163 using WTF::WeakPtr; |
| 63 using WTF::WeakPtrFactory; | 164 using WTF::WeakPtrFactory; |
| 165 using WTF::WeakReference; |
| 64 | 166 |
| 65 #endif | 167 #endif |
| OLD | NEW |