VTK  9.7.20260805
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
33
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 "vtkDeprecation.h"
44#include "vtkFeatures.h" // for VTK_USE_MEMKIND
45#include "vtkIndent.h"
46#include "vtkSystemIncludes.h"
47#include "vtkType.h"
48#include "vtkWrappingHints.h" // For VTK_MARSHALAUTO
49
50#include <atomic> // For std::atomic
51#include <memory> // for std::weak_ptr
52#include <mutex> // for std::mutex
53#include <string>
54
55VTK_ABI_NAMESPACE_BEGIN
57class vtkGarbageCollectorToObjectBaseFriendship;
59class vtkWeakPointerBaseToObjectBaseFriendship;
60template <typename T>
61class vtkWeakPtr;
62
63// typedefs for malloc and free compatible replacement functions
64typedef void* (*vtkMallocingFunction)(size_t);
65typedef void* (*vtkReallocingFunction)(void*, size_t);
66typedef void (*vtkFreeingFunction)(void*);
67
68class VTKCOMMONCORE_EXPORT VTK_MARSHALAUTO vtkObjectBase
69{
75 virtual const char* GetClassNameInternal() const { return "vtkObjectBase"; }
76
77public:
78#ifdef VTK_WORKAROUND_WINDOWS_MANGLE
79// Avoid windows name mangling.
80#define GetClassNameA GetClassName
81#define GetClassNameW GetClassName
82#endif
83
87 VTK_MARSHALGETTER(ClassName)
88 const char* GetClassName() const;
89
94 virtual std::string GetObjectDescription() const;
95
96#ifdef VTK_WORKAROUND_WINDOWS_MANGLE
97#undef GetClassNameW
98#undef GetClassNameA
99
100 // Define possible mangled names.
101 const char* GetClassNameA() const;
102 const char* GetClassNameW() const;
103
104#endif
105
111 static vtkTypeBool IsTypeOf(const char* name);
112
118 virtual vtkTypeBool IsA(const char* name);
119
129
138 virtual vtkIdType GetNumberOfGenerationsFromBase(const char* name);
139
145 virtual void Delete();
146
154 virtual void FastDelete();
155
161 {
164 return o;
165 }
166
167 // Called by implementations of vtkObject::New(). Centralized location for
168 // vtkDebugLeaks registration.
170
171#if defined(_WIN32) || defined(VTK_USE_MEMKIND)
172 // Take control of allocation to avoid dll boundary problems or to use memkind.
173 void* operator new(size_t tSize);
174 void operator delete(void* p);
175#endif
176
181 void Print(ostream& os);
182
184
190 virtual void PrintSelf(ostream& os, vtkIndent indent);
191 virtual void PrintHeader(ostream& os, vtkIndent indent);
192 virtual void PrintTrailer(ostream& os, vtkIndent indent);
194
199
205 // XXX(virtual): VTK_DEPRECATED_IN_9_7_0("Override `UsesGarbageCollector()` instead")
206 virtual void UnRegister(vtkObjectBase* o);
207
209
219 virtual bool UsesGarbageCollector() const { return false; }
221
226 int GetReferenceCount() { return this->ReferenceCount; }
227
233
240 static void SetMemkindDirectory(const char* directoryname);
241
243
248 static bool GetUsingMemkind();
250
256 class VTKCOMMONCORE_EXPORT vtkMemkindRAII
257 {
258#ifdef VTK_USE_MEMKIND
259 bool OriginalValue;
260#endif
261
262 public:
263 vtkMemkindRAII(bool newValue);
266
267 private:
268 void Save(bool newValue);
269 void Restore();
270 };
271
276 bool GetIsInMemkind() const;
277
278protected:
280 virtual ~vtkObjectBase();
281
282 std::atomic<int32_t> ReferenceCount;
284
285 // Internal Register/UnRegister implementation that accounts for
286 // possible garbage collection participation. The second argument
287 // indicates whether to participate in garbage collection.
290
291 // See vtkGarbageCollector.h:
293
294 // Call this to call from either malloc or memkind_malloc depending on current UsingMemkind
296 // Call this to call from either realloc or memkind_realloc depending on current UsingMemkind
298 // Call this to call from either free or memkind_free depending on instance's IsInMemkind
300 // Call this to unconditionally call memkind_free
302
303 virtual void ObjectFinalize();
304
305private:
306 friend VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, vtkObjectBase& o);
309
310 friend class vtkMemkindRAII;
311 friend class vtkTDSCMemkindRAII;
312 static void SetUsingMemkind(bool);
313 bool IsInMemkind;
314 void SetIsInMemkind(bool);
315
317 template <typename T>
318 friend class vtkWeakPtr;
319 struct WeakControlBlock
320 {
321 WeakControlBlock(vtkObjectBase* obj)
322 : Object(obj)
323 {
324 }
325
326 vtkObjectBase* Object;
327 std::mutex Mutex;
328 };
329 bool TryUpgradeRegister(vtkObjectBase* o);
330 std::shared_ptr<WeakControlBlock> GetWeakControlBlock();
331 std::mutex WeakBlockMutex;
332 std::weak_ptr<WeakControlBlock> WeakBlock;
334
336
340 friend class vtkInformationKey;
342 void ClearReferenceCounts();
344
345 friend class vtkDebugLeaks;
346 virtual const char* GetDebugClassName() const;
347
348protected:
350 void operator=(const vtkObjectBase&) {}
351};
352VTK_ABI_NAMESPACE_END
353#endif
354
355// VTK-HeaderTest-Exclude: vtkObjectBase.h
Detect and break reference loops.
a simple class to control print indentation
Definition vtkIndent.h:29
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).
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.
friend class vtkWeakPtr
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.
a weak reference to a vtkObjectBase.
Definition vtkWeakPtr.h:52
int vtkTypeBool
Definition vtkABI.h:64
void *(* vtkMallocingFunction)(size_t)
void *(* vtkReallocingFunction)(void *, size_t)
void(* vtkFreeingFunction)(void *)
int vtkIdType
Definition vtkType.h:363
#define VTK_MARSHALGETTER(property)
#define VTK_MARSHAL_EXCLUDE_REASON_IS_INTERNAL
#define VTK_MARSHALAUTO
#define VTK_MARSHALEXCLUDE(reason)