cesium-native 0.52.0
Loading...
Searching...
No Matches
SharedAsset.h
1#pragma once
2
3#include <CesiumUtility/Assert.h>
4#include <CesiumUtility/ExtensibleObject.h>
5#include <CesiumUtility/IDepotOwningAsset.h>
6#include <CesiumUtility/Library.h>
7
8#include <atomic>
9
10namespace CesiumAsync {
11
12template <typename TAssetType, typename TAssetKey, typename TContext>
14
15} // namespace CesiumAsync
16
17namespace CesiumUtility {
18
54template <typename T>
55class CESIUMUTILITY_API SharedAsset : public CesiumUtility::ExtensibleObject {
56public:
62 void addReference() const noexcept { this->addReference(false); }
63
70 void releaseReference() const noexcept { this->releaseReference(false); }
71
76 const IDepotOwningAsset<T>* getDepot() const { return this->_pDepot; }
77
82 IDepotOwningAsset<T>* getDepot() { return this->_pDepot; }
83
84private:
85 SharedAsset() = default;
86 ~SharedAsset() { CESIUM_ASSERT(this->_referenceCount == 0); }
87
92 SharedAsset(const SharedAsset& rhs)
93 : ExtensibleObject(rhs), _referenceCount(0), _pDepot(nullptr) {}
94
99 SharedAsset(SharedAsset&& rhs) noexcept
100 : ExtensibleObject(std::move(rhs)),
101 _referenceCount(0),
102 _pDepot(nullptr) {}
103
108 SharedAsset& operator=(const SharedAsset& rhs) noexcept {
109 if (&rhs != this) {
110 CesiumUtility::ExtensibleObject::operator=(rhs);
111 }
112 return *this;
113 }
114
119 SharedAsset& operator=(SharedAsset&& rhs) noexcept {
120 if (&rhs != this) {
121 CesiumUtility::ExtensibleObject::operator=(std::move(rhs));
122 }
123 return *this;
124 }
125
126private:
127 void addReference(bool threadOwnsDepotLock) const noexcept {
128 const int32_t prevReferences = this->_referenceCount++;
129 if (this->_pDepot && prevReferences <= 0) {
130 this->_pDepot->unmarkDeletionCandidate(
131 *static_cast<const T*>(this),
132 threadOwnsDepotLock);
133 }
134 }
135
136 void releaseReference(bool threadOwnsDepotLock) const noexcept {
137 CESIUM_ASSERT(this->_referenceCount > 0);
138 const int32_t references = --this->_referenceCount;
139 if (references == 0) {
140 IDepotOwningAsset<T>* pDepot = this->_pDepot;
141 if (pDepot) {
142 // Let the depot manage this object's lifetime.
143 pDepot->markDeletionCandidate(
144 *static_cast<const T*>(this),
145 threadOwnsDepotLock);
146 } else {
147 // No depot, so destroy this object directly.
148 delete static_cast<const T*>(this);
149 }
150 }
151 }
152
153 mutable std::atomic<std::int32_t> _referenceCount{0};
154 IDepotOwningAsset<T>* _pDepot{nullptr};
155 bool _isInvalidated{false};
156
157 // To allow the depot to modify _pDepot.
158 template <typename TAssetType, typename TAssetKey, typename TContext>
159 friend class CesiumAsync::SharedAssetDepot;
160 friend T;
161};
162
163} // namespace CesiumUtility
A depot for CesiumUtility::SharedAsset instances, which are potentially shared between multiple objec...
An interface representing the depot that owns a SharedAsset. This interface is an implementation deta...
An asset that is potentially shared between multiple objects, such as an image shared between multipl...
Definition SharedAsset.h:55
IDepotOwningAsset< T > * getDepot()
Gets the shared asset depot that owns this asset, or nullptr if this asset is independent of an asset...
Definition SharedAsset.h:82
void releaseReference() const noexcept
Removes a counted reference from this object. When the last reference is removed, this method will de...
Definition SharedAsset.h:70
const IDepotOwningAsset< T > * getDepot() const
Gets the shared asset depot that owns this asset, or nullptr if this asset is independent of an asset...
Definition SharedAsset.h:76
void addReference() const noexcept
Adds a counted reference to this object. Use CesiumUtility::IntrusivePointer instead of calling this ...
Definition SharedAsset.h:62
Classes that support asynchronous operations.
Utility classes for Cesium.
The base class for objects that have extensions and extras.