Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1038)

Side by Side Diff: third_party/WebKit/Source/wtf/WeakPtr.h

Issue 2329243002: Implement WTF::WeakPtr in terms of base::WeakPtr (Closed)
Patch Set: Thread-safety fix with comment Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
29 #include "wtf/Noncopyable.h" 30 #include "wtf/Noncopyable.h"
30 #include "wtf/PassRefPtr.h"
31 #include "wtf/RefPtr.h"
32 #include "wtf/ThreadSafeRefCounted.h"
33 #include "wtf/Threading.h"
34 31
35 namespace WTF { 32 namespace WTF {
36 33
37 template<typename T> 34 template<typename T>
38 class WeakReference : public ThreadSafeRefCounted<WeakReference<T>> { 35 using WeakPtr = base::WeakPtr<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 }
125 36
126 template<typename T> 37 template<typename T>
127 class WeakPtrFactory { 38 class WeakPtrFactory {
128 WTF_MAKE_NONCOPYABLE(WeakPtrFactory<T>); 39 WTF_MAKE_NONCOPYABLE(WeakPtrFactory<T>);
129 USING_FAST_MALLOC(WeakPtrFactory); 40 USING_FAST_MALLOC(WeakPtrFactory);
130 public: 41 public:
131 explicit WeakPtrFactory(T* ptr) : m_ref(WeakReference<T>::create(ptr)) { } 42 explicit WeakPtrFactory(T* ptr) : m_factory(ptr) { }
132 43
133 WeakPtrFactory(PassRefPtr<WeakReference<T>> ref, T* ptr) 44 WeakPtr<T> createWeakPtr() { return m_factory.GetWeakPtr(); }
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); }
143 45
144 void revokeAll() 46 void revokeAll()
145 { 47 {
146 T* ptr = m_ref->get(); 48 m_factory.InvalidateWeakPtrs();
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);
150 } 49 }
151 50
152 bool hasWeakPtrs() const 51 bool hasWeakPtrs() const
153 { 52 {
154 return m_ref->refCount() > 1; 53 return m_factory.HasWeakPtrs();
155 } 54 }
156 55
157 private: 56 private:
158 RefPtr<WeakReference<T>> m_ref; 57 base::WeakPtrFactory<T> m_factory;
159 }; 58 };
160 59
161 } // namespace WTF 60 } // namespace WTF
162 61
163 using WTF::WeakPtr; 62 using WTF::WeakPtr;
164 using WTF::WeakPtrFactory; 63 using WTF::WeakPtrFactory;
165 using WTF::WeakReference;
166 64
167 #endif 65 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698