VTK
dox/Common/Core/vtkObject.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkObject.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00049 #ifndef __vtkObject_h
00050 #define __vtkObject_h
00051 
00052 #include "vtkCommonCoreModule.h" // For export macro
00053 #include "vtkObjectBase.h"
00054 #include "vtkSetGet.h"
00055 #include "vtkTimeStamp.h"
00056 #include "vtkWeakPointerBase.h" // needed for vtkWeakPointer
00057 
00058 class vtkSubjectHelper;
00059 class vtkCommand;
00060 
00061 class VTKCOMMONCORE_EXPORT vtkObject : public vtkObjectBase
00062 {
00063 public:
00064   vtkTypeMacro(vtkObject,vtkObjectBase);
00065 
00068   static vtkObject *New();
00069 
00070 #ifdef _WIN32
00071   // avoid dll boundary problems
00072   void* operator new( size_t tSize );
00073   void operator delete( void* p );
00074 #endif
00075 
00077   virtual void DebugOn();
00078 
00080   virtual void DebugOff();
00081 
00083   unsigned char GetDebug();
00084 
00086   void SetDebug(unsigned char debugFlag);
00087 
00090   static void BreakOnError();
00091 
00096   virtual void Modified();
00097 
00099   virtual unsigned long GetMTime();
00100 
00105   virtual void PrintSelf(ostream& os, vtkIndent indent);
00106 
00108 
00110   static void SetGlobalWarningDisplay(int val);
00111   static void GlobalWarningDisplayOn(){vtkObject::SetGlobalWarningDisplay(1);};
00112   static void GlobalWarningDisplayOff()
00113     {vtkObject::SetGlobalWarningDisplay(0);};
00114   static int  GetGlobalWarningDisplay();
00116 
00117 //BTX
00119 
00128   unsigned long AddObserver(unsigned long event, vtkCommand *,
00129                             float priority=0.0f);
00130   unsigned long AddObserver(const char *event, vtkCommand *,
00131                             float priority=0.0f);
00132   vtkCommand *GetCommand(unsigned long tag);
00133   void RemoveObserver(vtkCommand*);
00134   void RemoveObservers(unsigned long event, vtkCommand *);
00135   void RemoveObservers(const char *event, vtkCommand *);
00136   int HasObserver(unsigned long event, vtkCommand *);
00137   int HasObserver(const char *event, vtkCommand *);
00138 //ETX
00139   void RemoveObserver(unsigned long tag);
00140   void RemoveObservers(unsigned long event);
00141   void RemoveObservers(const char *event);
00142   void RemoveAllObservers(); //remove every last one of them
00143   int HasObserver(unsigned long event);
00144   int HasObserver(const char *event);
00146 
00147 //BTX
00149 
00167   template <class U, class T>
00168   unsigned long AddObserver(unsigned long event,
00169     U observer, void (T::*callback)(), float priority=0.0f)
00170     {
00171     vtkClassMemberCallback<T> *callable =
00172       new vtkClassMemberCallback<T>(observer, callback);
00173     // callable is deleted when the observer is cleaned up (look at
00174     // vtkObjectCommandInternal)
00175     return this->AddTemplatedObserver(event, callable, priority);
00176     }
00177   template <class U, class T>
00178   unsigned long AddObserver(unsigned long event,
00179     U observer, void (T::*callback)(vtkObject*, unsigned long, void*),
00180     float priority=0.0f)
00181     {
00182     vtkClassMemberCallback<T> *callable =
00183       new vtkClassMemberCallback<T>(observer, callback);
00184     // callable is deleted when the observer is cleaned up (look at
00185     // vtkObjectCommandInternal)
00186     return this->AddTemplatedObserver(event, callable, priority);
00187     }
00189 
00191 
00193   template <class U, class T>
00194   unsigned long AddObserver(unsigned long event,
00195     U observer, bool (T::*callback)(vtkObject*, unsigned long, void*),
00196     float priority=0.0f)
00197     {
00198     vtkClassMemberCallback<T> *callable =
00199       new vtkClassMemberCallback<T>(observer, callback);
00200     // callable is deleted when the observer is cleaned up (look at
00201     // vtkObjectCommandInternal)
00202     return this->AddTemplatedObserver(event, callable, priority);
00203     }
00204 //ETX
00206 
00207 //BTX
00209 
00212   int InvokeEvent(unsigned long event, void *callData);
00213   int InvokeEvent(const char *event, void *callData);
00214 //ETX
00215   int InvokeEvent(unsigned long event) { return this->InvokeEvent(event, NULL); };
00216   int InvokeEvent(const char *event) { return this->InvokeEvent(event, NULL); };
00218 
00219 protected:
00220   vtkObject();
00221   virtual ~vtkObject();
00222 
00223   // See vtkObjectBase.h.
00224   virtual void RegisterInternal(vtkObjectBase*, int check);
00225   virtual void UnRegisterInternal(vtkObjectBase*, int check);
00226 
00227   unsigned char     Debug;      // Enable debug messages
00228   vtkTimeStamp      MTime;      // Keep track of modification time
00229   vtkSubjectHelper *SubjectHelper; // List of observers on this object
00230 
00231 //BTX
00233 
00240   void InternalGrabFocus(vtkCommand *mouseEvents, vtkCommand *keypressEvents=NULL);
00241   void InternalReleaseFocus();
00242 //ETX
00243 //BTX
00244 private:
00245   vtkObject(const vtkObject&);  // Not implemented.
00246   void operator=(const vtkObject&);  // Not implemented.
00248 
00250 
00255   class vtkClassMemberCallbackBase
00256     {
00257   public:
00259 
00260 
00261     virtual bool operator()(vtkObject*, unsigned long, void*) = 0;
00262     virtual ~vtkClassMemberCallbackBase(){}
00263     };
00265 
00267 
00269   template<class T>
00270     class vtkClassMemberHandlerPointer
00271       {
00272     public:
00273       void operator=(vtkObjectBase *o)
00274         {
00275         // The cast is needed in case "o" has multi-inheritance,
00276         // to offset the pointer to get the vtkObjectBase.
00277         if ((this->VoidPointer = dynamic_cast<T*>(o)) == 0)
00278           {
00279           // fallback to just using its vtkObjectBase as-is.
00280           this->VoidPointer = o;
00281           }
00282         this->WeakPointer = o;
00283         this->UseWeakPointer = true;
00284         }
00285       void operator=(void *o)
00286         {
00287         this->VoidPointer = o;
00288         this->WeakPointer = 0;
00289         this->UseWeakPointer = false;
00290         }
00291       T *GetPointer()
00292         {
00293         if (this->UseWeakPointer && !this->WeakPointer.GetPointer())
00294           {
00295           return 0;
00296           }
00297         return static_cast<T*>(this->VoidPointer);
00298         }
00299     private:
00300       vtkWeakPointerBase WeakPointer;
00301       void *VoidPointer;
00302       bool UseWeakPointer;
00303       };
00305 
00307 
00308   template <class T>
00309     class vtkClassMemberCallback : public vtkClassMemberCallbackBase
00310       {
00311       vtkClassMemberHandlerPointer<T> Handler;
00312       void (T::*Method1)();
00313       void (T::*Method2)(vtkObject*, unsigned long, void*);
00314       bool (T::*Method3)(vtkObject*, unsigned long, void*);
00316 
00317     public:
00318       vtkClassMemberCallback(T* handler, void (T::*method)())
00319         {
00320         this->Handler = handler;
00321         this->Method1 = method;
00322         this->Method2 = NULL;
00323         this->Method3 = NULL;
00324         }
00325 
00326       vtkClassMemberCallback(
00327         T* handler, void (T::*method)(vtkObject*, unsigned long, void*))
00328         {
00329         this->Handler = handler;
00330         this->Method1 = NULL;
00331         this->Method2 = method;
00332         this->Method3 = NULL;
00333         }
00334 
00335       vtkClassMemberCallback(
00336         T* handler, bool (T::*method)(vtkObject*, unsigned long, void*))
00337         {
00338         this->Handler = handler;
00339         this->Method1 = NULL;
00340         this->Method2 = NULL;
00341         this->Method3 = method;
00342         }
00343       virtual ~vtkClassMemberCallback() { }
00344 
00345       // Called when the event is invoked
00346       virtual bool operator()(
00347         vtkObject* caller, unsigned long event, void* calldata)
00348         {
00349         T *handler = this->Handler.GetPointer();
00350         if (handler)
00351           {
00352           if (this->Method1)
00353             {
00354             (handler->*this->Method1)();
00355             }
00356           else if (this->Method2)
00357             {
00358             (handler->*this->Method2)(caller, event, calldata);
00359             }
00360           else if (this->Method3)
00361             {
00362             return (handler->*this->Method3)(caller, event, calldata);
00363             }
00364           }
00365         return false;
00366         }
00367       };
00368 
00370 
00371   unsigned long AddTemplatedObserver(
00372     unsigned long event, vtkClassMemberCallbackBase* callable, float priority);
00373   // Friend to access AddTemplatedObserver().
00374   friend class vtkObjectCommandInternal;
00375 //ETX
00376 };
00378 
00379 #endif
00380 // VTK-HeaderTest-Exclude: vtkObject.h