VTK  9.7.20260822
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
39VTK_ABI_NAMESPACE_BEGIN
40class VTKCOMMONCORE_EXPORT vtkMarshalContext : public vtkObject
41{
42public:
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
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
74
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
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
193
197 void ResetDirectDependenciesForNode(vtkTypeUInt32 identifier);
198
202 vtkTypeUInt32 MakeId();
203
205
216 void SetAllocateIdsDescending(bool allocateIdsDescending);
219
226 {
227 vtkMarshalContext* Context = nullptr;
228
229 public:
230 explicit ScopedParentTracker(vtkMarshalContext* context, vtkTypeUInt32 identifier)
231 : Context(context)
232 {
233 if (this->Context)
234 {
235 this->Context->PushParent(identifier);
236 }
237 }
238
240 {
241 if (this->Context)
242 {
243 this->Context->PopParent();
244 }
245 }
246
248 void operator=(const ScopedParentTracker&) = delete;
251 };
252
259 void PushParent(vtkTypeUInt32 identifier);
260
267 void PopParent();
268
274 bool ShouldDataArrayBeModified(vtkTypeUInt32 id, const std::string& hash);
275
276protected:
279
286 bool HasId(vtkObjectBase* objectBase, vtkTypeUInt32& identifier);
287
292 bool IsProcessing(vtkTypeUInt32 identifier);
293
298 bool IsProcessed(vtkTypeUInt32 identifier);
299
310 void AddChild(vtkTypeUInt32 identifier);
311
312private:
313 vtkMarshalContext(const vtkMarshalContext&) = delete;
314 void operator=(const vtkMarshalContext&) = delete;
315
316 friend class vtkDeserializer;
317 friend class vtkSerializer;
318
319 class vtkInternals;
320 std::unique_ptr<vtkInternals> Internals;
321
322 static std::vector<vtkMarshalContextRegistrarFunc>& GetSerDesRegistrars();
323};
324
325VTK_ABI_NAMESPACE_END
326#endif
a simple class to control print indentation
Definition vtkIndent.h:108
void operator=(const ScopedParentTracker &)=delete
ScopedParentTracker(vtkMarshalContext *context, vtkTypeUInt32 identifier)
ScopedParentTracker(const ScopedParentTracker &)=delete
ScopedParentTracker(ScopedParentTracker &&)=delete
void operator=(ScopedParentTracker &&)=delete
bool RegisterBlob(vtkSmartPointer< vtkTypeUInt8Array > blob, std::string &hash)
Add a blob into the blob store associated with hash.
void SetAllocateIdsDescending(bool allocateIdsDescending)
When enabled, MakeId hands out identifiers counting down from the maximum value of vtkTypeUInt32 inst...
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
std::map< std::string, std::set< vtkSmartPointer< vtkObjectBase > > > StrongObjectStore
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.
friend class vtkDeserializer
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,...
static void AddRegistrar(vtkMarshalContextRegistrarFunc registrar)
Add a serialization registrar to the static vector.
bool UnRegisterObject(vtkTypeUInt32 identifier)
Removes an objectBase registered at identifier from the weak object store.
vtkSmartPointer< vtkTypeUInt8Array > GetBlob(const std::string &hash, bool copy=true)
Find and get the blob registered at hash.
std::map< vtkTypeUInt32, vtkWeakPointer< vtkObjectBase > > WeakObjectStore
nlohmann::json & GetState(vtkTypeUInt32 identifier) const
Find and get the state registered at identifier.
friend class vtkSerializer
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.
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.
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.
static bool CallRegistrars(void *ser, void *deser, void *invoker, const char **error)
Call all registrars with the given arguments.
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.
bool ShouldDataArrayBeModified(vtkTypeUInt32 id, const std::string &hash)
Returns true if the hash for the object at id differs from last used hash.
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.
bool GetAllocateIdsDescending() const
When enabled, MakeId hands out identifiers counting down from the maximum value of vtkTypeUInt32 inst...
Hold a reference to a vtkObjectBase instance.
int(* vtkMarshalContextRegistrarFunc)(void *ser, void *deser, void *invoker, const char **error)