Make RenderPtr a type alias of std::unique_ptr
authorantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 2 Oct 2017 21:06:32 +0000 (21:06 +0000)
committerantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 2 Oct 2017 21:06:32 +0000 (21:06 +0000)
https://bugs.webkit.org/show_bug.cgi?id=177739

Reviewed by Sam Weinig.

It just needs a custom deleter.

* page/FrameView.h:
* rendering/RenderElement.cpp:
(WebCore::RenderElement::insertChildInternal):
* rendering/RenderMultiColumnFlow.cpp:
(WebCore::RenderMultiColumnFlow::fragmentedFlowDescendantInserted):
(WebCore::RenderMultiColumnFlow::handleSpannerRemoval):
* rendering/RenderObject.cpp:
(WebCore::RenderObjectDeleter::operator() const):
(WebCore::RenderObject::willBeDestroyed):
* rendering/RenderPtr.h:
(WebCore::createRenderer):
(WebCore::static_pointer_cast):

    Make this safer with downcast.

(WebCore::RenderPtr::RenderPtr): Deleted.
(WebCore::RenderPtr::~RenderPtr): Deleted.
(WebCore::RenderPtr::get const): Deleted.
(WebCore::RenderPtr::operator* const): Deleted.
(WebCore::RenderPtr::operator-> const): Deleted.
(WebCore::RenderPtr::operator! const): Deleted.
(WebCore::RenderPtr::operator UnspecifiedBoolType const): Deleted.
(WebCore::RenderPtr::operator=): Deleted.
(WebCore::RenderPtr::swap): Deleted.
(WebCore::RenderPtr::operator==): Deleted.
(WebCore::RenderPtr::operator!=): Deleted.
(WebCore::RenderPtr<T>::clear): Deleted.
(WebCore::RenderPtr<T>::leakPtr): Deleted.
(WebCore::RenderPtr<T>::RenderPtr): Deleted.
(WebCore::=): Deleted.
(WebCore::swap): Deleted.
(WebCore::operator==): Deleted.
(WebCore::operator!=): Deleted.
(WebCore::getPtr): Deleted.
(WTF::HashTraits<WebCore::RenderPtr<T>>::emptyValue): Deleted.
(WTF::HashTraits<WebCore::RenderPtr<T>>::peek): Deleted.

    We already have hash traits for std::unique_ptr.

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

Source/WebCore/ChangeLog
Source/WebCore/dom/Document.cpp
Source/WebCore/page/FrameView.h
Source/WebCore/rendering/RenderElement.cpp
Source/WebCore/rendering/RenderObject.cpp
Source/WebCore/rendering/RenderPtr.h

index 7375627..ceb4c97 100644 (file)
@@ -1,3 +1,51 @@
+2017-10-02  Antti Koivisto  <antti@apple.com>
+
+        Make RenderPtr a type alias of std::unique_ptr
+        https://bugs.webkit.org/show_bug.cgi?id=177739
+
+        Reviewed by Sam Weinig.
+
+        It just needs a custom deleter.
+
+        * page/FrameView.h:
+        * rendering/RenderElement.cpp:
+        (WebCore::RenderElement::insertChildInternal):
+        * rendering/RenderMultiColumnFlow.cpp:
+        (WebCore::RenderMultiColumnFlow::fragmentedFlowDescendantInserted):
+        (WebCore::RenderMultiColumnFlow::handleSpannerRemoval):
+        * rendering/RenderObject.cpp:
+        (WebCore::RenderObjectDeleter::operator() const):
+        (WebCore::RenderObject::willBeDestroyed):
+        * rendering/RenderPtr.h:
+        (WebCore::createRenderer):
+        (WebCore::static_pointer_cast):
+
+            Make this safer with downcast.
+
+        (WebCore::RenderPtr::RenderPtr): Deleted.
+        (WebCore::RenderPtr::~RenderPtr): Deleted.
+        (WebCore::RenderPtr::get const): Deleted.
+        (WebCore::RenderPtr::operator* const): Deleted.
+        (WebCore::RenderPtr::operator-> const): Deleted.
+        (WebCore::RenderPtr::operator! const): Deleted.
+        (WebCore::RenderPtr::operator UnspecifiedBoolType const): Deleted.
+        (WebCore::RenderPtr::operator=): Deleted.
+        (WebCore::RenderPtr::swap): Deleted.
+        (WebCore::RenderPtr::operator==): Deleted.
+        (WebCore::RenderPtr::operator!=): Deleted.
+        (WebCore::RenderPtr<T>::clear): Deleted.
+        (WebCore::RenderPtr<T>::leakPtr): Deleted.
+        (WebCore::RenderPtr<T>::RenderPtr): Deleted.
+        (WebCore::=): Deleted.
+        (WebCore::swap): Deleted.
+        (WebCore::operator==): Deleted.
+        (WebCore::operator!=): Deleted.
+        (WebCore::getPtr): Deleted.
+        (WTF::HashTraits<WebCore::RenderPtr<T>>::emptyValue): Deleted.
+        (WTF::HashTraits<WebCore::RenderPtr<T>>::peek): Deleted.
+
+            We already have hash traits for std::unique_ptr.
+
 2017-10-02  Joseph Pecoraro  <pecoraro@apple.com>
 
         Web Inspector: Include Beacon and Ping requests in Network tab
index ef5ff0d..35fe2c6 100644 (file)
@@ -2259,7 +2259,10 @@ void Document::destroyRenderTree()
 
     unscheduleStyleRecalc();
 
-    m_renderView = nullptr;
+    // FIXME: RenderObject::view() uses m_renderView and we can't null it before destruction is completed
+    m_renderView->destroy();
+    m_renderView.release();
+
     Node::setRenderer(nullptr);
 
 #if ENABLE(TEXT_AUTOSIZING)
index 04ead09..f0c5ba6 100644 (file)
@@ -483,7 +483,7 @@ public:
     // Unlike client coordinates, layout viewport coordinates are affected by page zoom.
     FloatPoint clientToLayoutViewportPoint(FloatPoint) const;
 
-    bool isFrameViewScrollCorner(const RenderScrollbarPart& scrollCorner) const { return m_scrollCorner == &scrollCorner; }
+    bool isFrameViewScrollCorner(const RenderScrollbarPart& scrollCorner) const { return m_scrollCorner.get() == &scrollCorner; }
 
     // isScrollable() takes an optional Scrollability parameter that allows the caller to define what they mean by 'scrollable.'
     // Most callers are interested in the default value, Scrollability::Scrollable, which means that there is actually content
index 91bdf6b..41779a0 100644 (file)
@@ -523,7 +523,7 @@ void RenderElement::insertChildInternal(RenderPtr<RenderObject> newChildPtr, Ren
     ASSERT(!beforeChild || beforeChild->parent() == this);
 
     // Take the ownership.
-    auto* newChild = newChildPtr.leakPtr();
+    auto* newChild = newChildPtr.release();
 
     newChild->setParent(this);
 
index d56129f..dd5fbd3 100644 (file)
@@ -108,6 +108,11 @@ COMPILE_ASSERT(sizeof(RenderObject) == sizeof(SameSizeAsRenderObject), RenderObj
 
 DEFINE_DEBUG_ONLY_GLOBAL(WTF::RefCountedLeakCounter, renderObjectCounter, ("RenderObject"));
 
+void RenderObjectDeleter::operator() (RenderObject* renderer) const
+{
+    renderer->destroy();
+}
+
 RenderObject::RenderObject(Node& node)
     : CachedImageClient()
     , m_node(node)
@@ -1431,7 +1436,7 @@ void RenderObject::willBeDestroyed()
     if (m_parent) {
         // FIXME: We should have always been removed from the parent before being destroyed.
         auto takenThis = m_parent->takeChild(*this);
-        auto* leakedPtr = takenThis.leakPtr();
+        auto* leakedPtr = takenThis.release();
         UNUSED_PARAM(leakedPtr);
     }
 
index d2cbba2..351cb0c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Apple Inc. All rights reserved.
+ * Copyright (C) 2013, 2017 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
 
 #pragma once
 
-#include <algorithm>
-#include <cstddef>
-#include <memory>
-#include <wtf/Assertions.h>
-#include <wtf/HashTraits.h>
+#include <wtf/TypeCasts.h>
 
 namespace WebCore {
 
-template<typename T> class RenderPtr {
-public:
-    typedef T ValueType;
-    typedef ValueType* PtrType;
+class RenderObject;
 
-    RenderPtr() : m_ptr(nullptr) { }
-    RenderPtr(std::nullptr_t) : m_ptr(nullptr) { }
-    explicit RenderPtr(T* ptr) : m_ptr(ptr) { }
-
-    ~RenderPtr()
-    {
-        if (m_ptr)
-            m_ptr->destroy();
-    }
-
-    PtrType get() const { return m_ptr; }
-
-    void clear();
-    PtrType leakPtr() WARN_UNUSED_RETURN;
-
-    ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; }
-    PtrType operator->() const { ASSERT(m_ptr); return m_ptr; }
-
-    bool operator!() const { return !m_ptr; }
-
-    // This conversion operator allows implicit conversion to bool but not to other integer types.
-    typedef PtrType RenderPtr::*UnspecifiedBoolType;
-    operator UnspecifiedBoolType() const { return m_ptr ? &RenderPtr::m_ptr : nullptr; }
-
-    RenderPtr& operator=(std::nullptr_t) { clear(); return *this; }
-
-    RenderPtr(RenderPtr&&);
-    template<typename U> RenderPtr(RenderPtr<U>&&);
-
-    RenderPtr& operator=(RenderPtr&&);
-    template<typename U> RenderPtr& operator=(RenderPtr<U>&&);
-
-    void swap(RenderPtr& o) { std::swap(m_ptr, o.m_ptr); }
-
-private:
-    // We should never have two RenderPtrs for the same underlying object (otherwise we'll get
-    // double-destruction), so these equality operators should never be needed.
-    template<typename U> bool operator==(const RenderPtr<U>&) { COMPILE_ASSERT(!sizeof(U*), RenderPtrs_should_never_be_equal); return false; }
-    template<typename U> bool operator!=(const RenderPtr<U>&) { COMPILE_ASSERT(!sizeof(U*), RenderPtrs_should_never_be_equal); return false; }
-
-    PtrType m_ptr;
-};
-
-template<typename T> inline void RenderPtr<T>::clear()
+struct RenderObjectDeleter
 {
-    if (m_ptr)
-        m_ptr->destroy();
-    m_ptr = nullptr;
-}
-
-template<typename T> inline typename RenderPtr<T>::PtrType RenderPtr<T>::leakPtr()
-{
-    PtrType ptr = m_ptr;
-    m_ptr = nullptr;
-    return ptr;
-}
-
-template<typename T> inline RenderPtr<T>::RenderPtr(RenderPtr<T>&& o)
-    : m_ptr(o.leakPtr())
-{
-}
-
-template<typename T> template<typename U> inline RenderPtr<T>::RenderPtr(RenderPtr<U>&& o)
-    : m_ptr(o.leakPtr())
-{
-}
-
-template<typename T> inline auto RenderPtr<T>::operator=(RenderPtr&& o) -> RenderPtr&
-{
-    ASSERT(!o || o != m_ptr);
-    RenderPtr ptr = WTFMove(o);
-    swap(ptr);
-    return *this;
-}
-
-template<typename T> template<typename U> inline auto RenderPtr<T>::operator=(RenderPtr<U>&& o) -> RenderPtr&
-{
-    ASSERT(!o || o != m_ptr);
-    RenderPtr ptr = WTFMove(o);
-    swap(ptr);
-    return *this;
-}
-
-template<typename T> inline void swap(RenderPtr<T>& a, RenderPtr<T>& b)
-{
-    a.swap(b);
-}
-
-template<typename T, typename U> inline bool operator==(const RenderPtr<T>& a, U* b)
-{
-    return a.get() == b;
-}
-
-template<typename T, typename U> inline bool operator==(T* a, const RenderPtr<U>& b) 
-{
-    return a == b.get();
-}
-
-template<typename T, typename U> inline bool operator!=(const RenderPtr<T>& a, U* b)
-{
-    return a.get() != b;
-}
+    void operator() (RenderObject*) const;
+};
 
-template<typename T, typename U> inline bool operator!=(T* a, const RenderPtr<U>& b)
-{
-    return a != b.get();
-}
+template <typename T> using RenderPtr = std::unique_ptr<T, RenderObjectDeleter>;
 
-template<typename T> inline typename RenderPtr<T>::PtrType getPtr(const RenderPtr<T>& p)
-{
-    return p.get();
-}
-
-template<class T, class... Args> inline RenderPtr<T>
-createRenderer(Args&&... args)
+template<typename T, class... Args> inline RenderPtr<T> createRenderer(Args&&... args)
 {
     return RenderPtr<T>(new T(std::forward<Args>(args)...));
 }
 
 template<typename T, typename U> inline RenderPtr<T> static_pointer_cast(RenderPtr<U>&& p)
 {
-    return RenderPtr<T>(static_cast<T*>(p.leakPtr()));
+    return RenderPtr<T>(downcast<T>(p.release()));
 }
 
 } // namespace WebCore
-
-namespace WTF {
-
-template<typename T> struct HashTraits<WebCore::RenderPtr<T>> : SimpleClassHashTraits<WebCore::RenderPtr<T>> {
-    typedef std::nullptr_t EmptyValueType;
-    static EmptyValueType emptyValue() { return nullptr; }
-
-    typedef T* PeekType;
-    static T* peek(const WebCore::RenderPtr<T>& value) { return value.get(); }
-    static T* peek(std::nullptr_t) { return nullptr; }
-};
-
-} // namespace WTF