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=========================================================================*/
55#ifndef vtkObjectBase_h
56#define vtkObjectBase_h
57
58// Semantics around vtkDebugLeaks usage has changed. Now just call
59// vtkObjectBase::InitializeObjectBase() after creating an object with New().
60// The object factory methods take care of this automatically.
61#define VTK_HAS_INITIALIZE_OBJECT_BASE
62
63#include "vtkCommonCoreModule.h" // For export macro
64#include "vtkFeatures.h" // for VTK_USE_MEMKIND
65#include "vtkIndent.h"
66#include "vtkSystemIncludes.h"
67#include "vtkType.h"
68
69#include <atomic> // For std::atomic
70#include <string>
71
73class vtkGarbageCollectorToObjectBaseFriendship;
75class vtkWeakPointerBaseToObjectBaseFriendship;
76
77// typedefs for malloc and free compatible replacement functions
78typedef void* (*vtkMallocingFunction)(size_t);
79typedef void* (*vtkReallocingFunction)(void*, size_t);
80typedef void (*vtkFreeingFunction)(void*);
81
82class VTKCOMMONCORE_EXPORT vtkObjectBase
83{
89 virtual const char* GetClassNameInternal() const { return "vtkObjectBase"; }
90
91public:
92#ifdef VTK_WORKAROUND_WINDOWS_MANGLE
93// Avoid windows name mangling.
94#define GetClassNameA GetClassName
95#define GetClassNameW GetClassName
96#endif
97
101 const char* GetClassName() const;
102
107 virtual std::string GetObjectDescription() const;
108
109#ifdef VTK_WORKAROUND_WINDOWS_MANGLE
110#undef GetClassNameW
111#undef GetClassNameA
112
113 // Define possible mangled names.
114 const char* GetClassNameA() const;
115 const char* GetClassNameW() const;
116
117#endif
118
124 static vtkTypeBool IsTypeOf(const char* name);
125
131 virtual vtkTypeBool IsA(const char* name);
132
142
151 virtual vtkIdType GetNumberOfGenerationsFromBase(const char* name);
152
158 virtual void Delete();
159
167 virtual void FastDelete();
168
174 {
177 return o;
178 }
179
180 // Called by implementations of vtkObject::New(). Centralized location for
181 // vtkDebugLeaks registration.
183
184#if defined(_WIN32) || defined(VTK_USE_MEMKIND)
185 // Take control of allocation to avoid dll boundary problems or to use memkind.
186 void* operator new(size_t tSize);
187 void operator delete(void* p);
188#endif
189
194 void Print(ostream& os);
195
197
203 virtual void PrintSelf(ostream& os, vtkIndent indent);
204 virtual void PrintHeader(ostream& os, vtkIndent indent);
205 virtual void PrintTrailer(ostream& os, vtkIndent indent);
207
211 // XXX(virtual): VTK_DEPRECATED_IN_9_2_0("Override `UsesGarbageCollector()` instead")
212 virtual void Register(vtkObjectBase* o);
213
219 // XXX(virtual): VTK_DEPRECATED_IN_9_2_0("Override `UsesGarbageCollector()` instead")
220 virtual void UnRegister(vtkObjectBase* o);
221
223
233 virtual bool UsesGarbageCollector() const { return false; }
235
239 int GetReferenceCount() { return this->ReferenceCount; }
240
245
252 static void SetMemkindDirectory(const char* directoryname);
253
255
260 static bool GetUsingMemkind();
262
268 class VTKCOMMONCORE_EXPORT vtkMemkindRAII
269 {
270#ifdef VTK_USE_MEMKIND
271 bool OriginalValue;
272#endif
273
274 public:
275 vtkMemkindRAII(bool newValue);
278
279 private:
280 void Save(bool newValue);
281 void Restore();
282 };
283
288 bool GetIsInMemkind() const;
289
290protected:
292 virtual ~vtkObjectBase();
293
294 std::atomic<int32_t> ReferenceCount;
296
297 // Internal Register/UnRegister implementation that accounts for
298 // possible garbage collection participation. The second argument
299 // indicates whether to participate in garbage collection.
302
303 // See vtkGarbageCollector.h:
305
306 // Call this to call from either malloc or memkind_malloc depending on current UsingMemkind
308 // Call this to call from either realloc or memkind_realloc depending on current UsingMemkind
310 // Call this to call from either free or memkind_free depending on instance's IsInMemkind
312 // Call this to unconditionally call memkind_free
314
315 virtual void ObjectFinalize();
316
317private:
318 friend VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, vtkObjectBase& o);
319 friend class vtkGarbageCollectorToObjectBaseFriendship;
320 friend class vtkWeakPointerBaseToObjectBaseFriendship;
321
322 friend class vtkMemkindRAII;
323 friend class vtkTDSCMemkindRAII;
324 static void SetUsingMemkind(bool);
325 bool IsInMemkind;
326 void SetIsInMemkind(bool);
327
329
333 friend class vtkInformationKey;
335 void ClearReferenceCounts();
337
338 friend class vtkDebugLeaks;
339 virtual const char* GetDebugClassName() const;
340
341protected:
343 void operator=(const vtkObjectBase&) {}
344};
345#endif
346
347// 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:49
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.
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).
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()
virtual void Register(vtkObjectBase *o)
Increase the reference count (mark as used by another object).
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.
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