|
1 | 1 | #pragma once
|
2 | 2 |
|
3 |
| -#include <CesiumUtility/ReferenceCounted.h> |
| 3 | +#include <CesiumUtility/Assert.h> |
| 4 | + |
| 5 | +#include <atomic> |
| 6 | +#include <cstdint> |
| 7 | +#include <type_traits> |
4 | 8 |
|
5 | 9 | namespace CesiumForUnityNative {
|
6 | 10 |
|
7 |
| -template <typename TDerived> |
8 |
| -class CesiumImpl |
9 |
| - : public CesiumUtility::ReferenceCountedThreadSafe<CesiumImpl<TDerived>> { |
| 11 | +template <typename TDerived> class CesiumImpl { |
10 | 12 | public:
|
11 |
| - CesiumImpl() = default; |
| 13 | + /** |
| 14 | + * @brief Adds a counted reference to this object. Use |
| 15 | + * {@link CesiumUtility::IntrusivePointer} instead of calling this method |
| 16 | + * directly. |
| 17 | + */ |
| 18 | + void addReference() const /*noexcept*/ { ++this->_referenceCount; } |
| 19 | + |
| 20 | + /** |
| 21 | + * @brief Removes a counted reference from this object. When the last |
| 22 | + * reference is removed, this method will delete this instance. Use |
| 23 | + * {@link CesiumUtility::IntrusivePointer} instead of calling this method |
| 24 | + * directly. |
| 25 | + */ |
| 26 | + void releaseReference() const /*noexcept*/ { |
| 27 | + CESIUM_ASSERT(this->_referenceCount > 0); |
| 28 | + const int32_t references = --this->_referenceCount; |
| 29 | + if (references == 0) { |
| 30 | + delete static_cast<const TDerived*>(this); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @brief Returns the current reference count of this instance. |
| 36 | + */ |
| 37 | + std::int32_t getReferenceCount() const noexcept { |
| 38 | + return this->_referenceCount; |
| 39 | + } |
12 | 40 |
|
13 | 41 | // Prevent copying of impl classes
|
14 | 42 | CesiumImpl(CesiumImpl&&) = delete;
|
15 | 43 | CesiumImpl(const CesiumImpl&) = delete;
|
16 | 44 | CesiumImpl& operator=(CesiumImpl&&) = delete;
|
17 | 45 | CesiumImpl& operator=(const CesiumImpl&) = delete;
|
| 46 | + |
| 47 | +private: |
| 48 | + CesiumImpl() noexcept = default; |
| 49 | + ~CesiumImpl() noexcept { CESIUM_ASSERT(this->_referenceCount == 0); } |
| 50 | + |
| 51 | + friend TDerived; |
| 52 | + |
| 53 | + mutable std::atomic<std::int32_t> _referenceCount{0}; |
18 | 54 | };
|
19 | 55 |
|
20 | 56 | } // namespace CesiumForUnityNative
|
0 commit comments