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>
66 if constexpr (!isThreadSafe) {
67 CESIUM_ASSERT(std::this_thread::get_id() == this->_threadID);
71 ++this->_referenceCount;
82 if constexpr (!isThreadSafe) {
83 CESIUM_ASSERT(std::this_thread::get_id() == this->_threadID);
87 CESIUM_ASSERT(this->_referenceCount > 0);
88 const int32_t references = --this->_referenceCount;
89 if (references == 0) {
90 delete static_cast<const T*
>(
this);
98 return this->_referenceCount;
102 using ThreadSafeCounter = std::atomic<std::int32_t>;
103 using NonThreadSafeCounter = std::int32_t;
105 std::conditional_t<isThreadSafe, ThreadSafeCounter, NonThreadSafeCounter>;
107 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.