VTK  9.1.20211230
vtkObjectBase.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkObjectBase.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
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 
60 class vtkGarbageCollectorToObjectBaseFriendship;
61 class vtkWeakPointerBase;
62 class vtkWeakPointerBaseToObjectBaseFriendship;
63 
64 // typedefs for malloc and free compatible replacement functions
65 typedef void* (*vtkMallocingFunction)(size_t);
66 typedef void* (*vtkReallocingFunction)(void*, size_t);
67 typedef void (*vtkFreeingFunction)(void*);
68 
69 class VTKCOMMONCORE_EXPORT vtkObjectBase
70 {
76  virtual const char* GetClassNameInternal() const { return "vtkObjectBase"; }
77 
78 public:
79 #ifdef VTK_WORKAROUND_WINDOWS_MANGLE
80 // Avoid windows name mangling.
81 #define GetClassNameA GetClassName
82 #define GetClassNameW GetClassName
83 #endif
84 
88  const char* GetClassName() const;
89 
90 #ifdef VTK_WORKAROUND_WINDOWS_MANGLE
91 #undef GetClassNameW
92 #undef GetClassNameA
93 
94  // Define possible mangled names.
95  const char* GetClassNameA() const;
96  const char* GetClassNameW() const;
97 
98 #endif
99 
105  static vtkTypeBool IsTypeOf(const char* name);
106 
112  virtual vtkTypeBool IsA(const char* name);
113 
123 
133 
139  virtual void Delete();
140 
148  virtual void FastDelete();
149 
154  static vtkObjectBase* New()
155  {
156  vtkObjectBase* o = new vtkObjectBase;
158  return o;
159  }
160 
161  // Called by implementations of vtkObject::New(). Centralized location for
162  // vtkDebugLeaks registration.
164 
165 #if defined(_WIN32) || defined(VTK_USE_MEMKIND)
166  // Take control of allocation to avoid dll boundary problems or to use memkind.
167  void* operator new(size_t tSize);
168  void operator delete(void* p);
169 #endif
170 
175  void Print(ostream& os);
176 
178 
184  virtual void PrintSelf(ostream& os, vtkIndent indent);
185  virtual void PrintHeader(ostream& os, vtkIndent indent);
186  virtual void PrintTrailer(ostream& os, vtkIndent indent);
188 
192  // XXX(virtual): VTK_DEPRECATED_IN_9_2_0("Override `UsesGarbageCollector()` instead")
193  virtual void Register(vtkObjectBase* o);
194 
200  // XXX(virtual): VTK_DEPRECATED_IN_9_2_0("Override `UsesGarbageCollector()` instead")
201  virtual void UnRegister(vtkObjectBase* o);
202 
204 
214  virtual bool UsesGarbageCollector() const { return false; }
216 
220  int GetReferenceCount() { return this->ReferenceCount; }
221 
225  void SetReferenceCount(int);
226 
233  static void SetMemkindDirectory(const char* directoryname);
234 
236 
241  static bool GetUsingMemkind();
243 
249  class VTKCOMMONCORE_EXPORT vtkMemkindRAII
250  {
251 #ifdef VTK_USE_MEMKIND
252  bool OriginalValue;
253 #endif
254 
255  public:
256  vtkMemkindRAII(bool newValue);
258  vtkMemkindRAII(vtkMemkindRAII const&) = default;
259 
260  private:
261  void Save(bool newValue);
262  void Restore();
263  };
264 
269  bool GetIsInMemkind() const;
270 
271 protected:
273  virtual ~vtkObjectBase();
274 
275  std::atomic<int32_t> ReferenceCount;
277 
278  // Internal Register/UnRegister implementation that accounts for
279  // possible garbage collection participation. The second argument
280  // indicates whether to participate in garbage collection.
283 
284  // See vtkGarbageCollector.h:
286 
287  // Call this to call from either malloc or memkind_malloc depending on current UsingMemkind
289  // Call this to call from either realloc or memkind_realloc depending on current UsingMemkind
291  // Call this to call from either free or memkind_free depending on instance's IsInMemkind
293  // Call this to unconditionally call memkind_free
295 
296  virtual void ObjectFinalize();
297 
298 private:
299  friend VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, vtkObjectBase& o);
300  friend class vtkGarbageCollectorToObjectBaseFriendship;
301  friend class vtkWeakPointerBaseToObjectBaseFriendship;
302 
303  friend class vtkMemkindRAII;
304  friend class vtkTDSCMemkindRAII;
305  static void SetUsingMemkind(bool);
306  bool IsInMemkind;
307  void SetIsInMemkind(bool);
308 
310 
314  friend class vtkInformationKey;
315  friend class vtkGarbageCollector;
316  void ClearReferenceCounts();
318 
319  friend class vtkDebugLeaks;
320  virtual const char* GetDebugClassName() const;
321 
322 protected:
324  void operator=(const vtkObjectBase&) {}
325 };
326 #endif
327 
328 // VTK-HeaderTest-Exclude: vtkObjectBase.h
identify memory leaks at program termination vtkDebugLeaks is used to report memory leaks at the exit...
Definition: vtkDebugLeaks.h:65
Detect and break reference loops.
a simple class to control print indentation
Definition: vtkIndent.h:113
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:70
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 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:225
int vtkTypeBool
Definition: vtkABI.h:69
void *(* vtkReallocingFunction)(void *, size_t)
Definition: vtkObjectBase.h:66
void *(* vtkMallocingFunction)(size_t)
Definition: vtkObjectBase.h:65
void(* vtkFreeingFunction)(void *)
Definition: vtkObjectBase.h:67
int vtkIdType
Definition: vtkType.h:332