VTK  9.7.1
vtkObjectBase.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
34 #ifndef vtkObjectBase_h
35 #define vtkObjectBase_h
36 
37 // Semantics around vtkDebugLeaks usage has changed. Now just call
38 // vtkObjectBase::InitializeObjectBase() after creating an object with New().
39 // The object factory methods take care of this automatically.
40 #define VTK_HAS_INITIALIZE_OBJECT_BASE
41 
42 #include "vtkCommonCoreModule.h" // For export macro
43 #include "vtkDeprecation.h"
44 #include "vtkFeatures.h" // for VTK_USE_MEMKIND
45 #include "vtkIndent.h"
46 #include "vtkSystemIncludes.h"
47 #include "vtkType.h"
48 #include "vtkWrappingHints.h" // For VTK_MARSHALAUTO
49 
50 #include <atomic> // For std::atomic
51 #include <memory> // for std::weak_ptr
52 #include <mutex> // for std::mutex
53 #include <string>
54 
55 VTK_ABI_NAMESPACE_BEGIN
57 class vtkGarbageCollectorToObjectBaseFriendship;
58 class vtkWeakPointerBase;
59 class vtkWeakPointerBaseToObjectBaseFriendship;
60 template <typename T>
61 class vtkWeakPtr;
62 
63 // typedefs for malloc and free compatible replacement functions
64 typedef void* (*vtkMallocingFunction)(size_t);
65 typedef void* (*vtkReallocingFunction)(void*, size_t);
66 typedef void (*vtkFreeingFunction)(void*);
67 
68 class VTKCOMMONCORE_EXPORT VTK_MARSHALAUTO vtkObjectBase
69 {
75  virtual const char* GetClassNameInternal() const { return "vtkObjectBase"; }
76 
77 public:
78 #ifdef VTK_WORKAROUND_WINDOWS_MANGLE
79 // Avoid windows name mangling.
80 #define GetClassNameA GetClassName
81 #define GetClassNameW GetClassName
82 #endif
83 
87  VTK_MARSHALGETTER(ClassName)
88  const char* GetClassName() const;
89 
94  virtual std::string GetObjectDescription() const;
95 
96 #ifdef VTK_WORKAROUND_WINDOWS_MANGLE
97 #undef GetClassNameW
98 #undef GetClassNameA
99 
100  // Define possible mangled names.
101  const char* GetClassNameA() const;
102  const char* GetClassNameW() const;
103 
104 #endif
105 
111  static vtkTypeBool IsTypeOf(const char* name);
112 
118  virtual vtkTypeBool IsA(const char* name);
119 
128  static vtkIdType GetNumberOfGenerationsFromBaseType(const char* name);
129 
138  virtual vtkIdType GetNumberOfGenerationsFromBase(const char* name);
139 
145  virtual void Delete();
146 
154  virtual void FastDelete();
155 
160  static vtkObjectBase* New()
161  {
162  vtkObjectBase* o = new vtkObjectBase;
164  return o;
165  }
166 
167  // Called by implementations of vtkObject::New(). Centralized location for
168  // vtkDebugLeaks registration.
169  void InitializeObjectBase();
170 
171 #if defined(_WIN32) || defined(VTK_USE_MEMKIND)
172  // Take control of allocation to avoid dll boundary problems or to use memkind.
173  void* operator new(size_t tSize);
174  void operator delete(void* p);
175 #endif
176 
181  void Print(ostream& os);
182 
184 
190  virtual void PrintSelf(ostream& os, vtkIndent indent);
191  virtual void PrintHeader(ostream& os, vtkIndent indent);
192  virtual void PrintTrailer(ostream& os, vtkIndent indent);
194 
198  void Register(vtkObjectBase* o);
199 
205  // XXX(virtual): VTK_DEPRECATED_IN_9_7_0("Override `UsesGarbageCollector()` instead")
206  virtual void UnRegister(vtkObjectBase* o);
207 
209 
219  virtual bool UsesGarbageCollector() const { return false; }
221 
226  int GetReferenceCount() { return this->ReferenceCount; }
227 
232  void SetReferenceCount(int);
233 
240  static void SetMemkindDirectory(const char* directoryname);
241 
243 
248  static bool GetUsingMemkind();
250 
256  class VTKCOMMONCORE_EXPORT vtkMemkindRAII
257  {
258 #ifdef VTK_USE_MEMKIND
259  bool OriginalValue;
260 #endif
261 
262  public:
263  vtkMemkindRAII(bool newValue);
264  ~vtkMemkindRAII();
265  vtkMemkindRAII(vtkMemkindRAII const&) = default;
266 
267  private:
268  void Save(bool newValue);
269  void Restore();
270  };
271 
276  bool GetIsInMemkind() const;
277 
278 protected:
279  vtkObjectBase();
280  virtual ~vtkObjectBase();
281 
282  std::atomic<int32_t> ReferenceCount;
284 
285  // Internal Register/UnRegister implementation that accounts for
286  // possible garbage collection participation. The second argument
287  // indicates whether to participate in garbage collection.
288  virtual void RegisterInternal(vtkObjectBase*, vtkTypeBool check);
289  virtual void UnRegisterInternal(vtkObjectBase*, vtkTypeBool check);
290 
291  // See vtkGarbageCollector.h:
292  virtual void ReportReferences(vtkGarbageCollector*);
293 
294  // Call this to call from either malloc or memkind_malloc depending on current UsingMemkind
295  static vtkMallocingFunction GetCurrentMallocFunction();
296  // Call this to call from either realloc or memkind_realloc depending on current UsingMemkind
297  static vtkReallocingFunction GetCurrentReallocFunction();
298  // Call this to call from either free or memkind_free depending on instance's IsInMemkind
299  static vtkFreeingFunction GetCurrentFreeFunction();
300  // Call this to unconditionally call memkind_free
301  static vtkFreeingFunction GetAlternateFreeFunction();
302 
303  virtual void ObjectFinalize();
304 
305 private:
306  friend VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, vtkObjectBase& o);
307  friend class vtkGarbageCollectorToObjectBaseFriendship;
308  friend class vtkWeakPointerBaseToObjectBaseFriendship;
309 
310  friend class vtkMemkindRAII;
311  friend class vtkTDSCMemkindRAII;
312  static void SetUsingMemkind(bool);
313  bool IsInMemkind;
314  void SetIsInMemkind(bool);
315 
317  template <typename T>
318  friend class vtkWeakPtr;
319  struct WeakControlBlock
320  {
321  WeakControlBlock(vtkObjectBase* obj)
322  : Object(obj)
323  {
324  }
325 
326  vtkObjectBase* Object;
327  std::mutex Mutex;
328  };
329  bool TryUpgradeRegister(vtkObjectBase* o);
330  std::shared_ptr<WeakControlBlock> GetWeakControlBlock();
331  std::mutex WeakBlockMutex;
332  std::weak_ptr<WeakControlBlock> WeakBlock;
334 
336 
340  friend class vtkInformationKey;
341  friend class vtkGarbageCollector;
342  void ClearReferenceCounts();
344 
345  friend class vtkDebugLeaks;
346  virtual const char* GetDebugClassName() const;
347 
348 protected:
350  void operator=(const vtkObjectBase&) {}
351 };
352 VTK_ABI_NAMESPACE_END
353 #endif
354 
355 // VTK-HeaderTest-Exclude: vtkObjectBase.h
void InitializeObjectBase()
static vtkObjectBase * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
void(* vtkFreeingFunction)(void *)
Definition: vtkObjectBase.h:66
void Print(const std::vector< T > &input, const std::string &name)
Print a vector with an associated name.
int vtkIdType
Definition: vtkType.h:363
A class to help modify and restore the global UsingMemkind state, like SetUsingMemkind(newValue), but safer.
vtkObjectBase(const vtkObjectBase &)
Detect and break reference loops.
int vtkTypeBool
Definition: vtkABI.h:64
Superclass for vtkInformation keys.
a simple class to control print indentation
Definition: vtkIndent.h:28
#define VTK_MARSHALGETTER(property)
void *(* vtkMallocingFunction)(size_t)
Definition: vtkObjectBase.h:64
Non-templated superclass for vtkWeakPointer.
abstract base class for most VTK objects
Definition: vtkObjectBase.h:68
identify memory leaks at program termination vtkDebugLeaks is used to report memory leaks at the exit...
Definition: vtkDebugLeaks.h:58
#define VTK_MARSHALEXCLUDE(reason)
virtual bool UsesGarbageCollector() const
Indicate whether the class uses vtkGarbageCollector or not.
std::atomic< int32_t > ReferenceCount
VTKCOMMONCORE_EXPORT ostream & operator<<(ostream &os, const vtkIndent &o)
void *(* vtkReallocingFunction)(void *, size_t)
Definition: vtkObjectBase.h:65
#define VTK_MARSHALAUTO
vtkWeakPointerBase ** WeakPointers
void operator=(const vtkObjectBase &)
a weak reference to a vtkObjectBase.
Definition: vtkMeta.h:27
#define VTK_MARSHAL_EXCLUDE_REASON_IS_INTERNAL