From 917f4681583057375f727938689d981442a19d7e Mon Sep 17 00:00:00 2001 From: Kevin Ring Date: Thu, 23 Jan 2025 22:50:57 +1100 Subject: [PATCH] Better CesiumImpl implementation. The previous one wouldn't have called derived-class destructors properly. --- native~/Shared/src/CesiumImpl.h | 46 +++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/native~/Shared/src/CesiumImpl.h b/native~/Shared/src/CesiumImpl.h index faf9aa14..2cf03b1a 100644 --- a/native~/Shared/src/CesiumImpl.h +++ b/native~/Shared/src/CesiumImpl.h @@ -1,20 +1,56 @@ #pragma once -#include +#include + +#include +#include +#include namespace CesiumForUnityNative { -template -class CesiumImpl - : public CesiumUtility::ReferenceCountedThreadSafe> { +template class CesiumImpl { public: - CesiumImpl() = default; + /** + * @brief Adds a counted reference to this object. Use + * {@link CesiumUtility::IntrusivePointer} instead of calling this method + * directly. + */ + void addReference() const /*noexcept*/ { ++this->_referenceCount; } + + /** + * @brief Removes a counted reference from this object. When the last + * reference is removed, this method will delete this instance. Use + * {@link CesiumUtility::IntrusivePointer} instead of calling this method + * directly. + */ + void releaseReference() const /*noexcept*/ { + CESIUM_ASSERT(this->_referenceCount > 0); + const int32_t references = --this->_referenceCount; + if (references == 0) { + delete static_cast(this); + } + } + + /** + * @brief Returns the current reference count of this instance. + */ + std::int32_t getReferenceCount() const noexcept { + return this->_referenceCount; + } // Prevent copying of impl classes CesiumImpl(CesiumImpl&&) = delete; CesiumImpl(const CesiumImpl&) = delete; CesiumImpl& operator=(CesiumImpl&&) = delete; CesiumImpl& operator=(const CesiumImpl&) = delete; + +private: + CesiumImpl() noexcept = default; + ~CesiumImpl() noexcept { CESIUM_ASSERT(this->_referenceCount == 0); } + + friend TDerived; + + mutable std::atomic _referenceCount{0}; }; } // namespace CesiumForUnityNative