VTK  9.3.0
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
47 #ifndef vtkObject_h
48 #define vtkObject_h
49 
50 #include "vtkCommonCoreModule.h" // For export macro
51 #include "vtkObjectBase.h"
52 #include "vtkSetGet.h"
53 #include "vtkTimeStamp.h"
54 #include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
55 
56 VTK_ABI_NAMESPACE_BEGIN
57 class vtkSubjectHelper;
58 class vtkCommand;
59 
60 class VTKCOMMONCORE_EXPORT vtkObject : public vtkObjectBase
61 {
62 public:
64 
69  static vtkObject* New();
70 
71 #ifdef _WIN32
72  // avoid dll boundary problems
73  void* operator new(size_t tSize);
74  void operator delete(void* p);
75 #endif
76 
80  virtual void DebugOn();
81 
85  virtual void DebugOff();
86 
90  bool GetDebug();
91 
95  void SetDebug(bool debugFlag);
96 
101  static void BreakOnError();
102 
109  virtual void Modified();
110 
115 
122  void PrintSelf(ostream& os, vtkIndent indent) override;
123 
125 
134 
136 
148  unsigned long AddObserver(unsigned long event, vtkCommand*, float priority = 0.0f);
149  unsigned long AddObserver(const char* event, vtkCommand*, float priority = 0.0f);
150  vtkCommand* GetCommand(unsigned long tag);
152  void RemoveObservers(unsigned long event, vtkCommand*);
153  void RemoveObservers(const char* event, vtkCommand*);
154  vtkTypeBool HasObserver(unsigned long event, vtkCommand*);
155  vtkTypeBool HasObserver(const char* event, vtkCommand*);
157 
158  void RemoveObserver(unsigned long tag);
159  void RemoveObservers(unsigned long event);
160  void RemoveObservers(const char* event);
161  void RemoveAllObservers(); // remove every last one of them
162  vtkTypeBool HasObserver(unsigned long event);
163  vtkTypeBool HasObserver(const char* event);
164 
166 
191  template <class U, class T>
192  unsigned long AddObserver(
193  unsigned long event, U observer, void (T::*callback)(), 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  }
200  template <class U, class T>
201  unsigned long AddObserver(unsigned long event, U observer,
202  void (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
203  {
204  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
205  // callable is deleted when the observer is cleaned up (look at
206  // vtkObjectCommandInternal)
207  return this->AddTemplatedObserver(event, callable, priority);
208  }
210 
212 
216  template <class U, class T>
217  unsigned long AddObserver(unsigned long event, U observer,
218  bool (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
219  {
220  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
221  // callable is deleted when the observer is cleaned up (look at
222  // vtkObjectCommandInternal)
223  return this->AddTemplatedObserver(event, callable, priority);
224  }
226 
228 
233  vtkTypeBool InvokeEvent(unsigned long event, void* callData);
234  vtkTypeBool InvokeEvent(const char* event, void* callData);
236 
237  vtkTypeBool InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); }
238  vtkTypeBool InvokeEvent(const char* event) { return this->InvokeEvent(event, nullptr); }
239 
241 
247  virtual void SetObjectName(const std::string& objectName);
248  virtual std::string GetObjectName() const;
250 
256 
257 protected:
259  ~vtkObject() override;
260 
261  // See vtkObjectBase.h.
264 
265  bool Debug; // Enable debug messages
266  vtkTimeStamp MTime; // Keep track of modification time
267  vtkSubjectHelper* SubjectHelper; // List of observers on this object
268  std::string ObjectName; // Name of this object for reporting
269 
271 
279  void InternalGrabFocus(vtkCommand* mouseEvents, vtkCommand* keypressEvents = nullptr);
282 
283 private:
284  vtkObject(const vtkObject&) = delete;
285  void operator=(const vtkObject&) = delete;
286 
294  class vtkClassMemberCallbackBase
295  {
296  public:
298 
301  virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
302  virtual ~vtkClassMemberCallbackBase() = default;
304  };
305 
307 
311  template <class T>
312  class vtkClassMemberHandlerPointer
313  {
314  public:
315  void operator=(vtkObjectBase* o)
316  {
317  // The cast is needed in case "o" has multi-inheritance,
318  // to offset the pointer to get the vtkObjectBase.
319  if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
320  {
321  // fallback to just using its vtkObjectBase as-is.
322  this->VoidPointer = o;
323  }
324  this->WeakPointer = o;
325  this->UseWeakPointer = true;
326  }
327  void operator=(void* o)
328  {
329  this->VoidPointer = o;
330  this->WeakPointer = nullptr;
331  this->UseWeakPointer = false;
332  }
333  T* GetPointer()
334  {
335  if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
336  {
337  return nullptr;
338  }
339  return static_cast<T*>(this->VoidPointer);
340  }
341 
342  private:
343  vtkWeakPointerBase WeakPointer;
344  void* VoidPointer;
345  bool UseWeakPointer;
346  };
348 
350 
353  template <class T>
354  class vtkClassMemberCallback : public vtkClassMemberCallbackBase
355  {
356  vtkClassMemberHandlerPointer<T> Handler;
357  void (T::*Method1)();
358  void (T::*Method2)(vtkObject*, unsigned long, void*);
359  bool (T::*Method3)(vtkObject*, unsigned long, void*);
360 
361  public:
362  vtkClassMemberCallback(T* handler, void (T::*method)())
363  {
364  this->Handler = handler;
365  this->Method1 = method;
366  this->Method2 = nullptr;
367  this->Method3 = nullptr;
368  }
369 
370  vtkClassMemberCallback(T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
371  {
372  this->Handler = handler;
373  this->Method1 = nullptr;
374  this->Method2 = method;
375  this->Method3 = nullptr;
376  }
377 
378  vtkClassMemberCallback(T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
379  {
380  this->Handler = handler;
381  this->Method1 = nullptr;
382  this->Method2 = nullptr;
383  this->Method3 = method;
384  }
385  ~vtkClassMemberCallback() override = default;
386 
387  // Called when the event is invoked
388  bool operator()(vtkObject* caller, unsigned long event, void* calldata) override
389  {
390  T* handler = this->Handler.GetPointer();
391  if (handler)
392  {
393  if (this->Method1)
394  {
395  (handler->*this->Method1)();
396  }
397  else if (this->Method2)
398  {
399  (handler->*this->Method2)(caller, event, calldata);
400  }
401  else if (this->Method3)
402  {
403  return (handler->*this->Method3)(caller, event, calldata);
404  }
405  }
406  return false;
407  }
408  };
410 
412 
416  void ObjectFinalize() final;
418 
420 
423  unsigned long AddTemplatedObserver(
424  unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
425  // Friend to access AddTemplatedObserver().
426  friend class vtkObjectCommandInternal;
428 };
429 
430 VTK_ABI_NAMESPACE_END
431 #endif
432 // VTK-HeaderTest-Exclude: vtkObject.h
superclass for callback/observer methods
Definition: vtkCommand.h:384
a simple class to control print indentation
Definition: vtkIndent.h:38
abstract base class for most VTK objects
Definition: vtkObjectBase.h:72
virtual void ObjectFinalize()
void operator=(const vtkObjectBase &)
abstract base class for most VTK objects
Definition: vtkObject.h:61
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:267
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:217
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:201
vtkTypeBool InvokeEvent(unsigned long event)
Definition: vtkObject.h:237
~vtkObject() override
vtkTimeStamp MTime
Definition: vtkObject.h:266
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:238
static void GlobalWarningDisplayOff()
This is a global flag that controls whether any debug, warning or error messages are displayed.
Definition: vtkObject.h:131
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:268
bool Debug
Definition: vtkObject.h:265
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:130
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:192
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:34
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