VTK
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 =========================================================================*/
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 class vtkSubjectHelper;
57 class vtkCommand;
58 
59 class VTKCOMMONCORE_EXPORT vtkObject : public vtkObjectBase
60 {
61 public:
62  vtkBaseTypeMacro(vtkObject,vtkObjectBase);
63 
68  static vtkObject *New();
69 
70 #ifdef _WIN32
71  // avoid dll boundary problems
72  void* operator new( size_t tSize );
73  void operator delete( void* p );
74 #endif
75 
79  virtual void DebugOn();
80 
84  virtual void DebugOff();
85 
89  bool GetDebug();
90 
94  void SetDebug(bool debugFlag);
95 
100  static void BreakOnError();
101 
108  virtual void Modified();
109 
113  virtual vtkMTimeType GetMTime();
114 
121  void PrintSelf(ostream& os, vtkIndent indent) override;
122 
124 
128  static void SetGlobalWarningDisplay(int val);
132  static int GetGlobalWarningDisplay();
134 
136 
148  unsigned long AddObserver(unsigned long event, vtkCommand *,
149  float priority=0.0f);
150  unsigned long AddObserver(const char *event, vtkCommand *,
151  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(unsigned long event,
195  U observer, void (T::*callback)(), float priority=0.0f)
196  {
197  vtkClassMemberCallback<T> *callable =
198  new vtkClassMemberCallback<T>(observer, callback);
199  // callable is deleted when the observer is cleaned up (look at
200  // vtkObjectCommandInternal)
201  return this->AddTemplatedObserver(event, callable, priority);
202  }
203  template <class U, class T>
204  unsigned long AddObserver(unsigned long event,
205  U observer, void (T::*callback)(vtkObject*, unsigned long, void*),
206  float priority=0.0f)
207  {
208  vtkClassMemberCallback<T> *callable =
209  new vtkClassMemberCallback<T>(observer, callback);
210  // callable is deleted when the observer is cleaned up (look at
211  // vtkObjectCommandInternal)
212  return this->AddTemplatedObserver(event, callable, priority);
213  }
215 
217 
221  template <class U, class T>
222  unsigned long AddObserver(unsigned long event,
223  U observer, bool (T::*callback)(vtkObject*, unsigned long, void*),
224  float priority=0.0f)
225  {
226  vtkClassMemberCallback<T> *callable =
227  new vtkClassMemberCallback<T>(observer, callback);
228  // callable is deleted when the observer is cleaned up (look at
229  // vtkObjectCommandInternal)
230  return this->AddTemplatedObserver(event, callable, priority);
231  }
233 
235 
240  int InvokeEvent(unsigned long event, void *callData);
241  int InvokeEvent(const char *event, void *callData);
243 
244  int InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); };
245  int InvokeEvent(const char *event) { return this->InvokeEvent(event, nullptr); };
246 
247 protected:
248  vtkObject();
249  ~vtkObject() override;
250 
251  // See vtkObjectBase.h.
252  void RegisterInternal(vtkObjectBase*, vtkTypeBool check) override;
253  void UnRegisterInternal(vtkObjectBase*, vtkTypeBool check) override;
254 
255  bool Debug; // Enable debug messages
256  vtkTimeStamp MTime; // Keep track of modification time
257  vtkSubjectHelper *SubjectHelper; // List of observers on this object
258 
260 
268  void InternalGrabFocus(vtkCommand *mouseEvents, vtkCommand *keypressEvents=nullptr);
269  void InternalReleaseFocus();
271 
272 private:
273  vtkObject(const vtkObject&) = delete;
274  void operator=(const vtkObject&) = delete;
275 
283  class vtkClassMemberCallbackBase
284  {
285  public:
287 
290  virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
291  virtual ~vtkClassMemberCallbackBase(){}
292  };
294 
296 
300  template<class T>
301  class vtkClassMemberHandlerPointer
302  {
303  public:
304  void operator=(vtkObjectBase *o)
305  {
306  // The cast is needed in case "o" has multi-inheritance,
307  // to offset the pointer to get the vtkObjectBase.
308  if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
309  {
310  // fallback to just using its vtkObjectBase as-is.
311  this->VoidPointer = o;
312  }
313  this->WeakPointer = o;
314  this->UseWeakPointer = true;
315  }
316  void operator=(void *o)
317  {
318  this->VoidPointer = o;
319  this->WeakPointer = nullptr;
320  this->UseWeakPointer = false;
321  }
322  T *GetPointer()
323  {
324  if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
325  {
326  return nullptr;
327  }
328  return static_cast<T*>(this->VoidPointer);
329  }
330  private:
331  vtkWeakPointerBase WeakPointer;
332  void *VoidPointer;
333  bool UseWeakPointer;
334  };
336 
338 
341  template <class T>
342  class vtkClassMemberCallback : public vtkClassMemberCallbackBase
343  {
344  vtkClassMemberHandlerPointer<T> Handler;
345  void (T::*Method1)();
346  void (T::*Method2)(vtkObject*, unsigned long, void*);
347  bool (T::*Method3)(vtkObject*, unsigned long, void*);
349 
350  public:
351  vtkClassMemberCallback(T* handler, void (T::*method)())
352  {
353  this->Handler = handler;
354  this->Method1 = method;
355  this->Method2 = nullptr;
356  this->Method3 = nullptr;
357  }
358 
359  vtkClassMemberCallback(
360  T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
361  {
362  this->Handler = handler;
363  this->Method1 = nullptr;
364  this->Method2 = method;
365  this->Method3 = nullptr;
366  }
367 
368  vtkClassMemberCallback(
369  T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
370  {
371  this->Handler = handler;
372  this->Method1 = nullptr;
373  this->Method2 = nullptr;
374  this->Method3 = method;
375  }
376  ~vtkClassMemberCallback() override { }
377 
378  // Called when the event is invoked
379  bool operator()(
380  vtkObject* caller, unsigned long event, void* calldata) override
381  {
382  T *handler = this->Handler.GetPointer();
383  if (handler)
384  {
385  if (this->Method1)
386  {
387  (handler->*this->Method1)();
388  }
389  else if (this->Method2)
390  {
391  (handler->*this->Method2)(caller, event, calldata);
392  }
393  else if (this->Method3)
394  {
395  return (handler->*this->Method3)(caller, event, calldata);
396  }
397  }
398  return false;
399  }
400  };
401 
403 
406  unsigned long AddTemplatedObserver(
407  unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
408  // Friend to access AddTemplatedObserver().
409  friend class vtkObjectCommandInternal;
411 
412 };
413 
414 #endif
415 // 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:204
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:59
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:302
record modification and/or execution time
Definition: vtkTimeStamp.h:35
int InvokeEvent(unsigned long event)
Definition: vtkObject.h:244
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:130
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:222
superclass for callback/observer methods
Definition: vtkCommand.h:370
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:39
virtual void UnRegisterInternal(vtkObjectBase *, vtkTypeBool check)
Non-templated superclass for vtkWeakPointer.
abstract base class for most VTK objects
Definition: vtkObjectBase.h:65
virtual void RegisterInternal(vtkObjectBase *, vtkTypeBool check)
int InvokeEvent(const char *event)
Definition: vtkObject.h:245
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
bool Debug
Definition: vtkObject.h:255
void operator=(const vtkObjectBase &)
vtkSubjectHelper * SubjectHelper
Definition: vtkObject.h:257
static void GlobalWarningDisplayOn()
This is a global flag that controls whether any debug, warning or error messages are displayed...
Definition: vtkObject.h:129
vtkTimeStamp MTime
Definition: vtkObject.h:256