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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/wtf/WeakPtr.h
diff --git a/third_party/WebKit/Source/wtf/WeakPtr.h b/third_party/WebKit/Source/wtf/WeakPtr.h
index 8bec53fa7e75c9be75081ecd20dad4aaa4735cc2..5b790e4055a5880e374d02f88fb56bc048eab843 100644
--- a/third_party/WebKit/Source/wtf/WeakPtr.h
+++ b/third_party/WebKit/Source/wtf/WeakPtr.h
@@ -26,142 +26,40 @@
#ifndef WTF_WeakPtr_h
#define WTF_WeakPtr_h
+#include "base/memory/weak_ptr.h"
#include "wtf/Noncopyable.h"
-#include "wtf/PassRefPtr.h"
-#include "wtf/RefPtr.h"
-#include "wtf/ThreadSafeRefCounted.h"
-#include "wtf/Threading.h"
namespace WTF {
template<typename T>
-class WeakReference : public ThreadSafeRefCounted<WeakReference<T>> {
- WTF_MAKE_NONCOPYABLE(WeakReference<T>);
- USING_FAST_MALLOC(WeakReference);
-public:
- static PassRefPtr<WeakReference<T>> create(T* ptr) { return adoptRef(new WeakReference(ptr)); }
- static PassRefPtr<WeakReference<T>> createUnbound() { return adoptRef(new WeakReference()); }
-
- T* get() const
- {
-#if DCHECK_IS_ON()
- DCHECK_EQ(m_boundThread, currentThread());
-#endif
- return m_ptr;
- }
-
- void clear()
- {
-#if DCHECK_IS_ON()
- DCHECK_EQ(m_boundThread, currentThread());
-#endif
- m_ptr = 0;
- }
-
- void bindTo(T* ptr)
- {
- DCHECK(!m_ptr);
-#if DCHECK_IS_ON()
- m_boundThread = currentThread();
-#endif
- m_ptr = ptr;
- }
-
-private:
- WeakReference() : m_ptr(0) { }
-
- explicit WeakReference(T* ptr)
- : m_ptr(ptr)
-#if DCHECK_IS_ON()
- , m_boundThread(currentThread())
-#endif
- {
- }
-
- T* m_ptr;
-#if DCHECK_IS_ON()
- ThreadIdentifier m_boundThread;
-#endif
-};
-
-template<typename T>
-class WeakPtr {
- USING_FAST_MALLOC(WeakPtr);
-public:
- WeakPtr() { }
- WeakPtr(std::nullptr_t) { }
- WeakPtr(PassRefPtr<WeakReference<T>> ref) : m_ref(ref) { }
-
- T* get() const { return m_ref ? m_ref->get() : 0; }
- void clear() { m_ref.clear(); }
-
- T& operator*() const
- {
- DCHECK(get());
- return *get();
- }
-
- T* operator->() const
- {
- DCHECK(get());
- return get();
- }
-
- explicit operator bool() const { return get(); }
-
-private:
- RefPtr<WeakReference<T>> m_ref;
-};
-
-template<typename T, typename U> inline bool operator==(const WeakPtr<T>& a, const WeakPtr<U>& b)
-{
- return a.get() == b.get();
-}
-
-template<typename T, typename U> inline bool operator!=(const WeakPtr<T>& a, const WeakPtr<U>& b)
-{
- return a.get() != b.get();
-}
+using WeakPtr = base::WeakPtr<T>;
template<typename T>
class WeakPtrFactory {
WTF_MAKE_NONCOPYABLE(WeakPtrFactory<T>);
USING_FAST_MALLOC(WeakPtrFactory);
public:
- explicit WeakPtrFactory(T* ptr) : m_ref(WeakReference<T>::create(ptr)) { }
-
- WeakPtrFactory(PassRefPtr<WeakReference<T>> ref, T* ptr)
- : m_ref(ref)
- {
- m_ref->bindTo(ptr);
- }
-
- ~WeakPtrFactory() { m_ref->clear(); }
+ explicit WeakPtrFactory(T* ptr) : m_factory(ptr) { }
- // We should consider having createWeakPtr populate m_ref the first time createWeakPtr is called.
- WeakPtr<T> createWeakPtr() { return WeakPtr<T>(m_ref); }
+ WeakPtr<T> createWeakPtr() { return m_factory.GetWeakPtr(); }
void revokeAll()
{
- T* ptr = m_ref->get();
- m_ref->clear();
- // We create a new WeakReference so that future calls to createWeakPtr() create nonzero WeakPtrs.
- m_ref = WeakReference<T>::create(ptr);
+ m_factory.InvalidateWeakPtrs();
}
bool hasWeakPtrs() const
{
- return m_ref->refCount() > 1;
+ return m_factory.HasWeakPtrs();
}
private:
- RefPtr<WeakReference<T>> m_ref;
+ base::WeakPtrFactory<T> m_factory;
};
} // namespace WTF
using WTF::WeakPtr;
using WTF::WeakPtrFactory;
-using WTF::WeakReference;
#endif

Powered by Google App Engine
This is Rietveld 408576698