| 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 17 matching lines...) Expand all Loading... |
| 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 WTF_MAKE_FAST_ALLOCATED; | 40 WTF_MAKE_FAST_ALLOCATED; |
| 41 public: | 41 public: |
| 42 static PassRefPtr<WeakReference<T> > create(T* ptr) { return adoptRef(new We
akReference(ptr)); } | 42 static PassRefPtr<WeakReference<T>> create(T* ptr) { return adoptRef(new Wea
kReference(ptr)); } |
| 43 static PassRefPtr<WeakReference<T> > createUnbound() { return adoptRef(new W
eakReference()); } | 43 static PassRefPtr<WeakReference<T>> createUnbound() { return adoptRef(new We
akReference()); } |
| 44 | 44 |
| 45 T* get() const | 45 T* get() const |
| 46 { | 46 { |
| 47 ASSERT(m_boundThread == currentThread()); | 47 ASSERT(m_boundThread == currentThread()); |
| 48 return m_ptr; | 48 return m_ptr; |
| 49 } | 49 } |
| 50 | 50 |
| 51 void clear() | 51 void clear() |
| 52 { | 52 { |
| 53 ASSERT(m_boundThread == currentThread()); | 53 ASSERT(m_boundThread == currentThread()); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 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 WTF_MAKE_FAST_ALLOCATED; | 85 WTF_MAKE_FAST_ALLOCATED; |
| 86 public: | 86 public: |
| 87 WeakPtr() { } | 87 WeakPtr() { } |
| 88 WeakPtr(std::nullptr_t) { } | 88 WeakPtr(std::nullptr_t) { } |
| 89 WeakPtr(PassRefPtr<WeakReference<T> > ref) : m_ref(ref) { } | 89 WeakPtr(PassRefPtr<WeakReference<T>> ref) : m_ref(ref) { } |
| 90 | 90 |
| 91 T* get() const { return m_ref ? m_ref->get() : 0; } | 91 T* get() const { return m_ref ? m_ref->get() : 0; } |
| 92 void clear() { m_ref.clear(); } | 92 void clear() { m_ref.clear(); } |
| 93 | 93 |
| 94 T* operator->() const | 94 T* operator->() const |
| 95 { | 95 { |
| 96 ASSERT(get()); | 96 ASSERT(get()); |
| 97 return get(); | 97 return get(); |
| 98 } | 98 } |
| 99 | 99 |
| 100 typedef RefPtr<WeakReference<T> > (WeakPtr::*UnspecifiedBoolType); | 100 typedef RefPtr<WeakReference<T>> (WeakPtr::*UnspecifiedBoolType); |
| 101 operator UnspecifiedBoolType() const { return get() ? &WeakPtr::m_ref : 0; } | 101 operator UnspecifiedBoolType() const { return get() ? &WeakPtr::m_ref : 0; } |
| 102 | 102 |
| 103 private: | 103 private: |
| 104 RefPtr<WeakReference<T> > m_ref; | 104 RefPtr<WeakReference<T>> m_ref; |
| 105 }; | 105 }; |
| 106 | 106 |
| 107 template<typename T, typename U> inline bool operator==(const WeakPtr<T>& a, con
st WeakPtr<U>& b) | 107 template<typename T, typename U> inline bool operator==(const WeakPtr<T>& a, con
st WeakPtr<U>& b) |
| 108 { | 108 { |
| 109 return a.get() == b.get(); | 109 return a.get() == b.get(); |
| 110 } | 110 } |
| 111 | 111 |
| 112 template<typename T, typename U> inline bool operator!=(const WeakPtr<T>& a, con
st WeakPtr<U>& b) | 112 template<typename T, typename U> inline bool operator!=(const WeakPtr<T>& a, con
st WeakPtr<U>& b) |
| 113 { | 113 { |
| 114 return a.get() != b.get(); | 114 return a.get() != b.get(); |
| 115 } | 115 } |
| 116 | 116 |
| 117 template<typename T> | 117 template<typename T> |
| 118 class WeakPtrFactory { | 118 class WeakPtrFactory { |
| 119 WTF_MAKE_NONCOPYABLE(WeakPtrFactory<T>); | 119 WTF_MAKE_NONCOPYABLE(WeakPtrFactory<T>); |
| 120 WTF_MAKE_FAST_ALLOCATED; | 120 WTF_MAKE_FAST_ALLOCATED; |
| 121 public: | 121 public: |
| 122 explicit WeakPtrFactory(T* ptr) : m_ref(WeakReference<T>::create(ptr)) { } | 122 explicit WeakPtrFactory(T* ptr) : m_ref(WeakReference<T>::create(ptr)) { } |
| 123 | 123 |
| 124 WeakPtrFactory(PassRefPtr<WeakReference<T> > ref, T* ptr) | 124 WeakPtrFactory(PassRefPtr<WeakReference<T>> ref, T* ptr) |
| 125 : m_ref(ref) | 125 : m_ref(ref) |
| 126 { | 126 { |
| 127 m_ref->bindTo(ptr); | 127 m_ref->bindTo(ptr); |
| 128 } | 128 } |
| 129 | 129 |
| 130 ~WeakPtrFactory() { m_ref->clear(); } | 130 ~WeakPtrFactory() { m_ref->clear(); } |
| 131 | 131 |
| 132 // We should consider having createWeakPtr populate m_ref the first time cre
ateWeakPtr is called. | 132 // We should consider having createWeakPtr populate m_ref the first time cre
ateWeakPtr is called. |
| 133 WeakPtr<T> createWeakPtr() { return WeakPtr<T>(m_ref); } | 133 WeakPtr<T> createWeakPtr() { return WeakPtr<T>(m_ref); } |
| 134 | 134 |
| 135 void revokeAll() | 135 void revokeAll() |
| 136 { | 136 { |
| 137 T* ptr = m_ref->get(); | 137 T* ptr = m_ref->get(); |
| 138 m_ref->clear(); | 138 m_ref->clear(); |
| 139 // We create a new WeakReference so that future calls to createWeakPtr()
create nonzero WeakPtrs. | 139 // We create a new WeakReference so that future calls to createWeakPtr()
create nonzero WeakPtrs. |
| 140 m_ref = WeakReference<T>::create(ptr); | 140 m_ref = WeakReference<T>::create(ptr); |
| 141 } | 141 } |
| 142 | 142 |
| 143 private: | 143 private: |
| 144 RefPtr<WeakReference<T> > m_ref; | 144 RefPtr<WeakReference<T>> m_ref; |
| 145 }; | 145 }; |
| 146 | 146 |
| 147 } // namespace WTF | 147 } // namespace WTF |
| 148 | 148 |
| 149 using WTF::WeakPtr; | 149 using WTF::WeakPtr; |
| 150 using WTF::WeakPtrFactory; | 150 using WTF::WeakPtrFactory; |
| 151 using WTF::WeakReference; | 151 using WTF::WeakReference; |
| 152 | 152 |
| 153 #endif | 153 #endif |
| OLD | NEW |