Minor WeakPtr improvements
authorantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 6 Oct 2017 13:08:58 +0000 (13:08 +0000)
committerantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 6 Oct 2017 13:08:58 +0000 (13:08 +0000)
https://bugs.webkit.org/show_bug.cgi?id=177958

Reviewed by Sam Weinig.

Source/WTF:

* wtf/WeakPtr.h:
(WTF::WeakPtr::operator bool const):

    Make explicit.

(WTF::WeakPtrFactory::createWeakPtr const):
(WTF::makeWeakPtr):

    Remove templating from createWeakPtr, makeWeakPtr() is now the
    canonical way to make derived-type WeakPtrs.

Tools:

* TestWebKitAPI/Tests/WTF/WeakPtr.cpp:
(TestWebKitAPI::TEST):
(TestWebKitAPI::Base::createWeakPtr): Deleted.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@222977 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WTF/ChangeLog
Source/WTF/wtf/WeakPtr.h
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WTF/WeakPtr.cpp

index a34105c..c7e8d87 100644 (file)
@@ -1,3 +1,21 @@
+2017-10-06  Antti Koivisto  <antti@apple.com>
+
+        Minor WeakPtr improvements
+        https://bugs.webkit.org/show_bug.cgi?id=177958
+
+        Reviewed by Sam Weinig.
+
+        * wtf/WeakPtr.h:
+        (WTF::WeakPtr::operator bool const):
+
+            Make explicit.
+
+        (WTF::WeakPtrFactory::createWeakPtr const):
+        (WTF::makeWeakPtr):
+
+            Remove templating from createWeakPtr, makeWeakPtr() is now the
+            canonical way to make derived-type WeakPtrs.
+
 2017-10-04  Filip Pizlo  <fpizlo@apple.com>
 
         bmalloc mutex should be adaptive
index baf07c9..fd9ff76 100644 (file)
@@ -61,9 +61,6 @@ private:
     T* m_ptr;
 };
 
-template<typename T, typename U> WeakReference<T>* weak_reference_upcast(WeakReference<U>*);
-template<typename T, typename U> WeakReference<T>* weak_reference_downcast(WeakReference<U>*);
-
 template<typename T>
 class WeakPtr {
     WTF_MAKE_FAST_ALLOCATED;
@@ -75,7 +72,7 @@ public:
     template<typename U> WeakPtr(WeakPtr<U>&&);
 
     T* get() const { return m_ref ? m_ref->get() : nullptr; }
-    operator bool() const { return m_ref && m_ref->get(); }
+    explicit operator bool() const { return m_ref && m_ref->get(); }
 
     WeakPtr& operator=(std::nullptr_t) { m_ref = nullptr; return *this; }
     template<typename U> WeakPtr& operator=(const WeakPtr<U>&);
@@ -88,6 +85,7 @@ public:
 
 private:
     template<typename U> friend class WeakPtr;
+    template<typename U> friend WeakPtr<U> makeWeakPtr(U&);
 
     RefPtr<WeakReference<T>> m_ref;
 };
@@ -105,12 +103,11 @@ public:
         m_ref->clear();
     }
 
-    template<typename U = T>
-    WeakPtr<U> createWeakPtr(T& ptr) const
+    WeakPtr<T> createWeakPtr(T& ptr) const
     {
         if (!m_ref)
             m_ref = WeakReference<T>::create(&ptr);
-        return { makeRef(*weak_reference_downcast<U>(m_ref.get())) };
+        return { makeRef(*m_ref) };
     }
 
     void revokeAll()
@@ -162,7 +159,7 @@ template<typename T> template<typename U> inline WeakPtr<T>& WeakPtr<T>::operato
 
 template<typename T> inline WeakPtr<T> makeWeakPtr(T& ref)
 {
-    return ref.weakPtrFactory().template createWeakPtr<T>(ref);
+    return { adoptRef(*weak_reference_downcast<T>(ref.weakPtrFactory().createWeakPtr(ref).m_ref.leakRef())) };
 }
 
 template<typename T> inline WeakPtr<T> makeWeakPtr(T* ptr)
index 7d63fc0..0acbfde 100644 (file)
@@ -1,3 +1,14 @@
+2017-10-06  Antti Koivisto  <antti@apple.com>
+
+        Minor WeakPtr improvements
+        https://bugs.webkit.org/show_bug.cgi?id=177958
+
+        Reviewed by Sam Weinig.
+
+        * TestWebKitAPI/Tests/WTF/WeakPtr.cpp:
+        (TestWebKitAPI::TEST):
+        (TestWebKitAPI::Base::createWeakPtr): Deleted.
+
 2017-10-06  Carlos Garcia Campos  <cgarcia@igalia.com>
 
         Unreviewed. Fix parsing of GTK unit tests output in bot configuration.
index c6d26c9..ee259ba 100644 (file)
@@ -40,9 +40,9 @@ TEST(WTF_WeakPtr, Basic)
     EXPECT_EQ(weakPtr1.get(), &dummy);
     EXPECT_EQ(weakPtr2.get(), &dummy);
     EXPECT_EQ(weakPtr3.get(), &dummy);
-    EXPECT_TRUE(weakPtr1);
-    EXPECT_TRUE(weakPtr2);
-    EXPECT_TRUE(weakPtr3);
+    EXPECT_TRUE(!!weakPtr1);
+    EXPECT_TRUE(!!weakPtr2);
+    EXPECT_TRUE(!!weakPtr3);
     EXPECT_TRUE(weakPtr1 == weakPtr2);
     EXPECT_TRUE(weakPtr1 == &dummy);
     EXPECT_TRUE(&dummy == weakPtr2);
@@ -206,12 +206,6 @@ public:
         return 0;
     }
 
-    template<typename T>
-    WeakPtr<T> createWeakPtr()
-    {
-        return m_weakPtrFactory.createWeakPtr<T>(*this);
-    }
-
     auto& weakPtrFactory() { return m_weakPtrFactory; }
 
 private:
@@ -239,12 +233,12 @@ TEST(WTF_WeakPtr, Downcasting)
         Derived* derivedPtr = &object;
         Base* basePtr = static_cast<Base*>(&object);
 
-        baseWeakPtr = object.createWeakPtr<Base>();
+        baseWeakPtr = object.weakPtrFactory().createWeakPtr(object);
         EXPECT_EQ(basePtr->foo(), dummy0);
         EXPECT_EQ(baseWeakPtr->foo(), basePtr->foo());
         EXPECT_EQ(baseWeakPtr.get()->foo(), basePtr->foo());
 
-        derivedWeakPtr = object.createWeakPtr<Derived>();
+        derivedWeakPtr = makeWeakPtr(object);
         EXPECT_EQ(derivedWeakPtr->foo(), dummy1);
         EXPECT_EQ(derivedWeakPtr->foo(), derivedPtr->foo());
         EXPECT_EQ(derivedWeakPtr.get()->foo(), derivedPtr->foo());