1 /*========================================================================= 2 * 3 * Copyright Insight Software Consortium 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0.txt 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 *=========================================================================*/ 18 #ifndef itkCommand_h 19 #define itkCommand_h 20 21 #include "itkObject.h" 22 #include "itkObjectFactory.h" 23 24 namespace itk 25 { 26 /** \class Command 27 * \brief Superclass for callback/observer methods. 28 * 29 * Command is an implementation of the command design pattern that is used 30 * in callbacks (such as StartMethod(), ProgressMethod(), and EndMethod()) in 31 * ITK. itk::Object implements a Subject/Observer pattern. When a subject 32 * needs to notify a observer, it does so using a itk::Command. The Execute 33 * method is called to run the command. 34 * 35 * \ingroup ITKSystemObjects 36 * \ingroup ITKCommon 37 * 38 * \wiki 39 * \wikiexample{Utilities/ObserveEvent,Observe an event} 40 * \endwiki 41 */ 42 43 // The superclass that all commands should be subclasses of 44 class ITKCommon_EXPORT Command:public Object 45 { 46 public: 47 ITK_DISALLOW_COPY_AND_ASSIGN(Command); 48 49 /** Standard class type aliases. */ 50 using Self = Command; 51 using Superclass = Object; 52 using Pointer = SmartPointer< Self >; 53 using ConstPointer = SmartPointer< const Self >; 54 55 /** Run-time type information (and related methods). */ 56 itkTypeMacro(Command, Object); 57 58 /** Abstract method that defines the action to be taken by the command. */ 59 virtual void Execute(Object *caller, const EventObject & event) = 0; 60 61 /** Abstract method that defines the action to be taken by the command. 62 * This variant is expected to be used when requests comes from a 63 * const Object */ 64 virtual void Execute(const Object *caller, const EventObject & event) = 0; 65 66 protected: 67 Command(); 68 ~Command() override; 69 }; 70 71 // some implementations for several callback types 72 73 /** \class MemberCommand 74 * \brief A Command subclass that calls a pointer to a member function. 75 * 76 * MemberCommand calls a pointer to a member function with the same 77 * arguments as Execute on Command. 78 * 79 * \ingroup ITKSystemObjects 80 * \ingroup ITKCommon 81 */ 82 template< typename T > 83 class ITK_TEMPLATE_EXPORT MemberCommand:public Command 84 { 85 public: 86 ITK_DISALLOW_COPY_AND_ASSIGN(MemberCommand); 87 88 /** pointer to a member function that takes a Object* and the event */ 89 using TMemberFunctionPointer = void (T::*)(Object *, const EventObject &); 90 using TConstMemberFunctionPointer = void (T::*)(const Object *, const EventObject &); 91 92 /** Standard class type aliases. */ 93 using Self = MemberCommand; 94 using Pointer = SmartPointer< Self >; 95 96 /** Method for creation through the object factory. */ 97 itkNewMacro(Self); 98 99 /** Run-time type information (and related methods). */ 100 itkTypeMacro(MemberCommand, Command); 101 102 /** Set the callback function along with the object that it will 103 * be invoked on. */ SetCallbackFunction(T * object,TMemberFunctionPointer memberFunction)104 void SetCallbackFunction(T *object, 105 TMemberFunctionPointer memberFunction) 106 { 107 m_This = object; 108 m_MemberFunction = memberFunction; 109 } 110 SetCallbackFunction(T * object,TConstMemberFunctionPointer memberFunction)111 void SetCallbackFunction(T *object, 112 TConstMemberFunctionPointer memberFunction) 113 { 114 m_This = object; 115 m_ConstMemberFunction = memberFunction; 116 } 117 118 /** Invoke the member function. */ Execute(Object * caller,const EventObject & event)119 void Execute(Object *caller, const EventObject & event) override 120 { 121 if ( m_MemberFunction ) 122 { 123 ( ( *m_This ).*( m_MemberFunction ) )( caller, event ); 124 } 125 } 126 127 /** Invoke the member function with a const object. */ Execute(const Object * caller,const EventObject & event)128 void Execute(const Object *caller, const EventObject & event) override 129 { 130 if ( m_ConstMemberFunction ) 131 { 132 ( ( *m_This ).*( m_ConstMemberFunction ) )( caller, event ); 133 } 134 } 135 136 protected: 137 T * m_This; 138 TMemberFunctionPointer m_MemberFunction; 139 TConstMemberFunctionPointer m_ConstMemberFunction; 140 MemberCommand()141 MemberCommand() : 142 m_This( nullptr ), 143 m_MemberFunction( nullptr ), 144 m_ConstMemberFunction( nullptr ) 145 {} 146 147 ~MemberCommand() override = default; 148 }; 149 150 /** \class ReceptorMemberCommand 151 * \brief A Command subclass that calls a pointer to a member function. 152 * 153 * ReceptorMemberCommand calls a pointer to a member function with 154 * only and itk::EventObject as argument 155 * 156 * \ingroup ITKSystemObjects 157 * \ingroup ITKCommon 158 */ 159 template< typename T > 160 class ITK_TEMPLATE_EXPORT ReceptorMemberCommand:public Command 161 { 162 public: 163 ITK_DISALLOW_COPY_AND_ASSIGN(ReceptorMemberCommand); 164 165 /** pointer to a member function that takes a Object* and the event */ 166 using TMemberFunctionPointer = void (T::*)(const EventObject &); 167 168 /** Standard class type aliases. */ 169 using Self = ReceptorMemberCommand; 170 using Pointer = SmartPointer< Self >; 171 172 /** Method for creation through the object factory. */ 173 itkNewMacro(Self); 174 175 /** Run-time type information (and related methods). */ 176 itkTypeMacro(ReceptorMemberCommand, Command); 177 178 /** Set the callback function along with the object that it will 179 * be invoked on. */ SetCallbackFunction(T * object,TMemberFunctionPointer memberFunction)180 void SetCallbackFunction(T *object, 181 TMemberFunctionPointer memberFunction) 182 { 183 m_This = object; 184 m_MemberFunction = memberFunction; 185 } 186 187 /** Invoke the member function. */ Execute(Object *,const EventObject & event)188 void Execute(Object *, const EventObject & event) override 189 { 190 if ( m_MemberFunction ) 191 { 192 ( ( *m_This ).*( m_MemberFunction ) )( event ); 193 } 194 } 195 196 /** Invoke the member function with a const object */ Execute(const Object *,const EventObject & event)197 void Execute(const Object *, const EventObject & event) override 198 { 199 if ( m_MemberFunction ) 200 { 201 ( ( *m_This ).*( m_MemberFunction ) )( event ); 202 } 203 } 204 205 protected: 206 T * m_This; 207 TMemberFunctionPointer m_MemberFunction; 208 ReceptorMemberCommand()209 ReceptorMemberCommand() : 210 m_This( nullptr ), 211 m_MemberFunction( nullptr ) 212 {} 213 214 ~ReceptorMemberCommand() override = default; 215 }; 216 217 /** \class SimpleMemberCommand 218 * \brief A Command subclass that calls a pointer to a member function. 219 * 220 * SimpleMemberCommand calls a pointer to a member function with no 221 * arguments. 222 * 223 * \ingroup ITKSystemObjects 224 * \ingroup ITKCommon 225 */ 226 template< typename T > 227 class ITK_TEMPLATE_EXPORT SimpleMemberCommand:public Command 228 { 229 public: 230 ITK_DISALLOW_COPY_AND_ASSIGN(SimpleMemberCommand); 231 232 /** A method callback. */ 233 using TMemberFunctionPointer = void (T::*)(); 234 235 /** Standard class type aliases. */ 236 using Self = SimpleMemberCommand; 237 using Pointer = SmartPointer< Self >; 238 239 /** Run-time type information (and related methods). */ 240 itkTypeMacro(SimpleMemberCommand, Command); 241 242 /** Method for creation through the object factory. */ 243 itkNewMacro(Self); 244 245 /** Specify the callback function. */ SetCallbackFunction(T * object,TMemberFunctionPointer memberFunction)246 void SetCallbackFunction(T *object, 247 TMemberFunctionPointer memberFunction) 248 { 249 m_This = object; 250 m_MemberFunction = memberFunction; 251 } 252 253 /** Invoke the callback function. */ Execute(Object *,const EventObject &)254 void Execute(Object *, const EventObject &) override 255 { 256 if ( m_MemberFunction ) 257 { 258 ( ( *m_This ).*( m_MemberFunction ) )( ); 259 } 260 } 261 Execute(const Object *,const EventObject &)262 void Execute(const Object *, const EventObject &) override 263 { 264 if ( m_MemberFunction ) 265 { 266 ( ( *m_This ).*( m_MemberFunction ) )( ); 267 } 268 } 269 270 protected: 271 T * m_This; 272 TMemberFunctionPointer m_MemberFunction; 273 SimpleMemberCommand()274 SimpleMemberCommand() : 275 m_This( nullptr ), 276 m_MemberFunction( nullptr ) 277 {} 278 279 ~SimpleMemberCommand() override = default; 280 }; 281 282 /** \class SimpleConstMemberCommand 283 * \brief A Command subclass that calls a pointer to a member function. 284 * 285 * SimpleConstMemberCommand calls a pointer to a member function with no 286 * arguments. 287 * 288 * \ingroup ITKSystemObjects 289 * \ingroup ITKCommon 290 */ 291 template< typename T > 292 class ITK_TEMPLATE_EXPORT SimpleConstMemberCommand:public Command 293 { 294 public: 295 ITK_DISALLOW_COPY_AND_ASSIGN(SimpleConstMemberCommand); 296 297 /** A const member method callback. */ 298 using TMemberFunctionPointer = void (T::*)() const; 299 300 /** Standard class type aliases. */ 301 using Self = SimpleConstMemberCommand; 302 using Pointer = SmartPointer< Self >; 303 304 /** Run-time type information (and related methods). */ 305 itkTypeMacro(SimpleConstMemberCommand, Command); 306 307 /** Method for creation through the object factory. */ 308 itkNewMacro(Self); 309 310 /** Specify the const member method callback. */ SetCallbackFunction(const T * object,TMemberFunctionPointer memberFunction)311 void SetCallbackFunction(const T *object, 312 TMemberFunctionPointer memberFunction) 313 { 314 m_This = object; 315 m_MemberFunction = memberFunction; 316 } 317 318 /** Invoke the const member method callback. */ Execute(Object *,const EventObject &)319 void Execute(Object *, const EventObject &) override 320 { 321 if ( m_MemberFunction ) 322 { 323 ( ( *m_This ).*( m_MemberFunction ) )( ); 324 } 325 } 326 Execute(const Object *,const EventObject &)327 void Execute(const Object *, const EventObject &) override 328 { 329 if ( m_MemberFunction ) 330 { 331 ( ( *m_This ).*( m_MemberFunction ) )( ); 332 } 333 } 334 335 protected: 336 const T * m_This; 337 TMemberFunctionPointer m_MemberFunction; 338 SimpleConstMemberCommand()339 SimpleConstMemberCommand() : 340 m_This( nullptr ), 341 m_MemberFunction( nullptr ) 342 {} 343 344 ~SimpleConstMemberCommand() override = default; 345 }; 346 347 /** \class CStyleCommand 348 * \brief A Command subclass that calls a pointer to a C function. 349 * 350 * CStyleCommand calls a pointer to a C function with the following 351 * arguments void func(Object *,void *clientdata) 352 * The clientdata is data that the command wants passed to itself 353 * each time. 354 * 355 * \ingroup ITKSystemObjects 356 * \ingroup ITKCommon 357 */ 358 359 class ITKCommon_EXPORT CStyleCommand:public Command 360 { 361 public: 362 /** Typedefs for C-style callbacks. */ 363 using FunctionPointer = void (*)(Object *, const EventObject &, void *); 364 using ConstFunctionPointer = void (*)(const Object *, const EventObject &, void *); 365 using DeleteDataFunctionPointer = void (*)(void *); 366 367 /** Standard class type aliases. */ 368 using Self = CStyleCommand; 369 using Pointer = SmartPointer< Self >; 370 371 /** Run-time type information (and related methods). */ 372 itkTypeMacro(CStyleCommand, Command); 373 374 /** Method for creation through the object factory. */ 375 itkNewMacro(Self); 376 377 /** Set the client data that will be passed into the C function when 378 * it is called. */ 379 void SetClientData(void *cd); 380 381 /** Set the C callback function pointer to be called at Execute time. */ 382 void SetCallback(FunctionPointer f); 383 void SetConstCallback(ConstFunctionPointer f); 384 385 /** Set the callback to delete the client data. */ 386 void SetClientDataDeleteCallback(DeleteDataFunctionPointer f); 387 388 /** Execute the callback function. */ 389 void Execute(Object *caller, const EventObject & event) override; 390 391 /** Execute the callback function with a const Object */ 392 void Execute(const Object *caller, const EventObject & event) override; 393 394 protected: 395 CStyleCommand(); 396 ~CStyleCommand() override; 397 398 void * m_ClientData{ nullptr }; 399 FunctionPointer m_Callback{ nullptr }; 400 ConstFunctionPointer m_ConstCallback{ nullptr }; 401 DeleteDataFunctionPointer m_ClientDataDeleteCallback{ nullptr }; 402 }; 403 } // end namespace itk 404 405 #endif 406