VTK  9.0.20210518
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 =========================================================================*/
50 #ifndef vtkObject_h
51 #define vtkObject_h
52 
53 #include "vtkCommonCoreModule.h" // For export macro
54 #include "vtkObjectBase.h"
55 #include "vtkSetGet.h"
56 #include "vtkTimeStamp.h"
57 #include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
58 
59 class vtkSubjectHelper;
60 class vtkCommand;
61 
62 class VTKCOMMONCORE_EXPORT vtkObject : public vtkObjectBase
63 {
64 public:
66 
71  static vtkObject* New();
72 
73 #ifdef _WIN32
74  // avoid dll boundary problems
75  void* operator new(size_t tSize);
76  void operator delete(void* p);
77 #endif
78 
82  virtual void DebugOn();
83 
87  virtual void DebugOff();
88 
92  bool GetDebug();
93 
97  void SetDebug(bool debugFlag);
98 
103  static void BreakOnError();
104 
111  virtual void Modified();
112 
117 
124  void PrintSelf(ostream& os, vtkIndent indent) override;
125 
127 
131  static void SetGlobalWarningDisplay(int val);
136 
138 
150  unsigned long AddObserver(unsigned long event, vtkCommand*, float priority = 0.0f);
151  unsigned long AddObserver(const char* event, vtkCommand*, float priority = 0.0f);
152  vtkCommand* GetCommand(unsigned long tag);
154  void RemoveObservers(unsigned long event, vtkCommand*);
155  void RemoveObservers(const char* event, vtkCommand*);
156  vtkTypeBool HasObserver(unsigned long event, vtkCommand*);
157  vtkTypeBool HasObserver(const char* event, vtkCommand*);
159 
160  void RemoveObserver(unsigned long tag);
161  void RemoveObservers(unsigned long event);
162  void RemoveObservers(const char* event);
163  void RemoveAllObservers(); // remove every last one of them
164  vtkTypeBool HasObserver(unsigned long event);
165  vtkTypeBool HasObserver(const char* event);
166 
168 
193  template <class U, class T>
194  unsigned long AddObserver(
195  unsigned long event, U observer, void (T::*callback)(), float priority = 0.0f)
196  {
197  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
198  // callable is deleted when the observer is cleaned up (look at
199  // vtkObjectCommandInternal)
200  return this->AddTemplatedObserver(event, callable, priority);
201  }
202  template <class U, class T>
203  unsigned long AddObserver(unsigned long event, U observer,
204  void (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
205  {
206  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
207  // callable is deleted when the observer is cleaned up (look at
208  // vtkObjectCommandInternal)
209  return this->AddTemplatedObserver(event, callable, priority);
210  }
212 
214 
218  template <class U, class T>
219  unsigned long AddObserver(unsigned long event, U observer,
220  bool (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
221  {
222  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
223  // callable is deleted when the observer is cleaned up (look at
224  // vtkObjectCommandInternal)
225  return this->AddTemplatedObserver(event, callable, priority);
226  }
228 
230 
235  int InvokeEvent(unsigned long event, void* callData);
236  int InvokeEvent(const char* event, void* callData);
238 
239  int InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); }
240  int InvokeEvent(const char* event) { return this->InvokeEvent(event, nullptr); }
241 
242 protected:
244  ~vtkObject() override;
245 
246  // See vtkObjectBase.h.
249 
250  bool Debug; // Enable debug messages
251  vtkTimeStamp MTime; // Keep track of modification time
252  vtkSubjectHelper* SubjectHelper; // List of observers on this object
253 
255 
263  void InternalGrabFocus(vtkCommand* mouseEvents, vtkCommand* keypressEvents = nullptr);
266 
267 private:
268  vtkObject(const vtkObject&) = delete;
269  void operator=(const vtkObject&) = delete;
270 
278  class vtkClassMemberCallbackBase
279  {
280  public:
282 
285  virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
286  virtual ~vtkClassMemberCallbackBase() = default;
288  };
289 
291 
295  template <class T>
296  class vtkClassMemberHandlerPointer
297  {
298  public:
299  void operator=(vtkObjectBase* o)
300  {
301  // The cast is needed in case "o" has multi-inheritance,
302  // to offset the pointer to get the vtkObjectBase.
303  if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
304  {
305  // fallback to just using its vtkObjectBase as-is.
306  this->VoidPointer = o;
307  }
308  this->WeakPointer = o;
309  this->UseWeakPointer = true;
310  }
311  void operator=(void* o)
312  {
313  this->VoidPointer = o;
314  this->WeakPointer = nullptr;
315  this->UseWeakPointer = false;
316  }
317  T* GetPointer()
318  {
319  if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
320  {
321  return nullptr;
322  }
323  return static_cast<T*>(this->VoidPointer);
324  }
325 
326  private:
327  vtkWeakPointerBase WeakPointer;
328  void* VoidPointer;
329  bool UseWeakPointer;
330  };
332 
334 
337  template <class T>
338  class vtkClassMemberCallback : public vtkClassMemberCallbackBase
339  {
340  vtkClassMemberHandlerPointer<T> Handler;
341  void (T::*Method1)();
342  void (T::*Method2)(vtkObject*, unsigned long, void*);
343  bool (T::*Method3)(vtkObject*, unsigned long, void*);
344 
345  public:
346  vtkClassMemberCallback(T* handler, void (T::*method)())
347  {
348  this->Handler = handler;
349  this->Method1 = method;
350  this->Method2 = nullptr;
351  this->Method3 = nullptr;
352  }
353 
354  vtkClassMemberCallback(T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
355  {
356  this->Handler = handler;
357  this->Method1 = nullptr;
358  this->Method2 = method;
359  this->Method3 = nullptr;
360  }
361 
362  vtkClassMemberCallback(T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
363  {
364  this->Handler = handler;
365  this->Method1 = nullptr;
366  this->Method2 = nullptr;
367  this->Method3 = method;
368  }
369  ~vtkClassMemberCallback() override = default;
370 
371  // Called when the event is invoked
372  bool operator()(vtkObject* caller, unsigned long event, void* calldata) override
373  {
374  T* handler = this->Handler.GetPointer();
375  if (handler)
376  {
377  if (this->Method1)
378  {
379  (handler->*this->Method1)();
380  }
381  else if (this->Method2)
382  {
383  (handler->*this->Method2)(caller, event, calldata);
384  }
385  else if (this->Method3)
386  {
387  return (handler->*this->Method3)(caller, event, calldata);
388  }
389  }
390  return false;
391  }
392  };
394 
396 
399  unsigned long AddTemplatedObserver(
400  unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
401  // Friend to access AddTemplatedObserver().
402  friend class vtkObjectCommandInternal;
404 };
405 
406 #endif
407 // 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:250
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:33
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:63
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:219
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:194
vtkObject::MTime
vtkTimeStamp MTime
Definition: vtkObject.h:251
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:132
vtkWeakPointerBase
Non-templated superclass for vtkWeakPointer.
Definition: vtkWeakPointerBase.h:34
vtkObject::vtkObject
vtkObject()
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:34
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:133
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:203
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:240
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:239
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:252
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