VTK  9.7.1
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
38 #ifndef vtkObject_h
39 #define vtkObject_h
40 
41 #include "vtkCommonCoreModule.h" // For export macro
42 #include "vtkObjectBase.h"
43 #include "vtkSetGet.h"
44 #include "vtkTimeStamp.h"
45 #include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
46 #include "vtkWrappingHints.h" // For VTK_MARSHALAUTO
47 
48 VTK_ABI_NAMESPACE_BEGIN
49 class vtkSubjectHelper;
50 class vtkCommand;
51 
52 class VTKCOMMONCORE_EXPORT VTK_MARSHALAUTO vtkObject : public vtkObjectBase
53 {
54 public:
55  vtkBaseTypeMacro(vtkObject, vtkObjectBase);
56 
61  static vtkObject* New();
62 
63 #ifdef _WIN32
64  // avoid dll boundary problems
65  void* operator new(size_t tSize);
66  void operator delete(void* p);
67 #endif
68 
72  virtual void DebugOn();
73 
77  virtual void DebugOff();
78 
83  bool GetDebug();
84 
89  void SetDebug(bool debugFlag);
90 
95  static void BreakOnError();
96 
103  virtual void Modified();
104 
108  VTK_MARSHALGETTER(MTime)
109  virtual vtkMTimeType GetMTime();
110 
117  void PrintSelf(ostream& os, vtkIndent indent) override;
118 
120 
124  static void SetGlobalWarningDisplay(vtkTypeBool val);
127  static vtkTypeBool GetGlobalWarningDisplay();
129 
131 
143  unsigned long AddObserver(unsigned long event, vtkCommand*, float priority = 0.0f);
144  unsigned long AddObserver(const char* event, vtkCommand*, float priority = 0.0f);
145  vtkCommand* GetCommand(unsigned long tag);
146  void RemoveObserver(vtkCommand*);
147  void RemoveObservers(unsigned long event, vtkCommand*);
148  void RemoveObservers(const char* event, vtkCommand*);
149  vtkTypeBool HasObserver(unsigned long event, vtkCommand*);
150  vtkTypeBool HasObserver(const char* event, vtkCommand*);
152 
153  void RemoveObserver(unsigned long tag);
154  void RemoveObservers(unsigned long event);
155  void RemoveObservers(const char* event);
156  void RemoveAllObservers(); // remove every last one of them
157  vtkTypeBool HasObserver(unsigned long event);
158  vtkTypeBool HasObserver(const char* event);
159 
161 
186  template <class U, class T>
187  unsigned long AddObserver(
188  unsigned long event, U observer, void (T::*callback)(), float priority = 0.0f)
189  {
190  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
191  // callable is deleted when the observer is cleaned up (look at
192  // vtkObjectCommandInternal)
193  return this->AddTemplatedObserver(event, callable, priority);
194  }
195  template <class U, class T>
196  unsigned long AddObserver(unsigned long event, U observer,
197  void (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
198  {
199  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
200  // callable is deleted when the observer is cleaned up (look at
201  // vtkObjectCommandInternal)
202  return this->AddTemplatedObserver(event, callable, priority);
203  }
205 
207 
211  template <class U, class T>
212  unsigned long AddObserver(unsigned long event, U observer,
213  bool (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 
228  vtkTypeBool InvokeEvent(unsigned long event, void* callData);
229  vtkTypeBool InvokeEvent(const char* event, void* callData);
231 
232  vtkTypeBool InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); }
233  vtkTypeBool InvokeEvent(const char* event) { return this->InvokeEvent(event, nullptr); }
234 
236 
242  virtual void SetObjectName(const std::string& objectName);
243  virtual std::string GetObjectName() const;
245 
250  std::string GetObjectDescription() const override;
251 
252 protected:
253  vtkObject();
254  ~vtkObject() override;
255 
256  // See vtkObjectBase.h.
257  void RegisterInternal(vtkObjectBase*, vtkTypeBool check) override;
258  void UnRegisterInternal(vtkObjectBase*, vtkTypeBool check) override;
259 
260  bool Debug; // Enable debug messages
261  vtkTimeStamp MTime; // Keep track of modification time
262  vtkSubjectHelper* SubjectHelper; // List of observers on this object
263  std::string ObjectName; // Name of this object for reporting
264 
266 
274  void InternalGrabFocus(vtkCommand* mouseEvents, vtkCommand* keypressEvents = nullptr);
275  void InternalReleaseFocus();
277 
278 private:
279  vtkObject(const vtkObject&) = delete;
280  void operator=(const vtkObject&) = delete;
281 
289  class vtkClassMemberCallbackBase
290  {
291  public:
293 
296  virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
297  virtual ~vtkClassMemberCallbackBase() = default;
299  };
300 
302 
306  template <class T>
307  class vtkClassMemberHandlerPointer
308  {
309  public:
310  void operator=(vtkObjectBase* o)
311  {
312  // The cast is needed in case "o" has multi-inheritance,
313  // to offset the pointer to get the vtkObjectBase.
314  if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
315  {
316  // fallback to just using its vtkObjectBase as-is.
317  this->VoidPointer = o;
318  }
319  this->WeakPointer = o;
320  this->UseWeakPointer = true;
321  }
322  void operator=(void* o)
323  {
324  this->VoidPointer = o;
325  this->WeakPointer = nullptr;
326  this->UseWeakPointer = false;
327  }
328  T* GetPointer()
329  {
330  if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
331  {
332  return nullptr;
333  }
334  return static_cast<T*>(this->VoidPointer);
335  }
336 
337  private:
338  vtkWeakPointerBase WeakPointer;
339  void* VoidPointer;
340  bool UseWeakPointer;
341  };
343 
345 
348  template <class T>
349  class vtkClassMemberCallback : public vtkClassMemberCallbackBase
350  {
351  vtkClassMemberHandlerPointer<T> Handler;
352  void (T::*Method1)();
353  void (T::*Method2)(vtkObject*, unsigned long, void*);
354  bool (T::*Method3)(vtkObject*, unsigned long, void*);
355 
356  public:
357  vtkClassMemberCallback(T* handler, void (T::*method)())
358  {
359  this->Handler = handler;
360  this->Method1 = method;
361  this->Method2 = nullptr;
362  this->Method3 = nullptr;
363  }
364 
365  vtkClassMemberCallback(T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
366  {
367  this->Handler = handler;
368  this->Method1 = nullptr;
369  this->Method2 = method;
370  this->Method3 = nullptr;
371  }
372 
373  vtkClassMemberCallback(T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
374  {
375  this->Handler = handler;
376  this->Method1 = nullptr;
377  this->Method2 = nullptr;
378  this->Method3 = method;
379  }
380  ~vtkClassMemberCallback() override = default;
381 
382  // Called when the event is invoked
383  bool operator()(vtkObject* caller, unsigned long event, void* calldata) override
384  {
385  T* handler = this->Handler.GetPointer();
386  if (handler)
387  {
388  if (this->Method1)
389  {
390  (handler->*this->Method1)();
391  }
392  else if (this->Method2)
393  {
394  (handler->*this->Method2)(caller, event, calldata);
395  }
396  else if (this->Method3)
397  {
398  return (handler->*this->Method3)(caller, event, calldata);
399  }
400  }
401  return false;
402  }
403  };
405 
407 
411  void ObjectFinalize() final;
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 VTK_ABI_NAMESPACE_END
426 #endif
427 // VTK-HeaderTest-Exclude: vtkObject.h
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:196
static vtkObjectBase * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
abstract base class for most VTK objects
Definition: vtkObject.h:52
virtual void ObjectFinalize()
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:318
record modification and/or execution time
Definition: vtkTimeStamp.h:24
virtual std::string GetObjectDescription() const
The object description printed in messages and PrintSelf output.
virtual void PrintSelf(ostream &os, vtkIndent indent)
Methods invoked by print to print information about the object including superclasses.
static void GlobalWarningDisplayOff()
This is a global flag that controls whether any debug, warning or error messages are displayed...
Definition: vtkObject.h:126
std::string ObjectName
Definition: vtkObject.h:263
int vtkTypeBool
Definition: vtkABI.h:64
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:212
superclass for callback/observer methods
Definition: vtkCommand.h:384
a simple class to control print indentation
Definition: vtkIndent.h:28
#define VTK_MARSHALGETTER(property)
virtual void UnRegisterInternal(vtkObjectBase *, vtkTypeBool check)
Non-templated superclass for vtkWeakPointer.
abstract base class for most VTK objects
Definition: vtkObjectBase.h:68
virtual void RegisterInternal(vtkObjectBase *, vtkTypeBool check)
#define VTK_MARSHALEXCLUDE(reason)
#define VTK_MARSHALAUTO
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, 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:187
bool Debug
Definition: vtkObject.h:260
void operator=(const vtkObjectBase &)
vtkSubjectHelper * SubjectHelper
Definition: vtkObject.h:262
vtkTypeBool InvokeEvent(unsigned long event)
Definition: vtkObject.h:232
vtkTypeBool InvokeEvent(const char *event)
Definition: vtkObject.h:233
static void GlobalWarningDisplayOn()
This is a global flag that controls whether any debug, warning or error messages are displayed...
Definition: vtkObject.h:125
vtkTimeStamp MTime
Definition: vtkObject.h:261
#define VTK_MARSHAL_EXCLUDE_REASON_IS_INTERNAL