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: 2007-01-26 14:28:44 $
00007   Version:   $Revision: 1.36 $
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*, 
00087                                                   const EventObject &);
00089 
00091   typedef MemberCommand       Self;
00092   typedef SmartPointer<Self>  Pointer;
00093 
00095   itkNewMacro(Self);
00096 
00098   itkTypeMacro(MemberCommand,Command);
00099 
00102   void SetCallbackFunction(T* object,  
00103                            TMemberFunctionPointer memberFunction)
00104     {
00105     m_This = object;
00106     m_MemberFunction = memberFunction;
00107     }
00108   void SetCallbackFunction(T* object,  
00109                            TConstMemberFunctionPointer memberFunction)
00110     {
00111     m_This = object;
00112     m_ConstMemberFunction = memberFunction;
00113     }
00115 
00117   virtual void Execute(Object *caller, const EventObject & event )
00118     { 
00119     if( m_MemberFunction ) 
00120       {
00121       ((*m_This).*(m_MemberFunction))(caller, event);
00122       }
00123     }
00125 
00127   virtual void Execute( const Object *caller, const EventObject & event )
00128     { 
00129     if( m_ConstMemberFunction ) 
00130       {
00131       ((*m_This).*(m_ConstMemberFunction))(caller, event);
00132       }
00133     }
00135 
00136 protected:
00137 
00138   T* m_This;
00139   TMemberFunctionPointer m_MemberFunction;
00140   TConstMemberFunctionPointer m_ConstMemberFunction;
00141   MemberCommand():m_MemberFunction(0),m_ConstMemberFunction(0) {}
00142   virtual ~MemberCommand(){}
00143 
00144 private:
00145   MemberCommand(const Self&); //purposely not implemented
00146   void operator=(const Self&); //purposely not implemented
00147 
00148 };
00149 
00150 
00159 template <class T>
00160 class ReceptorMemberCommand : public Command
00161 {
00162 public:
00164   typedef  void (T::*TMemberFunctionPointer)(const EventObject &);
00165 
00167   typedef ReceptorMemberCommand  Self;
00168   typedef SmartPointer<Self>     Pointer;
00169 
00171   itkNewMacro(Self);
00172 
00174   itkTypeMacro(ReceptorMemberCommand,Command);
00175 
00178   void SetCallbackFunction(T* object,  
00179                            TMemberFunctionPointer memberFunction)
00180     {
00181     m_This = object;
00182     m_MemberFunction = memberFunction;
00183     }
00184 
00186   virtual void Execute(Object *, const EventObject & event )
00187     { 
00188     if( m_MemberFunction ) 
00189       {
00190       ((*m_This).*(m_MemberFunction))(event);
00191       }
00192     }
00194 
00196   virtual void Execute( const Object *, const EventObject & event )
00197     { 
00198     if( m_MemberFunction ) 
00199       {
00200       ((*m_This).*(m_MemberFunction))(event);
00201       }
00202     }
00204 
00205 protected:
00206   T* m_This;
00207   TMemberFunctionPointer m_MemberFunction;
00208   ReceptorMemberCommand():m_MemberFunction(0) {}
00209   virtual ~ReceptorMemberCommand() {}
00210 
00211 private:
00212   ReceptorMemberCommand(const Self&); //purposely not implemented
00213   void operator=(const Self&); //purposely not implemented
00214   
00215 };
00216 
00217 
00226 template <class T>
00227 class SimpleMemberCommand : public Command
00228 { 
00229 public:
00231   typedef  void (T::*TMemberFunctionPointer)(); 
00232 
00234   typedef SimpleMemberCommand   Self;
00235   typedef SmartPointer<Self>    Pointer;
00236 
00238   itkTypeMacro(SimpleMemberCommand,Command);
00239 
00241   itkNewMacro(Self);
00242 
00244   void SetCallbackFunction(T* object,  
00245                            TMemberFunctionPointer memberFunction)
00246     {
00247     m_This = object;
00248     m_MemberFunction = memberFunction;
00249     }
00250 
00252   virtual void Execute(Object *,const EventObject & ) 
00253     { 
00254     if( m_MemberFunction ) 
00255       {
00256       ((*m_This).*(m_MemberFunction))();
00257       }
00258     }
00259   virtual void Execute(const Object *,const EventObject & ) 
00260     { 
00261     if( m_MemberFunction ) 
00262       {
00263       ((*m_This).*(m_MemberFunction))();
00264       }
00265     }
00267 
00268 protected:
00269   T* m_This;
00270   TMemberFunctionPointer m_MemberFunction;
00271   SimpleMemberCommand():m_MemberFunction(0) {}
00272   virtual ~SimpleMemberCommand() {}
00273 
00274 private:
00275   SimpleMemberCommand(const Self&); //purposely not implemented
00276   void operator=(const Self&); //purposely not implemented
00277 
00278 };
00279 
00280 
00289 template <class T>
00290 class SimpleConstMemberCommand : public Command
00291 { 
00292 public:
00294   typedef  void (T::*TMemberFunctionPointer)() const; 
00295 
00297   typedef SimpleConstMemberCommand  Self;
00298   typedef SmartPointer<Self>        Pointer;
00299 
00301   itkTypeMacro(SimpleConstMemberCommand,Command);
00302 
00304   itkNewMacro(Self);
00305 
00307   void SetCallbackFunction(const T* object,  
00308                            TMemberFunctionPointer memberFunction)
00309     {
00310     m_This = object;
00311     m_MemberFunction = memberFunction;
00312     }
00313 
00315   virtual void Execute(Object *,const EventObject & ) 
00316     { 
00317     if( m_MemberFunction ) 
00318       {
00319       ((*m_This).*(m_MemberFunction))();
00320       }
00321     }
00322   virtual void Execute(const Object *,const EventObject & ) 
00323     { 
00324     if( m_MemberFunction ) 
00325       {
00326       ((*m_This).*(m_MemberFunction))();
00327       }
00328     }
00330 
00331 protected:
00332   const T* m_This;
00333   TMemberFunctionPointer m_MemberFunction;
00334   SimpleConstMemberCommand():m_MemberFunction(0) {}
00335   virtual ~SimpleConstMemberCommand() {} 
00336 
00337 private:
00338   SimpleConstMemberCommand(const Self&); //purposely not implemented
00339   void operator=(const Self&); //purposely not implemented
00340   
00341 };
00342 
00343 
00355 class CStyleCommand : public Command
00356 {
00357 public:
00359   typedef  void (*FunctionPointer)(Object*, const EventObject &, void*);
00360   typedef  void (*ConstFunctionPointer)(const Object*, 
00361                                         const EventObject &, void*);
00362   typedef  void (*DeleteDataFunctionPointer)(void*);
00364 
00366   typedef CStyleCommand       Self;
00367   typedef SmartPointer<Self>  Pointer;
00368 
00370   itkTypeMacro(CStyleCommand,Command);
00371 
00373   itkNewMacro(Self);
00374 
00377   void SetClientData(void *cd) {m_ClientData = cd;}
00378 
00380   void SetCallback(FunctionPointer f)
00381     {m_Callback = f;}
00382   void SetConstCallback(ConstFunctionPointer f)
00383     {m_ConstCallback = f;}
00385 
00387   void SetClientDataDeleteCallback(DeleteDataFunctionPointer f)
00388     {m_ClientDataDeleteCallback = f;}
00389 
00391   void Execute(Object *caller, const EventObject & event )
00392     {
00393     if (m_Callback)
00394       {
00395       m_Callback(caller, event, m_ClientData );
00396       }
00397     }
00399 
00401   void Execute(const Object *caller, const EventObject & event )
00402     {
00403     if (m_ConstCallback)
00404       {
00405       m_ConstCallback(caller, event, m_ClientData );
00406       }
00407     }
00409 
00410 protected:
00411   CStyleCommand(): m_ClientData(0), m_Callback(0), m_ConstCallback(0),
00412                    m_ClientDataDeleteCallback(0) 
00413     {
00414     // not implemented
00415     }
00416   ~CStyleCommand() 
00417     { 
00418     if (m_ClientDataDeleteCallback)
00419       {
00420       m_ClientDataDeleteCallback(m_ClientData);
00421       }
00422     }
00423 
00424   void *                    m_ClientData;
00425   FunctionPointer           m_Callback;
00426   ConstFunctionPointer      m_ConstCallback;
00427   DeleteDataFunctionPointer m_ClientDataDeleteCallback;
00428 };
00429 
00430 
00431 } // end namespace itk
00432 
00433 #endif
00434 

Generated at Mon Jul 12 2010 18:04:06 for ITK by doxygen 1.7.1 written by Dimitri van Heesch, © 1997-2000