VTK  9.4.20251203
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 "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 <string>
52
53VTK_ABI_NAMESPACE_BEGIN
55class vtkGarbageCollectorToObjectBaseFriendship;
57class vtkWeakPointerBaseToObjectBaseFriendship;
58
59// typedefs for malloc and free compatible replacement functions
60typedef void* (*vtkMallocingFunction)(size_t);
61typedef void* (*vtkReallocingFunction)(void*, size_t);
62typedef void (*vtkFreeingFunction)(void*);
63
64class VTKCOMMONCORE_EXPORT VTK_MARSHALAUTO vtkObjectBase
65{
71 virtual const char* GetClassNameInternal() const { return "vtkObjectBase"; }
72
73public:
74#ifdef VTK_WORKAROUND_WINDOWS_MANGLE
75// Avoid windows name mangling.
76#define GetClassNameA GetClassName
77#define GetClassNameW GetClassName
78#endif
79
83 VTK_MARSHALGETTER(ClassName)
84 const char* GetClassName() const;
85
90 virtual std::string GetObjectDescription() const;
91
92#ifdef VTK_WORKAROUND_WINDOWS_MANGLE
93#undef GetClassNameW
94#undef GetClassNameA
95
96 // Define possible mangled names.
97 const char* GetClassNameA() const;
98 const char* GetClassNameW() const;
99
100#endif
101
107 static vtkTypeBool IsTypeOf(const char* name);
108
114 virtual vtkTypeBool IsA(const char* name);
115
125
134 virtual vtkIdType GetNumberOfGenerationsFromBase(const char* name);
135
141 virtual void Delete();
142
150 virtual void FastDelete();
151
157 {
160 return o;
161 }
162
163 // Called by implementations of vtkObject::New(). Centralized location for
164 // vtkDebugLeaks registration.
166
167#if defined(_WIN32) || defined(VTK_USE_MEMKIND)
168 // Take control of allocation to avoid dll boundary problems or to use memkind.
169 void* operator new(size_t tSize);
170 void operator delete(void* p);
171#endif
172
177 void Print(ostream& os);
178
180
186 virtual void PrintSelf(ostream& os, vtkIndent indent);
187 virtual void PrintHeader(ostream& os, vtkIndent indent);
188 virtual void PrintTrailer(ostream& os, vtkIndent indent);
190
195
201 // XXX(virtual): VTK_DEPRECATED_IN_9_2_0("Override `UsesGarbageCollector()` instead")
202 virtual void UnRegister(vtkObjectBase* o);
203
205
215 virtual bool UsesGarbageCollector() const { return false; }
217
222 int GetReferenceCount() { return this->ReferenceCount; }
223
228 void SetReferenceCount(int);
229
236 static void SetMemkindDirectory(const char* directoryname);
237
239
244 static bool GetUsingMemkind();
246
252 class VTKCOMMONCORE_EXPORT vtkMemkindRAII
253 {
254#ifdef VTK_USE_MEMKIND
255 bool OriginalValue;
256#endif
257
258 public:
259 vtkMemkindRAII(bool newValue);
262
263 private:
264 void Save(bool newValue);
265 void Restore();
266 };
267
272 bool GetIsInMemkind() const;
273
274protected:
276 virtual ~vtkObjectBase();
277
278 std::atomic<int32_t> ReferenceCount;
280
281 // Internal Register/UnRegister implementation that accounts for
282 // possible garbage collection participation. The second argument
283 // indicates whether to participate in garbage collection.
286
287 // See vtkGarbageCollector.h:
289
290 // Call this to call from either malloc or memkind_malloc depending on current UsingMemkind
292 // Call this to call from either realloc or memkind_realloc depending on current UsingMemkind
294 // Call this to call from either free or memkind_free depending on instance's IsInMemkind
296 // Call this to unconditionally call memkind_free
298
299 virtual void ObjectFinalize();
300
301private:
302 friend VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, vtkObjectBase& o);
303 friend class vtkGarbageCollectorToObjectBaseFriendship;
304 friend class vtkWeakPointerBaseToObjectBaseFriendship;
305
306 friend class vtkMemkindRAII;
307 friend class vtkTDSCMemkindRAII;
308 static void SetUsingMemkind(bool);
309 bool IsInMemkind;
310 void SetIsInMemkind(bool);
311
313
317 friend class vtkInformationKey;
319 void ClearReferenceCounts();
321
322 friend class vtkDebugLeaks;
323 virtual const char* GetDebugClassName() const;
324
325protected:
327 void operator=(const vtkObjectBase&) {}
328};
329VTK_ABI_NAMESPACE_END
330#endif
331
332// 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.
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()
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 void RegisterInternal(vtkObjectBase *, vtkTypeBool check)
virtual void PrintHeader(ostream &os, vtkIndent indent)
Methods invoked by print to print information about the object including superclasses.
std::atomic< int32_t > ReferenceCount
void InitializeObjectBase()
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.
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:315
#define VTK_MARSHALGETTER(property)
#define VTK_MARSHAL_EXCLUDE_REASON_IS_INTERNAL
#define VTK_MARSHALAUTO
#define VTK_MARSHALEXCLUDE(reason)