Title: [222633] trunk
- Revision
- 222633
- Author
- [email protected]
- Date
- 2017-09-28 14:31:40 -0700 (Thu, 28 Sep 2017)
Log Message
WeakPtrFactory should allow downcasting
https://bugs.webkit.org/show_bug.cgi?id=177389
<rdar://problem/34604174>
Reviewed by Geoffrey Garen.
Source/WTF:
In this patch, WeakPtrFactory is enhanced with the ability to create WeakPtrs
of its owner's sub classes and have them point to the same WeakReference.
* wtf/WeakPtr.h:
(WTF::WeakPtr::WeakPtr):
We cannot determine the base class of type T, thus no friends. It is made public
such that WeakPtrFactory with a base class type U can create a derived type T
WeakPtr.
(WTF::WeakPtrFactory::createWeakPtr const):
Tools:
* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WTF/WeakPtr.cpp:
(TestWebKitAPI::Base::foo):
(TestWebKitAPI::Base::createWeakPtr):
(TestWebKitAPI::Derived::foo):
(TestWebKitAPI::TEST):
Modified Paths
Diff
Modified: trunk/Source/WTF/ChangeLog (222632 => 222633)
--- trunk/Source/WTF/ChangeLog 2017-09-28 21:13:13 UTC (rev 222632)
+++ trunk/Source/WTF/ChangeLog 2017-09-28 21:31:40 UTC (rev 222633)
@@ -1,3 +1,21 @@
+2017-09-28 Jiewen Tan <[email protected]>
+
+ WeakPtrFactory should allow downcasting
+ https://bugs.webkit.org/show_bug.cgi?id=177389
+ <rdar://problem/34604174>
+
+ Reviewed by Geoffrey Garen.
+
+ In this patch, WeakPtrFactory is enhanced with the ability to create WeakPtrs
+ of its owner's sub classes and have them point to the same WeakReference.
+
+ * wtf/WeakPtr.h:
+ (WTF::WeakPtr::WeakPtr):
+ We cannot determine the base class of type T, thus no friends. It is made public
+ such that WeakPtrFactory with a base class type U can create a derived type T
+ WeakPtr.
+ (WTF::WeakPtrFactory::createWeakPtr const):
+
2017-09-28 Don Olmstead <[email protected]>
Sync SYSTEM_MALLOC implementation of Gigacage
Modified: trunk/Source/WTF/wtf/WeakPtr.h (222632 => 222633)
--- trunk/Source/WTF/wtf/WeakPtr.h 2017-09-28 21:13:13 UTC (rev 222632)
+++ trunk/Source/WTF/wtf/WeakPtr.h 2017-09-28 21:31:40 UTC (rev 222633)
@@ -68,7 +68,7 @@
WeakPtr() : m_ref(WeakReference<T>::create(nullptr)) { }
WeakPtr(std::nullptr_t) : m_ref(WeakReference<T>::create(nullptr)) { }
WeakPtr(const WeakPtr& o) : m_ref(o.m_ref.copyRef()) { }
- template<typename U> WeakPtr(const WeakPtr<U>& o) : m_ref(o.m_ref.copyRef()) { }
+ WeakPtr(Ref<WeakReference<T>>&& ref) : m_ref(std::forward<Ref<WeakReference<T>>>(ref)) { }
T* get() const { return m_ref->get(); }
operator bool() const { return m_ref->get(); }
@@ -81,9 +81,6 @@
void clear() { m_ref = WeakReference<T>::create(nullptr); }
private:
- friend class WeakPtrFactory<T>;
- WeakPtr(Ref<WeakReference<T>>&& ref) : m_ref(std::forward<Ref<WeakReference<T>>>(ref)) { }
-
Ref<WeakReference<T>> m_ref;
};
@@ -100,12 +97,14 @@
m_ref->clear();
}
- WeakPtr<T> createWeakPtr(T& ptr) const
+ template<typename U = T>
+ WeakPtr<U> createWeakPtr(T& ptr) const
{
if (!m_ref)
m_ref = WeakReference<T>::create(&ptr);
ASSERT(&ptr == m_ref->get());
- return WeakPtr<T>(Ref<WeakReference<T>>(*m_ref));
+ static_assert(std::is_convertible<U*, T*>::value, "T* must be convertible to U*");
+ return WeakPtr<U>(Ref<WeakReference<U>>(reinterpret_cast<WeakReference<U>&>(*m_ref)));
}
void revokeAll()
Modified: trunk/Tools/ChangeLog (222632 => 222633)
--- trunk/Tools/ChangeLog 2017-09-28 21:13:13 UTC (rev 222632)
+++ trunk/Tools/ChangeLog 2017-09-28 21:31:40 UTC (rev 222633)
@@ -1,3 +1,18 @@
+2017-09-28 Jiewen Tan <[email protected]>
+
+ WeakPtrFactory should allow downcasting
+ https://bugs.webkit.org/show_bug.cgi?id=177389
+ <rdar://problem/34604174>
+
+ Reviewed by Geoffrey Garen.
+
+ * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+ * TestWebKitAPI/Tests/WTF/WeakPtr.cpp:
+ (TestWebKitAPI::Base::foo):
+ (TestWebKitAPI::Base::createWeakPtr):
+ (TestWebKitAPI::Derived::foo):
+ (TestWebKitAPI::TEST):
+
2017-09-28 Megan Gardner <[email protected]>
Add debug flag to WebKitTestRunner to show where touches are being generated
Modified: trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj (222632 => 222633)
--- trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj 2017-09-28 21:13:13 UTC (rev 222632)
+++ trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj 2017-09-28 21:31:40 UTC (rev 222633)
@@ -218,6 +218,7 @@
57901FB11CAF142D00ED64F9 /* LoadInvalidURLRequest.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 57901FB01CAF141C00ED64F9 /* LoadInvalidURLRequest.html */; };
5797FE311EB15A6800B2F4A0 /* NavigationClientDefaultCrypto.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5797FE2F1EB15A5F00B2F4A0 /* NavigationClientDefaultCrypto.cpp */; };
5797FE331EB15AB100B2F4A0 /* navigation-client-default-crypto.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 5797FE321EB15A8900B2F4A0 /* navigation-client-default-crypto.html */; };
+ 57C3FA661F7C248F009D4B80 /* WeakPtr.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1CB9BC371A67482300FE5678 /* WeakPtr.cpp */; };
57F56A5C1C7F8CC100F31D7E /* IsNavigationActionTrusted.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 57F56A5B1C7F8A4000F31D7E /* IsNavigationActionTrusted.html */; };
5C0BF88D1DD5964D00B00328 /* MemoryPressureHandler.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5C0BF88C1DD5957400B00328 /* MemoryPressureHandler.mm */; };
5C0BF8911DD599A900B00328 /* WebViewCanPasteZeroPng.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5C0BF88F1DD5999B00B00328 /* WebViewCanPasteZeroPng.mm */; };
@@ -3124,6 +3125,7 @@
7C83FC711D5535A8001DFBCD /* Variant.cpp in Sources */,
7C83DF4C1D0A590C00FEBCF3 /* Vector.cpp in Sources */,
37C7CC2D1EA4146B007BD956 /* WeakLinking.cpp in Sources */,
+ 57C3FA661F7C248F009D4B80 /* WeakPtr.cpp in Sources */,
7C83DF631D0A590C00FEBCF3 /* WTFString.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/WeakPtr.cpp (222632 => 222633)
--- trunk/Tools/TestWebKitAPI/Tests/WTF/WeakPtr.cpp 2017-09-28 21:13:13 UTC (rev 222632)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/WeakPtr.cpp 2017-09-28 21:31:40 UTC (rev 222633)
@@ -180,5 +180,60 @@
weakPtr7 = nullptr;
EXPECT_NULL(weakPtr7.get());
}
-
+
+class Base {
+public:
+ int foo()
+ {
+ return 0;
+ }
+
+ template<typename T>
+ WeakPtr<T> createWeakPtr()
+ {
+ return m_weakPtrFactory.createWeakPtr<T>(*this);
+ }
+
+private:
+ WeakPtrFactory<Base> m_weakPtrFactory;
+};
+
+class Derived : public Base {
+public:
+ int foo()
+ {
+ return 1;
+ }
+};
+
+TEST(WTF_WeakPtr, Downcasting)
+{
+ int dummy0 = 0;
+ int dummy1 = 1;
+
+ WeakPtr<Base> baseWeakPtr;
+ WeakPtr<Derived> derivedWeakPtr;
+
+ {
+ Derived object;
+ Derived* derivedPtr = &object;
+ Base* basePtr = static_cast<Base*>(&object);
+
+ baseWeakPtr = object.createWeakPtr<Base>();
+ EXPECT_EQ(basePtr->foo(), dummy0);
+ EXPECT_EQ(baseWeakPtr->foo(), basePtr->foo());
+ EXPECT_EQ(baseWeakPtr.get()->foo(), basePtr->foo());
+
+ derivedWeakPtr = object.createWeakPtr<Derived>();
+ EXPECT_EQ(derivedWeakPtr->foo(), dummy1);
+ EXPECT_EQ(derivedWeakPtr->foo(), derivedPtr->foo());
+ EXPECT_EQ(derivedWeakPtr.get()->foo(), derivedPtr->foo());
+
+ EXPECT_EQ(baseWeakPtr.get(), derivedWeakPtr.get());
+ }
+
+ EXPECT_NULL(baseWeakPtr.get());
+ EXPECT_NULL(derivedWeakPtr.get());
+}
+
} // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes