From 8c8d3619816e9652b76e7e8756af10e42e3c4a47 Mon Sep 17 00:00:00 2001 From: Steven Moreland Date: Mon, 12 Apr 2021 23:53:03 +0000 Subject: [PATCH] libutils: group deprecated RefBase functions Make it easier to see reference to usage documentation, as requested in review. Bug: 184190315 Test: libutils_test Change-Id: If9056e35b1c7a779dd78f2b986ad10d02f25eaf3 --- libutils/include/utils/RefBase.h | 75 +++++++++++--------------- libutils/include/utils/StrongPointer.h | 63 ++++++++++------------ 2 files changed, 59 insertions(+), 79 deletions(-) diff --git a/libutils/include/utils/RefBase.h b/libutils/include/utils/RefBase.h index 714894924..e07f57415 100644 --- a/libutils/include/utils/RefBase.h +++ b/libutils/include/utils/RefBase.h @@ -416,13 +416,16 @@ public: wp(std::nullptr_t) : wp() {} #else wp(T* other); // NOLINT(implicit) + template + wp(U* other); // NOLINT(implicit) + wp& operator=(T* other); + template + wp& operator=(U* other); #endif + wp(const wp& other); explicit wp(const sp& other); -#if !defined(ANDROID_UTILS_REF_BASE_DISABLE_IMPLICIT_CONSTRUCTION) - template wp(U* other); // NOLINT(implicit) -#endif template wp(const sp& other); // NOLINT(implicit) template wp(const wp& other); // NOLINT(implicit) @@ -430,15 +433,9 @@ public: // Assignment -#if !defined(ANDROID_UTILS_REF_BASE_DISABLE_IMPLICIT_CONSTRUCTION) - wp& operator = (T* other); -#endif wp& operator = (const wp& other); wp& operator = (const sp& other); -#if !defined(ANDROID_UTILS_REF_BASE_DISABLE_IMPLICIT_CONSTRUCTION) - template wp& operator = (U* other); -#endif template wp& operator = (const wp& other); template wp& operator = (const sp& other); @@ -559,6 +556,31 @@ wp::wp(T* other) { m_refs = other ? m_refs = other->createWeak(this) : nullptr; } + +template +template +wp::wp(U* other) : m_ptr(other) { + m_refs = other ? other->createWeak(this) : nullptr; +} + +template +wp& wp::operator=(T* other) { + weakref_type* newRefs = other ? other->createWeak(this) : nullptr; + if (m_ptr) m_refs->decWeak(this); + m_ptr = other; + m_refs = newRefs; + return *this; +} + +template +template +wp& wp::operator=(U* other) { + weakref_type* newRefs = other ? other->createWeak(this) : 0; + if (m_ptr) m_refs->decWeak(this); + m_ptr = other; + m_refs = newRefs; + return *this; +} #endif template @@ -575,15 +597,6 @@ wp::wp(const sp& other) m_refs = m_ptr ? m_ptr->createWeak(this) : nullptr; } -#if !defined(ANDROID_UTILS_REF_BASE_DISABLE_IMPLICIT_CONSTRUCTION) -template template -wp::wp(U* other) - : m_ptr(other) -{ - m_refs = other ? other->createWeak(this) : nullptr; -} -#endif - template template wp::wp(const wp& other) : m_ptr(other.m_ptr) @@ -609,19 +622,6 @@ wp::~wp() if (m_ptr) m_refs->decWeak(this); } -#if !defined(ANDROID_UTILS_REF_BASE_DISABLE_IMPLICIT_CONSTRUCTION) -template -wp& wp::operator = (T* other) -{ - weakref_type* newRefs = - other ? other->createWeak(this) : nullptr; - if (m_ptr) m_refs->decWeak(this); - m_ptr = other; - m_refs = newRefs; - return *this; -} -#endif - template wp& wp::operator = (const wp& other) { @@ -646,19 +646,6 @@ wp& wp::operator = (const sp& other) return *this; } -#if !defined(ANDROID_UTILS_REF_BASE_DISABLE_IMPLICIT_CONSTRUCTION) -template template -wp& wp::operator = (U* other) -{ - weakref_type* newRefs = - other ? other->createWeak(this) : 0; - if (m_ptr) m_refs->decWeak(this); - m_ptr = other; - m_refs = newRefs; - return *this; -} -#endif - template template wp& wp::operator = (const wp& other) { diff --git a/libutils/include/utils/StrongPointer.h b/libutils/include/utils/StrongPointer.h index dd53b9ec0..bb1941b79 100644 --- a/libutils/include/utils/StrongPointer.h +++ b/libutils/include/utils/StrongPointer.h @@ -62,13 +62,16 @@ public: sp(std::nullptr_t) : sp() {} #else sp(T* other); // NOLINT(implicit) + template + sp(U* other); // NOLINT(implicit) + sp& operator=(T* other); + template + sp& operator=(U* other); #endif + sp(const sp& other); sp(sp&& other) noexcept; -#if !defined(ANDROID_UTILS_REF_BASE_DISABLE_IMPLICIT_CONSTRUCTION) - template sp(U* other); // NOLINT(implicit) -#endif template sp(const sp& other); // NOLINT(implicit) template sp(sp&& other); // NOLINT(implicit) @@ -82,17 +85,11 @@ public: // Assignment -#if !defined(ANDROID_UTILS_REF_BASE_DISABLE_IMPLICIT_CONSTRUCTION) - sp& operator = (T* other); -#endif sp& operator = (const sp& other); sp& operator=(sp&& other) noexcept; template sp& operator = (const sp& other); template sp& operator = (sp&& other); -#if !defined(ANDROID_UTILS_REF_BASE_DISABLE_IMPLICIT_CONSTRUCTION) - template sp& operator = (U* other); -#endif //! Special optimization for use by ProcessState (and nobody else). void force_set(T* other); @@ -247,6 +244,28 @@ sp::sp(T* other) other->incStrong(this); } } + +template +template +sp::sp(U* other) : m_ptr(other) { + if (other) { + check_not_on_stack(other); + (static_cast(other))->incStrong(this); + } +} + +template +sp& sp::operator=(T* other) { + T* oldPtr(*const_cast(&m_ptr)); + if (other) { + check_not_on_stack(other); + other->incStrong(this); + } + if (oldPtr) oldPtr->decStrong(this); + if (oldPtr != *const_cast(&m_ptr)) sp_report_race(); + m_ptr = other; + return *this; +} #endif template @@ -261,17 +280,6 @@ sp::sp(sp&& other) noexcept : m_ptr(other.m_ptr) { other.m_ptr = nullptr; } -#if !defined(ANDROID_UTILS_REF_BASE_DISABLE_IMPLICIT_CONSTRUCTION) -template template -sp::sp(U* other) - : m_ptr(other) { - if (other) { - check_not_on_stack(other); - (static_cast(other))->incStrong(this); - } -} -#endif - template template sp::sp(const sp& other) : m_ptr(other.m_ptr) { @@ -319,21 +327,6 @@ sp& sp::operator=(sp&& other) noexcept { return *this; } -#if !defined(ANDROID_UTILS_REF_BASE_DISABLE_IMPLICIT_CONSTRUCTION) -template -sp& sp::operator =(T* other) { - T* oldPtr(*const_cast(&m_ptr)); - if (other) { - check_not_on_stack(other); - other->incStrong(this); - } - if (oldPtr) oldPtr->decStrong(this); - if (oldPtr != *const_cast(&m_ptr)) sp_report_race(); - m_ptr = other; - return *this; -} -#endif - template template sp& sp::operator =(const sp& other) { T* oldPtr(*const_cast(&m_ptr));