VTK  9.5.20251201
vtkGarbageCollector.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
62
63#ifndef vtkGarbageCollector_h
64#define vtkGarbageCollector_h
65
66#include "vtkCommonCoreModule.h" // For export macro
67#include "vtkGarbageCollectorManager.h" // Needed for singleton initialization.
68#include "vtkObject.h"
69
70// This function is a friend of the collector so that it can call the
71// internal report method.
72VTK_ABI_NAMESPACE_BEGIN
73void VTKCOMMONCORE_EXPORT vtkGarbageCollectorReportInternal(
74 vtkGarbageCollector*, vtkObjectBase*, void*, const char*);
75
76// This forward ref allows us to define methods with vtkNew.
77template <class T>
78class vtkNew;
79
80// This allows vtkObjectBase to get at the methods it needs.
81class vtkObjectBaseToGarbageCollectorFriendship;
82
83class VTKCOMMONCORE_EXPORT vtkGarbageCollector : public vtkObject
84{
85public:
87 void PrintSelf(ostream& os, vtkIndent indent) override;
89
102 static void Collect();
103
115 static void Collect(vtkObjectBase* root);
116
118
127
129
133 static void SetGlobalDebugFlag(bool flag);
134 static bool GetGlobalDebugFlag();
136
137protected:
140
141private:
152 static vtkTypeBool GiveReference(vtkObjectBase* obj);
153
162 static vtkTypeBool TakeReference(vtkObjectBase* obj);
163
164 // Singleton management functions.
165 static void ClassInitialize();
166 static void ClassFinalize();
167
170
171 // Internal report callback and friend function that calls it.
172 virtual void Report(vtkObjectBase* obj, void* ptr, const char* desc);
173 friend void VTKCOMMONCORE_EXPORT vtkGarbageCollectorReportInternal(
174 vtkGarbageCollector*, vtkObjectBase*, void*, const char*);
175
177 void operator=(const vtkGarbageCollector&) = delete;
178};
179
181
185void VTKCOMMONCORE_EXPORT vtkGarbageCollectorReport(
186 vtkGarbageCollector* collector, vtkSmartPointerBase& ptr, const char* desc);
187
191template <class T>
192void vtkGarbageCollectorReport(vtkGarbageCollector* collector, vtkNew<T>& ptr, const char* desc)
193{
195 collector, ptr.Object, reinterpret_cast<void*>(&ptr.Object), desc);
196}
197
201template <class T>
202void vtkGarbageCollectorReport(vtkGarbageCollector* collector, T*& ptr, const char* desc)
203{
204 vtkGarbageCollectorReportInternal(collector, ptr, reinterpret_cast<void*>(&ptr), desc);
205}
206
207VTK_ABI_NAMESPACE_END
208#endif
Detect and break reference loops.
static vtkGarbageCollector * New()
friend class vtkGarbageCollectorManager
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
static void DeferredCollectionPop()
Push/Pop whether to do deferred collection.
static void DeferredCollectionPush()
Push/Pop whether to do deferred collection.
static void Collect(vtkObjectBase *root)
Collect immediately using the given object as the root for a reference graph walk.
static void SetGlobalDebugFlag(bool flag)
Set/Get global garbage collection debugging flag.
~vtkGarbageCollector() override
static void Collect()
Collect immediately using any objects whose collection was previously deferred as a root for the refe...
friend class vtkObjectBaseToGarbageCollectorFriendship
static bool GetGlobalDebugFlag()
Set/Get global garbage collection debugging flag.
friend void VTKCOMMONCORE_EXPORT vtkGarbageCollectorReportInternal(vtkGarbageCollector *, vtkObjectBase *, void *, const char *)
a simple class to control print indentation
Definition vtkIndent.h:108
Allocate and hold a VTK object.
Definition vtkNew.h:167
friend class vtkNew
Definition vtkNew.h:170
abstract base class for most VTK objects
friend class vtkGarbageCollector
Some classes need to clear the reference counts manually due to the way they work.
Non-templated superclass for vtkSmartPointer.
int vtkTypeBool
Definition vtkABI.h:64
void VTKCOMMONCORE_EXPORT vtkGarbageCollectorReport(vtkGarbageCollector *collector, vtkSmartPointerBase &ptr, const char *desc)
Function to report a reference held by a smart pointer to a collector.
void VTKCOMMONCORE_EXPORT vtkGarbageCollectorReportInternal(vtkGarbageCollector *, vtkObjectBase *, void *, const char *)