VTK  9.5.20251120
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
52
53#ifndef vtkObjectBase_h
54#define vtkObjectBase_h
55
56// Semantics around vtkDebugLeaks usage has changed. Now just call
57// vtkObjectBase::InitializeObjectBase() after creating an object with New().
58// The object factory methods take care of this automatically.
59#define VTK_HAS_INITIALIZE_OBJECT_BASE
60
61#include "vtkCommonCoreModule.h" // For export macro
62#include "vtkDeprecation.h"
63#include "vtkFeatures.h" // for VTK_USE_MEMKIND
64#include "vtkIndent.h"
65#include "vtkSystemIncludes.h"
66#include "vtkType.h"
67#include "vtkWrappingHints.h" // For VTK_MARSHALAUTO
68
69#include <atomic> // For std::atomic
70#include <string>
71
72VTK_ABI_NAMESPACE_BEGIN
74class vtkGarbageCollectorToObjectBaseFriendship;
76class vtkWeakPointerBaseToObjectBaseFriendship;
77
78// typedefs for malloc and free compatible replacement functions
79typedef void* (*vtkMallocingFunction)(size_t);
80typedef void* (*vtkReallocingFunction)(void*, size_t);
81typedef void (*vtkFreeingFunction)(void*);
82
83class VTKCOMMONCORE_EXPORT VTK_MARSHALAUTO vtkObjectBase
84{
90 virtual const char* GetClassNameInternal() const { return "vtkObjectBase"; }
91
92public:
93#ifdef VTK_WORKAROUND_WINDOWS_MANGLE
94// Avoid windows name mangling.
95#define GetClassNameA GetClassName
96#define GetClassNameW GetClassName
97#endif
98
102 VTK_MARSHALGETTER(ClassName)
103 const char* GetClassName() const;
104
109 virtual std::string GetObjectDescription() const;
110
111#ifdef VTK_WORKAROUND_WINDOWS_MANGLE
112#undef GetClassNameW
113#undef GetClassNameA
114
115 // Define possible mangled names.
116 const char* GetClassNameA() const;
117 const char* GetClassNameW() const;
118
119#endif
120
126 static vtkTypeBool IsTypeOf(const char* name);
127
133 virtual vtkTypeBool IsA(const char* name);
134
144
153 virtual vtkIdType GetNumberOfGenerationsFromBase(const char* name);
154
160 virtual void Delete();
161
169 virtual void FastDelete();
170
176 {
179 return o;
180 }
181
182 // Called by implementations of vtkObject::New(). Centralized location for
183 // vtkDebugLeaks registration.
185
186#if defined(_WIN32) || defined(VTK_USE_MEMKIND)
187 // Take control of allocation to avoid dll boundary problems or to use memkind.
188 void* operator new(size_t tSize);
189 void operator delete(void* p);
190#endif
191
196 void Print(ostream& os);
197
199
205 virtual void PrintSelf(ostream& os, vtkIndent indent);
206 virtual void PrintHeader(ostream& os, vtkIndent indent);
207 virtual void PrintTrailer(ostream& os, vtkIndent indent);
209
214
220 // XXX(virtual): VTK_DEPRECATED_IN_9_2_0("Override `UsesGarbageCollector()` instead")
221 virtual void UnRegister(vtkObjectBase* o);
222
224
234 virtual bool UsesGarbageCollector() const { return false; }
236
241 int GetReferenceCount() { return this->ReferenceCount; }
242
248
255 static void SetMemkindDirectory(const char* directoryname);
256
258
263 static bool GetUsingMemkind();
265
271 class VTKCOMMONCORE_EXPORT vtkMemkindRAII
272 {
273#ifdef VTK_USE_MEMKIND
274 bool OriginalValue;
275#endif
276
277 public:
278 vtkMemkindRAII(bool newValue);
281
282 private:
283 void Save(bool newValue);
284 void Restore();
285 };
286
291 bool GetIsInMemkind() const;
292
293protected:
295 virtual ~vtkObjectBase();
296
297 std::atomic<int32_t> ReferenceCount;
299
300 // Internal Register/UnRegister implementation that accounts for
301 // possible garbage collection participation. The second argument
302 // indicates whether to participate in garbage collection.
305
306 // See vtkGarbageCollector.h:
308
309 // Call this to call from either malloc or memkind_malloc depending on current UsingMemkind
311 // Call this to call from either realloc or memkind_realloc depending on current UsingMemkind
313 // Call this to call from either free or memkind_free depending on instance's IsInMemkind
315 // Call this to unconditionally call memkind_free
317
318 virtual void ObjectFinalize();
319
320private:
321 friend VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, vtkObjectBase& o);
324
325 friend class vtkMemkindRAII;
326 friend class vtkTDSCMemkindRAII;
327 static void SetUsingMemkind(bool);
328 bool IsInMemkind;
329 void SetIsInMemkind(bool);
330
332
336 friend class vtkInformationKey;
338 void ClearReferenceCounts();
340
341 friend class vtkDebugLeaks;
342 virtual const char* GetDebugClassName() const;
343
344protected:
346 void operator=(const vtkObjectBase&) {}
347};
348VTK_ABI_NAMESPACE_END
349#endif
350
351// VTK-HeaderTest-Exclude: vtkObjectBase.h
Detect and break reference loops.
a simple class to control print indentation
Definition vtkIndent.h:108
A class to help modify and restore the global UsingMemkind state, like SetUsingMemkind(newValue),...
vtkMemkindRAII(vtkMemkindRAII const &)=default
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).
friend class vtkDebugLeaks
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()
friend class vtkWeakPointerBaseToObjectBaseFriendship
friend class vtkInformationKey
Some classes need to clear the reference counts manually due to the way they work.
friend class vtkGarbageCollector
Some classes need to clear the reference counts manually due to the way they work.
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.
friend class vtkGarbageCollectorToObjectBaseFriendship
virtual ~vtkObjectBase()
virtual void ReportReferences(vtkGarbageCollector *)
friend class vtkTDSCMemkindRAII
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 &)
void Register(vtkObjectBase *o)
Increase the reference count (mark as used by another object).
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:367
#define VTK_MARSHALGETTER(property)
#define VTK_MARSHAL_EXCLUDE_REASON_IS_INTERNAL
#define VTK_MARSHALAUTO
#define VTK_MARSHALEXCLUDE(reason)