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 "vtkObjectBase.h"
53 #include "vtkSetGet.h"
54 #include "vtkTimeStamp.h"
55 #include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
56 
57 class vtkSubjectHelper;
58 class vtkCommand;
59 
61 {
62 public:
63  vtkTypeMacro(vtkObject,vtkObjectBase);
64 
67  static vtkObject *New();
68 
69 #ifdef _WIN32
70  // avoid dll boundary problems
71  void* operator new( size_t tSize );
72  void operator delete( void* p );
73 #endif
74 
76  virtual void DebugOn();
77 
79  virtual void DebugOff();
80 
82  unsigned char GetDebug();
83 
85  void SetDebug(unsigned char debugFlag);
86 
89  static void BreakOnError();
90 
95  virtual void Modified();
96 
98  virtual unsigned long GetMTime();
99 
104  virtual void PrintSelf(ostream& os, vtkIndent indent);
105 
107 
109  static void SetGlobalWarningDisplay(int val);
111  static void GlobalWarningDisplayOff()
113  static int GetGlobalWarningDisplay();
115 
116 //BTX
118 
127  unsigned long AddObserver(unsigned long event, vtkCommand *,
128  float priority=0.0f);
129  unsigned long AddObserver(const char *event, vtkCommand *,
130  float priority=0.0f);
131  vtkCommand *GetCommand(unsigned long tag);
132  void RemoveObserver(vtkCommand*);
133  void RemoveObservers(unsigned long event, vtkCommand *);
134  void RemoveObservers(const char *event, vtkCommand *);
135  int HasObserver(unsigned long event, vtkCommand *);
136  int HasObserver(const char *event, vtkCommand *);
137 //ETX
138  void RemoveObserver(unsigned long tag);
139  void RemoveObservers(unsigned long event);
140  void RemoveObservers(const char *event);
141  void RemoveAllObservers(); //remove every last one of them
142  int HasObserver(unsigned long event);
143  int HasObserver(const char *event);
145 
146 //BTX
148 
166  template <class U, class T>
167  unsigned long AddObserver(unsigned long event,
168  U observer, void (T::*callback)(), float priority=0.0f)
169  {
170  vtkClassMemberCallback<T> *callable =
171  new vtkClassMemberCallback<T>(observer, callback);
172  // callable is deleted when the observer is cleaned up (look at
173  // vtkObjectCommandInternal)
174  return this->AddTemplatedObserver(event, callable, priority);
175  }
176  template <class U, class T>
177  unsigned long AddObserver(unsigned long event,
178  U observer, void (T::*callback)(vtkObject*, unsigned long, void*),
179  float priority=0.0f)
180  {
181  vtkClassMemberCallback<T> *callable =
182  new vtkClassMemberCallback<T>(observer, callback);
183  // callable is deleted when the observer is cleaned up (look at
184  // vtkObjectCommandInternal)
185  return this->AddTemplatedObserver(event, callable, priority);
186  }
187 //ETX
189 
190 //BTX
192 
195  int InvokeEvent(unsigned long event, void *callData);
196  int InvokeEvent(const char *event, void *callData);
197 //ETX
198  int InvokeEvent(unsigned long event) { return this->InvokeEvent(event, NULL); };
199  int InvokeEvent(const char *event) { return this->InvokeEvent(event, NULL); };
201 
202 protected:
203  vtkObject();
204  virtual ~vtkObject();
205 
206  // See vtkObjectBase.h.
207  virtual void RegisterInternal(vtkObjectBase*, int check);
208  virtual void UnRegisterInternal(vtkObjectBase*, int check);
209 
210  unsigned char Debug; // Enable debug messages
211  vtkTimeStamp MTime; // Keep track of modification time
212  vtkSubjectHelper *SubjectHelper; // List of observers on this object
213 
214 //BTX
216 
223  void InternalGrabFocus(vtkCommand *mouseEvents, vtkCommand *keypressEvents=NULL);
224  void InternalReleaseFocus();
225 //ETX
226 //BTX
227 private:
228  vtkObject(const vtkObject&); // Not implemented.
229  void operator=(const vtkObject&); // Not implemented.
231 
233 
238  class vtkClassMemberCallbackBase
239  {
240  public:
241  // Description:
242  // Called when the event is invoked
243  virtual void operator()(vtkObject*, unsigned long, void*) = 0;
244  virtual ~vtkClassMemberCallbackBase(){}
245  };
247 
249 
251  template<class T>
252  class vtkClassMemberHandlerPointer
253  {
254  public:
255  void operator=(vtkObjectBase *o)
256  {
257  // The cast is needed in case "o" has multi-inheritance,
258  // to offset the pointer to get the vtkObjectBase.
259  if ((this->VoidPointer = dynamic_cast<T*>(o)) == 0)
260  {
261  // fallback to just using its vtkObjectBase as-is.
262  this->VoidPointer = o;
263  }
264  this->WeakPointer = o;
265  this->UseWeakPointer = true;
266  }
267  void operator=(void *o)
268  {
269  this->VoidPointer = o;
270  this->WeakPointer = 0;
271  this->UseWeakPointer = false;
272  }
273  T *GetPointer()
274  {
275  if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
276  {
277  return 0;
278  }
279  return static_cast<T*>(this->VoidPointer);
280  }
281  private:
282  vtkWeakPointerBase WeakPointer;
283  void *VoidPointer;
284  bool UseWeakPointer;
285  };
287 
289 
290  template <class T>
291  class vtkClassMemberCallback : public vtkClassMemberCallbackBase
292  {
293  vtkClassMemberHandlerPointer<T> Handler;
294  void (T::*Method1)();
295  void (T::*Method2)(vtkObject*, unsigned long, void*);
297 
298  public:
299  vtkClassMemberCallback(T* handler, void (T::*method)())
300  {
301  this->Handler = handler;
302  this->Method1 = method;
303  this->Method2 = NULL;
304  }
305 
306  vtkClassMemberCallback(
307  T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
308  {
309  this->Handler = handler;
310  this->Method1 = NULL;
311  this->Method2 = method;
312  }
313  virtual ~vtkClassMemberCallback() { }
314 
315  // Called when the event is invoked
316  virtual void operator()(
317  vtkObject* caller, unsigned long event, void* calldata)
318  {
319  T *handler = this->Handler.GetPointer();
320  if (handler)
321  {
322  if (this->Method1)
323  {
324  (handler->*this->Method1)();
325  }
326  else if (this->Method2)
327  {
328  (handler->*this->Method2)(caller, event, calldata);
329  }
330  }
331  }
332  };
333 
335 
336  unsigned long AddTemplatedObserver(
337  unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
338  // Friend to access AddTemplatedObserver().
339  friend class vtkObjectCommandInternal;
340 //ETX
341 };
343 
344 #endif
unsigned long AddObserver(unsigned long event, U observer, void(T::*callback)(vtkObject *, unsigned long, void *), float priority=0.0f)
Definition: vtkObject.h:177
static vtkObjectBase * New()
abstract base class for most VTK objects
Definition: vtkObject.h:60
unsigned char Debug
Definition: vtkObject.h:210
record modification and/or execution time
Definition: vtkTimeStamp.h:33
int InvokeEvent(unsigned long event)
Definition: vtkObject.h:198
virtual void PrintSelf(ostream &os, vtkIndent indent)
virtual void RegisterInternal(vtkObjectBase *, int check)
static void GlobalWarningDisplayOff()
Definition: vtkObject.h:111
superclass for callback/observer methods
Definition: vtkCommand.h:211
vtkObjectBase * GetPointer() const
static void SetGlobalWarningDisplay(int val)
a simple class to control print indentation
Definition: vtkIndent.h:37
Non-templated superclass for vtkWeakPointer.
#define VTK_COMMON_EXPORT
abstract base class for most VTK objects
Definition: vtkObjectBase.h:59
int InvokeEvent(const char *event)
Definition: vtkObject.h:199
virtual void UnRegisterInternal(vtkObjectBase *, int check)
unsigned long AddObserver(unsigned long event, U observer, void(T::*callback)(), float priority=0.0f)
Definition: vtkObject.h:167
void operator=(const vtkObjectBase &)
vtkSubjectHelper * SubjectHelper
Definition: vtkObject.h:212
static void GlobalWarningDisplayOn()
Definition: vtkObject.h:110
vtkTimeStamp MTime
Definition: vtkObject.h:211