Skip to content

Commit 917f468

Browse files
committed
Better CesiumImpl implementation.
The previous one wouldn't have called derived-class destructors properly.
1 parent 3936614 commit 917f468

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

native~/Shared/src/CesiumImpl.h

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,56 @@
11
#pragma once
22

3-
#include <CesiumUtility/ReferenceCounted.h>
3+
#include <CesiumUtility/Assert.h>
4+
5+
#include <atomic>
6+
#include <cstdint>
7+
#include <type_traits>
48

59
namespace CesiumForUnityNative {
610

7-
template <typename TDerived>
8-
class CesiumImpl
9-
: public CesiumUtility::ReferenceCountedThreadSafe<CesiumImpl<TDerived>> {
11+
template <typename TDerived> class CesiumImpl {
1012
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+
}
1240

1341
// Prevent copying of impl classes
1442
CesiumImpl(CesiumImpl&&) = delete;
1543
CesiumImpl(const CesiumImpl&) = delete;
1644
CesiumImpl& operator=(CesiumImpl&&) = delete;
1745
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};
1854
};
1955

2056
} // namespace CesiumForUnityNative

0 commit comments

Comments
 (0)