VTK  9.2.6
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 =========================================================================*/
59 #ifndef vtkObject_h
60 #define vtkObject_h
61 
62 #include "vtkCommonCoreModule.h" // For export macro
63 #include "vtkObjectBase.h"
64 #include "vtkSetGet.h"
65 #include "vtkTimeStamp.h"
66 #include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
67 
68 class vtkSubjectHelper;
69 class vtkCommand;
70 
71 class VTKCOMMONCORE_EXPORT vtkObject : public vtkObjectBase
72 {
73 public:
75 
80  static vtkObject* New();
81 
82 #ifdef _WIN32
83  // avoid dll boundary problems
84  void* operator new(size_t tSize);
85  void operator delete(void* p);
86 #endif
87 
91  virtual void DebugOn();
92 
96  virtual void DebugOff();
97 
101  bool GetDebug();
102 
106  void SetDebug(bool debugFlag);
107 
112  static void BreakOnError();
113 
120  virtual void Modified();
121 
126 
133  void PrintSelf(ostream& os, vtkIndent indent) override;
134 
136 
140  static void SetGlobalWarningDisplay(int val);
145 
147 
159  unsigned long AddObserver(unsigned long event, vtkCommand*, float priority = 0.0f);
160  unsigned long AddObserver(const char* event, vtkCommand*, float priority = 0.0f);
161  vtkCommand* GetCommand(unsigned long tag);
163  void RemoveObservers(unsigned long event, vtkCommand*);
164  void RemoveObservers(const char* event, vtkCommand*);
165  vtkTypeBool HasObserver(unsigned long event, vtkCommand*);
166  vtkTypeBool HasObserver(const char* event, vtkCommand*);
168 
169  void RemoveObserver(unsigned long tag);
170  void RemoveObservers(unsigned long event);
171  void RemoveObservers(const char* event);
172  void RemoveAllObservers(); // remove every last one of them
173  vtkTypeBool HasObserver(unsigned long event);
174  vtkTypeBool HasObserver(const char* event);
175 
177 
202  template <class U, class T>
203  unsigned long AddObserver(
204  unsigned long event, U observer, void (T::*callback)(), 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  }
211  template <class U, class T>
212  unsigned long AddObserver(unsigned long event, U observer,
213  void (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
214  {
215  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
216  // callable is deleted when the observer is cleaned up (look at
217  // vtkObjectCommandInternal)
218  return this->AddTemplatedObserver(event, callable, priority);
219  }
221 
223 
227  template <class U, class T>
228  unsigned long AddObserver(unsigned long event, U observer,
229  bool (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
230  {
231  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
232  // callable is deleted when the observer is cleaned up (look at
233  // vtkObjectCommandInternal)
234  return this->AddTemplatedObserver(event, callable, priority);
235  }
237 
239 
244  int InvokeEvent(unsigned long event, void* callData);
245  int InvokeEvent(const char* event, void* callData);
247 
248  int InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); }
249  int InvokeEvent(const char* event) { return this->InvokeEvent(event, nullptr); }
250 
252 
258  virtual void SetObjectName(const std::string& objectName);
259  virtual std::string GetObjectName() const;
261 
267 
268 protected:
270  ~vtkObject() override;
271 
272  // See vtkObjectBase.h.
275 
276  bool Debug; // Enable debug messages
277  vtkTimeStamp MTime; // Keep track of modification time
278  vtkSubjectHelper* SubjectHelper; // List of observers on this object
279  std::string ObjectName; // Name of this object for reporting
280 
282 
290  void InternalGrabFocus(vtkCommand* mouseEvents, vtkCommand* keypressEvents = nullptr);
293 
294 private:
295  vtkObject(const vtkObject&) = delete;
296  void operator=(const vtkObject&) = delete;
297 
305  class vtkClassMemberCallbackBase
306  {
307  public:
309 
312  virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
313  virtual ~vtkClassMemberCallbackBase() = default;
315  };
316 
318 
322  template <class T>
323  class vtkClassMemberHandlerPointer
324  {
325  public:
326  void operator=(vtkObjectBase* o)
327  {
328  // The cast is needed in case "o" has multi-inheritance,
329  // to offset the pointer to get the vtkObjectBase.
330  if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
331  {
332  // fallback to just using its vtkObjectBase as-is.
333  this->VoidPointer = o;
334  }
335  this->WeakPointer = o;
336  this->UseWeakPointer = true;
337  }
338  void operator=(void* o)
339  {
340  this->VoidPointer = o;
341  this->WeakPointer = nullptr;
342  this->UseWeakPointer = false;
343  }
344  T* GetPointer()
345  {
346  if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
347  {
348  return nullptr;
349  }
350  return static_cast<T*>(this->VoidPointer);
351  }
352 
353  private:
354  vtkWeakPointerBase WeakPointer;
355  void* VoidPointer;
356  bool UseWeakPointer;
357  };
359 
361 
364  template <class T>
365  class vtkClassMemberCallback : public vtkClassMemberCallbackBase
366  {
367  vtkClassMemberHandlerPointer<T> Handler;
368  void (T::*Method1)();
369  void (T::*Method2)(vtkObject*, unsigned long, void*);
370  bool (T::*Method3)(vtkObject*, unsigned long, void*);
371 
372  public:
373  vtkClassMemberCallback(T* handler, void (T::*method)())
374  {
375  this->Handler = handler;
376  this->Method1 = method;
377  this->Method2 = nullptr;
378  this->Method3 = nullptr;
379  }
380 
381  vtkClassMemberCallback(T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
382  {
383  this->Handler = handler;
384  this->Method1 = nullptr;
385  this->Method2 = method;
386  this->Method3 = nullptr;
387  }
388 
389  vtkClassMemberCallback(T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
390  {
391  this->Handler = handler;
392  this->Method1 = nullptr;
393  this->Method2 = nullptr;
394  this->Method3 = method;
395  }
396  ~vtkClassMemberCallback() override = default;
397 
398  // Called when the event is invoked
399  bool operator()(vtkObject* caller, unsigned long event, void* calldata) override
400  {
401  T* handler = this->Handler.GetPointer();
402  if (handler)
403  {
404  if (this->Method1)
405  {
406  (handler->*this->Method1)();
407  }
408  else if (this->Method2)
409  {
410  (handler->*this->Method2)(caller, event, calldata);
411  }
412  else if (this->Method3)
413  {
414  return (handler->*this->Method3)(caller, event, calldata);
415  }
416  }
417  return false;
418  }
419  };
421 
423 
427  void ObjectFinalize() final;
429 
431 
434  unsigned long AddTemplatedObserver(
435  unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
436  // Friend to access AddTemplatedObserver().
437  friend class vtkObjectCommandInternal;
439 };
440 
441 #endif
442 // VTK-HeaderTest-Exclude: vtkObject.h
superclass for callback/observer methods
Definition: vtkCommand.h:395
a simple class to control print indentation
Definition: vtkIndent.h:49
abstract base class for most VTK objects
Definition: vtkObjectBase.h:83
virtual void ObjectFinalize()
void operator=(const vtkObjectBase &)
abstract base class for most VTK objects
Definition: vtkObject.h:72
vtkBaseTypeMacro(vtkObject, vtkObjectBase)
void InternalReleaseFocus()
These methods allow a command to exclusively grab all events.
vtkCommand * GetCommand(unsigned long tag)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
virtual void DebugOn()
Turn debugging output on.
vtkTypeBool HasObserver(unsigned long event)
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
void RemoveObservers(const char *event)
vtkSubjectHelper * SubjectHelper
Definition: vtkObject.h:278
std::string GetObjectDescription() const override
The object description printed in messages and PrintSelf output.
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:228
vtkTypeBool HasObserver(const char *event, vtkCommand *)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
virtual void DebugOff()
Turn debugging output off.
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:212
~vtkObject() override
vtkTimeStamp MTime
Definition: vtkObject.h:277
void InternalGrabFocus(vtkCommand *mouseEvents, vtkCommand *keypressEvents=nullptr)
These methods allow a command to exclusively grab all events.
static void SetGlobalWarningDisplay(int val)
This is a global flag that controls whether any debug, warning or error messages are displayed.
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on.
void RemoveObserver(vtkCommand *)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
void RemoveObservers(unsigned long event, vtkCommand *)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
virtual std::string GetObjectName() const
Set/get the name of this object for reporting purposes.
int InvokeEvent(unsigned long event)
Definition: vtkObject.h:248
void UnRegisterInternal(vtkObjectBase *, vtkTypeBool check) override
void RemoveAllObservers()
void RegisterInternal(vtkObjectBase *, vtkTypeBool check) override
static void GlobalWarningDisplayOff()
This is a global flag that controls whether any debug, warning or error messages are displayed.
Definition: vtkObject.h:142
int InvokeEvent(const char *event, void *callData)
This method invokes an event and return whether the event was aborted or not.
virtual void Modified()
Update the modification time for this object.
vtkTypeBool HasObserver(unsigned long event, vtkCommand *)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
std::string ObjectName
Definition: vtkObject.h:279
int InvokeEvent(unsigned long event, void *callData)
This method invokes an event and return whether the event was aborted or not.
bool Debug
Definition: vtkObject.h:276
static int GetGlobalWarningDisplay()
This is a global flag that controls whether any debug, warning or error messages are displayed.
unsigned long AddObserver(unsigned long event, vtkCommand *, float priority=0.0f)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
void RemoveObservers(const char *event, vtkCommand *)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
unsigned long AddObserver(const char *event, vtkCommand *, float priority=0.0f)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
void SetDebug(bool debugFlag)
Set the value of the debug flag.
vtkTypeBool HasObserver(const char *event)
static void BreakOnError()
This method is called when vtkErrorMacro executes.
static void GlobalWarningDisplayOn()
This is a global flag that controls whether any debug, warning or error messages are displayed.
Definition: vtkObject.h:141
bool GetDebug()
Get the value of the debug flag.
virtual vtkMTimeType GetMTime()
Return this object's modified time.
void RemoveObservers(unsigned long event)
int InvokeEvent(const char *event)
Definition: vtkObject.h:249
virtual void SetObjectName(const std::string &objectName)
Set/get the name of this object for reporting purposes.
void RemoveObserver(unsigned long tag)
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:203
record modification and/or execution time
Definition: vtkTimeStamp.h:45
Non-templated superclass for vtkWeakPointer.
@ priority
Definition: vtkX3D.h:456
@ string
Definition: vtkX3D.h:496
int vtkTypeBool
Definition: vtkABI.h:69
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:287