cesium-native 0.43.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 {
11template <typename TAssetType, typename TAssetKey> class SharedAssetDepot;
12}
13
14namespace CesiumUtility {
15
51template <typename T>
52class CESIUMUTILITY_API SharedAsset : public CesiumUtility::ExtensibleObject {
53public:
59 void addReference() const noexcept { this->addReference(false); }
60
67 void releaseReference() const noexcept { this->releaseReference(false); }
68
73 const IDepotOwningAsset<T>* getDepot() const { return this->_pDepot; }
74
79 IDepotOwningAsset<T>* getDepot() { return this->_pDepot; }
80
81protected:
82 SharedAsset() = default;
83 ~SharedAsset() { CESIUM_ASSERT(this->_referenceCount == 0); }
84
90 : ExtensibleObject(rhs), _referenceCount(0), _pDepot(nullptr) {}
91
97 : ExtensibleObject(std::move(rhs)),
98 _referenceCount(0),
99 _pDepot(nullptr) {}
100
106 CesiumUtility::ExtensibleObject::operator=(rhs);
107 return *this;
108 }
109
115 CesiumUtility::ExtensibleObject::operator=(std::move(rhs));
116 return *this;
117 }
118
119private:
120 void addReference(bool threadOwnsDepotLock) const noexcept {
121 const int32_t prevReferences = this->_referenceCount++;
122 if (this->_pDepot && prevReferences <= 0) {
123 this->_pDepot->unmarkDeletionCandidate(
124 *static_cast<const T*>(this),
125 threadOwnsDepotLock);
126 }
127 }
128
129 void releaseReference(bool threadOwnsDepotLock) const noexcept {
130 CESIUM_ASSERT(this->_referenceCount > 0);
131 const int32_t references = --this->_referenceCount;
132 if (references == 0) {
133 IDepotOwningAsset<T>* pDepot = this->_pDepot;
134 if (pDepot) {
135 // Let the depot manage this object's lifetime.
136 pDepot->markDeletionCandidate(
137 *static_cast<const T*>(this),
138 threadOwnsDepotLock);
139 } else {
140 // No depot, so destroy this object directly.
141 delete static_cast<const T*>(this);
142 }
143 }
144 }
145
146 mutable std::atomic<std::int32_t> _referenceCount{0};
147 IDepotOwningAsset<T>* _pDepot{nullptr};
148
149 // To allow the depot to modify _pDepot.
150 template <typename TAssetType, typename TAssetKey>
152};
153
154} // namespace CesiumUtility
A depot for CesiumUtility::SharedAsset instances, which are potentially shared between multiple objec...
Definition SharedAsset.h:11
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:52
SharedAsset & operator=(SharedAsset &&rhs)
SharedAsset(const SharedAsset &rhs)
Definition SharedAsset.h:89
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:79
void releaseReference() const noexcept
Removes a counted reference from this object. When the last reference is removed, this method will de...
Definition SharedAsset.h:67
SharedAsset & operator=(const SharedAsset &rhs)
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:73
void addReference() const noexcept
Adds a counted reference to this object. Use CesiumUtility::IntrusivePointer instead of calling this ...
Definition SharedAsset.h:59
SharedAsset(SharedAsset &&rhs)
Definition SharedAsset.h:96
Classes that support asynchronous operations.
Utility classes for Cesium.
STL namespace.
The base class for objects that have extensions and extras.