VTK  9.7.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 #include <mutex> // For `std::mutex` (cannot be forward-declared)
71 
72 // This function is a friend of the collector so that it can call the
73 // internal report method.
74 VTK_ABI_NAMESPACE_BEGIN
75 void VTKCOMMONCORE_EXPORT vtkGarbageCollectorReportInternal(
76  vtkGarbageCollector*, vtkObjectBase*, void*, const char*);
77 
78 // This forward ref allows us to define methods with vtkNew.
79 template <class T>
80 class vtkNew;
81 
82 // This allows vtkObjectBase to get at the methods it needs.
83 class vtkObjectBaseToGarbageCollectorFriendship;
84 
85 class VTKCOMMONCORE_EXPORT vtkGarbageCollector : public vtkObject
86 {
87 public:
89  void PrintSelf(ostream& os, vtkIndent indent) override;
90  static vtkGarbageCollector* New();
91 
104  static void Collect();
105 
117  static void Collect(vtkObjectBase* root);
118 
120 
126  static void DeferredCollectionPush();
127  static void DeferredCollectionPop();
129 
131 
135  static void SetGlobalDebugFlag(bool flag);
136  static bool GetGlobalDebugFlag();
138 
139 protected:
141  ~vtkGarbageCollector() override;
142 
143 private:
154  static vtkTypeBool GiveReference(vtkObjectBase* obj);
155 
164  static vtkTypeBool TakeReference(vtkObjectBase* obj);
165 
166  // Singleton management functions.
167  static void ClassInitialize();
168  static void ClassFinalize();
169 
171  friend class vtkObjectBaseToGarbageCollectorFriendship;
172 
173  static std::mutex* WeakPtrMutex();
174 
175  // Internal report callback and friend function that calls it.
176  virtual void Report(vtkObjectBase* obj, void* ptr, const char* desc);
177  friend void VTKCOMMONCORE_EXPORT vtkGarbageCollectorReportInternal(
178  vtkGarbageCollector*, vtkObjectBase*, void*, const char*);
179 
180  vtkGarbageCollector(const vtkGarbageCollector&) = delete;
181  void operator=(const vtkGarbageCollector&) = delete;
182 };
183 
184 class vtkSmartPointerBase;
185 
189 void VTKCOMMONCORE_EXPORT vtkGarbageCollectorReport(
190  vtkGarbageCollector* collector, vtkSmartPointerBase& ptr, const char* desc);
191 
195 template <class T>
196 void vtkGarbageCollectorReport(vtkGarbageCollector* collector, vtkNew<T>& ptr, const char* desc)
197 {
199  collector, ptr.Object, reinterpret_cast<void*>(&ptr.Object), desc);
200 }
201 
205 template <class T>
206 void vtkGarbageCollectorReport(vtkGarbageCollector* collector, T*& ptr, const char* desc)
207 {
208  vtkGarbageCollectorReportInternal(collector, ptr, reinterpret_cast<void*>(&ptr), desc);
209 }
210 
211 VTK_ABI_NAMESPACE_END
212 #endif
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.
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:68
Non-templated superclass for vtkSmartPointer.
Allocate and hold a VTK object.
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.