VTK  9.2.6
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 =========================================================================*/
46 #ifndef vtkObjectBase_h
47 #define vtkObjectBase_h
48 
49 // Semantics around vtkDebugLeaks usage has changed. Now just call
50 // vtkObjectBase::InitializeObjectBase() after creating an object with New().
51 // The object factory methods take care of this automatically.
52 #define VTK_HAS_INITIALIZE_OBJECT_BASE
53 
54 #include "vtkCommonCoreModule.h" // For export macro
55 #include "vtkFeatures.h" // for VTK_USE_MEMKIND
56 #include "vtkIndent.h"
57 #include "vtkSystemIncludes.h"
58 #include "vtkType.h"
59 
60 #include <atomic> // For std::atomic
61 #include <string>
62 
64 class vtkGarbageCollectorToObjectBaseFriendship;
65 class vtkWeakPointerBase;
66 class vtkWeakPointerBaseToObjectBaseFriendship;
67 
68 // typedefs for malloc and free compatible replacement functions
69 typedef void* (*vtkMallocingFunction)(size_t);
70 typedef void* (*vtkReallocingFunction)(void*, size_t);
71 typedef void (*vtkFreeingFunction)(void*);
72 
73 class VTKCOMMONCORE_EXPORT vtkObjectBase
74 {
80  virtual const char* GetClassNameInternal() const { return "vtkObjectBase"; }
81 
82 public:
83 #ifdef VTK_WORKAROUND_WINDOWS_MANGLE
84 // Avoid windows name mangling.
85 #define GetClassNameA GetClassName
86 #define GetClassNameW GetClassName
87 #endif
88 
92  const char* GetClassName() const;
93 
99 
100 #ifdef VTK_WORKAROUND_WINDOWS_MANGLE
101 #undef GetClassNameW
102 #undef GetClassNameA
103 
104  // Define possible mangled names.
105  const char* GetClassNameA() const;
106  const char* GetClassNameW() const;
107 
108 #endif
109 
115  static vtkTypeBool IsTypeOf(const char* name);
116 
122  virtual vtkTypeBool IsA(const char* name);
123 
133 
143 
149  virtual void Delete();
150 
158  virtual void FastDelete();
159 
164  static vtkObjectBase* New()
165  {
166  vtkObjectBase* o = new vtkObjectBase;
168  return o;
169  }
170 
171  // Called by implementations of vtkObject::New(). Centralized location for
172  // vtkDebugLeaks registration.
174 
175 #if defined(_WIN32) || defined(VTK_USE_MEMKIND)
176  // Take control of allocation to avoid dll boundary problems or to use memkind.
177  void* operator new(size_t tSize);
178  void operator delete(void* p);
179 #endif
180 
185  void Print(ostream& os);
186 
188 
194  virtual void PrintSelf(ostream& os, vtkIndent indent);
195  virtual void PrintHeader(ostream& os, vtkIndent indent);
196  virtual void PrintTrailer(ostream& os, vtkIndent indent);
198 
202  // XXX(virtual): VTK_DEPRECATED_IN_9_2_0("Override `UsesGarbageCollector()` instead")
203  virtual void Register(vtkObjectBase* o);
204 
210  // XXX(virtual): VTK_DEPRECATED_IN_9_2_0("Override `UsesGarbageCollector()` instead")
211  virtual void UnRegister(vtkObjectBase* o);
212 
214 
224  virtual bool UsesGarbageCollector() const { return false; }
226 
230  int GetReferenceCount() { return this->ReferenceCount; }
231 
235  void SetReferenceCount(int);
236 
243  static void SetMemkindDirectory(const char* directoryname);
244 
246 
251  static bool GetUsingMemkind();
253 
259  class VTKCOMMONCORE_EXPORT vtkMemkindRAII
260  {
261 #ifdef VTK_USE_MEMKIND
262  bool OriginalValue;
263 #endif
264 
265  public:
266  vtkMemkindRAII(bool newValue);
268  vtkMemkindRAII(vtkMemkindRAII const&) = default;
269 
270  private:
271  void Save(bool newValue);
272  void Restore();
273  };
274 
279  bool GetIsInMemkind() const;
280 
281 protected:
283  virtual ~vtkObjectBase();
284 
285  std::atomic<int32_t> ReferenceCount;
287 
288  // Internal Register/UnRegister implementation that accounts for
289  // possible garbage collection participation. The second argument
290  // indicates whether to participate in garbage collection.
293 
294  // See vtkGarbageCollector.h:
296 
297  // Call this to call from either malloc or memkind_malloc depending on current UsingMemkind
299  // Call this to call from either realloc or memkind_realloc depending on current UsingMemkind
301  // Call this to call from either free or memkind_free depending on instance's IsInMemkind
303  // Call this to unconditionally call memkind_free
305 
306  virtual void ObjectFinalize();
307 
308 private:
309  friend VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, vtkObjectBase& o);
310  friend class vtkGarbageCollectorToObjectBaseFriendship;
311  friend class vtkWeakPointerBaseToObjectBaseFriendship;
312 
313  friend class vtkMemkindRAII;
314  friend class vtkTDSCMemkindRAII;
315  static void SetUsingMemkind(bool);
316  bool IsInMemkind;
317  void SetIsInMemkind(bool);
318 
320 
324  friend class vtkInformationKey;
325  friend class vtkGarbageCollector;
326  void ClearReferenceCounts();
328 
329  friend class vtkDebugLeaks;
330  virtual const char* GetDebugClassName() const;
331 
332 protected:
334  void operator=(const vtkObjectBase&) {}
335 };
336 #endif
337 
338 // VTK-HeaderTest-Exclude: vtkObjectBase.h
identify memory leaks at program termination vtkDebugLeaks is used to report memory leaks at the exit...
Definition: vtkDebugLeaks.h:68
Detect and break reference loops.
a simple class to control print indentation
Definition: vtkIndent.h:119
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:74
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:225
@ string
Definition: vtkX3D.h:496
int vtkTypeBool
Definition: vtkABI.h:69
void *(* vtkReallocingFunction)(void *, size_t)
Definition: vtkObjectBase.h:70
void *(* vtkMallocingFunction)(size_t)
Definition: vtkObjectBase.h:69
void(* vtkFreeingFunction)(void *)
Definition: vtkObjectBase.h:71
int vtkIdType
Definition: vtkType.h:332