cesium-native 0.43.0
Loading...
Searching...
No Matches
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
13
17enum class ExtensionState {
25 Enabled,
26
33};
34
39class CESIUMJSONWRITER_API ExtensionWriterContext {
40private:
41 template <typename TExtension>
42 using ExtensionHandler = std::function<
43 void(const TExtension&, JsonWriter&, const ExtensionWriterContext&)>;
44
45public:
53 template <typename TExtended, typename TExtensionHandler>
54 void registerExtension(const std::string& extensionName) {
55 using TExtension = typename TExtensionHandler::ValueType;
56
57 auto it =
58 this->_extensions.emplace(extensionName, ObjectTypeToHandler()).first;
59 it->second.insert_or_assign(
60 TExtended::TypeName,
61 [](const std::any& obj,
62 JsonWriter& jsonWriter,
63 const ExtensionWriterContext& context) {
64 return TExtensionHandler::write(
65 std::any_cast<const TExtension&>(obj),
66 jsonWriter,
67 context);
68 });
69 }
70
79 template <typename TExtended, typename TExtensionHandler>
81 registerExtension<TExtended, TExtensionHandler>(
82 TExtensionHandler::ExtensionName);
83 }
84
92 ExtensionState getExtensionState(const std::string& extensionName) const;
93
106 void
107 setExtensionState(const std::string& extensionName, ExtensionState newState);
108
119 ExtensionHandler<std::any> createExtensionHandler(
120 const std::string_view& extensionName,
121 const std::any& obj,
122 const std::string& extendedObjectType) const;
123
124private:
125 using ObjectTypeToHandler =
126 std::unordered_map<std::string, ExtensionHandler<std::any>>;
127 using ExtensionNameMap = std::map<std::string, ObjectTypeToHandler>;
128
129 ExtensionNameMap _extensions;
130 std::unordered_map<std::string, ExtensionState> _extensionStates;
131};
132
133} // namespace CesiumJsonWriter
A context for writing extensions where known extensions and their handlers can be registered.
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.
ExtensionHandler< std::any > createExtensionHandler(const std::string_view &extensionName, const std::any &obj, const std::string &extendedObjectType) const
Attempts to create an ExtensionHandler for the given object, returning nullptr if no handler could be...
Wrapper around rapidjson::Writer for writing objects to JSON.
Definition JsonWriter.h:18
Classes for writing JSON.
ExtensionState
The state of an extension.
@ Enabled
The extension is enabled.
@ Disabled
The extension is disabled.