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

Unified Diff: third_party/WebKit/Source/wtf/WeakPtr.h

Issue 2318883002: Revert of Implement WTF::WeakPtr in terms of base::WeakPtr (Closed)
Patch Set: 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
« no previous file with comments | « third_party/WebKit/Source/wtf/Functional.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 5b790e4055a5880e374d02f88fb56bc048eab843..8bec53fa7e75c9be75081ecd20dad4aaa4735cc2 100644
--- a/third_party/WebKit/Source/wtf/WeakPtr.h
+++ b/third_party/WebKit/Source/wtf/WeakPtr.h
@@ -26,40 +26,142 @@
#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>
-using WeakPtr = base::WeakPtr<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();
+}
template<typename T>
class WeakPtrFactory {
WTF_MAKE_NONCOPYABLE(WeakPtrFactory<T>);
USING_FAST_MALLOC(WeakPtrFactory);
public:
- explicit WeakPtrFactory(T* ptr) : m_factory(ptr) { }
+ explicit WeakPtrFactory(T* ptr) : m_ref(WeakReference<T>::create(ptr)) { }
- WeakPtr<T> createWeakPtr() { return m_factory.GetWeakPtr(); }
+ WeakPtrFactory(PassRefPtr<WeakReference<T>> ref, T* ptr)
+ : m_ref(ref)
+ {
+ m_ref->bindTo(ptr);
+ }
+
+ ~WeakPtrFactory() { m_ref->clear(); }
+
+ // We should consider having createWeakPtr populate m_ref the first time createWeakPtr is called.
+ WeakPtr<T> createWeakPtr() { return WeakPtr<T>(m_ref); }
void revokeAll()
{
- m_factory.InvalidateWeakPtrs();
+ 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);
}
bool hasWeakPtrs() const
{
- return m_factory.HasWeakPtrs();
+ return m_ref->refCount() > 1;
}
private:
- base::WeakPtrFactory<T> m_factory;
+ RefPtr<WeakReference<T>> m_ref;
};
} // namespace WTF
using WTF::WeakPtr;
using WTF::WeakPtrFactory;
+using WTF::WeakReference;
#endif
« no previous file with comments | « third_party/WebKit/Source/wtf/Functional.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698