From 03d23a6e89f3f1ff65575ad088858e2b219daa3c Mon Sep 17 00:00:00 2001 From: leviw Date: Fri, 25 Mar 2016 18:19:53 -0700 Subject: Add const support to wtf/RefCounted This gets us to parity with base/ref_counted Review URL: https://codereview.chromium.org/1836693002 Cr-Commit-Position: refs/heads/master@{#383434} --- third_party/WebKit/Source/wtf/RefCounted.h | 16 ++++++++-------- third_party/WebKit/Source/wtf/RefPtrTest.cpp | 12 ++++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/third_party/WebKit/Source/wtf/RefCounted.h b/third_party/WebKit/Source/wtf/RefCounted.h index 299fb37..db9d082 100644 --- a/third_party/WebKit/Source/wtf/RefCounted.h +++ b/third_party/WebKit/Source/wtf/RefCounted.h @@ -41,7 +41,7 @@ namespace WTF { // generated by the compiler (technique called template hoisting). class WTF_EXPORT RefCountedBase { public: - void ref() + void ref() const { #if CHECK_REF_COUNTED_LIFECYCLE m_verifier.onRef(m_refCount); @@ -89,7 +89,7 @@ protected: } // Returns whether the pointer should be freed or not. - bool derefBase() + bool derefBase() const { ASSERT_WITH_SECURITY_IMPLICATION(!m_deletionHasBegun); #if CHECK_REF_COUNTED_LIFECYCLE @@ -122,13 +122,13 @@ private: friend void adopted(RefCountedBase*); #endif - int m_refCount; + mutable int m_refCount; #if ENABLE(SECURITY_ASSERT) - bool m_deletionHasBegun; + mutable bool m_deletionHasBegun; #endif #if CHECK_REF_COUNTED_LIFECYCLE - bool m_adoptionIsRequired; - ThreadRestrictionVerifier m_verifier; + mutable bool m_adoptionIsRequired; + mutable ThreadRestrictionVerifier m_verifier; #endif }; @@ -153,10 +153,10 @@ template class RefCounted : public RefCountedBase { USING_FAST_MALLOC(T); public: - void deref() + void deref() const { if (derefBase()) - delete static_cast(this); + delete static_cast(this); } protected: diff --git a/third_party/WebKit/Source/wtf/RefPtrTest.cpp b/third_party/WebKit/Source/wtf/RefPtrTest.cpp index 7f37fc3..a21b4f7 100644 --- a/third_party/WebKit/Source/wtf/RefPtrTest.cpp +++ b/third_party/WebKit/Source/wtf/RefPtrTest.cpp @@ -5,6 +5,7 @@ #include "wtf/RefPtr.h" #include "testing/gtest/include/gtest/gtest.h" +#include "wtf/RefCounted.h" #include "wtf/text/StringImpl.h" namespace WTF { @@ -28,4 +29,15 @@ TEST(RefPtrTest, MoveAssignmentOperator) EXPECT_TRUE(!a); } +class RefCountedClass : public RefCounted { +}; + +TEST(RefPtrTest, ConstObject) +{ + // This test is only to ensure we force the compilation of a const RefCounted + // object to ensure the generated code compiles. + RefPtr ptrToConst = adoptRef(new RefCountedClass()); +} + + } // namespace WTF -- cgit v1.1