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 =========================================================================*/
49 #ifndef vtkObject_h
50 #define vtkObject_h
51 
52 #include "vtkCommonCoreModule.h" // For export macro
53 #include "vtkObjectBase.h"
54 #include "vtkSetGet.h"
55 #include "vtkTimeStamp.h"
56 #include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
57 
58 class vtkSubjectHelper;
59 class vtkCommand;
60 
62 {
63 public:
64  vtkTypeMacro(vtkObject,vtkObjectBase);
65 
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 
77  virtual void DebugOn();
78 
80  virtual void DebugOff();
81 
83  bool GetDebug();
84 
86  void SetDebug(bool debugFlag);
87 
90  static void BreakOnError();
91 
96  virtual void Modified();
97 
99  virtual unsigned long GetMTime();
100 
105  virtual void PrintSelf(ostream& os, vtkIndent indent);
106 
108 
110  static void SetGlobalWarningDisplay(int val);
114  static int GetGlobalWarningDisplay();
116 
117 //BTX
119 
128  unsigned long AddObserver(unsigned long event, vtkCommand *,
129  float priority=0.0f);
130  unsigned long AddObserver(const char *event, vtkCommand *,
131  float priority=0.0f);
132  vtkCommand *GetCommand(unsigned long tag);
133  void RemoveObserver(vtkCommand*);
134  void RemoveObservers(unsigned long event, vtkCommand *);
135  void RemoveObservers(const char *event, vtkCommand *);
136  int HasObserver(unsigned long event, vtkCommand *);
137  int HasObserver(const char *event, vtkCommand *);
138 //ETX
139  void RemoveObserver(unsigned long tag);
140  void RemoveObservers(unsigned long event);
141  void RemoveObservers(const char *event);
142  void RemoveAllObservers(); //remove every last one of them
143  int HasObserver(unsigned long event);
144  int HasObserver(const char *event);
146 
147 //BTX
149 
167  template <class U, class T>
168  unsigned long AddObserver(unsigned long event,
169  U observer, void (T::*callback)(), float priority=0.0f)
170  {
171  vtkClassMemberCallback<T> *callable =
172  new vtkClassMemberCallback<T>(observer, callback);
173  // callable is deleted when the observer is cleaned up (look at
174  // vtkObjectCommandInternal)
175  return this->AddTemplatedObserver(event, callable, priority);
176  }
177  template <class U, class T>
178  unsigned long AddObserver(unsigned long event,
179  U observer, void (T::*callback)(vtkObject*, unsigned long, void*),
180  float priority=0.0f)
181  {
182  vtkClassMemberCallback<T> *callable =
183  new vtkClassMemberCallback<T>(observer, callback);
184  // callable is deleted when the observer is cleaned up (look at
185  // vtkObjectCommandInternal)
186  return this->AddTemplatedObserver(event, callable, priority);
187  }
189 
191 
193  template <class U, class T>
194  unsigned long AddObserver(unsigned long event,
195  U observer, bool (T::*callback)(vtkObject*, unsigned long, void*),
196  float priority=0.0f)
197  {
198  vtkClassMemberCallback<T> *callable =
199  new vtkClassMemberCallback<T>(observer, callback);
200  // callable is deleted when the observer is cleaned up (look at
201  // vtkObjectCommandInternal)
202  return this->AddTemplatedObserver(event, callable, priority);
203  }
204 //ETX
206 
207 //BTX
209 
212  int InvokeEvent(unsigned long event, void *callData);
213  int InvokeEvent(const char *event, void *callData);
214 //ETX
215  int InvokeEvent(unsigned long event) { return this->InvokeEvent(event, NULL); };
216  int InvokeEvent(const char *event) { return this->InvokeEvent(event, NULL); };
218 
219 protected:
220  vtkObject();
221  virtual ~vtkObject();
222 
223  // See vtkObjectBase.h.
224  virtual void RegisterInternal(vtkObjectBase*, int check);
225  virtual void UnRegisterInternal(vtkObjectBase*, int check);
226 
227  bool Debug; // Enable debug messages
228  vtkTimeStamp MTime; // Keep track of modification time
229  vtkSubjectHelper *SubjectHelper; // List of observers on this object
230 
231 //BTX
233 
240  void InternalGrabFocus(vtkCommand *mouseEvents, vtkCommand *keypressEvents=NULL);
241  void InternalReleaseFocus();
242 //ETX
243 //BTX
244 private:
245  vtkObject(const vtkObject&); // Not implemented.
246  void operator=(const vtkObject&); // Not implemented.
248 
250 
255  class vtkClassMemberCallbackBase
256  {
257  public:
259 
260 
261  virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
262  virtual ~vtkClassMemberCallbackBase(){}
263  };
265 
267 
269  template<class T>
270  class vtkClassMemberHandlerPointer
271  {
272  public:
273  void operator=(vtkObjectBase *o)
274  {
275  // The cast is needed in case "o" has multi-inheritance,
276  // to offset the pointer to get the vtkObjectBase.
277  if ((this->VoidPointer = dynamic_cast<T*>(o)) == 0)
278  {
279  // fallback to just using its vtkObjectBase as-is.
280  this->VoidPointer = o;
281  }
282  this->WeakPointer = o;
283  this->UseWeakPointer = true;
284  }
285  void operator=(void *o)
286  {
287  this->VoidPointer = o;
288  this->WeakPointer = 0;
289  this->UseWeakPointer = false;
290  }
291  T *GetPointer()
292  {
293  if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
294  {
295  return 0;
296  }
297  return static_cast<T*>(this->VoidPointer);
298  }
299  private:
300  vtkWeakPointerBase WeakPointer;
301  void *VoidPointer;
302  bool UseWeakPointer;
303  };
305 
307 
308  template <class T>
309  class vtkClassMemberCallback : public vtkClassMemberCallbackBase
310  {
311  vtkClassMemberHandlerPointer<T> Handler;
312  void (T::*Method1)();
313  void (T::*Method2)(vtkObject*, unsigned long, void*);
314  bool (T::*Method3)(vtkObject*, unsigned long, void*);
316 
317  public:
318  vtkClassMemberCallback(T* handler, void (T::*method)())
319  {
320  this->Handler = handler;
321  this->Method1 = method;
322  this->Method2 = NULL;
323  this->Method3 = NULL;
324  }
325 
326  vtkClassMemberCallback(
327  T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
328  {
329  this->Handler = handler;
330  this->Method1 = NULL;
331  this->Method2 = method;
332  this->Method3 = NULL;
333  }
334 
335  vtkClassMemberCallback(
336  T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
337  {
338  this->Handler = handler;
339  this->Method1 = NULL;
340  this->Method2 = NULL;
341  this->Method3 = method;
342  }
343  virtual ~vtkClassMemberCallback() { }
344 
345  // Called when the event is invoked
346  virtual bool operator()(
347  vtkObject* caller, unsigned long event, void* calldata)
348  {
349  T *handler = this->Handler.GetPointer();
350  if (handler)
351  {
352  if (this->Method1)
353  {
354  (handler->*this->Method1)();
355  }
356  else if (this->Method2)
357  {
358  (handler->*this->Method2)(caller, event, calldata);
359  }
360  else if (this->Method3)
361  {
362  return (handler->*this->Method3)(caller, event, calldata);
363  }
364  }
365  return false;
366  }
367  };
368 
370 
371  unsigned long AddTemplatedObserver(
372  unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
373  // Friend to access AddTemplatedObserver().
374  friend class vtkObjectCommandInternal;
375 //ETX
376 };
378 
379 #endif
380 // VTK-HeaderTest-Exclude: vtkObject.h
unsigned long AddObserver(unsigned long event, U observer, void(T::*callback)(vtkObject *, unsigned long, void *), float priority=0.0f)
Definition: vtkObject.h:178
static vtkObjectBase * New()
abstract base class for most VTK objects
Definition: vtkObject.h:61
#define VTKCOMMONCORE_EXPORT
record modification and/or execution time
Definition: vtkTimeStamp.h:34
int InvokeEvent(unsigned long event)
Definition: vtkObject.h:215
virtual void PrintSelf(ostream &os, vtkIndent indent)
virtual void RegisterInternal(vtkObjectBase *, int check)
static void GlobalWarningDisplayOff()
Definition: vtkObject.h:112
unsigned long AddObserver(unsigned long event, U observer, bool(T::*callback)(vtkObject *, unsigned long, void *), float priority=0.0f)
Definition: vtkObject.h:194
superclass for callback/observer methods
Definition: vtkCommand.h:325
static void SetGlobalWarningDisplay(int val)
a simple class to control print indentation
Definition: vtkIndent.h:38
Non-templated superclass for vtkWeakPointer.
abstract base class for most VTK objects
Definition: vtkObjectBase.h:59
int InvokeEvent(const char *event)
Definition: vtkObject.h:216
virtual void UnRegisterInternal(vtkObjectBase *, int check)
unsigned long AddObserver(unsigned long event, U observer, void(T::*callback)(), float priority=0.0f)
Definition: vtkObject.h:168
bool Debug
Definition: vtkObject.h:227
void operator=(const vtkObjectBase &)
vtkSubjectHelper * SubjectHelper
Definition: vtkObject.h:229
static void GlobalWarningDisplayOn()
Definition: vtkObject.h:111
vtkTimeStamp MTime
Definition: vtkObject.h:228