From 8deb7be114ad57700db8dd8104e51f7e3c5f7823 Mon Sep 17 00:00:00 2001 From: chenkeyu Date: Tue, 24 Dec 2024 16:15:20 +0800 Subject: [PATCH] make CanPromote only availiable in ohos Issue: https://gitee.com/openharmony/commonlibrary_c_utils/issues/IBDEZL?from=project-issue Signed-off-by: chenkeyu --- base/BUILD.gn | 3 +++ base/include/refbase.h | 6 +++--- base/src/refbase.cpp | 12 ++++++------ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/base/BUILD.gn b/base/BUILD.gn index 4778c44..d53a863 100644 --- a/base/BUILD.gn +++ b/base/BUILD.gn @@ -32,6 +32,9 @@ config("utils_config") { if (is_emulator == true) { defines += [ "EMULATOR_PLATFORM" ] } + if (current_os == "ohos") { + defines += [ "OHOS_PLATFORM" ] + } } config("utils_coverage_config") { diff --git a/base/include/refbase.h b/base/include/refbase.h index af3f6d6..089fc4b 100644 --- a/base/include/refbase.h +++ b/base/include/refbase.h @@ -240,7 +240,7 @@ public: */ void ExtendObjectLifetime(); -#ifndef EMULATOR_PLATFORM +#ifdef OHOS_PLATFORM using CanPromote = std::function; void SetCanPromote(const CanPromote& canPromote); @@ -267,7 +267,7 @@ private: std::atomic atomicAttempt_; // Number of attempts RefPtrCallback callback_ = nullptr; // Callback function to deallocate the corresponding RefBase object static constexpr unsigned int FLAG_EXTEND_LIFE_TIME = 0x00000002; // Extended life-time bit to be set via logic-OR -#ifndef EMULATOR_PLATFORM +#ifdef OHOS_PLATFORM CanPromote canPromote_ = nullptr; #endif #ifdef DEBUG_REFBASE @@ -595,7 +595,7 @@ public: */ void EnableTracker(); -#ifndef EMULATOR_PLATFORM +#ifdef OHOS_PLATFORM virtual bool CanPromote(); #endif diff --git a/base/src/refbase.cpp b/base/src/refbase.cpp index cb7426f..a39676c 100644 --- a/base/src/refbase.cpp +++ b/base/src/refbase.cpp @@ -211,7 +211,7 @@ bool RefCounter::IsRefPtrValid() return callback_ != nullptr; } -#ifndef EMULATOR_PLATFORM +#ifdef OHOS_PLATFORM void RefCounter::SetCanPromote(const CanPromote &canPromote) { canPromote_ = canPromote; @@ -374,7 +374,7 @@ bool RefCounter::AttemptIncStrongRef(const void *objectId, int &outCount) } if (IsLifeTimeExtended()) { -#ifndef EMULATOR_PLATFORM +#ifdef OHOS_PLATFORM if (!IsCanPromoteValid() || !canPromote_()) { return false; } @@ -418,7 +418,7 @@ RefBase::RefBase() : refs_(new RefCounter()) { refs_->IncRefCount(); refs_->SetCallback([this] { this->RefPtrCallback(); }); -#ifndef EMULATOR_PLATFORM +#ifdef OHOS_PLATFORM refs_->SetCanPromote([this] { return this->CanPromote(); }); #endif } @@ -429,13 +429,13 @@ RefBase::RefBase(const RefBase &) if (refs_ != nullptr) { refs_->IncRefCount(); refs_->SetCallback([this] { this->RefPtrCallback(); }); -#ifndef EMULATOR_PLATFORM +#ifdef OHOS_PLATFORM refs_->SetCanPromote([this] { return this->CanPromote(); }); #endif } } -#ifndef EMULATOR_PLATFORM +#ifdef OHOS_PLATFORM bool RefBase::CanPromote() { return true; @@ -464,7 +464,7 @@ RefBase &RefBase::operator=(const RefBase &) if (refs_ != nullptr) { refs_->IncRefCount(); refs_->SetCallback([this] { this->RefPtrCallback(); }); -#ifndef EMULATOR_PLATFORM +#ifdef OHOS_PLATFORM refs_->SetCanPromote([this] { return this->CanPromote(); }); #endif } -- Gitee