VTK  9.1.0
vtkObject.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkObject.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 =========================================================================*/
69 #ifndef vtkObject_h
70 #define vtkObject_h
71 
72 #include "vtkCommonCoreModule.h" // For export macro
73 #include "vtkObjectBase.h"
74 #include "vtkSetGet.h"
75 #include "vtkTimeStamp.h"
76 #include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
77 
78 class vtkSubjectHelper;
79 class vtkCommand;
80 
81 class VTKCOMMONCORE_EXPORT vtkObject : public vtkObjectBase
82 {
83 public:
85 
90  static vtkObject* New();
91 
92 #ifdef _WIN32
93  // avoid dll boundary problems
94  void* operator new(size_t tSize);
95  void operator delete(void* p);
96 #endif
97 
101  virtual void DebugOn();
102 
106  virtual void DebugOff();
107 
111  bool GetDebug();
112 
116  void SetDebug(bool debugFlag);
117 
122  static void BreakOnError();
123 
130  virtual void Modified();
131 
136 
143  void PrintSelf(ostream& os, vtkIndent indent) override;
144 
146 
150  static void SetGlobalWarningDisplay(int val);
155 
157 
169  unsigned long AddObserver(unsigned long event, vtkCommand*, float priority = 0.0f);
170  unsigned long AddObserver(const char* event, vtkCommand*, float priority = 0.0f);
171  vtkCommand* GetCommand(unsigned long tag);
173  void RemoveObservers(unsigned long event, vtkCommand*);
174  void RemoveObservers(const char* event, vtkCommand*);
175  vtkTypeBool HasObserver(unsigned long event, vtkCommand*);
176  vtkTypeBool HasObserver(const char* event, vtkCommand*);
178 
179  void RemoveObserver(unsigned long tag);
180  void RemoveObservers(unsigned long event);
181  void RemoveObservers(const char* event);
182  void RemoveAllObservers(); // remove every last one of them
183  vtkTypeBool HasObserver(unsigned long event);
184  vtkTypeBool HasObserver(const char* event);
185 
187 
212  template <class U, class T>
213  unsigned long AddObserver(
214  unsigned long event, U observer, void (T::*callback)(), float priority = 0.0f)
215  {
216  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
217  // callable is deleted when the observer is cleaned up (look at
218  // vtkObjectCommandInternal)
219  return this->AddTemplatedObserver(event, callable, priority);
220  }
221  template <class U, class T>
222  unsigned long AddObserver(unsigned long event, U observer,
223  void (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
224  {
225  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
226  // callable is deleted when the observer is cleaned up (look at
227  // vtkObjectCommandInternal)
228  return this->AddTemplatedObserver(event, callable, priority);
229  }
231 
233 
237  template <class U, class T>
238  unsigned long AddObserver(unsigned long event, U observer,
239  bool (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
240  {
241  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
242  // callable is deleted when the observer is cleaned up (look at
243  // vtkObjectCommandInternal)
244  return this->AddTemplatedObserver(event, callable, priority);
245  }
247 
249 
254  int InvokeEvent(unsigned long event, void* callData);
255  int InvokeEvent(const char* event, void* callData);
257 
258  int InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); }
259  int InvokeEvent(const char* event) { return this->InvokeEvent(event, nullptr); }
260 
261 protected:
263  ~vtkObject() override;
264 
265  // See vtkObjectBase.h.
268 
269  bool Debug; // Enable debug messages
270  vtkTimeStamp MTime; // Keep track of modification time
271  vtkSubjectHelper* SubjectHelper; // List of observers on this object
272 
274 
282  void InternalGrabFocus(vtkCommand* mouseEvents, vtkCommand* keypressEvents = nullptr);
285 
286 private:
287  vtkObject(const vtkObject&) = delete;
288  void operator=(const vtkObject&) = delete;
289 
297  class vtkClassMemberCallbackBase
298  {
299  public:
301 
304  virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
305  virtual ~vtkClassMemberCallbackBase() = default;
307  };
308 
310 
314  template <class T>
315  class vtkClassMemberHandlerPointer
316  {
317  public:
318  void operator=(vtkObjectBase* o)
319  {
320  // The cast is needed in case "o" has multi-inheritance,
321  // to offset the pointer to get the vtkObjectBase.
322  if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
323  {
324  // fallback to just using its vtkObjectBase as-is.
325  this->VoidPointer = o;
326  }
327  this->WeakPointer = o;
328  this->UseWeakPointer = true;
329  }
330  void operator=(void* o)
331  {
332  this->VoidPointer = o;
333  this->WeakPointer = nullptr;
334  this->UseWeakPointer = false;
335  }
336  T* GetPointer()
337  {
338  if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
339  {
340  return nullptr;
341  }
342  return static_cast<T*>(this->VoidPointer);
343  }
344 
345  private:
346  vtkWeakPointerBase WeakPointer;
347  void* VoidPointer;
348  bool UseWeakPointer;
349  };
351 
353 
356  template <class T>
357  class vtkClassMemberCallback : public vtkClassMemberCallbackBase
358  {
359  vtkClassMemberHandlerPointer<T> Handler;
360  void (T::*Method1)();
361  void (T::*Method2)(vtkObject*, unsigned long, void*);
362  bool (T::*Method3)(vtkObject*, unsigned long, void*);
363 
364  public:
365  vtkClassMemberCallback(T* handler, void (T::*method)())
366  {
367  this->Handler = handler;
368  this->Method1 = method;
369  this->Method2 = nullptr;
370  this->Method3 = nullptr;
371  }
372 
373  vtkClassMemberCallback(T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
374  {
375  this->Handler = handler;
376  this->Method1 = nullptr;
377  this->Method2 = method;
378  this->Method3 = nullptr;
379  }
380 
381  vtkClassMemberCallback(T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
382  {
383  this->Handler = handler;
384  this->Method1 = nullptr;
385  this->Method2 = nullptr;
386  this->Method3 = method;
387  }
388  ~vtkClassMemberCallback() override = default;
389 
390  // Called when the event is invoked
391  bool operator()(vtkObject* caller, unsigned long event, void* calldata) override
392  {
393  T* handler = this->Handler.GetPointer();
394  if (handler)
395  {
396  if (this->Method1)
397  {
398  (handler->*this->Method1)();
399  }
400  else if (this->Method2)
401  {
402  (handler->*this->Method2)(caller, event, calldata);
403  }
404  else if (this->Method3)
405  {
406  return (handler->*this->Method3)(caller, event, calldata);
407  }
408  }
409  return false;
410  }
411  };
413 
415 
418  unsigned long AddTemplatedObserver(
419  unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
420  // Friend to access AddTemplatedObserver().
421  friend class vtkObjectCommandInternal;
423 };
424 
425 #endif
426 // VTK-HeaderTest-Exclude: vtkObject.h
vtkCommand
superclass for callback/observer methods
Definition: vtkCommand.h:394
vtkObjectBase::operator=
void operator=(const vtkObjectBase &)
Definition: vtkObjectBase.h:293
vtkObject::RemoveObservers
void RemoveObservers(unsigned long event)
vtkObject::BreakOnError
static void BreakOnError()
This method is called when vtkErrorMacro executes.
vtkObject::vtkBaseTypeMacro
vtkBaseTypeMacro(vtkObject, vtkObjectBase)
vtkObjectBase.h
vtkTimeStamp.h
vtkObject::Debug
bool Debug
Definition: vtkObject.h:269
vtkObject::SetDebug
void SetDebug(bool debugFlag)
Set the value of the debug flag.
vtkObject::InvokeEvent
int InvokeEvent(unsigned long event, void *callData)
This method invokes an event and return whether the event was aborted or not.
vtkObject::~vtkObject
~vtkObject() override
vtkObject::HasObserver
vtkTypeBool HasObserver(unsigned long event)
vtkObject::New
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on.
vtkTimeStamp
record modification and/or execution time
Definition: vtkTimeStamp.h:52
vtkObject::DebugOn
virtual void DebugOn()
Turn debugging output on.
vtkObject::Modified
virtual void Modified()
Update the modification time for this object.
vtkObject::GetDebug
bool GetDebug()
Get the value of the debug flag.
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:82
vtkObject::RemoveObservers
void RemoveObservers(const char *event)
vtkObject::AddObserver
unsigned long AddObserver(unsigned long event, vtkCommand *, float priority=0.0f)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
vtkObject::SetGlobalWarningDisplay
static void SetGlobalWarningDisplay(int val)
This is a global flag that controls whether any debug, warning or error messages are displayed.
vtkObject::InternalGrabFocus
void InternalGrabFocus(vtkCommand *mouseEvents, vtkCommand *keypressEvents=nullptr)
These methods allow a command to exclusively grab all events.
vtkObject::UnRegisterInternal
void UnRegisterInternal(vtkObjectBase *, vtkTypeBool check) override
vtkObject::InternalReleaseFocus
void InternalReleaseFocus()
These methods allow a command to exclusively grab all events.
vtkObject::RemoveObserver
void RemoveObserver(unsigned long tag)
vtkWeakPointerBase.h
vtkObject::AddObserver
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:238
vtkObject::AddObserver
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:213
vtkObject::MTime
vtkTimeStamp MTime
Definition: vtkObject.h:270
vtkObject::RemoveObservers
void RemoveObservers(const char *event, vtkCommand *)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
vtkObject::GetMTime
virtual vtkMTimeType GetMTime()
Return this object's modified time.
vtkObjectBase
abstract base class for most VTK objects
Definition: vtkObjectBase.h:70
vtkObject::GlobalWarningDisplayOn
static void GlobalWarningDisplayOn()
This is a global flag that controls whether any debug, warning or error messages are displayed.
Definition: vtkObject.h:151
vtkWeakPointerBase
Non-templated superclass for vtkWeakPointer.
Definition: vtkWeakPointerBase.h:34
vtkObject::vtkObject
vtkObject()
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:113
vtkObject::HasObserver
vtkTypeBool HasObserver(const char *event)
vtkObject::GlobalWarningDisplayOff
static void GlobalWarningDisplayOff()
This is a global flag that controls whether any debug, warning or error messages are displayed.
Definition: vtkObject.h:152
vtkObject::HasObserver
vtkTypeBool HasObserver(const char *event, vtkCommand *)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
vtkObject::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkObject::GetCommand
vtkCommand * GetCommand(unsigned long tag)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
vtkObject::GetGlobalWarningDisplay
static int GetGlobalWarningDisplay()
This is a global flag that controls whether any debug, warning or error messages are displayed.
vtkObject::DebugOff
virtual void DebugOff()
Turn debugging output off.
vtkObject::AddObserver
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:222
vtkObject::InvokeEvent
int InvokeEvent(const char *event, void *callData)
This method invokes an event and return whether the event was aborted or not.
vtkObject::RemoveObserver
void RemoveObserver(vtkCommand *)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
vtkObject::InvokeEvent
int InvokeEvent(const char *event)
Definition: vtkObject.h:259
vtkObject::AddObserver
unsigned long AddObserver(const char *event, vtkCommand *, float priority=0.0f)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
vtkObject::InvokeEvent
int InvokeEvent(unsigned long event)
Definition: vtkObject.h:258
vtkObject::RemoveObservers
void RemoveObservers(unsigned long event, vtkCommand *)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
vtkObject::RemoveAllObservers
void RemoveAllObservers()
vtkObject::SubjectHelper
vtkSubjectHelper * SubjectHelper
Definition: vtkObject.h:271
vtkObject::RegisterInternal
void RegisterInternal(vtkObjectBase *, vtkTypeBool check) override
vtkX3D::priority
@ priority
Definition: vtkX3D.h:456
vtkTypeBool
int vtkTypeBool
Definition: vtkABI.h:69
vtkObject::HasObserver
vtkTypeBool HasObserver(unsigned long event, vtkCommand *)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
vtkMTimeType
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:287