VTK  9.5.20250805
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
10#ifndef vtkDeserializer_h
11#define vtkDeserializer_h
12
13#include "vtkObject.h"
14
15#include "vtkCommonCoreModule.h" // for export macro
16#include "vtkLogger.h" // for vtkLogger::Verbosity enum
17#include "vtkMarshalContext.h" // for vtkMarshalContext
18#include "vtkSmartPointer.h" // for vktSmartPointer
19
20// clang-format off
21#include "vtk_nlohmannjson.h" // for json
22#include VTK_NLOHMANN_JSON(json_fwd.hpp) // for json
23// clang-format on
24
25#include <memory> // for unique_ptr
26#include <typeinfo> // for type_info
27
28VTK_ABI_NAMESPACE_BEGIN
29
30class VTKCOMMONCORE_EXPORT vtkDeserializer : public vtkObject
31{
32public:
34 vtkTypeMacro(vtkDeserializer, vtkObject);
35 void PrintSelf(ostream& os, vtkIndent indent) override;
36
37 using HandlerType = std::function<void(const nlohmann::json&, vtkObjectBase*, vtkDeserializer*)>;
38 using ConstructorType = std::function<vtkObjectBase*()>;
39
61 const std::string& className, const std::vector<std::string>& superClassNames);
62
72 bool DeserializeJSON(const vtkTypeUInt32& identifier, vtkSmartPointer<vtkObjectBase>& objectBase);
73
75
82 void RegisterConstructor(const std::string& className, ConstructorType constructor);
84 const std::string& className, const std::vector<std::string>& superClassNames);
85 void UnRegisterConstructor(const std::string& className);
87
89
97 void RegisterHandler(const std::type_info& type, HandlerType deserializer);
98 HandlerType GetHandler(const std::type_info& type) const;
99 bool UnRegisterHandler(const std::type_info& type);
101
103
112 vtkSetSmartPointerMacro(Context, vtkMarshalContext);
113 vtkGetSmartPointerMacro(Context, vtkMarshalContext);
115
117
132
133protected:
136
139
140private:
141 vtkDeserializer(const vtkDeserializer&) = delete;
142 void operator=(const vtkDeserializer&) = delete;
143 class vtkInternals;
144 std::unique_ptr<vtkInternals> Internals;
145};
146
150#define VTK_DESERIALIZE_VALUE_FROM_STATE(name, type, state, object) \
151 do \
152 { \
153 const auto iter = state.find(#name); \
154 if ((iter != state.end()) && !iter->is_null()) \
155 { \
156 object->Set##name(iter->get<type>()); \
157 } \
158 } while (0)
159
165#define VTK_DESERIALIZE_VTK_OBJECT_FROM_STATE_DIFFERENT_NAMES( \
166 stateKey, propertyName, cls, state, object, deserializer) \
167 do \
168 { \
169 const auto iter = state.find(#stateKey); \
170 if ((iter != state.end()) && !iter->is_null()) \
171 { \
172 const auto* context = deserializer->GetContext(); \
173 const auto identifier = iter->at("Id").get<vtkTypeUInt32>(); \
174 auto subObject = context->GetObjectAtId(identifier); \
175 deserializer->DeserializeJSON(identifier, subObject); \
176 if (auto* asVtkType = cls::SafeDownCast(subObject)) \
177 { \
178 object->Set##propertyName(asVtkType); \
179 } \
180 } \
181 } while (0)
182
186#define VTK_DESERIALIZE_VTK_OBJECT_FROM_STATE(name, cls, state, object, deserializer) \
187 VTK_DESERIALIZE_VTK_OBJECT_FROM_STATE_DIFFERENT_NAMES( \
188 name, name, cls, state, object, deserializer)
189
194#define VTK_DESERIALIZE_VECTOR_FROM_STATE(name, type, state, object) \
195 do \
196 { \
197 const auto iter = state.find(#name); \
198 if ((iter != state.end()) && !iter->is_null()) \
199 { \
200 using namespace std; \
201 const auto elements = iter->get<vector<type>>(); \
202 object->Set##name(elements.data()); \
203 } \
204 } while (0)
205
206VTK_ABI_NAMESPACE_END
207#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...
void SetDeserializerLogVerbosity(vtkLogger::Verbosity verbosity)
Set/Get the log verbosity of messages that are emitted when data is uploaded to GPU memory.
std::function< void(const nlohmann::json &, vtkObjectBase *, vtkDeserializer *)> HandlerType
vtkLogger::Verbosity GetDeserializerLogVerbosity()
Set/Get the log verbosity of messages that are emitted when data is uploaded to GPU memory.
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.
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
@ VERBOSITY_INVALID
Definition vtkLogger.h:214
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.