VTK  9.0.1
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 =========================================================================*/
44 #ifndef vtkObject_h
45 #define vtkObject_h
46 
47 #include "vtkCommonCoreModule.h" // For export macro
48 #include "vtkObjectBase.h"
49 #include "vtkSetGet.h"
50 #include "vtkTimeStamp.h"
51 #include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
52 
53 class vtkSubjectHelper;
54 class vtkCommand;
55 
56 class VTKCOMMONCORE_EXPORT vtkObject : public vtkObjectBase
57 {
58 public:
59  vtkBaseTypeMacro(vtkObject, vtkObjectBase);
60 
65  static vtkObject* New();
66 
67 #ifdef _WIN32
68  // avoid dll boundary problems
69  void* operator new(size_t tSize);
70  void operator delete(void* p);
71 #endif
72 
76  virtual void DebugOn();
77 
81  virtual void DebugOff();
82 
86  bool GetDebug();
87 
91  void SetDebug(bool debugFlag);
92 
97  static void BreakOnError();
98 
105  virtual void Modified();
106 
110  virtual vtkMTimeType GetMTime();
111 
118  void PrintSelf(ostream& os, vtkIndent indent) override;
119 
121 
125  static void SetGlobalWarningDisplay(int val);
128  static int GetGlobalWarningDisplay();
130 
132 
144  unsigned long AddObserver(unsigned long event, vtkCommand*, float priority = 0.0f);
145  unsigned long AddObserver(const char* event, vtkCommand*, float priority = 0.0f);
146  vtkCommand* GetCommand(unsigned long tag);
147  void RemoveObserver(vtkCommand*);
148  void RemoveObservers(unsigned long event, vtkCommand*);
149  void RemoveObservers(const char* event, vtkCommand*);
150  vtkTypeBool HasObserver(unsigned long event, vtkCommand*);
151  vtkTypeBool HasObserver(const char* event, vtkCommand*);
153 
154  void RemoveObserver(unsigned long tag);
155  void RemoveObservers(unsigned long event);
156  void RemoveObservers(const char* event);
157  void RemoveAllObservers(); // remove every last one of them
158  vtkTypeBool HasObserver(unsigned long event);
159  vtkTypeBool HasObserver(const char* event);
160 
162 
187  template <class U, class T>
188  unsigned long AddObserver(
189  unsigned long event, U observer, void (T::*callback)(), float priority = 0.0f)
190  {
191  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
192  // callable is deleted when the observer is cleaned up (look at
193  // vtkObjectCommandInternal)
194  return this->AddTemplatedObserver(event, callable, priority);
195  }
196  template <class U, class T>
197  unsigned long AddObserver(unsigned long event, U observer,
198  void (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
199  {
200  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
201  // callable is deleted when the observer is cleaned up (look at
202  // vtkObjectCommandInternal)
203  return this->AddTemplatedObserver(event, callable, priority);
204  }
206 
208 
212  template <class U, class T>
213  unsigned long AddObserver(unsigned long event, U observer,
214  bool (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
215  {
216  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
217  // callable is deleted when the observer is cleaned up (look at
218  // vtkObjectCommandInternal)
219  return this->AddTemplatedObserver(event, callable, priority);
220  }
222 
224 
229  int InvokeEvent(unsigned long event, void* callData);
230  int InvokeEvent(const char* event, void* callData);
232 
233  int InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); }
234  int InvokeEvent(const char* event) { return this->InvokeEvent(event, nullptr); }
235 
236 protected:
237  vtkObject();
238  ~vtkObject() override;
239 
240  // See vtkObjectBase.h.
241  void RegisterInternal(vtkObjectBase*, vtkTypeBool check) override;
242  void UnRegisterInternal(vtkObjectBase*, vtkTypeBool check) override;
243 
244  bool Debug; // Enable debug messages
245  vtkTimeStamp MTime; // Keep track of modification time
246  vtkSubjectHelper* SubjectHelper; // List of observers on this object
247 
249 
257  void InternalGrabFocus(vtkCommand* mouseEvents, vtkCommand* keypressEvents = nullptr);
258  void InternalReleaseFocus();
260 
261 private:
262  vtkObject(const vtkObject&) = delete;
263  void operator=(const vtkObject&) = delete;
264 
272  class vtkClassMemberCallbackBase
273  {
274  public:
276 
279  virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
280  virtual ~vtkClassMemberCallbackBase() {}
282  };
283 
285 
289  template <class T>
290  class vtkClassMemberHandlerPointer
291  {
292  public:
293  void operator=(vtkObjectBase* o)
294  {
295  // The cast is needed in case "o" has multi-inheritance,
296  // to offset the pointer to get the vtkObjectBase.
297  if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
298  {
299  // fallback to just using its vtkObjectBase as-is.
300  this->VoidPointer = o;
301  }
302  this->WeakPointer = o;
303  this->UseWeakPointer = true;
304  }
305  void operator=(void* o)
306  {
307  this->VoidPointer = o;
308  this->WeakPointer = nullptr;
309  this->UseWeakPointer = false;
310  }
311  T* GetPointer()
312  {
313  if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
314  {
315  return nullptr;
316  }
317  return static_cast<T*>(this->VoidPointer);
318  }
319 
320  private:
321  vtkWeakPointerBase WeakPointer;
322  void* VoidPointer;
323  bool UseWeakPointer;
324  };
326 
328 
331  template <class T>
332  class vtkClassMemberCallback : public vtkClassMemberCallbackBase
333  {
334  vtkClassMemberHandlerPointer<T> Handler;
335  void (T::*Method1)();
336  void (T::*Method2)(vtkObject*, unsigned long, void*);
337  bool (T::*Method3)(vtkObject*, unsigned long, void*);
338 
339  public:
340  vtkClassMemberCallback(T* handler, void (T::*method)())
341  {
342  this->Handler = handler;
343  this->Method1 = method;
344  this->Method2 = nullptr;
345  this->Method3 = nullptr;
346  }
347 
348  vtkClassMemberCallback(T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
349  {
350  this->Handler = handler;
351  this->Method1 = nullptr;
352  this->Method2 = method;
353  this->Method3 = nullptr;
354  }
355 
356  vtkClassMemberCallback(T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
357  {
358  this->Handler = handler;
359  this->Method1 = nullptr;
360  this->Method2 = nullptr;
361  this->Method3 = method;
362  }
363  ~vtkClassMemberCallback() override {}
364 
365  // Called when the event is invoked
366  bool operator()(vtkObject* caller, unsigned long event, void* calldata) override
367  {
368  T* handler = this->Handler.GetPointer();
369  if (handler)
370  {
371  if (this->Method1)
372  {
373  (handler->*this->Method1)();
374  }
375  else if (this->Method2)
376  {
377  (handler->*this->Method2)(caller, event, calldata);
378  }
379  else if (this->Method3)
380  {
381  return (handler->*this->Method3)(caller, event, calldata);
382  }
383  }
384  return false;
385  }
386  };
388 
390 
393  unsigned long AddTemplatedObserver(
394  unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
395  // Friend to access AddTemplatedObserver().
396  friend class vtkObjectCommandInternal;
398 };
399 
400 #endif
401 // 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:197
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:56
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:293
record modification and/or execution time
Definition: vtkTimeStamp.h:32
int InvokeEvent(unsigned long event)
Definition: vtkObject.h:233
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:127
int vtkTypeBool
Definition: vtkABI.h:69
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:213
superclass for callback/observer methods
Definition: vtkCommand.h:377
static void SetGlobalWarningDisplay(int val)
This is a global flag that controls whether any debug, warning or error messages are displayed...
a simple class to control print indentation
Definition: vtkIndent.h:33
virtual void UnRegisterInternal(vtkObjectBase *, vtkTypeBool check)
Non-templated superclass for vtkWeakPointer.
abstract base class for most VTK objects
Definition: vtkObjectBase.h:63
virtual void RegisterInternal(vtkObjectBase *, vtkTypeBool check)
int InvokeEvent(const char *event)
Definition: vtkObject.h:234
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:188
bool Debug
Definition: vtkObject.h:244
void operator=(const vtkObjectBase &)
vtkSubjectHelper * SubjectHelper
Definition: vtkObject.h:246
static void GlobalWarningDisplayOn()
This is a global flag that controls whether any debug, warning or error messages are displayed...
Definition: vtkObject.h:126
vtkTimeStamp MTime
Definition: vtkObject.h:245