ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkCommand.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkCommand_h
00019 #define __itkCommand_h
00020 
00021 #include "itkObject.h"
00022 #include "itkObjectFactory.h"
00023 
00024 namespace itk
00025 {
00043 // The superclass that all commands should be subclasses of
00044 class ITKCommon_EXPORT Command:public Object
00045 {
00046 public:
00048   typedef Command                    Self;
00049   typedef Object                     Superclass;
00050   typedef SmartPointer< Self >       Pointer;
00051   typedef SmartPointer< const Self > ConstPointer;
00052 
00054   itkTypeMacro(Command, Object);
00055 
00057   virtual void Execute(Object *caller, const EventObject & event) = 0;
00058 
00062   virtual void Execute(const Object *caller, const EventObject & event) = 0;
00063 
00064 protected:
00065   Command();
00066   ~Command();
00067 private:
00068   Command(const Self &);        //purposely not implemented
00069   void operator=(const Self &); //purposely not implemented
00070 };
00071 
00072 // some implementations for several callback types
00073 
00083 template< class T >
00084 class MemberCommand:public Command
00085 {
00086 public:
00088   typedef  void ( T::*TMemberFunctionPointer )(Object *, const EventObject &);
00089   typedef  void ( T::*TConstMemberFunctionPointer )(const Object *,
00090                                                     const EventObject &);
00092 
00094   typedef MemberCommand        Self;
00095   typedef SmartPointer< Self > Pointer;
00096 
00098   itkNewMacro(Self);
00099 
00101   itkTypeMacro(MemberCommand, Command);
00102 
00105   void SetCallbackFunction(T *object,
00106                            TMemberFunctionPointer memberFunction)
00107   {
00108     m_This = object;
00109     m_MemberFunction = memberFunction;
00110   }
00111 
00112   void SetCallbackFunction(T *object,
00113                            TConstMemberFunctionPointer memberFunction)
00114   {
00115     m_This = object;
00116     m_ConstMemberFunction = memberFunction;
00117   }
00118 
00120   virtual void Execute(Object *caller, const EventObject & event)
00121   {
00122     if ( m_MemberFunction )
00123       {
00124       ( ( *m_This ).*( m_MemberFunction ) )( caller, event );
00125       }
00126   }
00128 
00130   virtual void Execute(const Object *caller, const EventObject & event)
00131   {
00132     if ( m_ConstMemberFunction )
00133       {
00134       ( ( *m_This ).*( m_ConstMemberFunction ) )( caller, event );
00135       }
00136   }
00138 
00139 protected:
00140 
00141   T *                         m_This;
00142   TMemberFunctionPointer      m_MemberFunction;
00143   TConstMemberFunctionPointer m_ConstMemberFunction;
00144   MemberCommand():m_MemberFunction(0), m_ConstMemberFunction(0) {}
00145   virtual ~MemberCommand(){}
00146 private:
00147   MemberCommand(const Self &);  //purposely not implemented
00148   void operator=(const Self &); //purposely not implemented
00149 };
00150 
00160 template< class T >
00161 class ReceptorMemberCommand:public Command
00162 {
00163 public:
00165   typedef  void ( T::*TMemberFunctionPointer )(const EventObject &);
00166 
00168   typedef ReceptorMemberCommand Self;
00169   typedef SmartPointer< Self >  Pointer;
00170 
00172   itkNewMacro(Self);
00173 
00175   itkTypeMacro(ReceptorMemberCommand, Command);
00176 
00179   void SetCallbackFunction(T *object,
00180                            TMemberFunctionPointer memberFunction)
00181   {
00182     m_This = object;
00183     m_MemberFunction = memberFunction;
00184   }
00185 
00187   virtual void Execute(Object *, const EventObject & event)
00188   {
00189     if ( m_MemberFunction )
00190       {
00191       ( ( *m_This ).*( m_MemberFunction ) )( event );
00192       }
00193   }
00195 
00197   virtual void Execute(const Object *, const EventObject & event)
00198   {
00199     if ( m_MemberFunction )
00200       {
00201       ( ( *m_This ).*( m_MemberFunction ) )( event );
00202       }
00203   }
00205 
00206 protected:
00207   T *                    m_This;
00208   TMemberFunctionPointer m_MemberFunction;
00209   ReceptorMemberCommand():m_MemberFunction(0) {}
00210   virtual ~ReceptorMemberCommand() {}
00211 private:
00212   ReceptorMemberCommand(const Self &); //purposely not implemented
00213   void operator=(const Self &);        //purposely not implemented
00214 };
00215 
00225 template< class T >
00226 class SimpleMemberCommand:public Command
00227 {
00228 public:
00230   typedef  void ( T::*TMemberFunctionPointer )();
00231 
00233   typedef SimpleMemberCommand  Self;
00234   typedef SmartPointer< Self > Pointer;
00235 
00237   itkTypeMacro(SimpleMemberCommand, Command);
00238 
00240   itkNewMacro(Self);
00241 
00243   void SetCallbackFunction(T *object,
00244                            TMemberFunctionPointer memberFunction)
00245   {
00246     m_This = object;
00247     m_MemberFunction = memberFunction;
00248   }
00249 
00251   virtual void Execute(Object *, const EventObject &)
00252   {
00253     if ( m_MemberFunction )
00254       {
00255       ( ( *m_This ).*( m_MemberFunction ) )( );
00256       }
00257   }
00259 
00260   virtual void Execute(const Object *, const EventObject &)
00261   {
00262     if ( m_MemberFunction )
00263       {
00264       ( ( *m_This ).*( m_MemberFunction ) )( );
00265       }
00266   }
00267 
00268 protected:
00269   T *                    m_This;
00270   TMemberFunctionPointer m_MemberFunction;
00271   SimpleMemberCommand():m_MemberFunction(0) {}
00272   virtual ~SimpleMemberCommand() {}
00273 private:
00274   SimpleMemberCommand(const Self &); //purposely not implemented
00275   void operator=(const Self &);      //purposely not implemented
00276 };
00277 
00287 template< class T >
00288 class SimpleConstMemberCommand:public Command
00289 {
00290 public:
00292   typedef  void ( T::*TMemberFunctionPointer )() const;
00293 
00295   typedef SimpleConstMemberCommand Self;
00296   typedef SmartPointer< Self >     Pointer;
00297 
00299   itkTypeMacro(SimpleConstMemberCommand, Command);
00300 
00302   itkNewMacro(Self);
00303 
00305   void SetCallbackFunction(const T *object,
00306                            TMemberFunctionPointer memberFunction)
00307   {
00308     m_This = object;
00309     m_MemberFunction = memberFunction;
00310   }
00311 
00313   virtual void Execute(Object *, const EventObject &)
00314   {
00315     if ( m_MemberFunction )
00316       {
00317       ( ( *m_This ).*( m_MemberFunction ) )( );
00318       }
00319   }
00321 
00322   virtual void Execute(const Object *, const EventObject &)
00323   {
00324     if ( m_MemberFunction )
00325       {
00326       ( ( *m_This ).*( m_MemberFunction ) )( );
00327       }
00328   }
00329 
00330 protected:
00331   const T *              m_This;
00332   TMemberFunctionPointer m_MemberFunction;
00333   SimpleConstMemberCommand():m_MemberFunction(0) {}
00334   virtual ~SimpleConstMemberCommand() {}
00335 private:
00336   SimpleConstMemberCommand(const Self &); //purposely not implemented
00337   void operator=(const Self &);           //purposely not implemented
00338 };
00339 
00352 class CStyleCommand:public Command
00353 {
00354 public:
00356   typedef  void ( *FunctionPointer )(Object *, const EventObject &, void *);
00357   typedef  void ( *ConstFunctionPointer )(const Object *,
00358                                           const EventObject &, void *);
00359   typedef  void ( *DeleteDataFunctionPointer )(void *);
00361 
00363   typedef CStyleCommand        Self;
00364   typedef SmartPointer< Self > Pointer;
00365 
00367   itkTypeMacro(CStyleCommand, Command);
00368 
00370   itkNewMacro(Self);
00371 
00374   void SetClientData(void *cd) { m_ClientData = cd; }
00375 
00377   void SetCallback(FunctionPointer f)
00378   { m_Callback = f; }
00379   void SetConstCallback(ConstFunctionPointer f)
00380   { m_ConstCallback = f; }
00382 
00384   void SetClientDataDeleteCallback(DeleteDataFunctionPointer f)
00385   { m_ClientDataDeleteCallback = f; }
00386 
00388   void Execute(Object *caller, const EventObject & event)
00389   {
00390     if ( m_Callback )
00391       {
00392       m_Callback(caller, event, m_ClientData);
00393       }
00394   }
00396 
00398   void Execute(const Object *caller, const EventObject & event)
00399   {
00400     if ( m_ConstCallback )
00401       {
00402       m_ConstCallback(caller, event, m_ClientData);
00403       }
00404   }
00406 
00407 protected:
00408   CStyleCommand():m_ClientData(0), m_Callback(0), m_ConstCallback(0),
00409     m_ClientDataDeleteCallback(0)
00410   {
00411     // not implemented
00412   }
00413 
00414   ~CStyleCommand()
00415   {
00416     if ( m_ClientDataDeleteCallback )
00417       {
00418       m_ClientDataDeleteCallback(m_ClientData);
00419       }
00420   }
00421 
00422   void *                    m_ClientData;
00423   FunctionPointer           m_Callback;
00424   ConstFunctionPointer      m_ConstCallback;
00425   DeleteDataFunctionPointer m_ClientDataDeleteCallback;
00426 };
00427 } // end namespace itk
00428 
00429 #endif
00430