VTK  9.7.1
vtkMarshalContext.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
18 #ifndef vtkMarshalContext_h
19 #define vtkMarshalContext_h
20 
21 #include "vtkObject.h"
22 
23 #include "vtkCommonCoreModule.h" // for export macro
24 #include "vtkSmartPointer.h" // for vtkSmartPointer
25 #include "vtkTypeUInt8Array.h" // for vtkTypeUInt8Array
26 #include "vtkWeakPointer.h" // for vtkWeakPointer
27 
28 // clang-format off
29 #include "vtk_nlohmannjson.h" // for json
30 #include VTK_NLOHMANN_JSON(json.hpp) // for json
31 // clang-format on
32 
33 #include <memory> // for unique_ptr
34 #include <set> // for set
35 
37  void* ser, void* deser, void* invoker, const char** error);
38 
39 VTK_ABI_NAMESPACE_BEGIN
40 class VTKCOMMONCORE_EXPORT vtkMarshalContext : public vtkObject
41 {
42 public:
43  vtkTypeMacro(vtkMarshalContext, vtkObject);
44  static vtkMarshalContext* New();
45  void PrintSelf(ostream& os, vtkIndent indent) override;
46 
47  using WeakObjectStore = std::map<vtkTypeUInt32, vtkWeakPointer<vtkObjectBase>>;
48  using StrongObjectStore = std::map<std::string, std::set<vtkSmartPointer<vtkObjectBase>>>;
49 
53  static void AddRegistrar(vtkMarshalContextRegistrarFunc registrar);
54 
58  static bool CallRegistrars(void* ser, void* deser, void* invoker, const char** error);
59 
63  const nlohmann::json& Blobs() const;
64 
68  const nlohmann::json& States() const;
69 
73  const WeakObjectStore& WeakObjects() const;
74 
78  const StrongObjectStore& StrongObjects() const;
79 
84  void KeepAlive(const std::string& owner, vtkObjectBase* objectBase);
85 
93  void Retire(const std::string& owner, vtkObjectBase* objectBase);
94 
103  bool RegisterState(nlohmann::json state);
104 
110  bool UnRegisterState(vtkTypeUInt32 identifier);
111 
117  nlohmann::json& GetState(vtkTypeUInt32 identifier) const;
118 
127  bool RegisterObject(vtkObjectBase* objectBase, vtkTypeUInt32& identifier);
128 
134  bool UnRegisterObject(vtkTypeUInt32 identifier);
135 
139  vtkSmartPointer<vtkObjectBase> GetObjectAtId(vtkTypeUInt32 identifier) const;
140 
144  vtkTypeUInt32 GetId(vtkObjectBase* objectBase) const;
145 
156  bool RegisterBlob(vtkSmartPointer<vtkTypeUInt8Array> blob, std::string& hash);
157 
163  bool UnRegisterBlob(const std::string& hash);
164 
175  vtkSmartPointer<vtkTypeUInt8Array> GetBlob(const std::string& hash, bool copy = true);
176 
187  std::vector<vtkTypeUInt32> GetDirectDependencies(vtkTypeUInt32 identifier) const;
188 
192  void ResetDirectDependencies();
193 
197  void ResetDirectDependenciesForNode(vtkTypeUInt32 identifier);
198 
202  vtkTypeUInt32 MakeId();
203 
210  {
211  vtkMarshalContext* Context = nullptr;
212 
213  public:
214  explicit ScopedParentTracker(vtkMarshalContext* context, vtkTypeUInt32 identifier)
215  : Context(context)
216  {
217  if (this->Context)
218  {
219  this->Context->PushParent(identifier);
220  }
221  }
222 
224  {
225  if (this->Context)
226  {
227  this->Context->PopParent();
228  }
229  }
230 
231  ScopedParentTracker(const ScopedParentTracker&) = delete;
232  void operator=(const ScopedParentTracker&) = delete;
234  void operator=(ScopedParentTracker&&) = delete;
235  };
236 
243  void PushParent(vtkTypeUInt32 identifier);
244 
251  void PopParent();
252 
253 protected:
255  ~vtkMarshalContext() override;
256 
263  bool HasId(vtkObjectBase* objectBase, vtkTypeUInt32& identifier);
264 
269  bool IsProcessing(vtkTypeUInt32 identifier);
270 
275  bool IsProcessed(vtkTypeUInt32 identifier);
276 
287  void AddChild(vtkTypeUInt32 identifier);
288 
289 private:
290  vtkMarshalContext(const vtkMarshalContext&) = delete;
291  void operator=(const vtkMarshalContext&) = delete;
292 
293  friend class vtkDeserializer;
294  friend class vtkSerializer;
295 
296  class vtkInternals;
297  std::unique_ptr<vtkInternals> Internals;
298 
299  static std::vector<vtkMarshalContextRegistrarFunc>& GetSerDesRegistrars();
300 };
301 
302 VTK_ABI_NAMESPACE_END
303 #endif
Deserialize VTK objects from JSON.
abstract base class for most VTK objects
Definition: vtkObject.h:52
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
std::map< vtkTypeUInt32, vtkWeakPointer< vtkObjectBase >> WeakObjectStore
Hold a reference to a vtkObjectBase instance.
Definition: vtkMeta.h:23
ScopedParentTracker(vtkMarshalContext *context, vtkTypeUInt32 identifier)
void PushParent(vtkTypeUInt32 identifier)
Make identifier the active identifier.
a simple class to control print indentation
Definition: vtkIndent.h:28
int(* vtkMarshalContextRegistrarFunc)(void *ser, void *deser, void *invoker, const char **error)
abstract base class for most VTK objects
Definition: vtkObjectBase.h:68
Shared context used by vtkSerializer and vtkDeserializer
std::map< std::string, std::set< vtkSmartPointer< vtkObjectBase >>> StrongObjectStore
void PopParent()
Adds the 'active' identifier into the genealogy.
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
Serialize VTK objects to JSON.
Definition: vtkSerializer.h:30
Convenient to push a parent as the 'active' identifier and add children to that parent when (de)seria...