VTK  9.3.1
vtkObject.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
38 #ifndef vtkObject_h
39 #define vtkObject_h
40 
41 #include "vtkCommonCoreModule.h" // For export macro
42 #include "vtkObjectBase.h"
43 #include "vtkSetGet.h"
44 #include "vtkTimeStamp.h"
45 #include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
46 
47 VTK_ABI_NAMESPACE_BEGIN
48 class vtkSubjectHelper;
49 class vtkCommand;
50 
51 class VTKCOMMONCORE_EXPORT vtkObject : public vtkObjectBase
52 {
53 public:
54  vtkBaseTypeMacro(vtkObject, vtkObjectBase);
55 
60  static vtkObject* New();
61 
62 #ifdef _WIN32
63  // avoid dll boundary problems
64  void* operator new(size_t tSize);
65  void operator delete(void* p);
66 #endif
67 
71  virtual void DebugOn();
72 
76  virtual void DebugOff();
77 
81  bool GetDebug();
82 
86  void SetDebug(bool debugFlag);
87 
92  static void BreakOnError();
93 
100  virtual void Modified();
101 
105  virtual vtkMTimeType GetMTime();
106 
113  void PrintSelf(ostream& os, vtkIndent indent) override;
114 
116 
120  static void SetGlobalWarningDisplay(vtkTypeBool val);
123  static vtkTypeBool GetGlobalWarningDisplay();
125 
127 
139  unsigned long AddObserver(unsigned long event, vtkCommand*, float priority = 0.0f);
140  unsigned long AddObserver(const char* event, vtkCommand*, float priority = 0.0f);
141  vtkCommand* GetCommand(unsigned long tag);
142  void RemoveObserver(vtkCommand*);
143  void RemoveObservers(unsigned long event, vtkCommand*);
144  void RemoveObservers(const char* event, vtkCommand*);
145  vtkTypeBool HasObserver(unsigned long event, vtkCommand*);
146  vtkTypeBool HasObserver(const char* event, vtkCommand*);
148 
149  void RemoveObserver(unsigned long tag);
150  void RemoveObservers(unsigned long event);
151  void RemoveObservers(const char* event);
152  void RemoveAllObservers(); // remove every last one of them
153  vtkTypeBool HasObserver(unsigned long event);
154  vtkTypeBool HasObserver(const char* event);
155 
157 
182  template <class U, class T>
183  unsigned long AddObserver(
184  unsigned long event, U observer, void (T::*callback)(), float priority = 0.0f)
185  {
186  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
187  // callable is deleted when the observer is cleaned up (look at
188  // vtkObjectCommandInternal)
189  return this->AddTemplatedObserver(event, callable, priority);
190  }
191  template <class U, class T>
192  unsigned long AddObserver(unsigned long event, U observer,
193  void (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
194  {
195  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
196  // callable is deleted when the observer is cleaned up (look at
197  // vtkObjectCommandInternal)
198  return this->AddTemplatedObserver(event, callable, priority);
199  }
201 
203 
207  template <class U, class T>
208  unsigned long AddObserver(unsigned long event, U observer,
209  bool (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
210  {
211  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
212  // callable is deleted when the observer is cleaned up (look at
213  // vtkObjectCommandInternal)
214  return this->AddTemplatedObserver(event, callable, priority);
215  }
217 
219 
224  vtkTypeBool InvokeEvent(unsigned long event, void* callData);
225  vtkTypeBool InvokeEvent(const char* event, void* callData);
227 
228  vtkTypeBool InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); }
229  vtkTypeBool InvokeEvent(const char* event) { return this->InvokeEvent(event, nullptr); }
230 
232 
238  virtual void SetObjectName(const std::string& objectName);
239  virtual std::string GetObjectName() const;
241 
246  std::string GetObjectDescription() const override;
247 
248 protected:
249  vtkObject();
250  ~vtkObject() override;
251 
252  // See vtkObjectBase.h.
253  void RegisterInternal(vtkObjectBase*, vtkTypeBool check) override;
254  void UnRegisterInternal(vtkObjectBase*, vtkTypeBool check) override;
255 
256  bool Debug; // Enable debug messages
257  vtkTimeStamp MTime; // Keep track of modification time
258  vtkSubjectHelper* SubjectHelper; // List of observers on this object
259  std::string ObjectName; // Name of this object for reporting
260 
262 
270  void InternalGrabFocus(vtkCommand* mouseEvents, vtkCommand* keypressEvents = nullptr);
271  void InternalReleaseFocus();
273 
274 private:
275  vtkObject(const vtkObject&) = delete;
276  void operator=(const vtkObject&) = delete;
277 
285  class vtkClassMemberCallbackBase
286  {
287  public:
289 
292  virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
293  virtual ~vtkClassMemberCallbackBase() = default;
295  };
296 
298 
302  template <class T>
303  class vtkClassMemberHandlerPointer
304  {
305  public:
306  void operator=(vtkObjectBase* o)
307  {
308  // The cast is needed in case "o" has multi-inheritance,
309  // to offset the pointer to get the vtkObjectBase.
310  if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
311  {
312  // fallback to just using its vtkObjectBase as-is.
313  this->VoidPointer = o;
314  }
315  this->WeakPointer = o;
316  this->UseWeakPointer = true;
317  }
318  void operator=(void* o)
319  {
320  this->VoidPointer = o;
321  this->WeakPointer = nullptr;
322  this->UseWeakPointer = false;
323  }
324  T* GetPointer()
325  {
326  if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
327  {
328  return nullptr;
329  }
330  return static_cast<T*>(this->VoidPointer);
331  }
332 
333  private:
334  vtkWeakPointerBase WeakPointer;
335  void* VoidPointer;
336  bool UseWeakPointer;
337  };
339 
341 
344  template <class T>
345  class vtkClassMemberCallback : public vtkClassMemberCallbackBase
346  {
347  vtkClassMemberHandlerPointer<T> Handler;
348  void (T::*Method1)();
349  void (T::*Method2)(vtkObject*, unsigned long, void*);
350  bool (T::*Method3)(vtkObject*, unsigned long, void*);
351 
352  public:
353  vtkClassMemberCallback(T* handler, void (T::*method)())
354  {
355  this->Handler = handler;
356  this->Method1 = method;
357  this->Method2 = nullptr;
358  this->Method3 = nullptr;
359  }
360 
361  vtkClassMemberCallback(T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
362  {
363  this->Handler = handler;
364  this->Method1 = nullptr;
365  this->Method2 = method;
366  this->Method3 = nullptr;
367  }
368 
369  vtkClassMemberCallback(T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
370  {
371  this->Handler = handler;
372  this->Method1 = nullptr;
373  this->Method2 = nullptr;
374  this->Method3 = method;
375  }
376  ~vtkClassMemberCallback() override = default;
377 
378  // Called when the event is invoked
379  bool operator()(vtkObject* caller, unsigned long event, void* calldata) override
380  {
381  T* handler = this->Handler.GetPointer();
382  if (handler)
383  {
384  if (this->Method1)
385  {
386  (handler->*this->Method1)();
387  }
388  else if (this->Method2)
389  {
390  (handler->*this->Method2)(caller, event, calldata);
391  }
392  else if (this->Method3)
393  {
394  return (handler->*this->Method3)(caller, event, calldata);
395  }
396  }
397  return false;
398  }
399  };
401 
403 
407  void ObjectFinalize() final;
409 
411 
414  unsigned long AddTemplatedObserver(
415  unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
416  // Friend to access AddTemplatedObserver().
417  friend class vtkObjectCommandInternal;
419 };
420 
421 VTK_ABI_NAMESPACE_END
422 #endif
423 // VTK-HeaderTest-Exclude: vtkObject.h
unsigned long AddObserver(unsigned long event, U observer, void(T::*callback)(vtkObject *, unsigned long, void *), float priority=0.0f)
Overloads to AddObserver that allow developers to add class member functions as callbacks for events...
Definition: vtkObject.h:192
static vtkObjectBase * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
abstract base class for most VTK objects
Definition: vtkObject.h:51
virtual void ObjectFinalize()
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:270
record modification and/or execution time
Definition: vtkTimeStamp.h:24
virtual std::string GetObjectDescription() const
The object description printed in messages and PrintSelf output.
virtual void PrintSelf(ostream &os, vtkIndent indent)
Methods invoked by print to print information about the object including superclasses.
static void GlobalWarningDisplayOff()
This is a global flag that controls whether any debug, warning or error messages are displayed...
Definition: vtkObject.h:122
std::string ObjectName
Definition: vtkObject.h:259
int vtkTypeBool
Definition: vtkABI.h:64
unsigned long AddObserver(unsigned long event, U observer, bool(T::*callback)(vtkObject *, unsigned long, void *), float priority=0.0f)
Allow user to set the AbortFlagOn() with the return value of the callback method. ...
Definition: vtkObject.h:208
superclass for callback/observer methods
Definition: vtkCommand.h:383
a simple class to control print indentation
Definition: vtkIndent.h:28
virtual void UnRegisterInternal(vtkObjectBase *, vtkTypeBool check)
Non-templated superclass for vtkWeakPointer.
abstract base class for most VTK objects
Definition: vtkObjectBase.h:62
virtual void RegisterInternal(vtkObjectBase *, vtkTypeBool check)
static void SetGlobalWarningDisplay(vtkTypeBool val)
This is a global flag that controls whether any debug, warning or error messages are displayed...
unsigned long AddObserver(unsigned long event, U observer, void(T::*callback)(), float priority=0.0f)
Overloads to AddObserver that allow developers to add class member functions as callbacks for events...
Definition: vtkObject.h:183
bool Debug
Definition: vtkObject.h:256
void operator=(const vtkObjectBase &)
vtkSubjectHelper * SubjectHelper
Definition: vtkObject.h:258
vtkTypeBool InvokeEvent(unsigned long event)
Definition: vtkObject.h:228
vtkTypeBool InvokeEvent(const char *event)
Definition: vtkObject.h:229
static void GlobalWarningDisplayOn()
This is a global flag that controls whether any debug, warning or error messages are displayed...
Definition: vtkObject.h:121
vtkTimeStamp MTime
Definition: vtkObject.h:257