vtk-dicom  0.8.17
vtkObject.h
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) VTK_OVERRIDE;
116 
118 
122  static void SetGlobalWarningDisplay(int val);
123  static void GlobalWarningDisplayOn(){vtkObject::SetGlobalWarningDisplay(1);};
124  static void GlobalWarningDisplayOff()
126  static int GetGlobalWarningDisplay();
128 
130 
142  unsigned long AddObserver(unsigned long event, vtkCommand *,
143  float priority=0.0f);
144  unsigned long AddObserver(const char *event, vtkCommand *,
145  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  int HasObserver(unsigned long event, vtkCommand *);
151  int 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  int HasObserver(unsigned long event);
159  int HasObserver(const char *event);
160 
162 
187  template <class U, class T>
188  unsigned long AddObserver(unsigned long event,
189  U observer, void (T::*callback)(), float priority=0.0f)
190  {
191  vtkClassMemberCallback<T> *callable =
192  new vtkClassMemberCallback<T>(observer, callback);
193  // callable is deleted when the observer is cleaned up (look at
194  // vtkObjectCommandInternal)
195  return this->AddTemplatedObserver(event, callable, priority);
196  }
197  template <class U, class T>
198  unsigned long AddObserver(unsigned long event,
199  U observer, void (T::*callback)(vtkObject*, unsigned long, void*),
200  float priority=0.0f)
201  {
202  vtkClassMemberCallback<T> *callable =
203  new vtkClassMemberCallback<T>(observer, callback);
204  // callable is deleted when the observer is cleaned up (look at
205  // vtkObjectCommandInternal)
206  return this->AddTemplatedObserver(event, callable, priority);
207  }
209 
211 
215  template <class U, class T>
216  unsigned long AddObserver(unsigned long event,
217  U observer, bool (T::*callback)(vtkObject*, unsigned long, void*),
218  float priority=0.0f)
219  {
220  vtkClassMemberCallback<T> *callable =
221  new vtkClassMemberCallback<T>(observer, callback);
222  // callable is deleted when the observer is cleaned up (look at
223  // vtkObjectCommandInternal)
224  return this->AddTemplatedObserver(event, callable, priority);
225  }
227 
229 
234  int InvokeEvent(unsigned long event, void *callData);
235  int InvokeEvent(const char *event, void *callData);
237 
238  int InvokeEvent(unsigned long event) { return this->InvokeEvent(event, NULL); };
239  int InvokeEvent(const char *event) { return this->InvokeEvent(event, NULL); };
240 
241 protected:
242  vtkObject();
243  ~vtkObject() VTK_OVERRIDE;
244 
245  // See vtkObjectBase.h.
246  void RegisterInternal(vtkObjectBase*, vtkTypeBool check) VTK_OVERRIDE;
247  void UnRegisterInternal(vtkObjectBase*, vtkTypeBool check) VTK_OVERRIDE;
248 
249  bool Debug; // Enable debug messages
250  vtkTimeStamp MTime; // Keep track of modification time
251  vtkSubjectHelper *SubjectHelper; // List of observers on this object
252 
254 
262  void InternalGrabFocus(vtkCommand *mouseEvents, vtkCommand *keypressEvents=NULL);
263  void InternalReleaseFocus();
265 
266 private:
267  vtkObject(const vtkObject&) VTK_DELETE_FUNCTION;
268  void operator=(const vtkObject&) VTK_DELETE_FUNCTION;
269 
277  class vtkClassMemberCallbackBase
278  {
279  public:
281 
284  virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
285  virtual ~vtkClassMemberCallbackBase(){}
286  };
288 
290 
294  template<class T>
295  class vtkClassMemberHandlerPointer
296  {
297  public:
298  void operator=(vtkObjectBase *o)
299  {
300  // The cast is needed in case "o" has multi-inheritance,
301  // to offset the pointer to get the vtkObjectBase.
302  if ((this->VoidPointer = dynamic_cast<T*>(o)) == 0)
303  {
304  // fallback to just using its vtkObjectBase as-is.
305  this->VoidPointer = o;
306  }
307  this->WeakPointer = o;
308  this->UseWeakPointer = true;
309  }
310  void operator=(void *o)
311  {
312  this->VoidPointer = o;
313  this->WeakPointer = 0;
314  this->UseWeakPointer = false;
315  }
316  T *GetPointer()
317  {
318  if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
319  {
320  return 0;
321  }
322  return static_cast<T*>(this->VoidPointer);
323  }
324  private:
325  vtkWeakPointerBase WeakPointer;
326  void *VoidPointer;
327  bool UseWeakPointer;
328  };
330 
332 
335  template <class T>
336  class vtkClassMemberCallback : public vtkClassMemberCallbackBase
337  {
338  vtkClassMemberHandlerPointer<T> Handler;
339  void (T::*Method1)();
340  void (T::*Method2)(vtkObject*, unsigned long, void*);
341  bool (T::*Method3)(vtkObject*, unsigned long, void*);
343 
344  public:
345  vtkClassMemberCallback(T* handler, void (T::*method)())
346  {
347  this->Handler = handler;
348  this->Method1 = method;
349  this->Method2 = NULL;
350  this->Method3 = NULL;
351  }
352 
353  vtkClassMemberCallback(
354  T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
355  {
356  this->Handler = handler;
357  this->Method1 = NULL;
358  this->Method2 = method;
359  this->Method3 = NULL;
360  }
361 
362  vtkClassMemberCallback(
363  T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
364  {
365  this->Handler = handler;
366  this->Method1 = NULL;
367  this->Method2 = NULL;
368  this->Method3 = method;
369  }
370  ~vtkClassMemberCallback() VTK_OVERRIDE { }
371 
372  // Called when the event is invoked
373  bool operator()(
374  vtkObject* caller, unsigned long event, void* calldata) VTK_OVERRIDE
375  {
376  T *handler = this->Handler.GetPointer();
377  if (handler)
378  {
379  if (this->Method1)
380  {
381  (handler->*this->Method1)();
382  }
383  else if (this->Method2)
384  {
385  (handler->*this->Method2)(caller, event, calldata);
386  }
387  else if (this->Method3)
388  {
389  return (handler->*this->Method3)(caller, event, calldata);
390  }
391  }
392  return false;
393  }
394  };
395 
397 
400  unsigned long AddTemplatedObserver(
401  unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
402  // Friend to access AddTemplatedObserver().
403  friend class vtkObjectCommandInternal;
405 
406 };
407 
408 #endif
409 // VTK-HeaderTest-Exclude: vtkObject.h
abstract base class for most VTK objects
Definition: vtkObjectBase.h:63
abstract base class for most VTK objects
Definition: vtkObject.h:54
virtual void DebugOn()
unsigned long AddObserver(unsigned long event, U observer, bool(T::*callback)(vtkObject *, unsigned long, void *), float priority=0.0f)
Definition: vtkObject.h:216
virtual void DebugOff()
static void SetGlobalWarningDisplay(int val)
static vtkObject * New()
virtual void Modified()
int InvokeEvent(unsigned long event, void *callData)
unsigned long AddObserver(unsigned long event, vtkCommand *, float priority=0.0f)
void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE
void SetDebug(bool debugFlag)
static void BreakOnError()
bool GetDebug()
virtual vtkMTimeType GetMTime()
unsigned long AddObserver(unsigned long event, U observer, void(T::*callback)(), float priority=0.0f)
Definition: vtkObject.h:188