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 
98  virtual std::string GetObjectDescription() const;
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 
132  static vtkIdType GetNumberOfGenerationsFromBaseType(const char* name);
133 
142  virtual vtkIdType GetNumberOfGenerationsFromBase(const char* name);
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.
173  void InitializeObjectBase();
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);
267  ~vtkMemkindRAII();
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:
282  vtkObjectBase();
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.
291  virtual void RegisterInternal(vtkObjectBase*, vtkTypeBool check);
292  virtual void UnRegisterInternal(vtkObjectBase*, vtkTypeBool check);
293 
294  // See vtkGarbageCollector.h:
295  virtual void ReportReferences(vtkGarbageCollector*);
296 
297  // Call this to call from either malloc or memkind_malloc depending on current UsingMemkind
298  static vtkMallocingFunction GetCurrentMallocFunction();
299  // Call this to call from either realloc or memkind_realloc depending on current UsingMemkind
300  static vtkReallocingFunction GetCurrentReallocFunction();
301  // Call this to call from either free or memkind_free depending on instance's IsInMemkind
302  static vtkFreeingFunction GetCurrentFreeFunction();
303  // Call this to unconditionally call memkind_free
304  static vtkFreeingFunction GetAlternateFreeFunction();
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
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:71
void Print(const std::vector< T > &input, const std::string &name)
Print a vector with an associated name.
int vtkIdType
Definition: vtkType.h:332
A class to help modify and restore the global UsingMemkind state, like SetUsingMemkind(newValue), but safer.
vtkObjectBase(const vtkObjectBase &)
int GetReferenceCount()
Return the current reference count of this object.
Detect and break reference loops.
int vtkTypeBool
Definition: vtkABI.h:69
Superclass for vtkInformation keys.
a simple class to control print indentation
Definition: vtkIndent.h:39
void *(* vtkMallocingFunction)(size_t)
Definition: vtkObjectBase.h:69
Non-templated superclass for vtkWeakPointer.
abstract base class for most VTK objects
Definition: vtkObjectBase.h:73
identify memory leaks at program termination vtkDebugLeaks is used to report memory leaks at the exit...
Definition: vtkDebugLeaks.h:67
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:70
vtkWeakPointerBase ** WeakPointers
void operator=(const vtkObjectBase &)