ITK  4.6.0
Insight Segmentation and Registration Toolkit
itkCommand.h
Go to the documentation of this file.
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 {
43 // The superclass that all commands should be subclasses of
44 class ITKCommon_EXPORT Command:public Object
45 {
46 public:
48  typedef Command Self;
49  typedef Object Superclass;
52 
54  itkTypeMacro(Command, Object);
55 
57  virtual void Execute(Object *caller, const EventObject & event) = 0;
58 
62  virtual void Execute(const Object *caller, const EventObject & event) = 0;
63 
64 protected:
65  Command();
66  ~Command();
67 
68 private:
69  Command(const Self &); //purposely not implemented
70  void operator=(const Self &); //purposely not implemented
71 };
72 
73 // some implementations for several callback types
74 
84 template< typename T >
85 class MemberCommand:public Command
86 {
87 public:
89  typedef void ( T::*TMemberFunctionPointer )(Object *, const EventObject &);
90  typedef void ( T::*TConstMemberFunctionPointer )(const Object *,
91  const EventObject &);
93 
97 
99  itkNewMacro(Self);
100 
102  itkTypeMacro(MemberCommand, Command);
103 
106  void SetCallbackFunction(T *object,
107  TMemberFunctionPointer memberFunction)
108  {
109  m_This = object;
110  m_MemberFunction = memberFunction;
111  }
112 
113  void SetCallbackFunction(T *object,
114  TConstMemberFunctionPointer memberFunction)
115  {
116  m_This = object;
117  m_ConstMemberFunction = memberFunction;
118  }
119 
121  virtual void Execute(Object *caller, const EventObject & event) ITK_OVERRIDE
122  {
123  if ( m_MemberFunction )
124  {
125  ( ( *m_This ).*( m_MemberFunction ) )( caller, event );
126  }
127  }
129 
131  virtual void Execute(const Object *caller, const EventObject & event) ITK_OVERRIDE
132  {
133  if ( m_ConstMemberFunction )
134  {
135  ( ( *m_This ).*( m_ConstMemberFunction ) )( caller, event );
136  }
137  }
139 
140 protected:
141  T * m_This;
144 
146  m_This( ITK_NULLPTR ),
147  m_MemberFunction( ITK_NULLPTR ),
148  m_ConstMemberFunction( ITK_NULLPTR )
149  {}
150 
151  virtual ~MemberCommand(){}
152 
153 private:
154  MemberCommand(const Self &); //purposely not implemented
155  void operator=(const Self &); //purposely not implemented
156 };
157 
167 template< typename T >
169 {
170 public:
172  typedef void ( T::*TMemberFunctionPointer )(const EventObject &);
173 
177 
179  itkNewMacro(Self);
180 
182  itkTypeMacro(ReceptorMemberCommand, Command);
183 
186  void SetCallbackFunction(T *object,
187  TMemberFunctionPointer memberFunction)
188  {
189  m_This = object;
190  m_MemberFunction = memberFunction;
191  }
192 
194  virtual void Execute(Object *, const EventObject & event) ITK_OVERRIDE
195  {
196  if ( m_MemberFunction )
197  {
198  ( ( *m_This ).*( m_MemberFunction ) )( event );
199  }
200  }
202 
204  virtual void Execute(const Object *, const EventObject & event) ITK_OVERRIDE
205  {
206  if ( m_MemberFunction )
207  {
208  ( ( *m_This ).*( m_MemberFunction ) )( event );
209  }
210  }
212 
213 protected:
214  T * m_This;
216 
218  m_This( ITK_NULLPTR ),
219  m_MemberFunction( ITK_NULLPTR )
220  {}
221 
223 
224 private:
225  ReceptorMemberCommand(const Self &); //purposely not implemented
226  void operator=(const Self &); //purposely not implemented
227 };
228 
238 template< typename T >
240 {
241 public:
243  typedef void ( T::*TMemberFunctionPointer )();
244 
248 
250  itkTypeMacro(SimpleMemberCommand, Command);
251 
253  itkNewMacro(Self);
254 
256  void SetCallbackFunction(T *object,
257  TMemberFunctionPointer memberFunction)
258  {
259  m_This = object;
260  m_MemberFunction = memberFunction;
261  }
262 
264  virtual void Execute(Object *, const EventObject &) ITK_OVERRIDE
265  {
266  if ( m_MemberFunction )
267  {
268  ( ( *m_This ).*( m_MemberFunction ) )( );
269  }
270  }
272 
273  virtual void Execute(const Object *, const EventObject &) ITK_OVERRIDE
274  {
275  if ( m_MemberFunction )
276  {
277  ( ( *m_This ).*( m_MemberFunction ) )( );
278  }
279  }
280 
281 protected:
282  T * m_This;
284 
286  m_This( ITK_NULLPTR ),
287  m_MemberFunction( ITK_NULLPTR )
288  {}
289 
290  virtual ~SimpleMemberCommand() {}
291 
292 private:
293  SimpleMemberCommand(const Self &); //purposely not implemented
294  void operator=(const Self &); //purposely not implemented
295 };
296 
306 template< typename T >
308 {
309 public:
311  typedef void ( T::*TMemberFunctionPointer )() const;
312 
316 
318  itkTypeMacro(SimpleConstMemberCommand, Command);
319 
321  itkNewMacro(Self);
322 
324  void SetCallbackFunction(const T *object,
325  TMemberFunctionPointer memberFunction)
326  {
327  m_This = object;
328  m_MemberFunction = memberFunction;
329  }
330 
332  virtual void Execute(Object *, const EventObject &) ITK_OVERRIDE
333  {
334  if ( m_MemberFunction )
335  {
336  ( ( *m_This ).*( m_MemberFunction ) )( );
337  }
338  }
340 
341  virtual void Execute(const Object *, const EventObject &) ITK_OVERRIDE
342  {
343  if ( m_MemberFunction )
344  {
345  ( ( *m_This ).*( m_MemberFunction ) )( );
346  }
347  }
348 
349 protected:
350  const T * m_This;
352 
354  m_This( NULL ),
356  {}
357 
359 
360 private:
361  SimpleConstMemberCommand(const Self &); //purposely not implemented
362  void operator=(const Self &); //purposely not implemented
363 };
364 
377 class CStyleCommand:public Command
378 {
379 public:
381  typedef void ( *FunctionPointer )(Object *, const EventObject &, void *);
382  typedef void ( *ConstFunctionPointer )(const Object *,
383  const EventObject &, void *);
384  typedef void ( *DeleteDataFunctionPointer )(void *);
386 
390 
392  itkTypeMacro(CStyleCommand, Command);
393 
395  itkNewMacro(Self);
396 
399  void SetClientData(void *cd) { m_ClientData = cd; }
400 
403  { m_Callback = f; }
405  { m_ConstCallback = f; }
407 
411 
413  virtual void Execute(Object *caller, const EventObject & event) ITK_OVERRIDE
414  {
415  if ( m_Callback )
416  {
417  m_Callback(caller, event, m_ClientData);
418  }
419  }
421 
423  virtual void Execute(const Object *caller, const EventObject & event) ITK_OVERRIDE
424  {
425  if ( m_ConstCallback )
426  {
427  m_ConstCallback(caller, event, m_ClientData);
428  }
429  }
431 
432 protected:
434  m_ClientData( ITK_NULLPTR ),
435  m_Callback( ITK_NULLPTR ),
436  m_ConstCallback( ITK_NULLPTR ),
437  m_ClientDataDeleteCallback( ITK_NULLPTR )
438  {}
439 
441  {
443  {
445  }
446  }
447 
448  void * m_ClientData;
452 };
453 } // end namespace itk
454 
455 #endif
FunctionPointer m_Callback
Definition: itkCommand.h:449
void operator=(const Self &)
virtual void Execute(Object *, const EventObject &event) ITK_OVERRIDE
Definition: itkCommand.h:194
MemberCommand Self
Definition: itkCommand.h:95
void(T::* TMemberFunctionPointer)() const
Definition: itkCommand.h:311
ReceptorMemberCommand Self
Definition: itkCommand.h:175
void SetConstCallback(ConstFunctionPointer f)
Definition: itkCommand.h:404
SimpleMemberCommand Self
Definition: itkCommand.h:246
SmartPointer< Self > Pointer
Definition: itkCommand.h:247
TConstMemberFunctionPointer m_ConstMemberFunction
Definition: itkCommand.h:143
virtual void Execute(Object *, const EventObject &) ITK_OVERRIDE
Definition: itkCommand.h:332
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:351
void(T::* TMemberFunctionPointer)()
Definition: itkCommand.h:243
void SetCallbackFunction(const T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:324
void SetCallback(FunctionPointer f)
Definition: itkCommand.h:402
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:142
void(T::* TMemberFunctionPointer)(const EventObject &)
Definition: itkCommand.h:172
SmartPointer< Self > Pointer
Definition: itkCommand.h:96
virtual void Execute(const Object *, const EventObject &) ITK_OVERRIDE
Definition: itkCommand.h:341
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:283
A Command subclass that calls a pointer to a C function.
Definition: itkCommand.h:377
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:215
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:85
virtual void Execute(const Object *, const EventObject &event) ITK_OVERRIDE
Definition: itkCommand.h:204
virtual void Execute(const Object *caller, const EventObject &event) ITK_OVERRIDE
Definition: itkCommand.h:423
void SetClientData(void *cd)
Definition: itkCommand.h:399
virtual ~ReceptorMemberCommand()
Definition: itkCommand.h:222
virtual ~MemberCommand()
Definition: itkCommand.h:151
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:106
ConstFunctionPointer m_ConstCallback
Definition: itkCommand.h:450
SmartPointer< Self > Pointer
Definition: itkCommand.h:389
void operator=(const Self &)
SmartPointer< Self > Pointer
Definition: itkCommand.h:50
void(* DeleteDataFunctionPointer)(void *)
Definition: itkCommand.h:384
void SetCallbackFunction(T *object, TConstMemberFunctionPointer memberFunction)
Definition: itkCommand.h:113
virtual void Execute(Object *caller, const EventObject &event) ITK_OVERRIDE
Definition: itkCommand.h:413
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:168
Abstraction of the Events used to communicating among filters and with GUIs.
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:239
virtual void Execute(const Object *, const EventObject &) ITK_OVERRIDE
Definition: itkCommand.h:273
virtual void Execute(const Object *caller, const EventObject &event) ITK_OVERRIDE
Definition: itkCommand.h:131
void(* FunctionPointer)(Object *, const EventObject &, void *)
Definition: itkCommand.h:381
DeleteDataFunctionPointer m_ClientDataDeleteCallback
Definition: itkCommand.h:451
virtual ~SimpleMemberCommand()
Definition: itkCommand.h:290
SmartPointer< Self > Pointer
Definition: itkCommand.h:315
SmartPointer< const Self > ConstPointer
Definition: itkCommand.h:51
void(T::* TMemberFunctionPointer)(Object *, const EventObject &)
Definition: itkCommand.h:89
Object Superclass
Definition: itkCommand.h:49
#define NULL
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:307
Command Self
Definition: itkCommand.h:48
SmartPointer< Self > Pointer
Definition: itkCommand.h:176
Base class for most ITK classes.
Definition: itkObject.h:57
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:256
void operator=(const Self &)
Superclass for callback/observer methods.
Definition: itkCommand.h:44
void(T::* TConstMemberFunctionPointer)(const Object *, const EventObject &)
Definition: itkCommand.h:90
virtual void Execute(Object *caller, const EventObject &event) ITK_OVERRIDE
Definition: itkCommand.h:121
void SetClientDataDeleteCallback(DeleteDataFunctionPointer f)
Definition: itkCommand.h:409
SimpleConstMemberCommand Self
Definition: itkCommand.h:314
void(* ConstFunctionPointer)(const Object *, const EventObject &, void *)
Definition: itkCommand.h:382
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:186
void operator=(const Self &)
virtual void Execute(Object *, const EventObject &) ITK_OVERRIDE
Definition: itkCommand.h:264
CStyleCommand Self
Definition: itkCommand.h:388