Allow creating WeakPtrs to const objects
authorantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 24 Aug 2018 09:30:52 +0000 (09:30 +0000)
committerantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 24 Aug 2018 09:30:52 +0000 (09:30 +0000)
https://bugs.webkit.org/show_bug.cgi?id=188785

Reviewed by Geoff Garen.

Source/WebCore:

Remove some unneeded const_casts.

* css/MediaQueryEvaluator.cpp:
(WebCore::MediaQueryEvaluator::MediaQueryEvaluator):
(WebCore::MediaQueryEvaluator::evaluate const):
* css/MediaQueryEvaluator.h:
* rendering/FloatingObjects.cpp:
(WebCore::ComputeFloatOffsetAdapter::ComputeFloatOffsetAdapter):
(WebCore::ComputeFloatOffsetForFloatLayoutAdapter::ComputeFloatOffsetForFloatLayoutAdapter):
(WebCore::ComputeFloatOffsetForLineLayoutAdapter::ComputeFloatOffsetForLineLayoutAdapter):
(WebCore::FindNextFloatLogicalBottomAdapter::FindNextFloatLogicalBottomAdapter):
(WebCore::FloatingObjects::FloatingObjects):
* rendering/FloatingObjects.h:
(WebCore::FloatingObjects::renderer const):

Source/WTF:

const Foo foo;
WeakPtr<const Foo> weakConstFoo = makeWeakPtr(foo);

* wtf/WeakPtr.h:
(WTF::WeakPtrFactory::createWeakPtr const):

Add a separate factory function for const T.
The underlying WeakReference is kept non-const in all cases.

Tools:

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

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

Source/WTF/ChangeLog
Source/WTF/wtf/WeakPtr.h
Source/WebCore/ChangeLog
Source/WebCore/css/MediaQueryEvaluator.cpp
Source/WebCore/css/MediaQueryEvaluator.h
Source/WebCore/rendering/FloatingObjects.cpp
Source/WebCore/rendering/FloatingObjects.h
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WTF/WeakPtr.cpp

index 77e5ab0..d6a8cc9 100644 (file)
@@ -1,3 +1,19 @@
+2018-08-24  Antti Koivisto  <antti@apple.com>
+
+        Allow creating WeakPtrs to const objects
+        https://bugs.webkit.org/show_bug.cgi?id=188785
+
+        Reviewed by Geoff Garen.
+
+        const Foo foo;
+        WeakPtr<const Foo> weakConstFoo = makeWeakPtr(foo);
+
+        * wtf/WeakPtr.h:
+        (WTF::WeakPtrFactory::createWeakPtr const):
+
+        Add a separate factory function for const T.
+        The underlying WeakReference is kept non-const in all cases.
+
 2018-08-23  David Fenton  <david_fenton@apple.com>
 
         Unreviewed, rolling out r235129.
index 56f3866..e524225 100644 (file)
@@ -111,6 +111,13 @@ public:
         return { makeRef(*m_ref) };
     }
 
+    WeakPtr<const T> createWeakPtr(const T& ptr) const
+    {
+        if (!m_ref)
+            m_ref = WeakReference<T>::create(const_cast<T*>(&ptr));
+        return { makeRef(reinterpret_cast<WeakReference<const T>&>(*m_ref)) };
+    }
+
     void revokeAll()
     {
         if (!m_ref)
index 3f962e0..626c8b6 100644 (file)
@@ -1,3 +1,25 @@
+2018-08-24  Antti Koivisto  <antti@apple.com>
+
+        Allow creating WeakPtrs to const objects
+        https://bugs.webkit.org/show_bug.cgi?id=188785
+
+        Reviewed by Geoff Garen.
+
+        Remove some unneeded const_casts.
+
+        * css/MediaQueryEvaluator.cpp:
+        (WebCore::MediaQueryEvaluator::MediaQueryEvaluator):
+        (WebCore::MediaQueryEvaluator::evaluate const):
+        * css/MediaQueryEvaluator.h:
+        * rendering/FloatingObjects.cpp:
+        (WebCore::ComputeFloatOffsetAdapter::ComputeFloatOffsetAdapter):
+        (WebCore::ComputeFloatOffsetForFloatLayoutAdapter::ComputeFloatOffsetForFloatLayoutAdapter):
+        (WebCore::ComputeFloatOffsetForLineLayoutAdapter::ComputeFloatOffsetForLineLayoutAdapter):
+        (WebCore::FindNextFloatLogicalBottomAdapter::FindNextFloatLogicalBottomAdapter):
+        (WebCore::FloatingObjects::FloatingObjects):
+        * rendering/FloatingObjects.h:
+        (WebCore::FloatingObjects::renderer const):
+
 2018-08-24  Zan Dobersek  <zdobersek@igalia.com>
 
         [CoordGraphics] Move inline methods on CoordinatedGraphicsLayer out-of-line
index 06368c0..0fb9e99 100644 (file)
@@ -110,7 +110,7 @@ MediaQueryEvaluator::MediaQueryEvaluator(const String& acceptedMediaType, bool m
 
 MediaQueryEvaluator::MediaQueryEvaluator(const String& acceptedMediaType, const Document& document, const RenderStyle* style)
     : m_mediaType(acceptedMediaType)
-    , m_document(makeWeakPtr(const_cast<Document&>(document)))
+    , m_document(makeWeakPtr(document))
     , m_style(style)
 {
 }
@@ -787,7 +787,7 @@ bool MediaQueryEvaluator::evaluate(const MediaQueryExpression& expression) const
     if (!m_document)
         return m_fallbackResult;
 
-    Document& document = *m_document;
+    auto& document = *m_document;
     auto* frame = document.frame();
     if (!frame || !frame->view() || !m_style)
         return m_fallbackResult;
index 8be2f0e..08de8ee 100644 (file)
@@ -75,7 +75,7 @@ public:
 
 private:
     String m_mediaType;
-    WeakPtr<Document> m_document;
+    WeakPtr<const Document> m_document;
     const RenderStyle* m_style { nullptr };
     bool m_fallbackResult { false };
 };
index 3f7db1b..4338bd3 100644 (file)
@@ -124,7 +124,7 @@ class ComputeFloatOffsetAdapter {
 public:
     typedef FloatingObjectInterval IntervalType;
 
-    ComputeFloatOffsetAdapter(RenderBlockFlow& renderer, LayoutUnit lineTop, LayoutUnit lineBottom, LayoutUnit offset)
+    ComputeFloatOffsetAdapter(const RenderBlockFlow& renderer, LayoutUnit lineTop, LayoutUnit lineBottom, LayoutUnit offset)
         : m_renderer(makeWeakPtr(renderer))
         , m_lineTop(lineTop)
         , m_lineBottom(lineBottom)
@@ -144,7 +144,7 @@ public:
 protected:
     virtual bool updateOffsetIfNeeded(const FloatingObject&) = 0;
 
-    WeakPtr<RenderBlockFlow> m_renderer;
+    WeakPtr<const RenderBlockFlow> m_renderer;
     LayoutUnit m_lineTop;
     LayoutUnit m_lineBottom;
     LayoutUnit m_offset;
@@ -154,7 +154,7 @@ protected:
 template <FloatingObject::Type FloatTypeValue>
 class ComputeFloatOffsetForFloatLayoutAdapter : public ComputeFloatOffsetAdapter<FloatTypeValue> {
 public:
-    ComputeFloatOffsetForFloatLayoutAdapter(RenderBlockFlow& renderer, LayoutUnit lineTop, LayoutUnit lineBottom, LayoutUnit offset)
+    ComputeFloatOffsetForFloatLayoutAdapter(const RenderBlockFlow& renderer, LayoutUnit lineTop, LayoutUnit lineBottom, LayoutUnit offset)
         : ComputeFloatOffsetAdapter<FloatTypeValue>(renderer, lineTop, lineBottom, offset)
     {
     }
@@ -170,7 +170,7 @@ protected:
 template <FloatingObject::Type FloatTypeValue>
 class ComputeFloatOffsetForLineLayoutAdapter : public ComputeFloatOffsetAdapter<FloatTypeValue> {
 public:
-    ComputeFloatOffsetForLineLayoutAdapter(RenderBlockFlow& renderer, LayoutUnit lineTop, LayoutUnit lineBottom, LayoutUnit offset)
+    ComputeFloatOffsetForLineLayoutAdapter(const RenderBlockFlow& renderer, LayoutUnit lineTop, LayoutUnit lineBottom, LayoutUnit offset)
         : ComputeFloatOffsetAdapter<FloatTypeValue>(renderer, lineTop, lineBottom, offset)
     {
     }
@@ -185,7 +185,7 @@ class FindNextFloatLogicalBottomAdapter {
 public:
     typedef FloatingObjectInterval IntervalType;
 
-    FindNextFloatLogicalBottomAdapter(RenderBlockFlow& renderer, LayoutUnit belowLogicalHeight)
+    FindNextFloatLogicalBottomAdapter(const RenderBlockFlow& renderer, LayoutUnit belowLogicalHeight)
         : m_renderer(makeWeakPtr(renderer))
         , m_belowLogicalHeight(belowLogicalHeight)
     {
@@ -199,7 +199,7 @@ public:
     LayoutUnit nextShapeLogicalBottom() const { return m_nextShapeLogicalBottom.value_or(nextLogicalBottom()); }
 
 private:
-    WeakPtr<RenderBlockFlow> m_renderer;
+    WeakPtr<const RenderBlockFlow> m_renderer;
     LayoutUnit m_belowLogicalHeight;
     std::optional<LayoutUnit> m_nextLogicalBottom;
     std::optional<LayoutUnit> m_nextShapeLogicalBottom;
@@ -250,7 +250,7 @@ FloatingObjects::FloatingObjects(const RenderBlockFlow& renderer)
     : m_leftObjectsCount(0)
     , m_rightObjectsCount(0)
     , m_horizontalWritingMode(renderer.isHorizontalWritingMode())
-    , m_renderer(makeWeakPtr(const_cast<RenderBlockFlow&>(renderer)))
+    , m_renderer(makeWeakPtr(renderer))
 {
 }
 
index 11e68a7..e5ecf9a 100644 (file)
@@ -161,7 +161,7 @@ public:
     LayoutUnit findNextFloatLogicalBottomBelowForBlock(LayoutUnit logicalHeight);
 
 private:
-    RenderBlockFlow& renderer() const { return *m_renderer; }
+    const RenderBlockFlow& renderer() const { return *m_renderer; }
     void computePlacedFloatsTree();
     const FloatingObjectTree* placedFloatsTree();
     void increaseObjectsCount(FloatingObject::Type);
@@ -173,7 +173,7 @@ private:
     unsigned m_leftObjectsCount;
     unsigned m_rightObjectsCount;
     bool m_horizontalWritingMode;
-    WeakPtr<RenderBlockFlow> m_renderer;
+    WeakPtr<const RenderBlockFlow> m_renderer;
 };
 
 } // namespace WebCore
index 1a8fc82..54c668c 100644 (file)
@@ -1,3 +1,15 @@
+2018-08-24  Antti Koivisto  <antti@apple.com>
+
+        Allow creating WeakPtrs to const objects
+        https://bugs.webkit.org/show_bug.cgi?id=188785
+
+        Reviewed by Geoff Garen.
+
+        * TestWebKitAPI/Tests/WTF/WeakPtr.cpp:
+        (TestWebKitAPI::Base::weakPtrFactory const):
+        (TestWebKitAPI::TEST):
+        (TestWebKitAPI::Base::weakPtrFactory): Deleted.
+
 2018-08-23  Carlos Garcia Campos  <cgarcia@igalia.com>
 
         [GTK][WPE] Add API to inject/register user content in isolated worlds
index ee259ba..bc25d22 100644 (file)
@@ -201,12 +201,14 @@ TEST(WTF_WeakPtr, Forget)
 
 class Base {
 public:
+    Base() { }
+
     int foo()
     {
         return 0;
     }
 
-    auto& weakPtrFactory() { return m_weakPtrFactory; }
+    auto& weakPtrFactory() const { return m_weakPtrFactory; }
 
 private:
     WeakPtrFactory<Base> m_weakPtrFactory;
@@ -214,6 +216,8 @@ private:
 
 class Derived : public Base {
 public:
+    Derived() { }
+
     int foo()
     {
         return 1;
@@ -284,4 +288,38 @@ TEST(WTF_WeakPtr, DerivedConstructAndAssign)
     }
 }
 
+TEST(WTF_WeakPtr, DerivedConstructAndAssignConst)
+{
+    const Derived derived;
+    {
+        auto derivedWeakPtr = makeWeakPtr(derived);
+        WeakPtr<const Base> baseWeakPtr { WTFMove(derivedWeakPtr) };
+        EXPECT_EQ(baseWeakPtr.get(), &derived);
+        EXPECT_NULL(derivedWeakPtr.get());
+    }
+
+    {
+        auto derivedWeakPtr = makeWeakPtr(derived);
+        WeakPtr<const Base> baseWeakPtr { derivedWeakPtr };
+        EXPECT_EQ(baseWeakPtr.get(), &derived);
+        EXPECT_EQ(derivedWeakPtr.get(), &derived);
+    }
+
+    {
+        auto derivedWeakPtr = makeWeakPtr(derived);
+        WeakPtr<const Base> baseWeakPtr;
+        baseWeakPtr = WTFMove(derivedWeakPtr);
+        EXPECT_EQ(baseWeakPtr.get(), &derived);
+        EXPECT_NULL(derivedWeakPtr.get());
+    }
+
+    {
+        auto derivedWeakPtr = makeWeakPtr(derived);
+        WeakPtr<const Base> baseWeakPtr;
+        baseWeakPtr = derivedWeakPtr;
+        EXPECT_EQ(baseWeakPtr.get(), &derived);
+        EXPECT_EQ(derivedWeakPtr.get(), &derived);
+    }
+}
+
 } // namespace TestWebKitAPI