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
34#ifndef vtkObjectBase_h
35#define vtkObjectBase_h
36
37// Semantics around vtkDebugLeaks usage has changed. Now just call
38// vtkObjectBase::InitializeObjectBase() after creating an object with New().
39// The object factory methods take care of this automatically.
40#define VTK_HAS_INITIALIZE_OBJECT_BASE
41
42#include "vtkCommonCoreModule.h" // For export macro
43#include "vtkFeatures.h" // for VTK_USE_MEMKIND
44#include "vtkIndent.h"
45#include "vtkSystemIncludes.h"
46#include "vtkType.h"
47
48#include <atomic> // For std::atomic
49#include <string>
50
51VTK_ABI_NAMESPACE_BEGIN
53class vtkGarbageCollectorToObjectBaseFriendship;
55class vtkWeakPointerBaseToObjectBaseFriendship;
56
57// typedefs for malloc and free compatible replacement functions
58typedef void* (*vtkMallocingFunction)(size_t);
59typedef void* (*vtkReallocingFunction)(void*, size_t);
60typedef void (*vtkFreeingFunction)(void*);
61
62class VTKCOMMONCORE_EXPORT vtkObjectBase
63{
69 virtual const char* GetClassNameInternal() const { return "vtkObjectBase"; }
70
71public:
72#ifdef VTK_WORKAROUND_WINDOWS_MANGLE
73// Avoid windows name mangling.
74#define GetClassNameA GetClassName
75#define GetClassNameW GetClassName
76#endif
77
81 const char* GetClassName() const;
82
87 virtual std::string GetObjectDescription() const;
88
89#ifdef VTK_WORKAROUND_WINDOWS_MANGLE
90#undef GetClassNameW
91#undef GetClassNameA
92
93 // Define possible mangled names.
94 const char* GetClassNameA() const;
95 const char* GetClassNameW() const;
96
97#endif
98
104 static vtkTypeBool IsTypeOf(const char* name);
105
111 virtual vtkTypeBool IsA(const char* name);
112
122
131 virtual vtkIdType GetNumberOfGenerationsFromBase(const char* name);
132
138 virtual void Delete();
139
147 virtual void FastDelete();
148
154 {
157 return o;
158 }
159
160 // Called by implementations of vtkObject::New(). Centralized location for
161 // vtkDebugLeaks registration.
163
164#if defined(_WIN32) || defined(VTK_USE_MEMKIND)
165 // Take control of allocation to avoid dll boundary problems or to use memkind.
166 void* operator new(size_t tSize);
167 void operator delete(void* p);
168#endif
169
174 void Print(ostream& os);
175
177
183 virtual void PrintSelf(ostream& os, vtkIndent indent);
184 virtual void PrintHeader(ostream& os, vtkIndent indent);
185 virtual void PrintTrailer(ostream& os, vtkIndent indent);
187
191 // XXX(virtual): VTK_DEPRECATED_IN_9_2_0("Override `UsesGarbageCollector()` instead")
192 virtual void Register(vtkObjectBase* o);
193
199 // XXX(virtual): VTK_DEPRECATED_IN_9_2_0("Override `UsesGarbageCollector()` instead")
200 virtual void UnRegister(vtkObjectBase* o);
201
203
213 virtual bool UsesGarbageCollector() const { return false; }
215
219 int GetReferenceCount() { return this->ReferenceCount; }
220
225
232 static void SetMemkindDirectory(const char* directoryname);
233
235
240 static bool GetUsingMemkind();
242
248 class VTKCOMMONCORE_EXPORT vtkMemkindRAII
249 {
250#ifdef VTK_USE_MEMKIND
251 bool OriginalValue;
252#endif
253
254 public:
255 vtkMemkindRAII(bool newValue);
258
259 private:
260 void Save(bool newValue);
261 void Restore();
262 };
263
268 bool GetIsInMemkind() const;
269
270protected:
272 virtual ~vtkObjectBase();
273
274 std::atomic<int32_t> ReferenceCount;
276
277 // Internal Register/UnRegister implementation that accounts for
278 // possible garbage collection participation. The second argument
279 // indicates whether to participate in garbage collection.
282
283 // See vtkGarbageCollector.h:
285
286 // Call this to call from either malloc or memkind_malloc depending on current UsingMemkind
288 // Call this to call from either realloc or memkind_realloc depending on current UsingMemkind
290 // Call this to call from either free or memkind_free depending on instance's IsInMemkind
292 // Call this to unconditionally call memkind_free
294
295 virtual void ObjectFinalize();
296
297private:
298 friend VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, vtkObjectBase& o);
299 friend class vtkGarbageCollectorToObjectBaseFriendship;
300 friend class vtkWeakPointerBaseToObjectBaseFriendship;
301
302 friend class vtkMemkindRAII;
303 friend class vtkTDSCMemkindRAII;
304 static void SetUsingMemkind(bool);
305 bool IsInMemkind;
306 void SetIsInMemkind(bool);
307
309
313 friend class vtkInformationKey;
315 void ClearReferenceCounts();
317
318 friend class vtkDebugLeaks;
319 virtual const char* GetDebugClassName() const;
320
321protected:
323 void operator=(const vtkObjectBase&) {}
324};
325VTK_ABI_NAMESPACE_END
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...
Detect and break reference loops.
a simple class to control print indentation
Definition vtkIndent.h:29
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
static vtkFreeingFunction GetCurrentFreeFunction()
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 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.
static vtkObjectBase * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on.
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
friend VTKCOMMONCORE_EXPORT ostream & operator<<(ostream &os, vtkObjectBase &o)
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.
const char * GetClassName() const
Return the class name as a string.
vtkObjectBase(const vtkObjectBase &)
Non-templated superclass for vtkWeakPointer.
int vtkTypeBool
Definition vtkABI.h:64
void *(* vtkMallocingFunction)(size_t)
void *(* vtkReallocingFunction)(void *, size_t)
void(* vtkFreeingFunction)(void *)
int vtkIdType
Definition vtkType.h:315