VTK  9.4.20241016
vtkDeserializer.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
7#ifndef vtkDeserializer_h
8#define vtkDeserializer_h
9
10#include "vtkObject.h"
11
12#include "vtkCommonCoreModule.h" // for export macro
13#include "vtkMarshalContext.h" // for vtkMarshalContext
14#include "vtkSmartPointer.h" // for vktSmartPointer
15
16// clang-format off
17#include "vtk_nlohmannjson.h" // for json
18#include VTK_NLOHMANN_JSON(json_fwd.hpp) // for json
19// clang-format on
20
21#include <memory> // for unique_ptr
22#include <typeinfo> // for type_info
23
24VTK_ABI_NAMESPACE_BEGIN
25
26class VTKCOMMONCORE_EXPORT vtkDeserializer : public vtkObject
27{
28public:
30 vtkTypeMacro(vtkDeserializer, vtkObject);
31 void PrintSelf(ostream& os, vtkIndent indent) override;
32
33 using HandlerType = std::function<void(const nlohmann::json&, vtkObjectBase*, vtkDeserializer*)>;
34 using ConstructorType = std::function<vtkObjectBase*()>;
35
57 const std::string& className, const std::vector<std::string>& superClassNames);
58
68 bool DeserializeJSON(const vtkTypeUInt32& identifier, vtkSmartPointer<vtkObjectBase>& objectBase);
69
71
78 void RegisterConstructor(const std::string& className, ConstructorType constructor);
80 const std::string& className, const std::vector<std::string>& superClassNames);
81 void UnRegisterConstructor(const std::string& className);
83
85
93 void RegisterHandler(const std::type_info& type, HandlerType deserializer);
94 HandlerType GetHandler(const std::type_info& type) const;
95 bool UnRegisterHandler(const std::type_info& type);
97
99
111protected:
114
116
117private:
118 vtkDeserializer(const vtkDeserializer&) = delete;
119 void operator=(const vtkDeserializer&) = delete;
120 class vtkInternals;
121 std::unique_ptr<vtkInternals> Internals;
122};
123
127#define VTK_DESERIALIZE_VALUE_FROM_STATE(name, type, state, object) \
128 do \
129 { \
130 const auto iter = state.find(#name); \
131 if ((iter != state.end()) && !iter->is_null()) \
132 { \
133 object->Set##name(iter->get<type>()); \
134 } \
135 } while (0)
136
142#define VTK_DESERIALIZE_VTK_OBJECT_FROM_STATE_DIFFERENT_NAMES( \
143 stateKey, propertyName, cls, state, object, deserializer) \
144 do \
145 { \
146 const auto iter = state.find(#stateKey); \
147 if ((iter != state.end()) && !iter->is_null()) \
148 { \
149 const auto* context = deserializer->GetContext(); \
150 const auto identifier = iter->at("Id").get<vtkTypeUInt32>(); \
151 auto subObject = context->GetObjectAtId(identifier); \
152 deserializer->DeserializeJSON(identifier, subObject); \
153 if (auto* asVtkType = cls::SafeDownCast(subObject)) \
154 { \
155 object->Set##propertyName(asVtkType); \
156 } \
157 } \
158 } while (0)
159
163#define VTK_DESERIALIZE_VTK_OBJECT_FROM_STATE(name, cls, state, object, deserializer) \
164 VTK_DESERIALIZE_VTK_OBJECT_FROM_STATE_DIFFERENT_NAMES( \
165 name, name, cls, state, object, deserializer)
166
171#define VTK_DESERIALIZE_VECTOR_FROM_STATE(name, type, state, object) \
172 do \
173 { \
174 const auto iter = state.find(#name); \
175 if ((iter != state.end()) && !iter->is_null()) \
176 { \
177 using namespace std; \
178 const auto elements = iter->get<vector<type>>(); \
179 object->Set##name(elements.data()); \
180 } \
181 } while (0)
182
183VTK_ABI_NAMESPACE_END
184#endif
Deserialize VTK objects from JSON.
bool UnRegisterHandler(const std::type_info &type)
The handlers are invoked to deserialize a json state into a vtkObjectBase derived instance of type ty...
std::function< void(const nlohmann::json &, vtkObjectBase *, vtkDeserializer *)> HandlerType
vtkGetSmartPointerMacro(Context, vtkMarshalContext)
Get/Set the marshalling context.
void RegisterConstructor(const std::string &className, ConstructorType constructor)
The constructors are invoked to construct an instance of className.
vtkSmartPointer< vtkMarshalContext > Context
ConstructorType GetConstructor(const std::string &className, const std::vector< std::string > &superClassNames)
The constructors are invoked to construct an instance of className.
static vtkDeserializer * New()
void UnRegisterConstructor(const std::string &className)
The constructors are invoked to construct an instance of className.
vtkSetSmartPointerMacro(Context, vtkMarshalContext)
Get/Set the marshalling context.
HandlerType GetHandler(const std::type_info &type) const
The handlers are invoked to deserialize a json state into a vtkObjectBase derived instance of type ty...
std::function< vtkObjectBase *()> ConstructorType
~vtkDeserializer() override
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
bool DeserializeJSON(const vtkTypeUInt32 &identifier, vtkSmartPointer< vtkObjectBase > &objectBase)
Deserialize a state registered with the context at identifier into objectBase.
vtkObjectBase * ConstructObject(const std::string &className, const std::vector< std::string > &superClassNames)
Constructs an object of type className.
void RegisterHandler(const std::type_info &type, HandlerType deserializer)
The handlers are invoked to deserialize a json state into a vtkObjectBase derived instance of type ty...
a simple class to control print indentation
Definition vtkIndent.h:108
Shared context used by vtkSerializer and vtkDeserializer
abstract base class for most VTK objects
abstract base class for most VTK objects
Definition vtkObject.h:162
Hold a reference to a vtkObjectBase instance.