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: 2002/09/11 19:39:03 $
00007   Version:   $Revision: 1.28 $
00008 
00009   Copyright (c) 2002 Insight 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 ITK_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 };
00061 
00062  
00063 // some implementations for several callback types
00064 
00073 template <class T>
00074 class MemberCommand : public Command
00075 {
00076 public:
00078   typedef  void (T::*TMemberFunctionPointer)(Object*, const EventObject &);
00079   typedef  void (T::*TConstMemberFunctionPointer)(const Object*, const EventObject &);
00080     
00082   typedef MemberCommand         Self;
00083   typedef SmartPointer<Self>  Pointer;
00084   
00086   itkNewMacro(Self);
00087   
00089   itkTypeMacro(MemberCommand,Command);
00090 
00093   void SetCallbackFunction(T* object,  
00094                            TMemberFunctionPointer memberFunction)
00095     {
00096       m_This = object;
00097       m_MemberFunction = memberFunction;
00098     }
00099   void SetCallbackFunction(T* object,  
00100                            TConstMemberFunctionPointer memberFunction)
00101     {
00102       m_This = object;
00103       m_ConstMemberFunction = memberFunction;
00104     }
00105   
00107   virtual void Execute(Object *caller, const EventObject & event )
00108     { 
00109       if( m_MemberFunction ) 
00110       {
00111         ((*m_This).*(m_MemberFunction))(caller, event);
00112       }
00113     }
00114 
00116   virtual void Execute( const Object *caller, const EventObject & event )
00117     { 
00118       if( m_ConstMemberFunction ) 
00119       {
00120         ((*m_This).*(m_ConstMemberFunction))(caller, event);
00121       }
00122     }
00123 
00124 protected:
00125   T* m_This;
00126   TMemberFunctionPointer m_MemberFunction;
00127   TConstMemberFunctionPointer m_ConstMemberFunction;
00128   MemberCommand():m_MemberFunction(0),m_ConstMemberFunction(0) {}
00129   virtual ~MemberCommand(){}
00130 
00131 private:
00132   MemberCommand(const Self&); //purposely not implemented
00133   void operator=(const Self&); //purposely not implemented
00134 
00135 };
00136 
00137 
00146 template <class T>
00147 class ReceptorMemberCommand : public Command
00148 {
00149 public:
00151   typedef  void (T::*TMemberFunctionPointer)(const EventObject &);
00152   
00154   typedef ReceptorMemberCommand         Self;
00155   typedef SmartPointer<Self>  Pointer;
00156   
00158   itkNewMacro(Self);
00159   
00161   itkTypeMacro(ReceptorMemberCommand,Command);
00162 
00165   void SetCallbackFunction(T* object,  
00166                            TMemberFunctionPointer memberFunction)
00167     {
00168       m_This = object;
00169       m_MemberFunction = memberFunction;
00170     }
00171 
00173   virtual void Execute(Object *caller, const EventObject & event )
00174     { 
00175       if( m_MemberFunction ) 
00176       {
00177         ((*m_This).*(m_MemberFunction))(event);
00178       }
00179     }
00180 
00182   virtual void Execute( const Object *caller, const EventObject & event )
00183     { 
00184       if( m_MemberFunction ) 
00185       {
00186         ((*m_This).*(m_MemberFunction))(event);
00187       }
00188     }
00189 
00190 protected:
00191   T* m_This;
00192   TMemberFunctionPointer m_MemberFunction;
00193   ReceptorMemberCommand():m_MemberFunction(0) {}
00194   virtual ~ReceptorMemberCommand() {}
00195 
00196 private:
00197   ReceptorMemberCommand(const Self&); //purposely not implemented
00198   void operator=(const Self&); //purposely not implemented
00199   
00200 };
00201 
00202 
00211 template <class T>
00212 class SimpleMemberCommand : public Command
00213 { 
00214 public:
00216   typedef  void (T::*TMemberFunctionPointer)(); 
00217   
00219   typedef SimpleMemberCommand         Self;
00220   typedef SmartPointer<Self>  Pointer;
00221   
00223   itkTypeMacro(SimpleMemberCommand,Command);
00224 
00226   itkNewMacro(Self);
00227 
00229   void SetCallbackFunction(T* object,  
00230                            TMemberFunctionPointer memberFunction)
00231     {
00232       m_This = object;
00233       m_MemberFunction = memberFunction;
00234     }
00235   
00237   virtual void Execute(Object *,const EventObject & ) 
00238     { 
00239       if( m_MemberFunction ) 
00240       {
00241         ((*m_This).*(m_MemberFunction))();
00242       }
00243     }
00244   virtual void Execute(const Object *,const EventObject & ) 
00245     { 
00246       if( m_MemberFunction ) 
00247       {
00248         ((*m_This).*(m_MemberFunction))();
00249       }
00250     }
00251   
00252 protected:
00253   T* m_This;
00254   TMemberFunctionPointer m_MemberFunction;
00255   SimpleMemberCommand():m_MemberFunction(0) {}
00256   virtual ~SimpleMemberCommand() {}
00257 
00258 private:
00259   SimpleMemberCommand(const Self&); //purposely not implemented
00260   void operator=(const Self&); //purposely not implemented
00261 
00262 };
00263 
00264 
00273 template <class T>
00274 class SimpleConstMemberCommand : public Command
00275 { 
00276 public:
00278   typedef  void (T::*TMemberFunctionPointer)() const; 
00279 
00281   typedef SimpleConstMemberCommand         Self;
00282   typedef SmartPointer<Self>  Pointer;
00283   
00285   itkTypeMacro(SimpleConstMemberCommand,Command);
00286 
00288   itkNewMacro(Self);
00289 
00291   void SetCallbackFunction(const T* object,  
00292                            TMemberFunctionPointer memberFunction)
00293     {
00294       m_This = object;
00295       m_MemberFunction = memberFunction;
00296     }
00297   
00299   virtual void Execute(Object *,const EventObject & event ) 
00300     { 
00301       if( m_MemberFunction ) 
00302       {
00303         ((*m_This).*(m_MemberFunction))();
00304       }
00305     }
00306   virtual void Execute(const Object *,const EventObject & event ) 
00307     { 
00308       if( m_MemberFunction ) 
00309       {
00310         ((*m_This).*(m_MemberFunction))();
00311       }
00312     }
00313   
00314 protected:
00315   const T* m_This;
00316   TMemberFunctionPointer m_MemberFunction;
00317   SimpleConstMemberCommand():m_MemberFunction(0) {}
00318   virtual ~SimpleConstMemberCommand() {} 
00319 
00320 private:
00321   SimpleConstMemberCommand(const Self&); //purposely not implemented
00322   void operator=(const Self&); //purposely not implemented
00323   
00324 };
00325 
00326 
00338 class CStyleCommand : public Command
00339 {
00340 public:
00342   typedef  void (*FunctionPointer)(Object*, const EventObject &, void*);
00343   typedef  void (*ConstFunctionPointer)(const Object*, const EventObject &, void*);
00344   typedef  void (*DeleteDataFunctionPointer)(void*);
00345   
00347   typedef CStyleCommand         Self;
00348   typedef SmartPointer<Self>  Pointer;
00349   
00351   itkTypeMacro(CStyleCommand,Command);
00352   
00354   itkNewMacro(Self);
00355 
00358   void SetClientData(void *cd) {m_ClientData = cd;}
00359 
00361   void SetCallback(FunctionPointer f)
00362     {m_Callback = f;}
00363   void SetConstCallback(ConstFunctionPointer f)
00364     {m_ConstCallback = f;}
00365   
00367   void SetClientDataDeleteCallback(DeleteDataFunctionPointer f)
00368     {m_ClientDataDeleteCallback = f;}
00369   
00371   void Execute(Object *caller, const EventObject & event )
00372     {
00373     if (m_Callback)
00374       {
00375       m_Callback(caller, event, m_ClientData );
00376       }
00377     };
00378 
00380   void Execute(const Object *caller, const EventObject & event )
00381     {
00382     if (m_ConstCallback)
00383       {
00384       m_ConstCallback(caller, event, m_ClientData );
00385       }
00386     };
00387 
00388 protected:
00389   CStyleCommand(): m_ClientData(0),
00390                    m_Callback(0),
00391                    m_ConstCallback(0),
00392                    m_ClientDataDeleteCallback(0) {}
00393   ~CStyleCommand() 
00394     { 
00395     if (m_ClientDataDeleteCallback)
00396       {
00397       m_ClientDataDeleteCallback(m_ClientData);
00398       }
00399     };
00400   void *m_ClientData;
00401   FunctionPointer m_Callback;
00402   ConstFunctionPointer m_ConstCallback;
00403   DeleteDataFunctionPointer m_ClientDataDeleteCallback;
00404 };
00405 
00406 
00407 } // end namespace itk
00408 
00409 #endif

Generated at Wed Mar 12 01:12:50 2003 for ITK by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2000