VTK  9.0.20200529
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:
65  vtkBaseTypeMacro(vtkObject, vtkObjectBase);
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 
116  virtual vtkMTimeType GetMTime();
117 
124  void PrintSelf(ostream& os, vtkIndent indent) override;
125 
127 
131  static void SetGlobalWarningDisplay(int val);
134  static int GetGlobalWarningDisplay();
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);
153  void RemoveObserver(vtkCommand*);
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:
243  vtkObject();
244  ~vtkObject() override;
245 
246  // See vtkObjectBase.h.
247  void RegisterInternal(vtkObjectBase*, vtkTypeBool check) override;
248  void UnRegisterInternal(vtkObjectBase*, vtkTypeBool check) override;
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);
264  void InternalReleaseFocus();
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:377
vtkObjectBase::operator=
void operator=(const vtkObjectBase &)
Definition: vtkObjectBase.h:287
vtkObjectBase.h
vtkTimeStamp.h
vtkObject::Debug
bool Debug
Definition: vtkObject.h:250
vtkObjectBase::RegisterInternal
virtual void RegisterInternal(vtkObjectBase *, vtkTypeBool check)
vtkObjectBase::New
static vtkObjectBase * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on.
Definition: vtkObjectBase.h:154
vtkTimeStamp
record modification and/or execution time
Definition: vtkTimeStamp.h:32
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:62
vtkObjectBase::UnRegisterInternal
virtual void UnRegisterInternal(vtkObjectBase *, vtkTypeBool check)
vtkObject::SetGlobalWarningDisplay
static void SetGlobalWarningDisplay(int val)
This is a global flag that controls whether any debug, warning or error messages are displayed.
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
vtkObjectBase
abstract base class for most VTK objects
Definition: vtkObjectBase.h:69
vtkObject::GlobalWarningDisplayOn
static void GlobalWarningDisplayOn()
Definition: vtkObject.h:132
vtkWeakPointerBase
Non-templated superclass for vtkWeakPointer.
Definition: vtkWeakPointerBase.h:33
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:33
vtkObject::GlobalWarningDisplayOff
static void GlobalWarningDisplayOff()
Definition: vtkObject.h:133
vtkObject::AddObserver
unsigned long AddObserver(unsigned long event, U observer, void(T::*callback)(vtkObject *, unsigned long, void *), float priority=0.0f)
Definition: vtkObject.h:203
vtkObject::InvokeEvent
int InvokeEvent(const char *event)
Definition: vtkObject.h:240
vtkObjectBase::PrintSelf
virtual void PrintSelf(ostream &os, vtkIndent indent)
Methods invoked by print to print information about the object including superclasses.
vtkObject::InvokeEvent
int InvokeEvent(unsigned long event)
Definition: vtkObject.h:239
vtkObject::SubjectHelper
vtkSubjectHelper * SubjectHelper
Definition: vtkObject.h:252
vtkX3D::priority
Definition: vtkX3D.h:456
vtkTypeBool
int vtkTypeBool
Definition: vtkABI.h:69
vtkMTimeType
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:285