cesium-native 0.43.0
Loading...
Searching...
No Matches
ExtensibleObject.h
1#pragma once
2
3#include "JsonValue.h"
4#include "Library.h"
5
6#include <any>
7#include <unordered_map>
8#include <utility>
9#include <vector>
10
11namespace CesiumUtility {
15struct CESIUMUTILITY_API ExtensibleObject {
22 template <typename T> bool hasExtension() const noexcept {
23 auto it = this->extensions.find(T::ExtensionName);
24 return it != this->extensions.end();
25 }
26
34 template <typename T> const T* getExtension() const noexcept {
35 auto it = this->extensions.find(T::ExtensionName);
36 if (it == this->extensions.end()) {
37 return nullptr;
38 }
39
40 return std::any_cast<T>(&it->second);
41 }
42
44 template <typename T> T* getExtension() noexcept {
45 return const_cast<T*>(std::as_const(*this).getExtension<T>());
46 }
47
59 const JsonValue*
60 getGenericExtension(const std::string& extensionName) const noexcept;
61
63 JsonValue* getGenericExtension(const std::string& extensionName) noexcept;
64
73 template <typename T, typename... ConstructorArgumentTypes>
74 T& addExtension(ConstructorArgumentTypes&&... constructorArguments) {
75 std::any& extension =
76 extensions
77 .try_emplace(
78 T::ExtensionName,
79 std::make_any<T>(std::forward<ConstructorArgumentTypes>(
80 constructorArguments)...))
81 .first->second;
82 return std::any_cast<T&>(extension);
83 }
84
90 template <typename T> void removeExtension() {
91 extensions.erase(T::ExtensionName);
92 }
93
101 std::unordered_map<std::string, std::any> extensions;
102
112
121
127 int64_t getSizeBytes() const {
128 int64_t accum = 0;
129 accum += int64_t(sizeof(ExtensibleObject));
130
131 accum += int64_t(
132 this->extras.size() * (sizeof(std::string) + sizeof(JsonValue)));
133 for (const auto& [k, v] : this->extras) {
134 accum += int64_t(k.capacity() * sizeof(char) - sizeof(std::string));
135 accum += v.getSizeBytes() - int64_t(sizeof(JsonValue));
136 }
137
138 accum += int64_t(
139 this->unknownProperties.size() *
140 (sizeof(std::string) + sizeof(JsonValue)));
141 for (const auto& [k, v] : this->unknownProperties) {
142 accum += int64_t(k.capacity() * sizeof(char) - sizeof(std::string));
143 accum += v.getSizeBytes() - int64_t(sizeof(JsonValue));
144 }
145
146 return accum;
147 }
148};
149} // namespace CesiumUtility
A generic implementation of a value in a JSON structure.
Definition JsonValue.h:75
std::map< std::string, JsonValue > Object
The type to represent an Object JSON value.
Definition JsonValue.h:95
Utility classes for Cesium.
The base class for objects that have extensions and extras.
const T * getExtension() const noexcept
Gets an extension given its static type.
JsonValue::Object extras
Application-specific data.
T * getExtension() noexcept
Gets an extension given its static type.
JsonValue::Object unknownProperties
Unknown properties that exist on this object but do not have any representation in the statically-typ...
int64_t getSizeBytes() const
Calculates the size in bytes of this ExtensibleObject, including all of its extras but NOT including ...
const JsonValue * getGenericExtension(const std::string &extensionName) const noexcept
Gets a generic extension with the given name as a CesiumUtility::JsonValue.
T & addExtension(ConstructorArgumentTypes &&... constructorArguments)
Adds a statically-typed extension to this object.
JsonValue * getGenericExtension(const std::string &extensionName) noexcept
Gets a generic extension with the given name as a CesiumUtility::JsonValue.
void removeExtension()
Removes a statically-typed extension from this object.
bool hasExtension() const noexcept
Checks if an extension exists given its static type.
std::unordered_map< std::string, std::any > extensions
The extensions attached to this object.