| 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 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 #define WTF_WeakPtr_h | 27 #define WTF_WeakPtr_h |
| 28 | 28 |
| 29 #include "wtf/Noncopyable.h" | 29 #include "wtf/Noncopyable.h" |
| 30 #include "wtf/PassRefPtr.h" | 30 #include "wtf/PassRefPtr.h" |
| 31 #include "wtf/RefPtr.h" | 31 #include "wtf/RefPtr.h" |
| 32 #include "wtf/ThreadSafeRefCounted.h" | 32 #include "wtf/ThreadSafeRefCounted.h" |
| 33 #include "wtf/Threading.h" | 33 #include "wtf/Threading.h" |
| 34 | 34 |
| 35 namespace WTF { | 35 namespace WTF { |
| 36 | 36 |
| 37 template<typename T> | 37 template <typename T> |
| 38 class WeakReference : public ThreadSafeRefCounted<WeakReference<T>> { | 38 class WeakReference : public ThreadSafeRefCounted<WeakReference<T>> { |
| 39 WTF_MAKE_NONCOPYABLE(WeakReference<T>); | 39 WTF_MAKE_NONCOPYABLE(WeakReference<T>); |
| 40 USING_FAST_MALLOC(WeakReference); | 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 | 41 |
| 45 T* get() const | 42 public: |
| 46 { | 43 static PassRefPtr<WeakReference<T>> create(T* ptr) { return adoptRef(new WeakR
eference(ptr)); } |
| 47 ASSERT(m_boundThread == currentThread()); | 44 static PassRefPtr<WeakReference<T>> createUnbound() { return adoptRef(new Weak
Reference()); } |
| 48 return m_ptr; | |
| 49 } | |
| 50 | 45 |
| 51 void clear() | 46 T* get() const { |
| 52 { | 47 ASSERT(m_boundThread == currentThread()); |
| 53 ASSERT(m_boundThread == currentThread()); | 48 return m_ptr; |
| 54 m_ptr = 0; | 49 } |
| 55 } | |
| 56 | 50 |
| 57 void bindTo(T* ptr) | 51 void clear() { |
| 58 { | 52 ASSERT(m_boundThread == currentThread()); |
| 59 ASSERT(!m_ptr); | 53 m_ptr = 0; |
| 54 } |
| 55 |
| 56 void bindTo(T* ptr) { |
| 57 ASSERT(!m_ptr); |
| 60 #if ENABLE(ASSERT) | 58 #if ENABLE(ASSERT) |
| 61 m_boundThread = currentThread(); | 59 m_boundThread = currentThread(); |
| 62 #endif | 60 #endif |
| 63 m_ptr = ptr; | 61 m_ptr = ptr; |
| 64 } | 62 } |
| 65 | 63 |
| 66 private: | 64 private: |
| 67 WeakReference() : m_ptr(0) { } | 65 WeakReference() |
| 66 : m_ptr(0) {} |
| 68 | 67 |
| 69 explicit WeakReference(T* ptr) | 68 explicit WeakReference(T* ptr) |
| 70 : m_ptr(ptr) | 69 : m_ptr(ptr) |
| 71 #if ENABLE(ASSERT) | 70 #if ENABLE(ASSERT) |
| 72 , m_boundThread(currentThread()) | 71 , |
| 72 m_boundThread(currentThread()) |
| 73 #endif | 73 #endif |
| 74 { | 74 { |
| 75 } | 75 } |
| 76 | 76 |
| 77 T* m_ptr; | 77 T* m_ptr; |
| 78 #if ENABLE(ASSERT) | 78 #if ENABLE(ASSERT) |
| 79 ThreadIdentifier m_boundThread; | 79 ThreadIdentifier m_boundThread; |
| 80 #endif | 80 #endif |
| 81 }; | 81 }; |
| 82 | 82 |
| 83 template<typename T> | 83 template <typename T> |
| 84 class WeakPtr { | 84 class WeakPtr { |
| 85 USING_FAST_MALLOC(WeakPtr); | 85 USING_FAST_MALLOC(WeakPtr); |
| 86 public: | |
| 87 WeakPtr() { } | |
| 88 WeakPtr(std::nullptr_t) { } | |
| 89 WeakPtr(PassRefPtr<WeakReference<T>> ref) : m_ref(ref) { } | |
| 90 | 86 |
| 91 T* get() const { return m_ref ? m_ref->get() : 0; } | 87 public: |
| 92 void clear() { m_ref.clear(); } | 88 WeakPtr() {} |
| 89 WeakPtr(std::nullptr_t) {} |
| 90 WeakPtr(PassRefPtr<WeakReference<T>> ref) |
| 91 : m_ref(ref) {} |
| 93 | 92 |
| 94 T* operator->() const | 93 T* get() const { return m_ref ? m_ref->get() : 0; } |
| 95 { | 94 void clear() { m_ref.clear(); } |
| 96 ASSERT(get()); | |
| 97 return get(); | |
| 98 } | |
| 99 | 95 |
| 100 typedef RefPtr<WeakReference<T>> (WeakPtr::*UnspecifiedBoolType); | 96 T* operator->() const { |
| 101 operator UnspecifiedBoolType() const { return get() ? &WeakPtr::m_ref : 0; } | 97 ASSERT(get()); |
| 98 return get(); |
| 99 } |
| 102 | 100 |
| 103 private: | 101 typedef RefPtr<WeakReference<T>>(WeakPtr::*UnspecifiedBoolType); |
| 104 RefPtr<WeakReference<T>> m_ref; | 102 operator UnspecifiedBoolType() const { return get() ? &WeakPtr::m_ref : 0; } |
| 103 |
| 104 private: |
| 105 RefPtr<WeakReference<T>> m_ref; |
| 105 }; | 106 }; |
| 106 | 107 |
| 107 template<typename T, typename U> inline bool operator==(const WeakPtr<T>& a, con
st WeakPtr<U>& b) | 108 template <typename T, typename U> |
| 108 { | 109 inline bool operator==(const WeakPtr<T>& a, const WeakPtr<U>& b) { |
| 109 return a.get() == b.get(); | 110 return a.get() == b.get(); |
| 110 } | 111 } |
| 111 | 112 |
| 112 template<typename T, typename U> inline bool operator!=(const WeakPtr<T>& a, con
st WeakPtr<U>& b) | 113 template <typename T, typename U> |
| 113 { | 114 inline bool operator!=(const WeakPtr<T>& a, const WeakPtr<U>& b) { |
| 114 return a.get() != b.get(); | 115 return a.get() != b.get(); |
| 115 } | 116 } |
| 116 | 117 |
| 117 template<typename T> | 118 template <typename T> |
| 118 class WeakPtrFactory { | 119 class WeakPtrFactory { |
| 119 WTF_MAKE_NONCOPYABLE(WeakPtrFactory<T>); | 120 WTF_MAKE_NONCOPYABLE(WeakPtrFactory<T>); |
| 120 USING_FAST_MALLOC(WeakPtrFactory); | 121 USING_FAST_MALLOC(WeakPtrFactory); |
| 121 public: | |
| 122 explicit WeakPtrFactory(T* ptr) : m_ref(WeakReference<T>::create(ptr)) { } | |
| 123 | 122 |
| 124 WeakPtrFactory(PassRefPtr<WeakReference<T>> ref, T* ptr) | 123 public: |
| 125 : m_ref(ref) | 124 explicit WeakPtrFactory(T* ptr) |
| 126 { | 125 : m_ref(WeakReference<T>::create(ptr)) {} |
| 127 m_ref->bindTo(ptr); | |
| 128 } | |
| 129 | 126 |
| 130 ~WeakPtrFactory() { m_ref->clear(); } | 127 WeakPtrFactory(PassRefPtr<WeakReference<T>> ref, T* ptr) |
| 128 : m_ref(ref) { |
| 129 m_ref->bindTo(ptr); |
| 130 } |
| 131 | 131 |
| 132 // We should consider having createWeakPtr populate m_ref the first time cre
ateWeakPtr is called. | 132 ~WeakPtrFactory() { m_ref->clear(); } |
| 133 WeakPtr<T> createWeakPtr() { return WeakPtr<T>(m_ref); } | |
| 134 | 133 |
| 135 void revokeAll() | 134 // We should consider having createWeakPtr populate m_ref the first time creat
eWeakPtr is called. |
| 136 { | 135 WeakPtr<T> createWeakPtr() { return WeakPtr<T>(m_ref); } |
| 137 T* ptr = m_ref->get(); | |
| 138 m_ref->clear(); | |
| 139 // We create a new WeakReference so that future calls to createWeakPtr()
create nonzero WeakPtrs. | |
| 140 m_ref = WeakReference<T>::create(ptr); | |
| 141 } | |
| 142 | 136 |
| 143 bool hasWeakPtrs() const | 137 void revokeAll() { |
| 144 { | 138 T* ptr = m_ref->get(); |
| 145 return m_ref->refCount() > 1; | 139 m_ref->clear(); |
| 146 } | 140 // We create a new WeakReference so that future calls to createWeakPtr() cre
ate nonzero WeakPtrs. |
| 141 m_ref = WeakReference<T>::create(ptr); |
| 142 } |
| 147 | 143 |
| 148 private: | 144 bool hasWeakPtrs() const { |
| 149 RefPtr<WeakReference<T>> m_ref; | 145 return m_ref->refCount() > 1; |
| 146 } |
| 147 |
| 148 private: |
| 149 RefPtr<WeakReference<T>> m_ref; |
| 150 }; | 150 }; |
| 151 | 151 |
| 152 } // namespace WTF | 152 } // namespace WTF |
| 153 | 153 |
| 154 using WTF::WeakPtr; | 154 using WTF::WeakPtr; |
| 155 using WTF::WeakPtrFactory; | 155 using WTF::WeakPtrFactory; |
| 156 using WTF::WeakReference; | 156 using WTF::WeakReference; |
| 157 | 157 |
| 158 #endif | 158 #endif |
| OLD | NEW |