VTK  9.4.20250306
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 "vtkLogger.h" // for vtkLogger::Verbosity enum
14#include "vtkMarshalContext.h" // for vtkMarshalContext
15#include "vtkSmartPointer.h" // for vktSmartPointer
16
17// clang-format off
18#include "vtk_nlohmannjson.h" // for json
19#include VTK_NLOHMANN_JSON(json_fwd.hpp) // for json
20// clang-format on
21
22#include <memory> // for unique_ptr
23#include <typeinfo> // for type_info
24
25VTK_ABI_NAMESPACE_BEGIN
26
27class VTKCOMMONCORE_EXPORT vtkDeserializer : public vtkObject
28{
29public:
31 vtkTypeMacro(vtkDeserializer, vtkObject);
32 void PrintSelf(ostream& os, vtkIndent indent) override;
33
34 using HandlerType = std::function<void(const nlohmann::json&, vtkObjectBase*, vtkDeserializer*)>;
35 using ConstructorType = std::function<vtkObjectBase*()>;
36
58 const std::string& className, const std::vector<std::string>& superClassNames);
59
69 bool DeserializeJSON(const vtkTypeUInt32& identifier, vtkSmartPointer<vtkObjectBase>& objectBase);
70
72
79 void RegisterConstructor(const std::string& className, ConstructorType constructor);
81 const std::string& className, const std::vector<std::string>& superClassNames);
82 void UnRegisterConstructor(const std::string& className);
84
86
94 void RegisterHandler(const std::type_info& type, HandlerType deserializer);
95 HandlerType GetHandler(const std::type_info& type) const;
96 bool UnRegisterHandler(const std::type_info& type);
98
100
109 vtkSetSmartPointerMacro(Context, vtkMarshalContext);
110 vtkGetSmartPointerMacro(Context, vtkMarshalContext);
112
114
129
130protected:
133
136
137private:
138 vtkDeserializer(const vtkDeserializer&) = delete;
139 void operator=(const vtkDeserializer&) = delete;
140 class vtkInternals;
141 std::unique_ptr<vtkInternals> Internals;
142};
143
147#define VTK_DESERIALIZE_VALUE_FROM_STATE(name, type, state, object) \
148 do \
149 { \
150 const auto iter = state.find(#name); \
151 if ((iter != state.end()) && !iter->is_null()) \
152 { \
153 object->Set##name(iter->get<type>()); \
154 } \
155 } while (0)
156
162#define VTK_DESERIALIZE_VTK_OBJECT_FROM_STATE_DIFFERENT_NAMES( \
163 stateKey, propertyName, cls, state, object, deserializer) \
164 do \
165 { \
166 const auto iter = state.find(#stateKey); \
167 if ((iter != state.end()) && !iter->is_null()) \
168 { \
169 const auto* context = deserializer->GetContext(); \
170 const auto identifier = iter->at("Id").get<vtkTypeUInt32>(); \
171 auto subObject = context->GetObjectAtId(identifier); \
172 deserializer->DeserializeJSON(identifier, subObject); \
173 if (auto* asVtkType = cls::SafeDownCast(subObject)) \
174 { \
175 object->Set##propertyName(asVtkType); \
176 } \
177 } \
178 } while (0)
179
183#define VTK_DESERIALIZE_VTK_OBJECT_FROM_STATE(name, cls, state, object, deserializer) \
184 VTK_DESERIALIZE_VTK_OBJECT_FROM_STATE_DIFFERENT_NAMES( \
185 name, name, cls, state, object, deserializer)
186
191#define VTK_DESERIALIZE_VECTOR_FROM_STATE(name, type, state, object) \
192 do \
193 { \
194 const auto iter = state.find(#name); \
195 if ((iter != state.end()) && !iter->is_null()) \
196 { \
197 using namespace std; \
198 const auto elements = iter->get<vector<type>>(); \
199 object->Set##name(elements.data()); \
200 } \
201 } while (0)
202
203VTK_ABI_NAMESPACE_END
204#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.