cesium-native  0.41.0
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 
11 namespace CesiumUtility {
15 struct 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 };
122 } // namespace CesiumUtility
A generic implementation of a value in a JSON structure.
Definition: JsonValue.h:67
std::map< std::string, JsonValue > Object
The type to represent an Object JSON value.
Definition: JsonValue.h:87
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.
JsonValue::Object unknownProperties
Unknown properties that exist on this object but do not have any representation in the statically-typ...
T * getExtension() noexcept
Gets an extension given its static type.
T & addExtension(ConstructorArgumentTypes &&... constructorArguments)
Adds a statically-typed extension to this object.
void removeExtension()
Removes a statically-typed extension from this object.
JsonValue * getGenericExtension(const std::string &extensionName) noexcept
Gets a generic extension with the given name as a CesiumUtility::JsonValue.
const JsonValue * getGenericExtension(const std::string &extensionName) const noexcept
Gets a generic extension with the given name as a CesiumUtility::JsonValue.
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.