VTK  9.3.20240328
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
147 #ifndef vtkObject_h
148 #define vtkObject_h
149 
150 #include "vtkCommonCoreModule.h" // For export macro
151 #include "vtkObjectBase.h"
152 #include "vtkSetGet.h"
153 #include "vtkTimeStamp.h"
154 #include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
155 
156 VTK_ABI_NAMESPACE_BEGIN
157 class vtkSubjectHelper;
158 class vtkCommand;
159 
160 class VTKCOMMONCORE_EXPORT vtkObject : public vtkObjectBase
161 {
162 public:
164 
169  static vtkObject* New();
170 
171 #ifdef _WIN32
172  // avoid dll boundary problems
173  void* operator new(size_t tSize);
174  void operator delete(void* p);
175 #endif
176 
180  virtual void DebugOn();
181 
185  virtual void DebugOff();
186 
190  bool GetDebug();
191 
195  void SetDebug(bool debugFlag);
196 
201  static void BreakOnError();
202 
209  virtual void Modified();
210 
215 
222  void PrintSelf(ostream& os, vtkIndent indent) override;
223 
225 
234 
236 
248  unsigned long AddObserver(unsigned long event, vtkCommand*, float priority = 0.0f);
249  unsigned long AddObserver(const char* event, vtkCommand*, float priority = 0.0f);
250  vtkCommand* GetCommand(unsigned long tag);
252  void RemoveObservers(unsigned long event, vtkCommand*);
253  void RemoveObservers(const char* event, vtkCommand*);
254  vtkTypeBool HasObserver(unsigned long event, vtkCommand*);
255  vtkTypeBool HasObserver(const char* event, vtkCommand*);
257 
258  void RemoveObserver(unsigned long tag);
259  void RemoveObservers(unsigned long event);
260  void RemoveObservers(const char* event);
261  void RemoveAllObservers(); // remove every last one of them
262  vtkTypeBool HasObserver(unsigned long event);
263  vtkTypeBool HasObserver(const char* event);
264 
266 
291  template <class U, class T>
292  unsigned long AddObserver(
293  unsigned long event, U observer, void (T::*callback)(), float priority = 0.0f)
294  {
295  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
296  // callable is deleted when the observer is cleaned up (look at
297  // vtkObjectCommandInternal)
298  return this->AddTemplatedObserver(event, callable, priority);
299  }
300  template <class U, class T>
301  unsigned long AddObserver(unsigned long event, U observer,
302  void (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
303  {
304  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
305  // callable is deleted when the observer is cleaned up (look at
306  // vtkObjectCommandInternal)
307  return this->AddTemplatedObserver(event, callable, priority);
308  }
310 
312 
316  template <class U, class T>
317  unsigned long AddObserver(unsigned long event, U observer,
318  bool (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
319  {
320  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
321  // callable is deleted when the observer is cleaned up (look at
322  // vtkObjectCommandInternal)
323  return this->AddTemplatedObserver(event, callable, priority);
324  }
326 
328 
333  vtkTypeBool InvokeEvent(unsigned long event, void* callData);
334  vtkTypeBool InvokeEvent(const char* event, void* callData);
336 
337  vtkTypeBool InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); }
338  vtkTypeBool InvokeEvent(const char* event) { return this->InvokeEvent(event, nullptr); }
339 
341 
347  virtual void SetObjectName(const std::string& objectName);
348  virtual std::string GetObjectName() const;
350 
356 
357 protected:
359  ~vtkObject() override;
360 
361  // See vtkObjectBase.h.
364 
365  bool Debug; // Enable debug messages
366  vtkTimeStamp MTime; // Keep track of modification time
367  vtkSubjectHelper* SubjectHelper; // List of observers on this object
368  std::string ObjectName; // Name of this object for reporting
369 
371 
379  void InternalGrabFocus(vtkCommand* mouseEvents, vtkCommand* keypressEvents = nullptr);
382 
383 private:
384  vtkObject(const vtkObject&) = delete;
385  void operator=(const vtkObject&) = delete;
386 
394  class vtkClassMemberCallbackBase
395  {
396  public:
398 
401  virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
402  virtual ~vtkClassMemberCallbackBase() = default;
404  };
405 
407 
411  template <class T>
412  class vtkClassMemberHandlerPointer
413  {
414  public:
415  void operator=(vtkObjectBase* o)
416  {
417  // The cast is needed in case "o" has multi-inheritance,
418  // to offset the pointer to get the vtkObjectBase.
419  if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
420  {
421  // fallback to just using its vtkObjectBase as-is.
422  this->VoidPointer = o;
423  }
424  this->WeakPointer = o;
425  this->UseWeakPointer = true;
426  }
427  void operator=(void* o)
428  {
429  this->VoidPointer = o;
430  this->WeakPointer = nullptr;
431  this->UseWeakPointer = false;
432  }
433  T* GetPointer()
434  {
435  if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
436  {
437  return nullptr;
438  }
439  return static_cast<T*>(this->VoidPointer);
440  }
441 
442  private:
443  vtkWeakPointerBase WeakPointer;
444  void* VoidPointer;
445  bool UseWeakPointer;
446  };
448 
450 
453  template <class T>
454  class vtkClassMemberCallback : public vtkClassMemberCallbackBase
455  {
456  vtkClassMemberHandlerPointer<T> Handler;
457  void (T::*Method1)();
458  void (T::*Method2)(vtkObject*, unsigned long, void*);
459  bool (T::*Method3)(vtkObject*, unsigned long, void*);
460 
461  public:
462  vtkClassMemberCallback(T* handler, void (T::*method)())
463  {
464  this->Handler = handler;
465  this->Method1 = method;
466  this->Method2 = nullptr;
467  this->Method3 = nullptr;
468  }
469 
470  vtkClassMemberCallback(T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
471  {
472  this->Handler = handler;
473  this->Method1 = nullptr;
474  this->Method2 = method;
475  this->Method3 = nullptr;
476  }
477 
478  vtkClassMemberCallback(T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
479  {
480  this->Handler = handler;
481  this->Method1 = nullptr;
482  this->Method2 = nullptr;
483  this->Method3 = method;
484  }
485  ~vtkClassMemberCallback() override = default;
486 
487  // Called when the event is invoked
488  bool operator()(vtkObject* caller, unsigned long event, void* calldata) override
489  {
490  T* handler = this->Handler.GetPointer();
491  if (handler)
492  {
493  if (this->Method1)
494  {
495  (handler->*this->Method1)();
496  }
497  else if (this->Method2)
498  {
499  (handler->*this->Method2)(caller, event, calldata);
500  }
501  else if (this->Method3)
502  {
503  return (handler->*this->Method3)(caller, event, calldata);
504  }
505  }
506  return false;
507  }
508  };
510 
512 
516  void ObjectFinalize() final;
518 
520 
523  unsigned long AddTemplatedObserver(
524  unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
525  // Friend to access AddTemplatedObserver().
526  friend class vtkObjectCommandInternal;
528 };
529 
530 VTK_ABI_NAMESPACE_END
531 #endif
532 // VTK-HeaderTest-Exclude: vtkObject.h
superclass for callback/observer methods
Definition: vtkCommand.h:384
a simple class to control print indentation
Definition: vtkIndent.h:108
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:161
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:367
std::string GetObjectDescription() const override
The object description printed in messages and PrintSelf output.
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, 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:317
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:301
vtkTypeBool InvokeEvent(unsigned long event)
Definition: vtkObject.h:337
~vtkObject() override
vtkTimeStamp MTime
Definition: vtkObject.h:366
void InternalGrabFocus(vtkCommand *mouseEvents, vtkCommand *keypressEvents=nullptr)
These methods allow a command to exclusively grab all events.
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.
void UnRegisterInternal(vtkObjectBase *, vtkTypeBool check) override
void RemoveAllObservers()
void RegisterInternal(vtkObjectBase *, vtkTypeBool check) override
vtkTypeBool InvokeEvent(const char *event)
Definition: vtkObject.h:338
static void GlobalWarningDisplayOff()
This is a global flag that controls whether any debug, warning or error messages are displayed.
Definition: vtkObject.h:231
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.
vtkTypeBool InvokeEvent(unsigned long event, void *callData)
This method invokes an event and return whether the event was aborted or not.
std::string ObjectName
Definition: vtkObject.h:368
bool Debug
Definition: vtkObject.h:365
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:230
bool GetDebug()
Get the value of the debug flag.
virtual vtkMTimeType GetMTime()
Return this object's modified time.
void RemoveObservers(unsigned long event)
virtual void SetObjectName(const std::string &objectName)
Set/get the name of this object for reporting purposes.
void RemoveObserver(unsigned long tag)
static vtkTypeBool GetGlobalWarningDisplay()
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:292
vtkTypeBool InvokeEvent(const char *event, void *callData)
This method invokes an event and return whether the event was aborted or not.
record modification and/or execution time
Definition: vtkTimeStamp.h:44
Non-templated superclass for vtkWeakPointer.
@ priority
Definition: vtkX3D.h:450
@ string
Definition: vtkX3D.h:490
int vtkTypeBool
Definition: vtkABI.h:64
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:270