cesium-native 0.55.0
Loading...
Searching...
No Matches
CreditSystem.h
1#pragma once
2
3#include <CesiumUtility/Library.h>
4
5#include <cstdint>
6#include <limits>
7#include <memory>
8#include <string>
9#include <unordered_map>
10#include <utility>
11#include <vector>
12
13namespace CesiumUtility {
14
15class CreditSystem;
16
21enum class CreditFilteringMode : uint8_t {
25 None = 0,
26
40
52};
53
59struct CESIUMUTILITY_API Credit {
60public:
64 bool operator==(const Credit& rhs) const noexcept {
65 return this->_id == rhs._id && this->_generation == rhs._generation;
66 }
67
68private:
69 uint32_t _id;
70 uint32_t _generation;
71
72 Credit(uint32_t id, uint32_t generation) noexcept
73 : _id(id), _generation(generation) {}
74
75 friend class CreditSystem;
76 friend class CreditReferencer;
77};
78
91class CESIUMUTILITY_API CreditSource {
92public:
99 CreditSource(CreditSystem& creditSystem) noexcept;
100
109 CreditSource(const std::shared_ptr<CreditSystem>& pCreditSystem) noexcept;
110
111 ~CreditSource() noexcept;
112
119 CreditSystem* getCreditSystem() noexcept;
120
124 const CreditSystem* getCreditSystem() const noexcept;
125
126private:
127 void notifyCreditSystemDestroyed() noexcept;
128
129 CreditSystem* _pCreditSystem;
130
131 friend class CreditSystem;
132};
133
141 std::vector<Credit> currentCredits;
142
147 std::vector<Credit> removedCredits;
148};
149
155class CESIUMUTILITY_API CreditSystem final {
156public:
160 CreditSystem() noexcept = default;
161 ~CreditSystem() noexcept;
162
163 CreditSystem(const CreditSystem&) = delete;
164 CreditSystem& operator=(const CreditSystem&) = delete;
165
179 const CreditSource& source,
180 std::string&& html,
181 bool showOnScreen = false);
182
185 const CreditSource& source,
186 const std::string& html,
187 bool showOnScreen = false);
188
198 Credit createCredit(std::string&& html, bool showOnScreen = false);
199
209 Credit createCredit(const std::string& html, bool showOnScreen = false);
210
214 bool shouldBeShownOnScreen(Credit credit) const noexcept;
215
219 void setShowOnScreen(Credit credit, bool showOnScreen) noexcept;
220
224 const std::string& getHtml(Credit credit) const noexcept;
225
232 const CreditSource* getCreditSource(Credit credit) const noexcept;
233
245
256
269 CreditFilteringMode filteringMode =
271
279 const CreditSource& getDefaultCreditSource() const noexcept;
280
281private:
282 struct CreditRecord {
283 std::string html{};
284 bool showOnScreen{false};
285 int32_t referenceCount{0};
286 bool shownLastSnapshot{0};
287 uint32_t generation{0};
288 const CreditSource* pSource{nullptr};
289 uint32_t previousCreditWithSameHtml{INVALID_CREDIT_INDEX};
290 uint32_t nextCreditWithSameHtml{INVALID_CREDIT_INDEX};
291 };
292
293 void addBulkReferences(
294 const std::vector<int32_t>& references,
295 const std::vector<uint32_t>& generations) noexcept;
296 void releaseBulkReferences(
297 const std::vector<int32_t>& references,
298 const std::vector<uint32_t>& generations) noexcept;
299
300 void createCreditSource(CreditSource& creditSource) noexcept;
301 void destroyCreditSource(CreditSource& creditSource) noexcept;
302
303 uint32_t filterCreditForSnapshot(
304 CreditFilteringMode filteringMode,
305 const CreditRecord& record) noexcept;
306
307 const std::string INVALID_CREDIT_MESSAGE =
308 "Error: Invalid Credit, cannot get HTML string.";
309
310 static const uint32_t INVALID_CREDIT_INDEX{
311 std::numeric_limits<uint32_t>::max()};
312
313 std::vector<CreditSource*> _creditSources;
314 std::vector<CreditRecord> _credits;
315 CreditsSnapshot _snapshot;
316 std::vector<int32_t> _referenceCountScratch;
317
318 // These are credits that were shown in the last snapshot but whose
319 // CreditSources have since been destroyed. They need to be reported in
320 // removedCredits in the next snapshot.
321 std::vector<Credit> _shownCreditsDestroyed;
322
323 // Each entry in this vector is an index into _credits that is unused and can
324 // be reused for a new credit.
325 std::vector<size_t> _unusedCreditRecords;
326
327 CreditSource _defaultCreditSource{*this};
328
329 friend class CreditReferencer;
330 friend class CreditSource;
331};
332} // namespace CesiumUtility
Represents a source of credits, such as a tileset or raster overlay, provided to a CreditSystem.
CreditSystem * getCreditSystem() noexcept
Gets the CreditSystem associated with this source.
CreditSource(CreditSystem &creditSystem) noexcept
Constructs a new credit source associated with a given credit system.
CreditSource(const std::shared_ptr< CreditSystem > &pCreditSystem) noexcept
Constructs a new credit source associated with a given credit system.
Creates and manages Credit objects. Avoids repetitions and tracks which credits should be shown and w...
bool shouldBeShownOnScreen(Credit credit) const noexcept
Gets whether or not the credit should be shown on screen.
const CreditSource * getCreditSource(Credit credit) const noexcept
Gets the source of this credit.
bool addCreditReference(Credit credit)
Adds a reference to a credit, incrementing its reference count. The referenced credit will be shown u...
const CreditSource & getDefaultCreditSource() const noexcept
Gets the default credit source used when no other source is specified.
CreditSystem() noexcept=default
Constructs a new instance.
const CreditsSnapshot & getSnapshot(CreditFilteringMode filteringMode=CreditFilteringMode::UniqueHtml) noexcept
Gets a snapshot of the credits. The returned instance is only valid until the next call to this metho...
void setShowOnScreen(Credit credit, bool showOnScreen) noexcept
Sets whether or not the credit should be shown on screen.
bool removeCreditReference(Credit credit)
Removes a reference from a credit, decrementing its reference count. When the reference count goes to...
Credit createCredit(const CreditSource &source, std::string &&html, bool showOnScreen=false)
Inserts a credit string.
const std::string & getHtml(Credit credit) const noexcept
Get the HTML string for this credit.
Utility classes for Cesium.
CreditFilteringMode
Specifies how credit system snapshots should handle multiple credits with the same HTML string.
@ None
No filtering is performed. Each unique Credit is reported.
@ UniqueHtmlAndShowOnScreen
Credits are filtered so that each reported credit has a combination of HTML string and CreditSystem::...
@ UniqueHtml
Credits with identical HTML strings are reported as one Credit even if they have a different CreditSo...
STL namespace.
Represents an HTML string that should be shown on screen to attribute third parties for used data,...
bool operator==(const Credit &rhs) const noexcept
Returns true if two credit objects have the same ID.
A snapshot of the credits currently active in a CreditSystem.
std::vector< Credit > currentCredits
The credits that are currently active.
std::vector< Credit > removedCredits
The credits that were removed since the last call to CreditSystem::getSnapshot.