Title: [190572] trunk
Revision
190572
Author
[email protected]
Date
2015-10-05 13:31:41 -0700 (Mon, 05 Oct 2015)

Log Message

Allow WKRetainPtrs to be used as keys in hashing data structures
https://bugs.webkit.org/show_bug.cgi?id=149762

Reviewed by Anders Carlsson.

Source/WebKit2:

Use RefPtr as a model.

* UIProcess/API/cpp/WKRetainPtr.h:
(WebKit::WKRetainPtr::WKRetainPtr):
(WebKit::WKRetainPtr::isHashTableDeletedValue):
(WebKit::WKRetainPtr::hashTableDeletedValue):
(WTF::HashTraits<WKRetainPtr<P>>::emptyValue):
(WTF::HashTraits<WKRetainPtr<P>>::peek):

Tools:

* TestWebKitAPI/PlatformGTK.cmake:
* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WebKit2/WKRetainPtr.cpp: Added.
(TestWebKitAPI::TEST):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebKit2/ChangeLog (190571 => 190572)


--- trunk/Source/WebKit2/ChangeLog	2015-10-05 20:00:24 UTC (rev 190571)
+++ trunk/Source/WebKit2/ChangeLog	2015-10-05 20:31:41 UTC (rev 190572)
@@ -1,3 +1,19 @@
+2015-10-05  Myles C. Maxfield  <[email protected]>
+
+        Allow WKRetainPtrs to be used as keys in hashing data structures
+        https://bugs.webkit.org/show_bug.cgi?id=149762
+
+        Reviewed by Anders Carlsson.
+
+        Use RefPtr as a model.
+
+        * UIProcess/API/cpp/WKRetainPtr.h:
+        (WebKit::WKRetainPtr::WKRetainPtr):
+        (WebKit::WKRetainPtr::isHashTableDeletedValue):
+        (WebKit::WKRetainPtr::hashTableDeletedValue):
+        (WTF::HashTraits<WKRetainPtr<P>>::emptyValue):
+        (WTF::HashTraits<WKRetainPtr<P>>::peek):
+
 2015-10-05  Enrica Casucci  <[email protected]>
 
         Cannot select certain text on Wikipedia homepage even when zoomed in.

Modified: trunk/Source/WebKit2/UIProcess/API/cpp/WKRetainPtr.h (190571 => 190572)


--- trunk/Source/WebKit2/UIProcess/API/cpp/WKRetainPtr.h	2015-10-05 20:00:24 UTC (rev 190571)
+++ trunk/Source/WebKit2/UIProcess/API/cpp/WKRetainPtr.h	2015-10-05 20:31:41 UTC (rev 190572)
@@ -28,6 +28,9 @@
 
 #include <WebKit/WKType.h>
 #include <algorithm>
+#include <wtf/HashFunctions.h>
+#include <wtf/HashTraits.h>
+#include <wtf/RefPtr.h>
 
 namespace WebKit {
 
@@ -84,6 +87,15 @@
             WKRelease(ptr);
     }
 
+    // Hash table deleted values, which are only constructed and never copied or destroyed.
+    WKRetainPtr(WTF::HashTableDeletedValueType)
+        : m_ptr(hashTableDeletedValue())
+    {
+    }
+
+    bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue(); }
+    constexpr static T hashTableDeletedValue() { return reinterpret_cast<T>(-1); }
+
     PtrType get() const { return m_ptr; }
 
     void clear()
@@ -250,4 +262,24 @@
 using WebKit::AdoptWK;
 using WebKit::adoptWK;
 
+namespace WTF {
+
+template <typename T> struct IsSmartPtr<WKRetainPtr<T>> {
+    static const bool value = true;
+};
+
+template<typename P> struct DefaultHash<WKRetainPtr<P>> {
+    typedef PtrHash<WKRetainPtr<P>> Hash;
+};
+
+template<typename P> struct HashTraits<WKRetainPtr<P>> : SimpleClassHashTraits<WKRetainPtr<P>> {
+    static P emptyValue() { return nullptr; }
+
+    typedef P PeekType;
+    static PeekType peek(const WKRetainPtr<P>& value) { return value.get(); }
+    static PeekType peek(P value) { return value; }
+};
+
+} // namespace WTF
+
 #endif // WKRetainPtr_h

Modified: trunk/Tools/ChangeLog (190571 => 190572)


--- trunk/Tools/ChangeLog	2015-10-05 20:00:24 UTC (rev 190571)
+++ trunk/Tools/ChangeLog	2015-10-05 20:31:41 UTC (rev 190572)
@@ -1,3 +1,15 @@
+2015-10-05  Myles C. Maxfield  <[email protected]>
+
+        Allow WKRetainPtrs to be used as keys in hashing data structures
+        https://bugs.webkit.org/show_bug.cgi?id=149762
+
+        Reviewed by Anders Carlsson.
+
+        * TestWebKitAPI/PlatformGTK.cmake:
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/WebKit2/WKRetainPtr.cpp: Added.
+        (TestWebKitAPI::TEST):
+
 2015-10-05  Simon Fraser  <[email protected]>
 
         Fix the iOS 8 build, where -[UIApplication _enqueueHIDEvent:] is not

Modified: trunk/Tools/TestWebKitAPI/PlatformGTK.cmake (190571 => 190572)


--- trunk/Tools/TestWebKitAPI/PlatformGTK.cmake	2015-10-05 20:00:24 UTC (rev 190571)
+++ trunk/Tools/TestWebKitAPI/PlatformGTK.cmake	2015-10-05 20:31:41 UTC (rev 190572)
@@ -110,6 +110,7 @@
     ${TESTWEBKITAPI_DIR}/Tests/WebKit2/WillSendSubmitEvent.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WebKit2/WKPageGetScaleFactorNotZero.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WebKit2/WKPreferences.cpp
+    ${TESTWEBKITAPI_DIR}/Tests/WebKit2/WKRetainPtr.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WebKit2/WKString.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WebKit2/WKStringJSString.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WebKit2/WKURL.cpp

Modified: trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj (190571 => 190572)


--- trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2015-10-05 20:00:24 UTC (rev 190571)
+++ trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2015-10-05 20:31:41 UTC (rev 190572)
@@ -22,6 +22,7 @@
 		1ADBEFE3130C6AA100D61D19 /* simple-accelerated-compositing.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 1ADBEFBC130C6A0100D61D19 /* simple-accelerated-compositing.html */; };
 		1AEDE22613E5E7E700E62FE8 /* InjectedBundleControllerMac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1AEDE22413E5E7A000E62FE8 /* InjectedBundleControllerMac.mm */; };
 		1CB9BC381A67482300FE5678 /* WeakPtr.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1CB9BC371A67482300FE5678 /* WeakPtr.cpp */; };
+		1CF0D3791BBF2F3D00B4EF54 /* WKRetainPtr.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1CF0D3781BBF2F3D00B4EF54 /* WKRetainPtr.cpp */; };
 		260BA5791B1D2E7B004FA07C /* DFACombiner.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 260BA5781B1D2E7B004FA07C /* DFACombiner.cpp */; };
 		26DF5A6315A2A27E003689C2 /* CancelLoadFromResourceLoadDelegate.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 26DF5A6115A2A22B003689C2 /* CancelLoadFromResourceLoadDelegate.html */; };
 		26F52EAD1828827B0023D412 /* geolocationGetCurrentPosition.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 26F52EAC1828820E0023D412 /* geolocationGetCurrentPosition.html */; };
@@ -470,6 +471,7 @@
 		1AEF994817A09F5300998EF0 /* GetPIDAfterAbortedProcessLaunch.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = GetPIDAfterAbortedProcessLaunch.cpp; sourceTree = "<group>"; };
 		1AFDE6541953B2C000C48FFA /* Optional.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Optional.cpp; sourceTree = "<group>"; };
 		1CB9BC371A67482300FE5678 /* WeakPtr.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WeakPtr.cpp; sourceTree = "<group>"; };
+		1CF0D3781BBF2F3D00B4EF54 /* WKRetainPtr.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WKRetainPtr.cpp; sourceTree = "<group>"; };
 		260BA5781B1D2E7B004FA07C /* DFACombiner.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DFACombiner.cpp; sourceTree = "<group>"; };
 		260BA57A1B1D2EE2004FA07C /* DFAHelpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DFAHelpers.h; sourceTree = "<group>"; };
 		261516D515B0E60500A2C201 /* SetAndUpdateCacheModel.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = SetAndUpdateCacheModel.mm; sourceTree = "<group>"; };
@@ -1141,6 +1143,7 @@
 				BC7B619A1299FE9E00D174A4 /* WKPreferences.cpp */,
 				BC90995D12567BC100083756 /* WKString.cpp */,
 				BC9099931256ACF100083756 /* WKStringJSString.cpp */,
+				1CF0D3781BBF2F3D00B4EF54 /* WKRetainPtr.cpp */,
 			);
 			path = WebKit2;
 			sourceTree = "<group>";
@@ -1745,6 +1748,7 @@
 				A1C4FB6E1BACCE50003742D0 /* QuickLook.mm in Sources */,
 				7A5623111AD5AF3E0096B920 /* MenuTypesForMouseEvents.cpp in Sources */,
 				51CB4AD81B3A079C00C1B1C6 /* ModalAlertsSPI.cpp in Sources */,
+				1CF0D3791BBF2F3D00B4EF54 /* WKRetainPtr.cpp in Sources */,
 				26F6E1F01ADC749B00DE696B /* DFAMinimizer.cpp in Sources */,
 				260BA5791B1D2E7B004FA07C /* DFACombiner.cpp in Sources */,
 				7A99D9941AD4A29D00373141 /* MenuTypesForMouseEvents.mm in Sources */,

Added: trunk/Tools/TestWebKitAPI/Tests/WebKit2/WKRetainPtr.cpp (0 => 190572)


--- trunk/Tools/TestWebKitAPI/Tests/WebKit2/WKRetainPtr.cpp	                        (rev 0)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKit2/WKRetainPtr.cpp	2015-10-05 20:31:41 UTC (rev 190572)
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2011 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#if WK_HAVE_C_SPI
+
+#include "PlatformUtilities.h"
+#include <wtf/HashMap.h>
+
+namespace TestWebKitAPI {
+
+TEST(WebKit2, WKRetainPtr)
+{
+    WKRetainPtr<WKStringRef> string1 = adoptWK(WKStringCreateWithUTF8CString("a"));
+    WKRetainPtr<WKStringRef> string2 = adoptWK(WKStringCreateWithUTF8CString("a"));
+    WKRetainPtr<WKStringRef> string3 = adoptWK(WKStringCreateWithUTF8CString("a"));
+    WKRetainPtr<WKStringRef> string4 = adoptWK(WKStringCreateWithUTF8CString("a"));
+
+    HashMap<WKRetainPtr<WKStringRef>, int> map;
+
+    map.set(string2, 2);
+    map.set(string1, 1);
+
+    EXPECT_TRUE(map.contains(string1));
+    EXPECT_TRUE(map.contains(string2));
+    EXPECT_FALSE(map.contains(string3));
+    EXPECT_FALSE(map.contains(string4));
+
+    EXPECT_EQ(1, map.get(string1));
+    EXPECT_EQ(2, map.get(string2));
+}
+
+} // namespace TestWebKitAPI
+
+#endif
Property changes on: trunk/Tools/TestWebKitAPI/Tests/WebKit2/WKRetainPtr.cpp
___________________________________________________________________

Added: svn:eol-style

_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to