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 =========================================================================*/
41 #ifndef vtkObject_h
42 #define vtkObject_h
43 
44 #include "vtkCommonCoreModule.h" // For export macro
45 #include "vtkObjectBase.h"
46 #include "vtkSetGet.h"
47 #include "vtkTimeStamp.h"
48 #include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
49 
50 class vtkSubjectHelper;
51 class vtkCommand;
52 
53 class VTKCOMMONCORE_EXPORT vtkObject : public vtkObjectBase
54 {
55 public:
56  vtkBaseTypeMacro(vtkObject, vtkObjectBase);
57 
62  static vtkObject* New();
63 
64 #ifdef _WIN32
65  // avoid dll boundary problems
66  void* operator new(size_t tSize);
67  void operator delete(void* p);
68 #endif
69 
73  virtual void DebugOn();
74 
78  virtual void DebugOff();
79 
83  bool GetDebug();
84 
88  void SetDebug(bool debugFlag);
89 
94  static void BreakOnError();
95 
102  virtual void Modified();
103 
107  virtual vtkMTimeType GetMTime();
108 
115  void PrintSelf(ostream& os, vtkIndent indent) override;
116 
118 
122  static void SetGlobalWarningDisplay(int val);
125  static int GetGlobalWarningDisplay();
127 
129 
141  unsigned long AddObserver(unsigned long event, vtkCommand*, float priority = 0.0f);
142  unsigned long AddObserver(const char* event, vtkCommand*, float priority = 0.0f);
143  vtkCommand* GetCommand(unsigned long tag);
144  void RemoveObserver(vtkCommand*);
145  void RemoveObservers(unsigned long event, vtkCommand*);
146  void RemoveObservers(const char* event, vtkCommand*);
147  vtkTypeBool HasObserver(unsigned long event, vtkCommand*);
148  vtkTypeBool HasObserver(const char* event, vtkCommand*);
150 
151  void RemoveObserver(unsigned long tag);
152  void RemoveObservers(unsigned long event);
153  void RemoveObservers(const char* event);
154  void RemoveAllObservers(); // remove every last one of them
155  vtkTypeBool HasObserver(unsigned long event);
156  vtkTypeBool HasObserver(const char* event);
157 
159 
184  template <class U, class T>
185  unsigned long AddObserver(
186  unsigned long event, U observer, void (T::*callback)(), float priority = 0.0f)
187  {
188  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
189  // callable is deleted when the observer is cleaned up (look at
190  // vtkObjectCommandInternal)
191  return this->AddTemplatedObserver(event, callable, priority);
192  }
193  template <class U, class T>
194  unsigned long AddObserver(unsigned long event, U observer,
195  void (T::*callback)(vtkObject*, unsigned long, void*), 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  }
203 
205 
209  template <class U, class T>
210  unsigned long AddObserver(unsigned long event, U observer,
211  bool (T::*callback)(vtkObject*, unsigned long, void*), float priority = 0.0f)
212  {
213  vtkClassMemberCallback<T>* callable = new vtkClassMemberCallback<T>(observer, callback);
214  // callable is deleted when the observer is cleaned up (look at
215  // vtkObjectCommandInternal)
216  return this->AddTemplatedObserver(event, callable, priority);
217  }
219 
221 
226  int InvokeEvent(unsigned long event, void* callData);
227  int InvokeEvent(const char* event, void* callData);
229 
230  int InvokeEvent(unsigned long event) { return this->InvokeEvent(event, nullptr); }
231  int InvokeEvent(const char* event) { return this->InvokeEvent(event, nullptr); }
232 
233 protected:
234  vtkObject();
235  ~vtkObject() override;
236 
237  // See vtkObjectBase.h.
238  void RegisterInternal(vtkObjectBase*, vtkTypeBool check) override;
239  void UnRegisterInternal(vtkObjectBase*, vtkTypeBool check) override;
240 
241  bool Debug; // Enable debug messages
242  vtkTimeStamp MTime; // Keep track of modification time
243  vtkSubjectHelper* SubjectHelper; // List of observers on this object
244 
246 
254  void InternalGrabFocus(vtkCommand* mouseEvents, vtkCommand* keypressEvents = nullptr);
255  void InternalReleaseFocus();
257 
258 private:
259  vtkObject(const vtkObject&) = delete;
260  void operator=(const vtkObject&) = delete;
261 
269  class vtkClassMemberCallbackBase
270  {
271  public:
273 
276  virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
277  virtual ~vtkClassMemberCallbackBase() {}
279  };
280 
282 
286  template <class T>
287  class vtkClassMemberHandlerPointer
288  {
289  public:
290  void operator=(vtkObjectBase* o)
291  {
292  // The cast is needed in case "o" has multi-inheritance,
293  // to offset the pointer to get the vtkObjectBase.
294  if ((this->VoidPointer = dynamic_cast<T*>(o)) == nullptr)
295  {
296  // fallback to just using its vtkObjectBase as-is.
297  this->VoidPointer = o;
298  }
299  this->WeakPointer = o;
300  this->UseWeakPointer = true;
301  }
302  void operator=(void* o)
303  {
304  this->VoidPointer = o;
305  this->WeakPointer = nullptr;
306  this->UseWeakPointer = false;
307  }
308  T* GetPointer()
309  {
310  if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
311  {
312  return nullptr;
313  }
314  return static_cast<T*>(this->VoidPointer);
315  }
316 
317  private:
318  vtkWeakPointerBase WeakPointer;
319  void* VoidPointer;
320  bool UseWeakPointer;
321  };
323 
325 
328  template <class T>
329  class vtkClassMemberCallback : public vtkClassMemberCallbackBase
330  {
331  vtkClassMemberHandlerPointer<T> Handler;
332  void (T::*Method1)();
333  void (T::*Method2)(vtkObject*, unsigned long, void*);
334  bool (T::*Method3)(vtkObject*, unsigned long, void*);
335 
336  public:
337  vtkClassMemberCallback(T* handler, void (T::*method)())
338  {
339  this->Handler = handler;
340  this->Method1 = method;
341  this->Method2 = nullptr;
342  this->Method3 = nullptr;
343  }
344 
345  vtkClassMemberCallback(T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
346  {
347  this->Handler = handler;
348  this->Method1 = nullptr;
349  this->Method2 = method;
350  this->Method3 = nullptr;
351  }
352 
353  vtkClassMemberCallback(T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
354  {
355  this->Handler = handler;
356  this->Method1 = nullptr;
357  this->Method2 = nullptr;
358  this->Method3 = method;
359  }
360  ~vtkClassMemberCallback() override {}
361 
362  // Called when the event is invoked
363  bool operator()(vtkObject* caller, unsigned long event, void* calldata) override
364  {
365  T* handler = this->Handler.GetPointer();
366  if (handler)
367  {
368  if (this->Method1)
369  {
370  (handler->*this->Method1)();
371  }
372  else if (this->Method2)
373  {
374  (handler->*this->Method2)(caller, event, calldata);
375  }
376  else if (this->Method3)
377  {
378  return (handler->*this->Method3)(caller, event, calldata);
379  }
380  }
381  return false;
382  }
383  };
385 
387 
390  unsigned long AddTemplatedObserver(
391  unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
392  // Friend to access AddTemplatedObserver().
393  friend class vtkObjectCommandInternal;
395 };
396 
397 #endif
398 // VTK-HeaderTest-Exclude: vtkObject.h
vtkCommand
superclass for callback/observer methods
Definition: vtkCommand.h:375
vtkObjectBase::operator=
void operator=(const vtkObjectBase &)
Definition: vtkObjectBase.h:219
vtkObjectBase.h
vtkTimeStamp.h
vtkObject::Debug
bool Debug
Definition: vtkObject.h:241
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:128
vtkTimeStamp
record modification and/or execution time
Definition: vtkTimeStamp.h:32
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:53
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:210
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:185
vtkObject::MTime
vtkTimeStamp MTime
Definition: vtkObject.h:242
vtkObjectBase
abstract base class for most VTK objects
Definition: vtkObjectBase.h:63
vtkObject::GlobalWarningDisplayOn
static void GlobalWarningDisplayOn()
Definition: vtkObject.h:123
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:124
vtkObject::AddObserver
unsigned long AddObserver(unsigned long event, U observer, void(T::*callback)(vtkObject *, unsigned long, void *), float priority=0.0f)
Definition: vtkObject.h:194
vtkObject::InvokeEvent
int InvokeEvent(const char *event)
Definition: vtkObject.h:231
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:230
vtkObject::SubjectHelper
vtkSubjectHelper * SubjectHelper
Definition: vtkObject.h:243
vtkX3D::priority
Definition: vtkX3D.h:456
vtkTypeBool
int vtkTypeBool
Definition: vtkABI.h:69
vtkMTimeType
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:298