3#include <CesiumUtility/Assert.h>
16template <
bool isThreadSafe>
class ThreadIdHolder;
18template <>
class ThreadIdHolder<false> {
19 ThreadIdHolder() : _threadID(
std::this_thread::get_id()) {}
21 std::thread::id _threadID;
23 template <
typename T,
bool isThreadSafe>
friend class ReferenceCounted;
26template <>
class ThreadIdHolder<true> {};
49template <
typename T,
bool isThreadSafe = true>
52 :
public ThreadIdHolder<isThreadSafe>
65 if constexpr (!isThreadSafe) {
66 CESIUM_ASSERT(std::this_thread::get_id() == this->_threadID);
70 ++this->_referenceCount;
81 if constexpr (!isThreadSafe) {
82 CESIUM_ASSERT(std::this_thread::get_id() == this->_threadID);
86 CESIUM_ASSERT(this->_referenceCount > 0);
87 const int32_t references = --this->_referenceCount;
88 if (references == 0) {
89 delete static_cast<const T*
>(
this);
97 return this->_referenceCount;
104 using ThreadSafeCounter =
std::atomic<
std::int32_t>;
105 using NonThreadSafeCounter =
std::int32_t;
107 std::conditional_t<isThreadSafe, ThreadSafeCounter, NonThreadSafeCounter>;
109 mutable CounterType _referenceCount{0};
A reference-counted base class, meant to be used with IntrusivePointer.
std::int32_t getReferenceCount() const noexcept
Returns the current reference count of this instance.
void releaseReference() const
Removes a counted reference from this object. When the last reference is removed, this method will de...
void addReference() const
Adds a counted reference to this object. Use CesiumUtility::IntrusivePointer instead of calling this ...
Utility classes for Cesium.