cesium-native  0.41.0
ExtensionWriterContext.h
1 #pragma once
2 
3 #include "CesiumJsonWriter/JsonWriter.h"
4 #include "CesiumJsonWriter/Library.h"
5 
6 #include <any>
7 #include <functional>
8 #include <map>
9 #include <string>
10 #include <unordered_map>
11 
12 namespace CesiumJsonWriter {
13 
17 enum class ExtensionState {
25  Enabled,
26 
32  Disabled
33 };
34 
35 class CESIUMJSONWRITER_API ExtensionWriterContext {
36 private:
37  template <typename TExtension>
38  using ExtensionHandler = std::function<
39  void(const TExtension&, JsonWriter&, const ExtensionWriterContext&)>;
40 
41 public:
49  template <typename TExtended, typename TExtensionHandler>
50  void registerExtension(const std::string& extensionName) {
51  using TExtension = typename TExtensionHandler::ValueType;
52 
53  auto it =
54  this->_extensions.emplace(extensionName, ObjectTypeToHandler()).first;
55  it->second.insert_or_assign(
56  TExtended::TypeName,
57  [](const std::any& obj,
58  JsonWriter& jsonWriter,
59  const ExtensionWriterContext& context) {
60  return TExtensionHandler::write(
61  std::any_cast<const TExtension&>(obj),
62  jsonWriter,
63  context);
64  });
65  }
66 
75  template <typename TExtended, typename TExtensionHandler>
77  registerExtension<TExtended, TExtensionHandler>(
78  TExtensionHandler::ExtensionName);
79  }
80 
88  ExtensionState getExtensionState(const std::string& extensionName) const;
89 
102  void
103  setExtensionState(const std::string& extensionName, ExtensionState newState);
104 
105  ExtensionHandler<std::any> createExtensionHandler(
106  const std::string_view& extensionName,
107  const std::any& obj,
108  const std::string& extendedObjectType) const;
109 
110 private:
111  using ObjectTypeToHandler =
112  std::unordered_map<std::string, ExtensionHandler<std::any>>;
113  using ExtensionNameMap = std::map<std::string, ObjectTypeToHandler>;
114 
115  ExtensionNameMap _extensions;
116  std::unordered_map<std::string, ExtensionState> _extensionStates;
117 };
118 
119 } // namespace CesiumJsonWriter
void setExtensionState(const std::string &extensionName, ExtensionState newState)
Enables or disables an extension.
void registerExtension(const std::string &extensionName)
Registers an extension for an object.
void registerExtension()
Registers an extension for an object.
ExtensionState getExtensionState(const std::string &extensionName) const
Returns whether an extension is enabled or disabled.
Classes for writing JSON.
ExtensionState
The state of an extension.
@ Enabled
The extension is enabled.
@ Disabled
The extension is disabled.