VTK  9.3.20240418
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
53 #ifndef vtkObjectBase_h
54 #define vtkObjectBase_h
55 
56 // Semantics around vtkDebugLeaks usage has changed. Now just call
57 // vtkObjectBase::InitializeObjectBase() after creating an object with New().
58 // The object factory methods take care of this automatically.
59 #define VTK_HAS_INITIALIZE_OBJECT_BASE
60 
61 #include "vtkCommonCoreModule.h" // For export macro
62 #include "vtkDeprecation.h"
63 #include "vtkFeatures.h" // for VTK_USE_MEMKIND
64 #include "vtkIndent.h"
65 #include "vtkSystemIncludes.h"
66 #include "vtkType.h"
67 #include "vtkWrappingHints.h" // For VTK_MARSHALAUTO
68 
69 #include <atomic> // For std::atomic
70 #include <string>
71 
72 VTK_ABI_NAMESPACE_BEGIN
74 class vtkGarbageCollectorToObjectBaseFriendship;
75 class vtkWeakPointerBase;
76 class vtkWeakPointerBaseToObjectBaseFriendship;
77 
78 // typedefs for malloc and free compatible replacement functions
79 typedef void* (*vtkMallocingFunction)(size_t);
80 typedef void* (*vtkReallocingFunction)(void*, size_t);
81 typedef void (*vtkFreeingFunction)(void*);
82 
83 class VTKCOMMONCORE_EXPORT VTK_MARSHALAUTO vtkObjectBase
84 {
90  virtual const char* GetClassNameInternal() const { return "vtkObjectBase"; }
91 
92 public:
93 #ifdef VTK_WORKAROUND_WINDOWS_MANGLE
94 // Avoid windows name mangling.
95 #define GetClassNameA GetClassName
96 #define GetClassNameW GetClassName
97 #endif
98 
102  VTK_MARSHALGETTER(ClassName)
103  const char* GetClassName() const;
104 
109  virtual std::string GetObjectDescription() const;
110 
111 #ifdef VTK_WORKAROUND_WINDOWS_MANGLE
112 #undef GetClassNameW
113 #undef GetClassNameA
114 
115  // Define possible mangled names.
116  const char* GetClassNameA() const;
117  const char* GetClassNameW() const;
118 
119 #endif
120 
126  static vtkTypeBool IsTypeOf(const char* name);
127 
133  virtual vtkTypeBool IsA(const char* name);
134 
144 
154 
160  virtual void Delete();
161 
169  virtual void FastDelete();
170 
175  static vtkObjectBase* New()
176  {
177  vtkObjectBase* o = new vtkObjectBase;
179  return o;
180  }
181 
182  // Called by implementations of vtkObject::New(). Centralized location for
183  // vtkDebugLeaks registration.
185 
186 #if defined(_WIN32) || defined(VTK_USE_MEMKIND)
187  // Take control of allocation to avoid dll boundary problems or to use memkind.
188  void* operator new(size_t tSize);
189  void operator delete(void* p);
190 #endif
191 
196  void Print(ostream& os);
197 
199 
205  virtual void PrintSelf(ostream& os, vtkIndent indent);
206  virtual void PrintHeader(ostream& os, vtkIndent indent);
207  virtual void PrintTrailer(ostream& os, vtkIndent indent);
209 
214 
220  // XXX(virtual): VTK_DEPRECATED_IN_9_2_0("Override `UsesGarbageCollector()` instead")
221  virtual void UnRegister(vtkObjectBase* o);
222 
224 
234  virtual bool UsesGarbageCollector() const { return false; }
236 
241  int GetReferenceCount() { return this->ReferenceCount; }
242 
247  void SetReferenceCount(int);
248 
255  static void SetMemkindDirectory(const char* directoryname);
256 
258 
263  static bool GetUsingMemkind();
265 
271  class VTKCOMMONCORE_EXPORT vtkMemkindRAII
272  {
273 #ifdef VTK_USE_MEMKIND
274  bool OriginalValue;
275 #endif
276 
277  public:
278  vtkMemkindRAII(bool newValue);
280  vtkMemkindRAII(vtkMemkindRAII const&) = default;
281 
282  private:
283  void Save(bool newValue);
284  void Restore();
285  };
286 
291  bool GetIsInMemkind() const;
292 
293 protected:
295  virtual ~vtkObjectBase();
296 
297  std::atomic<int32_t> ReferenceCount;
299 
300  // Internal Register/UnRegister implementation that accounts for
301  // possible garbage collection participation. The second argument
302  // indicates whether to participate in garbage collection.
305 
306  // See vtkGarbageCollector.h:
308 
309  // Call this to call from either malloc or memkind_malloc depending on current UsingMemkind
311  // Call this to call from either realloc or memkind_realloc depending on current UsingMemkind
313  // Call this to call from either free or memkind_free depending on instance's IsInMemkind
315  // Call this to unconditionally call memkind_free
317 
318  virtual void ObjectFinalize();
319 
320 private:
321  friend VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, vtkObjectBase& o);
322  friend class vtkGarbageCollectorToObjectBaseFriendship;
323  friend class vtkWeakPointerBaseToObjectBaseFriendship;
324 
325  friend class vtkMemkindRAII;
326  friend class vtkTDSCMemkindRAII;
327  static void SetUsingMemkind(bool);
328  bool IsInMemkind;
329  void SetIsInMemkind(bool);
330 
332 
336  friend class vtkInformationKey;
337  friend class vtkGarbageCollector;
338  void ClearReferenceCounts();
340 
341  friend class vtkDebugLeaks;
342  virtual const char* GetDebugClassName() const;
343 
344 protected:
346  void operator=(const vtkObjectBase&) {}
347 };
348 VTK_ABI_NAMESPACE_END
349 #endif
350 
351 // VTK-HeaderTest-Exclude: vtkObjectBase.h
identify memory leaks at program termination vtkDebugLeaks is used to report memory leaks at the exit...
Definition: vtkDebugLeaks.h:57
Detect and break reference loops.
a simple class to control print indentation
Definition: vtkIndent.h:108
Superclass for vtkInformation keys.
A class to help modify and restore the global UsingMemkind state, like SetUsingMemkind(newValue),...
vtkMemkindRAII(vtkMemkindRAII const &)=default
abstract base class for most VTK objects
Definition: vtkObjectBase.h:84
static vtkFreeingFunction GetCurrentFreeFunction()
virtual void PrintSelf(ostream &os, vtkIndent indent)
Methods invoked by print to print information about the object including superclasses.
virtual void ObjectFinalize()
static vtkObjectBase * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on.
static vtkIdType GetNumberOfGenerationsFromBaseType(const char *name)
Given a the name of a base class of this class type, return the distance of inheritance between this ...
virtual void PrintTrailer(ostream &os, vtkIndent indent)
Methods invoked by print to print information about the object including superclasses.
void operator=(const vtkObjectBase &)
static vtkMallocingFunction GetCurrentMallocFunction()
virtual void UnRegister(vtkObjectBase *o)
Decrease the reference count (release by another object).
static vtkReallocingFunction GetCurrentReallocFunction()
static vtkFreeingFunction GetAlternateFreeFunction()
virtual void UnRegisterInternal(vtkObjectBase *, vtkTypeBool check)
virtual vtkTypeBool IsA(const char *name)
Return 1 if this class is the same type of (or a subclass of) the named class.
virtual void RegisterInternal(vtkObjectBase *, vtkTypeBool check)
virtual void PrintHeader(ostream &os, vtkIndent indent)
Methods invoked by print to print information about the object including superclasses.
std::atomic< int32_t > ReferenceCount
void InitializeObjectBase()
vtkWeakPointerBase ** WeakPointers
virtual bool UsesGarbageCollector() const
Indicate whether the class uses vtkGarbageCollector or not.
virtual void Delete()
Delete a VTK object.
virtual ~vtkObjectBase()
virtual void ReportReferences(vtkGarbageCollector *)
virtual vtkIdType GetNumberOfGenerationsFromBase(const char *name)
Given the name of a base class of this class type, return the distance of inheritance between this cl...
static vtkTypeBool IsTypeOf(const char *name)
Return 1 if this class type is the same type of (or a subclass of) the named class.
virtual void FastDelete()
Delete a reference to this object.
void Print(ostream &os)
Print an object to an ostream.
bool GetIsInMemkind() const
A local state flag that remembers whether this object lives in the normal or extended memory space.
vtkObjectBase(const vtkObjectBase &)
void Register(vtkObjectBase *o)
Increase the reference count (mark as used by another object).
friend VTKCOMMONCORE_EXPORT ostream & operator<<(ostream &os, vtkObjectBase &o)
Non-templated superclass for vtkWeakPointer.
@ name
Definition: vtkX3D.h:219
int vtkTypeBool
Definition: vtkABI.h:64
void *(* vtkReallocingFunction)(void *, size_t)
Definition: vtkObjectBase.h:80
void *(* vtkMallocingFunction)(size_t)
Definition: vtkObjectBase.h:79
void(* vtkFreeingFunction)(void *)
Definition: vtkObjectBase.h:81
int vtkIdType
Definition: vtkType.h:315
#define VTK_MARSHALGETTER(property)
#define VTK_MARSHAL_EXCLUDE_REASON_IS_INTERNAL
#define VTK_MARSHALAUTO
#define VTK_MARSHALEXCLUDE(reason)