VTK  9.3.0
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
43 #ifndef vtkObjectBase_h
44 #define vtkObjectBase_h
45 
46 // Semantics around vtkDebugLeaks usage has changed. Now just call
47 // vtkObjectBase::InitializeObjectBase() after creating an object with New().
48 // The object factory methods take care of this automatically.
49 #define VTK_HAS_INITIALIZE_OBJECT_BASE
50 
51 #include "vtkCommonCoreModule.h" // For export macro
52 #include "vtkFeatures.h" // for VTK_USE_MEMKIND
53 #include "vtkIndent.h"
54 #include "vtkSystemIncludes.h"
55 #include "vtkType.h"
56 
57 #include <atomic> // For std::atomic
58 #include <string>
59 
60 VTK_ABI_NAMESPACE_BEGIN
62 class vtkGarbageCollectorToObjectBaseFriendship;
63 class vtkWeakPointerBase;
64 class vtkWeakPointerBaseToObjectBaseFriendship;
65 
66 // typedefs for malloc and free compatible replacement functions
67 typedef void* (*vtkMallocingFunction)(size_t);
68 typedef void* (*vtkReallocingFunction)(void*, size_t);
69 typedef void (*vtkFreeingFunction)(void*);
70 
71 class VTKCOMMONCORE_EXPORT vtkObjectBase
72 {
78  virtual const char* GetClassNameInternal() const { return "vtkObjectBase"; }
79 
80 public:
81 #ifdef VTK_WORKAROUND_WINDOWS_MANGLE
82 // Avoid windows name mangling.
83 #define GetClassNameA GetClassName
84 #define GetClassNameW GetClassName
85 #endif
86 
90  const char* GetClassName() const;
91 
97 
98 #ifdef VTK_WORKAROUND_WINDOWS_MANGLE
99 #undef GetClassNameW
100 #undef GetClassNameA
101 
102  // Define possible mangled names.
103  const char* GetClassNameA() const;
104  const char* GetClassNameW() const;
105 
106 #endif
107 
113  static vtkTypeBool IsTypeOf(const char* name);
114 
120  virtual vtkTypeBool IsA(const char* name);
121 
131 
141 
147  virtual void Delete();
148 
156  virtual void FastDelete();
157 
162  static vtkObjectBase* New()
163  {
164  vtkObjectBase* o = new vtkObjectBase;
166  return o;
167  }
168 
169  // Called by implementations of vtkObject::New(). Centralized location for
170  // vtkDebugLeaks registration.
172 
173 #if defined(_WIN32) || defined(VTK_USE_MEMKIND)
174  // Take control of allocation to avoid dll boundary problems or to use memkind.
175  void* operator new(size_t tSize);
176  void operator delete(void* p);
177 #endif
178 
183  void Print(ostream& os);
184 
186 
192  virtual void PrintSelf(ostream& os, vtkIndent indent);
193  virtual void PrintHeader(ostream& os, vtkIndent indent);
194  virtual void PrintTrailer(ostream& os, vtkIndent indent);
196 
200  // XXX(virtual): VTK_DEPRECATED_IN_9_2_0("Override `UsesGarbageCollector()` instead")
201  virtual void Register(vtkObjectBase* o);
202 
208  // XXX(virtual): VTK_DEPRECATED_IN_9_2_0("Override `UsesGarbageCollector()` instead")
209  virtual void UnRegister(vtkObjectBase* o);
210 
212 
222  virtual bool UsesGarbageCollector() const { return false; }
224 
228  int GetReferenceCount() { return this->ReferenceCount; }
229 
233  void SetReferenceCount(int);
234 
241  static void SetMemkindDirectory(const char* directoryname);
242 
244 
249  static bool GetUsingMemkind();
251 
257  class VTKCOMMONCORE_EXPORT vtkMemkindRAII
258  {
259 #ifdef VTK_USE_MEMKIND
260  bool OriginalValue;
261 #endif
262 
263  public:
264  vtkMemkindRAII(bool newValue);
266  vtkMemkindRAII(vtkMemkindRAII const&) = default;
267 
268  private:
269  void Save(bool newValue);
270  void Restore();
271  };
272 
277  bool GetIsInMemkind() const;
278 
279 protected:
281  virtual ~vtkObjectBase();
282 
283  std::atomic<int32_t> ReferenceCount;
285 
286  // Internal Register/UnRegister implementation that accounts for
287  // possible garbage collection participation. The second argument
288  // indicates whether to participate in garbage collection.
291 
292  // See vtkGarbageCollector.h:
294 
295  // Call this to call from either malloc or memkind_malloc depending on current UsingMemkind
297  // Call this to call from either realloc or memkind_realloc depending on current UsingMemkind
299  // Call this to call from either free or memkind_free depending on instance's IsInMemkind
301  // Call this to unconditionally call memkind_free
303 
304  virtual void ObjectFinalize();
305 
306 private:
307  friend VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, vtkObjectBase& o);
308  friend class vtkGarbageCollectorToObjectBaseFriendship;
309  friend class vtkWeakPointerBaseToObjectBaseFriendship;
310 
311  friend class vtkMemkindRAII;
312  friend class vtkTDSCMemkindRAII;
313  static void SetUsingMemkind(bool);
314  bool IsInMemkind;
315  void SetIsInMemkind(bool);
316 
318 
322  friend class vtkInformationKey;
323  friend class vtkGarbageCollector;
324  void ClearReferenceCounts();
326 
327  friend class vtkDebugLeaks;
328  virtual const char* GetDebugClassName() const;
329 
330 protected:
332  void operator=(const vtkObjectBase&) {}
333 };
334 VTK_ABI_NAMESPACE_END
335 #endif
336 
337 // 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:38
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:72
static vtkFreeingFunction GetCurrentFreeFunction()
const char * GetClassName() const
Return the class name as a string.
virtual void PrintSelf(ostream &os, vtkIndent indent)
Methods invoked by print to print information about the object including superclasses.
static void SetMemkindDirectory(const char *directoryname)
The name of a directory, ideally mounted -o dax, to memory map an extended memory space within.
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()
static bool GetUsingMemkind()
A global state flag that controls whether vtkObjects are constructed in the usual way (the default) o...
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 std::string GetObjectDescription() const
The object description printed in messages and PrintSelf output.
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.
void SetReferenceCount(int)
Sets the reference count.
int GetReferenceCount()
Return the current reference count of this object.
std::atomic< int32_t > ReferenceCount
void InitializeObjectBase()
virtual void Register(vtkObjectBase *o)
Increase the reference count (mark as used by another object).
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 &)
friend VTKCOMMONCORE_EXPORT ostream & operator<<(ostream &os, vtkObjectBase &o)
Non-templated superclass for vtkWeakPointer.
@ name
Definition: vtkX3D.h:219
@ string
Definition: vtkX3D.h:490
int vtkTypeBool
Definition: vtkABI.h:64
void *(* vtkReallocingFunction)(void *, size_t)
Definition: vtkObjectBase.h:68
void *(* vtkMallocingFunction)(size_t)
Definition: vtkObjectBase.h:67
void(* vtkFreeingFunction)(void *)
Definition: vtkObjectBase.h:69
int vtkIdType
Definition: vtkType.h:315