VTK  9.2.6
vtkObjectBase.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkObjectBase.h
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14=========================================================================*/
42
43#ifndef vtkObjectBase_h
44#define vtkObjectBase_h
45
46// Semantics around vtkDebugLeaks usage has changed. Now just call
47// vtkObjectBase::InitializeObjectBase() after creating an object with New().
48// The object factory methods take care of this automatically.
49#define VTK_HAS_INITIALIZE_OBJECT_BASE
50
51#include "vtkCommonCoreModule.h" // For export macro
52#include "vtkFeatures.h" // for VTK_USE_MEMKIND
53#include "vtkIndent.h"
54#include "vtkSystemIncludes.h"
55#include "vtkType.h"
56
57#include <atomic> // For std::atomic
58#include <string>
59
61class vtkGarbageCollectorToObjectBaseFriendship;
63class vtkWeakPointerBaseToObjectBaseFriendship;
64
65// typedefs for malloc and free compatible replacement functions
66typedef void* (*vtkMallocingFunction)(size_t);
67typedef void* (*vtkReallocingFunction)(void*, size_t);
68typedef void (*vtkFreeingFunction)(void*);
69
70class VTKCOMMONCORE_EXPORT vtkObjectBase
71{
77 virtual const char* GetClassNameInternal() const { return "vtkObjectBase"; }
78
79public:
80#ifdef VTK_WORKAROUND_WINDOWS_MANGLE
81// Avoid windows name mangling.
82#define GetClassNameA GetClassName
83#define GetClassNameW GetClassName
84#endif
85
89 const char* GetClassName() const;
90
95 virtual std::string GetObjectDescription() const;
96
97#ifdef VTK_WORKAROUND_WINDOWS_MANGLE
98#undef GetClassNameW
99#undef GetClassNameA
100
101 // Define possible mangled names.
102 const char* GetClassNameA() const;
103 const char* GetClassNameW() const;
104
105#endif
106
112 static vtkTypeBool IsTypeOf(const char* name);
113
119 virtual vtkTypeBool IsA(const char* name);
120
130
139 virtual vtkIdType GetNumberOfGenerationsFromBase(const char* name);
140
146 virtual void Delete();
147
155 virtual void FastDelete();
156
162 {
165 return o;
166 }
167
168 // Called by implementations of vtkObject::New(). Centralized location for
169 // vtkDebugLeaks registration.
171
172#if defined(_WIN32) || defined(VTK_USE_MEMKIND)
173 // Take control of allocation to avoid dll boundary problems or to use memkind.
174 void* operator new(size_t tSize);
175 void operator delete(void* p);
176#endif
177
182 void Print(ostream& os);
183
185
191 virtual void PrintSelf(ostream& os, vtkIndent indent);
192 virtual void PrintHeader(ostream& os, vtkIndent indent);
193 virtual void PrintTrailer(ostream& os, vtkIndent indent);
195
199 // XXX(virtual): VTK_DEPRECATED_IN_9_2_0("Override `UsesGarbageCollector()` instead")
200 virtual void Register(vtkObjectBase* o);
201
207 // XXX(virtual): VTK_DEPRECATED_IN_9_2_0("Override `UsesGarbageCollector()` instead")
208 virtual void UnRegister(vtkObjectBase* o);
209
211
221 virtual bool UsesGarbageCollector() const { return false; }
223
227 int GetReferenceCount() { return this->ReferenceCount; }
228
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
321 friend class vtkInformationKey;
323 void ClearReferenceCounts();
325
326 friend class vtkDebugLeaks;
327 virtual const char* GetDebugClassName() const;
328
329protected:
331 void operator=(const vtkObjectBase&) {}
332};
333#endif
334
335// VTK-HeaderTest-Exclude: vtkObjectBase.h
Detect and break reference loops.
a simple class to control print indentation
Definition vtkIndent.h:34
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
virtual void Register(vtkObjectBase *o)
Increase the reference count (mark as used by another object).
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 &)
Non-templated superclass for vtkWeakPointer.
int vtkTypeBool
Definition vtkABI.h:69
void *(* vtkMallocingFunction)(size_t)
void *(* vtkReallocingFunction)(void *, size_t)
void(* vtkFreeingFunction)(void *)
int vtkIdType
Definition vtkType.h:332