From 1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c Mon Sep 17 00:00:00 2001 From: Lorry Tar Creator Date: Tue, 27 Jun 2017 06:07:23 +0000 Subject: webkitgtk-2.16.5 --- Source/WTF/wtf/WeakPtr.h | 84 +++++++++++++++++++++++++++++++----------------- 1 file changed, 55 insertions(+), 29 deletions(-) (limited to 'Source/WTF/wtf/WeakPtr.h') diff --git a/Source/WTF/wtf/WeakPtr.h b/Source/WTF/wtf/WeakPtr.h index 97da507aa..f15160bec 100644 --- a/Source/WTF/wtf/WeakPtr.h +++ b/Source/WTF/wtf/WeakPtr.h @@ -1,5 +1,6 @@ /* * Copyright (C) 2013 Google, Inc. All Rights Reserved. + * Copyright (C) 2015 Apple Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -27,8 +28,7 @@ #define WTF_WeakPtr_h #include -#include -#include +#include #include #include @@ -38,14 +38,15 @@ namespace WTF { +template class WeakPtr; +template class WeakPtrFactory; + +// Note: WeakReference is an implementation detail, and should not be used directly. template class WeakReference : public ThreadSafeRefCounted> { WTF_MAKE_NONCOPYABLE(WeakReference); WTF_MAKE_FAST_ALLOCATED; public: - static PassRefPtr> create(T* ptr) { return adoptRef(new WeakReference(ptr)); } - static PassRefPtr> createUnbound() { return adoptRef(new WeakReference()); } - T* get() const { #if USE(WEB_THREAD) @@ -63,20 +64,14 @@ public: #else ASSERT(m_boundThread == currentThread()); #endif - m_ptr = 0; - } - - void bindTo(T* ptr) - { - ASSERT(!m_ptr); -#ifndef NDEBUG - m_boundThread = currentThread(); -#endif - m_ptr = ptr; + m_ptr = nullptr; } private: - WeakReference() : m_ptr(0) { } + friend class WeakPtr; + friend class WeakPtrFactory; + + static Ref> create(T* ptr) { return adoptRef(*new WeakReference(ptr)); } explicit WeakReference(T* ptr) : m_ptr(ptr) @@ -96,18 +91,25 @@ template class WeakPtr { WTF_MAKE_FAST_ALLOCATED; public: - WeakPtr() { } - WeakPtr(PassRefPtr> ref) : m_ref(ref) { } + WeakPtr() : m_ref(WeakReference::create(nullptr)) { } + WeakPtr(const WeakPtr& o) : m_ref(o.m_ref.copyRef()) { } + template WeakPtr(const WeakPtr& o) : m_ref(o.m_ref.copyRef()) { } T* get() const { return m_ref->get(); } - explicit operator bool() const { return m_ref->get(); } + operator bool() const { return m_ref->get(); } + + WeakPtr& operator=(const WeakPtr& o) { m_ref = o.m_ref.copyRef(); return *this; } + WeakPtr& operator=(std::nullptr_t) { m_ref = WeakReference::create(nullptr); return *this; } T* operator->() const { return m_ref->get(); } - WeakPtr& operator=(std::nullptr_t) { m_ref = WeakReference::create(nullptr); return *this; } + void clear() { m_ref = WeakReference::create(nullptr); } private: - RefPtr> m_ref; + friend class WeakPtrFactory; + WeakPtr(Ref>&& ref) : m_ref(std::forward>>(ref)) { } + + Ref> m_ref; }; template @@ -117,16 +119,10 @@ class WeakPtrFactory { public: explicit WeakPtrFactory(T* ptr) : m_ref(WeakReference::create(ptr)) { } - WeakPtrFactory(PassRefPtr> 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 createWeakPtr() { return WeakPtr(m_ref); } + WeakPtr createWeakPtr() const { return WeakPtr(m_ref.copyRef()); } void revokeAll() { @@ -137,9 +133,39 @@ public: } private: - RefPtr> m_ref; + Ref> m_ref; }; +template inline bool operator==(const WeakPtr& a, const WeakPtr& b) +{ + return a.get() == b.get(); +} + +template inline bool operator==(const WeakPtr& a, U* b) +{ + return a.get() == b; +} + +template inline bool operator==(T* a, const WeakPtr& b) +{ + return a == b.get(); +} + +template inline bool operator!=(const WeakPtr& a, const WeakPtr& b) +{ + return a.get() != b.get(); +} + +template inline bool operator!=(const WeakPtr& a, U* b) +{ + return a.get() != b; +} + +template inline bool operator!=(T* a, const WeakPtr& b) +{ + return a != b.get(); +} + } // namespace WTF using WTF::WeakPtr; -- cgit v1.2.1