VTK  9.3.1
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
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.
72 VTK_ABI_NAMESPACE_BEGIN
73 void VTKCOMMONCORE_EXPORT vtkGarbageCollectorReportInternal(
74  vtkGarbageCollector*, vtkObjectBase*, void*, const char*);
75 
76 // This allows vtkObjectBase to get at the methods it needs.
77 class vtkObjectBaseToGarbageCollectorFriendship;
78 
79 class VTKCOMMONCORE_EXPORT vtkGarbageCollector : public vtkObject
80 {
81 public:
83  void PrintSelf(ostream& os, vtkIndent indent) override;
84  static vtkGarbageCollector* New();
85 
98  static void Collect();
99 
111  static void Collect(vtkObjectBase* root);
112 
114 
120  static void DeferredCollectionPush();
121  static void DeferredCollectionPop();
123 
125 
129  static void SetGlobalDebugFlag(bool flag);
130  static bool GetGlobalDebugFlag();
132 
133 protected:
135  ~vtkGarbageCollector() override;
136 
137 private:
148  static vtkTypeBool GiveReference(vtkObjectBase* obj);
149 
158  static vtkTypeBool TakeReference(vtkObjectBase* obj);
159 
160  // Singleton management functions.
161  static void ClassInitialize();
162  static void ClassFinalize();
163 
165  friend class vtkObjectBaseToGarbageCollectorFriendship;
166 
167  // Internal report callback and friend function that calls it.
168  virtual void Report(vtkObjectBase* obj, void* ptr, const char* desc);
169  friend void VTKCOMMONCORE_EXPORT vtkGarbageCollectorReportInternal(
170  vtkGarbageCollector*, vtkObjectBase*, void*, const char*);
171 
172  vtkGarbageCollector(const vtkGarbageCollector&) = delete;
173  void operator=(const vtkGarbageCollector&) = delete;
174 };
175 
176 class vtkSmartPointerBase;
177 
181 void VTKCOMMONCORE_EXPORT vtkGarbageCollectorReport(
182  vtkGarbageCollector* collector, vtkSmartPointerBase& ptr, const char* desc);
183 
187 template <class T>
188 void vtkGarbageCollectorReport(vtkGarbageCollector* collector, T*& ptr, const char* desc)
189 {
190  vtkGarbageCollectorReportInternal(collector, ptr, &ptr, desc);
191 }
192 
193 VTK_ABI_NAMESPACE_END
194 #endif
abstract base class for most VTK objects
Definition: vtkObject.h:51
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
friend class vtkGarbageCollector
Some classes need to clear the reference counts manually due to the way they work.
Detect and break reference loops.
int vtkTypeBool
Definition: vtkABI.h:64
a simple class to control print indentation
Definition: vtkIndent.h:28
Manages the vtkGarbageCollector singleton.
abstract base class for most VTK objects
Definition: vtkObjectBase.h:62
Non-templated superclass for vtkSmartPointer.
void VTKCOMMONCORE_EXPORT vtkGarbageCollectorReportInternal(vtkGarbageCollector *, vtkObjectBase *, void *, const char *)
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
void VTKCOMMONCORE_EXPORT vtkGarbageCollectorReport(vtkGarbageCollector *collector, vtkSmartPointerBase &ptr, const char *desc)
Function to report a reference held by a smart pointer to a collector.