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