VTK  9.0.20200602
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 =========================================================================*/
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 
60 class vtkGarbageCollectorToObjectBaseFriendship;
61 class vtkWeakPointerBase;
62 class vtkWeakPointerBaseToObjectBaseFriendship;
63 
64 // typedefs for malloc and free compatible replacement functions
65 typedef void* (*vtkMallocingFunction)(size_t);
66 typedef void* (*vtkReallocingFunction)(void*, size_t);
67 typedef void (*vtkFreeingFunction)(void*);
68 
69 class VTKCOMMONCORE_EXPORT vtkObjectBase
70 {
76  virtual const char* GetClassNameInternal() const { return "vtkObjectBase"; }
77 
78 public:
79 #ifdef VTK_WORKAROUND_WINDOWS_MANGLE
80 // Avoid windows name mangling.
81 #define GetClassNameA GetClassName
82 #define GetClassNameW GetClassName
83 #endif
84 
88  const char* GetClassName() const;
89 
90 #ifdef VTK_WORKAROUND_WINDOWS_MANGLE
91 #undef GetClassNameW
92 #undef GetClassNameA
93 
94  // Define possible mangled names.
95  const char* GetClassNameA() const;
96  const char* GetClassNameW() const;
97 
98 #endif
99 
105  static vtkTypeBool IsTypeOf(const char* name);
106 
112  virtual vtkTypeBool IsA(const char* name);
113 
122  static vtkIdType GetNumberOfGenerationsFromBaseType(const char* name);
123 
132  virtual vtkIdType GetNumberOfGenerationsFromBase(const char* name);
133 
139  virtual void Delete();
140 
148  virtual void FastDelete();
149 
154  static vtkObjectBase* New()
155  {
156  vtkObjectBase* o = new vtkObjectBase;
158  return o;
159  }
160 
161  // Called by implementations of vtkObject::New(). Centralized location for
162  // vtkDebugLeaks registration:
163  void InitializeObjectBase();
164 
165 #if defined(_WIN32) || defined(VTK_USE_MEMKIND)
166  // Take control of allocation to avoid dll boundary problems or to use memkind.
167  void* operator new(size_t tSize);
168  void operator delete(void* p);
169 #endif
170 
175  void Print(ostream& os);
176 
178 
184  virtual void PrintSelf(ostream& os, vtkIndent indent);
185  virtual void PrintHeader(ostream& os, vtkIndent indent);
186  virtual void PrintTrailer(ostream& os, vtkIndent indent);
188 
192  virtual void Register(vtkObjectBase* o);
193 
199  virtual void UnRegister(vtkObjectBase* o);
200 
204  int GetReferenceCount() { return this->ReferenceCount; }
205 
209  void SetReferenceCount(int);
210 
217  static void SetMemkindDirectory(const char* directoryname);
218 
220 
225  static bool GetUsingMemkind();
227 
233  class VTKCOMMONCORE_EXPORT vtkMemkindRAII
234  {
235  bool OriginalValue;
236 
237  public:
238  vtkMemkindRAII(bool newValue);
239  ~vtkMemkindRAII();
240  vtkMemkindRAII(vtkMemkindRAII const&) = default;
241  };
242 
247  bool GetIsInMemkind() const;
248 
249 protected:
250  vtkObjectBase();
251  virtual ~vtkObjectBase();
252 
253  std::atomic<int32_t> ReferenceCount;
255 
256  // Internal Register/UnRegister implementation that accounts for
257  // possible garbage collection participation. The second argument
258  // indicates whether to participate in garbage collection.
259  virtual void RegisterInternal(vtkObjectBase*, vtkTypeBool check);
260  virtual void UnRegisterInternal(vtkObjectBase*, vtkTypeBool check);
261 
262  // See vtkGarbageCollector.h:
263  virtual void ReportReferences(vtkGarbageCollector*);
264 
265  // Call this to call from either malloc or memkind_malloc depending on current UsingMemkind
266  static vtkMallocingFunction GetCurrentMallocFunction();
267  // Call this to call from either realloc or memkind_realloc depending on current UsingMemkind
268  static vtkReallocingFunction GetCurrentReallocFunction();
269  // Call this to call from either free or memkind_free depending on instance's IsInMemkind
270  static vtkFreeingFunction GetCurrentFreeFunction();
271  // Call this to unconditionally call memkind_free
272  static vtkFreeingFunction GetAlternateFreeFunction();
273 
274 private:
275  friend VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, vtkObjectBase& o);
276  friend class vtkGarbageCollectorToObjectBaseFriendship;
277  friend class vtkWeakPointerBaseToObjectBaseFriendship;
278 
279  friend class vtkMemkindRAII;
280  friend class vtkTDSCMemkindRAII;
281  static void SetUsingMemkind(bool);
282  bool IsInMemkind;
283  void SetIsInMemkind(bool);
284 
285 protected:
287  void operator=(const vtkObjectBase&) {}
288 };
289 #endif
290 
291 // VTK-HeaderTest-Exclude: vtkObjectBase.h
vtkObjectBase::WeakPointers
vtkWeakPointerBase ** WeakPointers
Definition: vtkObjectBase.h:254
vtkObjectBase::operator=
void operator=(const vtkObjectBase &)
Definition: vtkObjectBase.h:287
vtkFreeingFunction
void(* vtkFreeingFunction)(void *)
Definition: vtkObjectBase.h:67
vtkIdType
int vtkIdType
Definition: vtkType.h:330
vtkObjectBase::New
static vtkObjectBase * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on.
Definition: vtkObjectBase.h:154
vtkObjectBase::ReferenceCount
std::atomic< int32_t > ReferenceCount
Definition: vtkObjectBase.h:253
vtkObjectBase::InitializeObjectBase
void InitializeObjectBase()
vtkType.h
vtx::helper::Print
void Print(const std::vector< T > &input, const std::string &name)
Print a vector with an associated name.
vtkObjectBase::GetReferenceCount
int GetReferenceCount()
Return the current reference count of this object.
Definition: vtkObjectBase.h:204
vtkObjectBase::vtkObjectBase
vtkObjectBase(const vtkObjectBase &)
Definition: vtkObjectBase.h:286
vtkMallocingFunction
void *(* vtkMallocingFunction)(size_t)
Definition: vtkObjectBase.h:65
vtkObjectBase
abstract base class for most VTK objects
Definition: vtkObjectBase.h:69
vtkWeakPointerBase
Non-templated superclass for vtkWeakPointer.
Definition: vtkWeakPointerBase.h:33
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:33
operator<<
VTKCOMMONCORE_EXPORT ostream & operator<<(ostream &os, const vtkIndent &o)
vtkX3D::name
Definition: vtkX3D.h:225
vtkGarbageCollector
Detect and break reference loops.
Definition: vtkGarbageCollector.h:94
vtkIndent.h
vtkObjectBase::vtkMemkindRAII
A class to help modify and restore the global UsingMemkind state, like SetUsingMemkind(newValue),...
Definition: vtkObjectBase.h:233
vtkReallocingFunction
void *(* vtkReallocingFunction)(void *, size_t)
Definition: vtkObjectBase.h:66
vtkTypeBool
int vtkTypeBool
Definition: vtkABI.h:69
vtkSystemIncludes.h