VTK  9.6.20260313
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 <memory> // for std::weak_ptr
71#include <mutex> // for std::mutex
72#include <string>
73
74VTK_ABI_NAMESPACE_BEGIN
76class vtkGarbageCollectorToObjectBaseFriendship;
78class vtkWeakPointerBaseToObjectBaseFriendship;
79template <typename T>
80class vtkWeakPtr;
81
82// typedefs for malloc and free compatible replacement functions
83typedef void* (*vtkMallocingFunction)(size_t);
84typedef void* (*vtkReallocingFunction)(void*, size_t);
85typedef void (*vtkFreeingFunction)(void*);
86
87class VTKCOMMONCORE_EXPORT VTK_MARSHALAUTO vtkObjectBase
88{
94 virtual const char* GetClassNameInternal() const { return "vtkObjectBase"; }
95
96public:
97#ifdef VTK_WORKAROUND_WINDOWS_MANGLE
98// Avoid windows name mangling.
99#define GetClassNameA GetClassName
100#define GetClassNameW GetClassName
101#endif
102
106 VTK_MARSHALGETTER(ClassName)
107 const char* GetClassName() const;
108
113 virtual std::string GetObjectDescription() const;
114
115#ifdef VTK_WORKAROUND_WINDOWS_MANGLE
116#undef GetClassNameW
117#undef GetClassNameA
118
119 // Define possible mangled names.
120 const char* GetClassNameA() const;
121 const char* GetClassNameW() const;
122
123#endif
124
130 static vtkTypeBool IsTypeOf(const char* name);
131
137 virtual vtkTypeBool IsA(const char* name);
138
148
157 virtual vtkIdType GetNumberOfGenerationsFromBase(const char* name);
158
164 virtual void Delete();
165
173 virtual void FastDelete();
174
180 {
183 return o;
184 }
185
186 // Called by implementations of vtkObject::New(). Centralized location for
187 // vtkDebugLeaks registration.
189
190#if defined(_WIN32) || defined(VTK_USE_MEMKIND)
191 // Take control of allocation to avoid dll boundary problems or to use memkind.
192 void* operator new(size_t tSize);
193 void operator delete(void* p);
194#endif
195
200 void Print(ostream& os);
201
203
209 virtual void PrintSelf(ostream& os, vtkIndent indent);
210 virtual void PrintHeader(ostream& os, vtkIndent indent);
211 virtual void PrintTrailer(ostream& os, vtkIndent indent);
213
218
224 // XXX(virtual): VTK_DEPRECATED_IN_9_7_0("Override `UsesGarbageCollector()` instead")
225 virtual void UnRegister(vtkObjectBase* o);
226
228
238 virtual bool UsesGarbageCollector() const { return false; }
240
245 int GetReferenceCount() { return this->ReferenceCount; }
246
252
259 static void SetMemkindDirectory(const char* directoryname);
260
262
267 static bool GetUsingMemkind();
269
275 class VTKCOMMONCORE_EXPORT vtkMemkindRAII
276 {
277#ifdef VTK_USE_MEMKIND
278 bool OriginalValue;
279#endif
280
281 public:
282 vtkMemkindRAII(bool newValue);
285
286 private:
287 void Save(bool newValue);
288 void Restore();
289 };
290
295 bool GetIsInMemkind() const;
296
297protected:
299 virtual ~vtkObjectBase();
300
301 std::atomic<int32_t> ReferenceCount;
303
304 // Internal Register/UnRegister implementation that accounts for
305 // possible garbage collection participation. The second argument
306 // indicates whether to participate in garbage collection.
309
310 // See vtkGarbageCollector.h:
312
313 // Call this to call from either malloc or memkind_malloc depending on current UsingMemkind
315 // Call this to call from either realloc or memkind_realloc depending on current UsingMemkind
317 // Call this to call from either free or memkind_free depending on instance's IsInMemkind
319 // Call this to unconditionally call memkind_free
321
322 virtual void ObjectFinalize();
323
324private:
325 friend VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, vtkObjectBase& o);
328
329 friend class vtkMemkindRAII;
330 friend class vtkTDSCMemkindRAII;
331 static void SetUsingMemkind(bool);
332 bool IsInMemkind;
333 void SetIsInMemkind(bool);
334
336 template <typename T>
337 friend class vtkWeakPtr;
338 struct WeakControlBlock
339 {
340 WeakControlBlock(vtkObjectBase* obj)
341 : Object(obj)
342 {
343 }
344
345 vtkObjectBase* Object;
346 std::mutex Mutex;
347 };
348 bool TryUpgradeRegister(vtkObjectBase* o);
349 std::shared_ptr<WeakControlBlock> GetWeakControlBlock();
350 std::mutex WeakBlockMutex;
351 std::weak_ptr<WeakControlBlock> WeakBlock;
353
355
359 friend class vtkInformationKey;
361 void ClearReferenceCounts();
363
364 friend class vtkDebugLeaks;
365 virtual const char* GetDebugClassName() const;
366
367protected:
369 void operator=(const vtkObjectBase&) {}
370};
371VTK_ABI_NAMESPACE_END
372#endif
373
374// 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
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)