Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itkCommand.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkCommand.h,v $
00005   Language:  C++
00006   Date:      $Date: 2005/03/22 18:25:12 $
00007   Version:   $Revision: 1.33 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even 
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00014      PURPOSE.  See the above copyright notices for more information.
00015 
00016 =========================================================================*/
00017 #ifndef __itkCommand_h
00018 #define __itkCommand_h
00019 
00020 #include "itkObject.h"
00021 #include "itkObjectFactory.h"
00022 
00023 
00024 namespace itk
00025 {
00026 
00039 // The superclass that all commands should be subclasses of
00040 class ITKCommon_EXPORT Command : public Object
00041 {
00042 public:
00044   typedef Command         Self;
00045   typedef Object Superclass;
00046   typedef SmartPointer<Self>  Pointer;
00047   typedef SmartPointer<const Self>  ConstPointer;
00048   
00050   itkTypeMacro(Command,Object);
00051   
00053   virtual void Execute(Object *caller, const EventObject & event ) = 0;
00054 
00058   virtual void Execute(const Object *caller, const EventObject & event ) = 0;
00059 
00060 protected:
00061   Command();
00062   ~Command();
00063 
00064 private:
00065   Command(const Self&); //purposely not implemented
00066   void operator=(const Self&); //purposely not implemented
00067 };
00068 
00069  
00070 // some implementations for several callback types
00071 
00080 template <class T>
00081 class MemberCommand : public Command
00082 {
00083 public:
00085   typedef  void (T::*TMemberFunctionPointer)(Object*, const EventObject &);
00086   typedef  void (T::*TConstMemberFunctionPointer)(const Object*, const EventObject &);
00087     
00089   typedef MemberCommand         Self;
00090   typedef SmartPointer<Self>  Pointer;
00091   
00093   itkNewMacro(Self);
00094   
00096   itkTypeMacro(MemberCommand,Command);
00097 
00100   void SetCallbackFunction(T* object,  
00101                            TMemberFunctionPointer memberFunction)
00102     {
00103       m_This = object;
00104       m_MemberFunction = memberFunction;
00105     }
00106   void SetCallbackFunction(T* object,  
00107                            TConstMemberFunctionPointer memberFunction)
00108     {
00109       m_This = object;
00110       m_ConstMemberFunction = memberFunction;
00111     }
00112   
00114   virtual void Execute(Object *caller, const EventObject & event )
00115     { 
00116       if( m_MemberFunction ) 
00117       {
00118         ((*m_This).*(m_MemberFunction))(caller, event);
00119       }
00120     }
00121 
00123   virtual void Execute( const Object *caller, const EventObject & event )
00124     { 
00125       if( m_ConstMemberFunction ) 
00126       {
00127         ((*m_This).*(m_ConstMemberFunction))(caller, event);
00128       }
00129     }
00130 
00131 protected:
00132   T* m_This;
00133   TMemberFunctionPointer m_MemberFunction;
00134   TConstMemberFunctionPointer m_ConstMemberFunction;
00135   MemberCommand():m_MemberFunction(0),m_ConstMemberFunction(0) {}
00136   virtual ~MemberCommand(){}
00137 
00138 private:
00139   MemberCommand(const Self&); //purposely not implemented
00140   void operator=(const Self&); //purposely not implemented
00141 
00142 };
00143 
00144 
00153 template <class T>
00154 class ReceptorMemberCommand : public Command
00155 {
00156 public:
00158   typedef  void (T::*TMemberFunctionPointer)(const EventObject &);
00159   
00161   typedef ReceptorMemberCommand         Self;
00162   typedef SmartPointer<Self>  Pointer;
00163   
00165   itkNewMacro(Self);
00166   
00168   itkTypeMacro(ReceptorMemberCommand,Command);
00169 
00172   void SetCallbackFunction(T* object,  
00173                            TMemberFunctionPointer memberFunction)
00174     {
00175       m_This = object;
00176       m_MemberFunction = memberFunction;
00177     }
00178 
00180   virtual void Execute(Object *, const EventObject & event )
00181     { 
00182       if( m_MemberFunction ) 
00183       {
00184         ((*m_This).*(m_MemberFunction))(event);
00185       }
00186     }
00187 
00189   virtual void Execute( const Object *, const EventObject & event )
00190     { 
00191       if( m_MemberFunction ) 
00192       {
00193         ((*m_This).*(m_MemberFunction))(event);
00194       }
00195     }
00196 
00197 protected:
00198   T* m_This;
00199   TMemberFunctionPointer m_MemberFunction;
00200   ReceptorMemberCommand():m_MemberFunction(0) {}
00201   virtual ~ReceptorMemberCommand() {}
00202 
00203 private:
00204   ReceptorMemberCommand(const Self&); //purposely not implemented
00205   void operator=(const Self&); //purposely not implemented
00206   
00207 };
00208 
00209 
00218 template <class T>
00219 class SimpleMemberCommand : public Command
00220 { 
00221 public:
00223   typedef  void (T::*TMemberFunctionPointer)(); 
00224   
00226   typedef SimpleMemberCommand         Self;
00227   typedef SmartPointer<Self>  Pointer;
00228   
00230   itkTypeMacro(SimpleMemberCommand,Command);
00231 
00233   itkNewMacro(Self);
00234 
00236   void SetCallbackFunction(T* object,  
00237                            TMemberFunctionPointer memberFunction)
00238     {
00239       m_This = object;
00240       m_MemberFunction = memberFunction;
00241     }
00242   
00244   virtual void Execute(Object *,const EventObject & ) 
00245     { 
00246       if( m_MemberFunction ) 
00247       {
00248         ((*m_This).*(m_MemberFunction))();
00249       }
00250     }
00251   virtual void Execute(const Object *,const EventObject & ) 
00252     { 
00253       if( m_MemberFunction ) 
00254       {
00255         ((*m_This).*(m_MemberFunction))();
00256       }
00257     }
00258   
00259 protected:
00260   T* m_This;
00261   TMemberFunctionPointer m_MemberFunction;
00262   SimpleMemberCommand():m_MemberFunction(0) {}
00263   virtual ~SimpleMemberCommand() {}
00264 
00265 private:
00266   SimpleMemberCommand(const Self&); //purposely not implemented
00267   void operator=(const Self&); //purposely not implemented
00268 
00269 };
00270 
00271 
00280 template <class T>
00281 class SimpleConstMemberCommand : public Command
00282 { 
00283 public:
00285   typedef  void (T::*TMemberFunctionPointer)() const; 
00286 
00288   typedef SimpleConstMemberCommand         Self;
00289   typedef SmartPointer<Self>  Pointer;
00290   
00292   itkTypeMacro(SimpleConstMemberCommand,Command);
00293 
00295   itkNewMacro(Self);
00296 
00298   void SetCallbackFunction(const T* object,  
00299                            TMemberFunctionPointer memberFunction)
00300     {
00301       m_This = object;
00302       m_MemberFunction = memberFunction;
00303     }
00304   
00306   virtual void Execute(Object *,const EventObject & ) 
00307     { 
00308       if( m_MemberFunction ) 
00309       {
00310         ((*m_This).*(m_MemberFunction))();
00311       }
00312     }
00313   virtual void Execute(const Object *,const EventObject & ) 
00314     { 
00315       if( m_MemberFunction ) 
00316       {
00317         ((*m_This).*(m_MemberFunction))();
00318       }
00319     }
00320   
00321 protected:
00322   const T* m_This;
00323   TMemberFunctionPointer m_MemberFunction;
00324   SimpleConstMemberCommand():m_MemberFunction(0) {}
00325   virtual ~SimpleConstMemberCommand() {} 
00326 
00327 private:
00328   SimpleConstMemberCommand(const Self&); //purposely not implemented
00329   void operator=(const Self&); //purposely not implemented
00330   
00331 };
00332 
00333 
00345 class CStyleCommand : public Command
00346 {
00347 public:
00349   typedef  void (*FunctionPointer)(Object*, const EventObject &, void*);
00350   typedef  void (*ConstFunctionPointer)(const Object*, const EventObject &, void*);
00351   typedef  void (*DeleteDataFunctionPointer)(void*);
00352   
00354   typedef CStyleCommand         Self;
00355   typedef SmartPointer<Self>  Pointer;
00356   
00358   itkTypeMacro(CStyleCommand,Command);
00359   
00361   itkNewMacro(Self);
00362 
00365   void SetClientData(void *cd) {m_ClientData = cd;}
00366 
00368   void SetCallback(FunctionPointer f)
00369     {m_Callback = f;}
00370   void SetConstCallback(ConstFunctionPointer f)
00371     {m_ConstCallback = f;}
00372   
00374   void SetClientDataDeleteCallback(DeleteDataFunctionPointer f)
00375     {m_ClientDataDeleteCallback = f;}
00376   
00378   void Execute(Object *caller, const EventObject & event )
00379     {
00380     if (m_Callback)
00381       {
00382       m_Callback(caller, event, m_ClientData );
00383       }
00384     };
00385 
00387   void Execute(const Object *caller, const EventObject & event )
00388     {
00389     if (m_ConstCallback)
00390       {
00391       m_ConstCallback(caller, event, m_ClientData );
00392       }
00393     };
00394 
00395 protected:
00396   CStyleCommand(): m_ClientData(0),
00397                    m_Callback(0),
00398                    m_ConstCallback(0),
00399                    m_ClientDataDeleteCallback(0) {}
00400   ~CStyleCommand() 
00401     { 
00402     if (m_ClientDataDeleteCallback)
00403       {
00404       m_ClientDataDeleteCallback(m_ClientData);
00405       }
00406     };
00407   void *m_ClientData;
00408   FunctionPointer m_Callback;
00409   ConstFunctionPointer m_ConstCallback;
00410   DeleteDataFunctionPointer m_ClientDataDeleteCallback;
00411 };
00412 
00413 
00414 } // end namespace itk
00415 
00416 #endif

Generated at Wed May 24 22:57:17 2006 for ITK by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2000