VTK  9.5.20250718
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
15#ifndef vtkMarshalContext_h
16#define vtkMarshalContext_h
17
18#include "vtkObject.h"
19
20#include "vtkCommonCoreModule.h" // for export macro
21#include "vtkSmartPointer.h" // for vtkSmartPointer
22#include "vtkTypeUInt8Array.h" // for vtkTypeUInt8Array
23#include "vtkWeakPointer.h" // for vtkWeakPointer
24
25// clang-format off
26#include "vtk_nlohmannjson.h" // for json
27#include VTK_NLOHMANN_JSON(json.hpp) // for json
28// clang-format on
29
30#include <memory> // for unique_ptr
31#include <set> // for set
32
33VTK_ABI_NAMESPACE_BEGIN
34class VTKCOMMONCORE_EXPORT vtkMarshalContext : public vtkObject
35{
36public:
39 void PrintSelf(ostream& os, vtkIndent indent) override;
40
41 using WeakObjectStore = std::map<vtkTypeUInt32, vtkWeakPointer<vtkObjectBase>>;
42 using StrongObjectStore = std::map<std::string, std::set<vtkSmartPointer<vtkObjectBase>>>;
43
47 const nlohmann::json& Blobs() const;
48
52 const nlohmann::json& States() const;
53
58
63
68 void KeepAlive(const std::string& owner, vtkObjectBase* objectBase);
69
77 void Retire(const std::string& owner, vtkObjectBase* objectBase);
78
87 bool RegisterState(nlohmann::json state);
88
94 bool UnRegisterState(vtkTypeUInt32 identifier);
95
101 nlohmann::json& GetState(vtkTypeUInt32 identifier) const;
102
111 bool RegisterObject(vtkObjectBase* objectBase, vtkTypeUInt32& identifier);
112
118 bool UnRegisterObject(vtkTypeUInt32 identifier);
119
123 vtkSmartPointer<vtkObjectBase> GetObjectAtId(vtkTypeUInt32 identifier) const;
124
128 vtkTypeUInt32 GetId(vtkObjectBase* objectBase) const;
129
141
147 bool UnRegisterBlob(const std::string& hash);
148
155 vtkSmartPointer<vtkTypeUInt8Array> GetBlob(const std::string& hash, bool copy = false);
156
167 std::vector<vtkTypeUInt32> GetDirectDependencies(vtkTypeUInt32 identifier) const;
168
173
177 void ResetDirectDependenciesForNode(vtkTypeUInt32 identifier);
178
182 vtkTypeUInt32 MakeId();
183
190 {
191 vtkMarshalContext* Context = nullptr;
192
193 public:
194 explicit ScopedParentTracker(vtkMarshalContext* context, vtkTypeUInt32 identifier)
195 : Context(context)
196 {
197 if (this->Context)
198 {
199 this->Context->PushParent(identifier);
200 }
201 }
202
204 {
205 if (this->Context)
206 {
207 this->Context->PopParent();
208 }
209 }
210
212 void operator=(const ScopedParentTracker&) = delete;
215 };
216
223 void PushParent(vtkTypeUInt32 identifier);
224
231 void PopParent();
232
233protected:
236
243 bool HasId(vtkObjectBase* objectBase, vtkTypeUInt32& identifier);
244
249 bool IsProcessing(vtkTypeUInt32 identifier);
250
255 bool IsProcessed(vtkTypeUInt32 identifier);
256
267 void AddChild(vtkTypeUInt32 identifier);
268
269private:
270 vtkMarshalContext(const vtkMarshalContext&) = delete;
271 void operator=(const vtkMarshalContext&) = delete;
272
273 friend class vtkDeserializer;
274 friend class vtkSerializer;
275
276 class vtkInternals;
277 std::unique_ptr<vtkInternals> Internals;
278};
279
280VTK_ABI_NAMESPACE_END
281#endif
Deserialize VTK objects from JSON.
a simple class to control print indentation
Definition vtkIndent.h:108
Convenient to push a parent as the 'active' identifier and add children to that parent when (de)seria...
void operator=(const ScopedParentTracker &)=delete
ScopedParentTracker(vtkMarshalContext *context, vtkTypeUInt32 identifier)
ScopedParentTracker(const ScopedParentTracker &)=delete
ScopedParentTracker(ScopedParentTracker &&)=delete
void operator=(ScopedParentTracker &&)=delete
Shared context used by vtkSerializer and vtkDeserializer
bool RegisterBlob(vtkSmartPointer< vtkTypeUInt8Array > blob, std::string &hash)
Add a blob into the blob store associated with hash.
void AddChild(vtkTypeUInt32 identifier)
Records identifier in the list of children of the 'active' identifier.
static vtkMarshalContext * New()
bool RegisterState(nlohmann::json state)
Add a state.
bool IsProcessed(vtkTypeUInt32 identifier)
Returnstrue if identifier exists in the genealogy of object(s)/state(s), false otherwise.
~vtkMarshalContext() override
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
const WeakObjectStore & WeakObjects() const
Get map of weak objects.
const StrongObjectStore & StrongObjects() const
Get map of strong objects.
void PopParent()
Adds the 'active' identifier into the genealogy.
bool IsProcessing(vtkTypeUInt32 identifier)
Return true if identifier has been through PushParent, but not yet been through PopParent,...
bool UnRegisterObject(vtkTypeUInt32 identifier)
Removes an objectBase registered at identifier from the weak object store.
nlohmann::json & GetState(vtkTypeUInt32 identifier) const
Find and get the state registered at identifier.
bool HasId(vtkObjectBase *objectBase, vtkTypeUInt32 &identifier)
Return true if objectBase exists in the weak object store, false otherwise If the objectBase exists,...
void ResetDirectDependenciesForNode(vtkTypeUInt32 identifier)
Reset the dependency cache for the given identifier
std::map< vtkTypeUInt32, vtkWeakPointer< vtkObjectBase > > WeakObjectStore
vtkTypeUInt32 GetId(vtkObjectBase *objectBase) const
Get the identifier associated with objectBase.
void Retire(const std::string &owner, vtkObjectBase *objectBase)
Release the strong reference kept by owner to the given objectBase.
vtkSmartPointer< vtkTypeUInt8Array > GetBlob(const std::string &hash, bool copy=false)
Find and get the blob registered at hash.
vtkTypeUInt32 MakeId()
Make a new identifier.
void PushParent(vtkTypeUInt32 identifier)
Make identifier the active identifier.
void ResetDirectDependencies()
Reset the dependency cache.
bool UnRegisterState(vtkTypeUInt32 identifier)
Removes a state.
const nlohmann::json & Blobs() const
Get blobs.
std::map< std::string, std::set< vtkSmartPointer< vtkObjectBase > > > StrongObjectStore
bool RegisterObject(vtkObjectBase *objectBase, vtkTypeUInt32 &identifier)
Add an objectBase into the weak object store associated with identifier.
bool UnRegisterBlob(const std::string &hash)
Removes a blob registered at hash from the blob store.
const nlohmann::json & States() const
Get states.
std::vector< vtkTypeUInt32 > GetDirectDependencies(vtkTypeUInt32 identifier) const
Return all direct dependencies of the object/state registered at identifier.
void KeepAlive(const std::string &owner, vtkObjectBase *objectBase)
This method creates a record of ownership between owner and objectBase.
vtkSmartPointer< vtkObjectBase > GetObjectAtId(vtkTypeUInt32 identifier) const
Find and get the objectBase registered at identifier.
abstract base class for most VTK objects
abstract base class for most VTK objects
Definition vtkObject.h:162
Serialize VTK objects to JSON.
Hold a reference to a vtkObjectBase instance.