Title: [203257] trunk
Revision
203257
Author
[email protected]
Date
2016-07-14 17:00:36 -0700 (Thu, 14 Jul 2016)

Log Message

Allow RefPtrs of const RefCounted types
https://bugs.webkit.org/show_bug.cgi?id=158269

Patch by Alex Christensen <[email protected]> on 2016-07-14
Reviewed by Anders Carlsson.

Source/WTF:

* wtf/RefCounted.h:
(WTF::RefCountedBase::ref):
(WTF::RefCountedBase::~RefCountedBase):
(WTF::RefCountedBase::derefBase):
(WTF::RefCounted::deref):
Creating references to a const object does not really modify the object,
so everything in RefCounted is now mutable, and ref and deref are const.

Tools:

* TestWebKitAPI/Tests/WTF/RefPtr.cpp:
(TestWebKitAPI::TEST):
(TestWebKitAPI::ConstRefCounted::create):
(TestWebKitAPI::returnConstRefCountedRef):
(TestWebKitAPI::returnRefCountedRef):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (203256 => 203257)


--- trunk/Source/WTF/ChangeLog	2016-07-14 23:46:55 UTC (rev 203256)
+++ trunk/Source/WTF/ChangeLog	2016-07-15 00:00:36 UTC (rev 203257)
@@ -1,3 +1,18 @@
+2016-07-14  Alex Christensen  <[email protected]>
+
+        Allow RefPtrs of const RefCounted types
+        https://bugs.webkit.org/show_bug.cgi?id=158269
+
+        Reviewed by Anders Carlsson.
+
+        * wtf/RefCounted.h:
+        (WTF::RefCountedBase::ref):
+        (WTF::RefCountedBase::~RefCountedBase):
+        (WTF::RefCountedBase::derefBase):
+        (WTF::RefCounted::deref):
+        Creating references to a const object does not really modify the object,
+        so everything in RefCounted is now mutable, and ref and deref are const.
+
 2016-07-14  Chris Dumez  <[email protected]>
 
         Avoid an extra heap allocation when dispatching Functions to WorkQueue

Modified: trunk/Source/WTF/wtf/RefCounted.h (203256 => 203257)


--- trunk/Source/WTF/wtf/RefCounted.h	2016-07-14 23:46:55 UTC (rev 203256)
+++ trunk/Source/WTF/wtf/RefCounted.h	2016-07-15 00:00:36 UTC (rev 203257)
@@ -18,8 +18,7 @@
  *
  */
 
-#ifndef RefCounted_h
-#define RefCounted_h
+#pragma once
 
 #include <wtf/Assertions.h>
 #include <wtf/FastMalloc.h>
@@ -38,7 +37,7 @@
 // generated by the compiler (technique called template hoisting).
 class RefCountedBase {
 public:
-    void ref()
+    void ref() const
     {
 #if CHECK_REF_COUNTED_LIFECYCLE
         ASSERT_WITH_SECURITY_IMPLICATION(!m_deletionHasBegun);
@@ -88,7 +87,7 @@
     }
 
     // Returns whether the pointer should be freed or not.
-    bool derefBase()
+    bool derefBase() const
     {
 #if CHECK_REF_COUNTED_LIFECYCLE
         ASSERT_WITH_SECURITY_IMPLICATION(!m_deletionHasBegun);
@@ -120,10 +119,10 @@
     friend void adopted(RefCountedBase*);
 #endif
 
-    unsigned m_refCount;
+    mutable unsigned m_refCount;
 #if CHECK_REF_COUNTED_LIFECYCLE
-    bool m_deletionHasBegun;
-    bool m_adoptionIsRequired;
+    mutable bool m_deletionHasBegun;
+    mutable bool m_adoptionIsRequired;
 #endif
 };
 
@@ -140,10 +139,10 @@
 template<typename T> class RefCounted : public RefCountedBase {
     WTF_MAKE_NONCOPYABLE(RefCounted); WTF_MAKE_FAST_ALLOCATED;
 public:
-    void deref()
+    void deref() const
     {
         if (derefBase())
-            delete static_cast<T*>(this);
+            delete static_cast<const T*>(this);
     }
 
 protected:
@@ -156,5 +155,3 @@
 } // namespace WTF
 
 using WTF::RefCounted;
-
-#endif // RefCounted_h

Modified: trunk/Tools/ChangeLog (203256 => 203257)


--- trunk/Tools/ChangeLog	2016-07-14 23:46:55 UTC (rev 203256)
+++ trunk/Tools/ChangeLog	2016-07-15 00:00:36 UTC (rev 203257)
@@ -1,3 +1,16 @@
+2016-07-14  Alex Christensen  <[email protected]>
+
+        Allow RefPtrs of const RefCounted types
+        https://bugs.webkit.org/show_bug.cgi?id=158269
+
+        Reviewed by Anders Carlsson.
+
+        * TestWebKitAPI/Tests/WTF/RefPtr.cpp:
+        (TestWebKitAPI::TEST):
+        (TestWebKitAPI::ConstRefCounted::create):
+        (TestWebKitAPI::returnConstRefCountedRef):
+        (TestWebKitAPI::returnRefCountedRef):
+
 2016-07-13  Carlos Garcia Campos  <[email protected]>
 
         Test WTF.StringViewIterators is crashing since r203119

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/RefPtr.cpp (203256 => 203257)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/RefPtr.cpp	2016-07-14 23:46:55 UTC (rev 203256)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/RefPtr.cpp	2016-07-15 00:00:36 UTC (rev 203257)
@@ -26,6 +26,8 @@
 #include "config.h"
 
 #include "RefLogger.h"
+#include <wtf/NeverDestroyed.h>
+#include <wtf/RefCounted.h>
 #include <wtf/RefPtr.h>
 
 namespace TestWebKitAPI {
@@ -403,4 +405,34 @@
     ASSERT_STREQ("ref(a) deref(a) ", takeLogStr().c_str());
 }
 
+    
+struct ConstRefCounted : RefCounted<ConstRefCounted> {
+    static Ref<ConstRefCounted> create() { return adoptRef(*new ConstRefCounted); }
+};
+
+const ConstRefCounted& returnConstRefCountedRef()
+{
+    static NeverDestroyed<ConstRefCounted> instance;
+    return instance.get();
+}
+ConstRefCounted& returnRefCountedRef()
+{
+    static NeverDestroyed<ConstRefCounted> instance;
+    return instance.get();
+}
+
+TEST(WTF_RefPtr, Const)
+{
+    // This test passes if it compiles without an error.
+    auto a = ConstRefCounted::create();
+    Ref<const ConstRefCounted> b = WTFMove(a);
+    RefPtr<const ConstRefCounted> c = b.ptr();
+    Ref<const ConstRefCounted> d = returnConstRefCountedRef();
+    RefPtr<const ConstRefCounted> e = &returnConstRefCountedRef();
+    RefPtr<ConstRefCounted> f = ConstRefCounted::create();
+    RefPtr<const ConstRefCounted> g = f;
+    RefPtr<const ConstRefCounted> h(f);
+    Ref<const ConstRefCounted> i(returnRefCountedRef());
+}
+
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to