ITK  4.5.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)
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)
132  {
133  if ( m_ConstMemberFunction )
134  {
135  ( ( *m_This ).*( m_ConstMemberFunction ) )( caller, event );
136  }
137  }
139 
140 protected:
141 
142  T * m_This;
146  m_This( NULL ),
149  {}
150  virtual ~MemberCommand(){}
151 
152 private:
153  MemberCommand(const Self &); //purposely not implemented
154  void operator=(const Self &); //purposely not implemented
155 };
156 
166 template< typename T >
168 {
169 public:
171  typedef void ( T::*TMemberFunctionPointer )(const EventObject &);
172 
176 
178  itkNewMacro(Self);
179 
181  itkTypeMacro(ReceptorMemberCommand, Command);
182 
185  void SetCallbackFunction(T *object,
186  TMemberFunctionPointer memberFunction)
187  {
188  m_This = object;
189  m_MemberFunction = memberFunction;
190  }
191 
193  virtual void Execute(Object *, const EventObject & event)
194  {
195  if ( m_MemberFunction )
196  {
197  ( ( *m_This ).*( m_MemberFunction ) )( event );
198  }
199  }
201 
203  virtual void Execute(const Object *, const EventObject & event)
204  {
205  if ( m_MemberFunction )
206  {
207  ( ( *m_This ).*( m_MemberFunction ) )( event );
208  }
209  }
211 
212 protected:
213  T * m_This;
217 
218 private:
219  ReceptorMemberCommand(const Self &); //purposely not implemented
220  void operator=(const Self &); //purposely not implemented
221 };
222 
232 template< typename T >
234 {
235 public:
237  typedef void ( T::*TMemberFunctionPointer )();
238 
242 
244  itkTypeMacro(SimpleMemberCommand, Command);
245 
247  itkNewMacro(Self);
248 
250  void SetCallbackFunction(T *object,
251  TMemberFunctionPointer memberFunction)
252  {
253  m_This = object;
254  m_MemberFunction = memberFunction;
255  }
256 
258  virtual void Execute(Object *, const EventObject &)
259  {
260  if ( m_MemberFunction )
261  {
262  ( ( *m_This ).*( m_MemberFunction ) )( );
263  }
264  }
266 
267  virtual void Execute(const Object *, const EventObject &)
268  {
269  if ( m_MemberFunction )
270  {
271  ( ( *m_This ).*( m_MemberFunction ) )( );
272  }
273  }
274 
275 protected:
276  T * m_This;
279  m_This( NULL ),
281  {}
282  virtual ~SimpleMemberCommand() {}
283 
284 private:
285  SimpleMemberCommand(const Self &); //purposely not implemented
286  void operator=(const Self &); //purposely not implemented
287 };
288 
298 template< typename T >
300 {
301 public:
303  typedef void ( T::*TMemberFunctionPointer )() const;
304 
308 
310  itkTypeMacro(SimpleConstMemberCommand, Command);
311 
313  itkNewMacro(Self);
314 
316  void SetCallbackFunction(const T *object,
317  TMemberFunctionPointer memberFunction)
318  {
319  m_This = object;
320  m_MemberFunction = memberFunction;
321  }
322 
324  virtual void Execute(Object *, const EventObject &)
325  {
326  if ( m_MemberFunction )
327  {
328  ( ( *m_This ).*( m_MemberFunction ) )( );
329  }
330  }
332 
333  virtual void Execute(const Object *, const EventObject &)
334  {
335  if ( m_MemberFunction )
336  {
337  ( ( *m_This ).*( m_MemberFunction ) )( );
338  }
339  }
340 
341 protected:
342  const T * m_This;
345  m_This( NULL ),
347  {}
349 
350 private:
351  SimpleConstMemberCommand(const Self &); //purposely not implemented
352  void operator=(const Self &); //purposely not implemented
353 };
354 
367 class CStyleCommand:public Command
368 {
369 public:
371  typedef void ( *FunctionPointer )(Object *, const EventObject &, void *);
372  typedef void ( *ConstFunctionPointer )(const Object *,
373  const EventObject &, void *);
374  typedef void ( *DeleteDataFunctionPointer )(void *);
376 
380 
382  itkTypeMacro(CStyleCommand, Command);
383 
385  itkNewMacro(Self);
386 
389  void SetClientData(void *cd) { m_ClientData = cd; }
390 
393  { m_Callback = f; }
395  { m_ConstCallback = f; }
397 
401 
403  void Execute(Object *caller, const EventObject & event)
404  {
405  if ( m_Callback )
406  {
407  m_Callback(caller, event, m_ClientData);
408  }
409  }
411 
413  void Execute(const Object *caller, const EventObject & event)
414  {
415  if ( m_ConstCallback )
416  {
417  m_ConstCallback(caller, event, m_ClientData);
418  }
419  }
421 
422 protected:
425  {
426  // not implemented
427  }
428 
430  {
432  {
434  }
435  }
436 
437  void * m_ClientData;
441 };
442 } // end namespace itk
443 
444 #endif
FunctionPointer m_Callback
Definition: itkCommand.h:438
virtual void Execute(Object *, const EventObject &)
Definition: itkCommand.h:258
void operator=(const Self &)
MemberCommand Self
Definition: itkCommand.h:95
void(T::* TMemberFunctionPointer)() const
Definition: itkCommand.h:303
ReceptorMemberCommand Self
Definition: itkCommand.h:174
void SetConstCallback(ConstFunctionPointer f)
Definition: itkCommand.h:394
SimpleMemberCommand Self
Definition: itkCommand.h:240
SmartPointer< Self > Pointer
Definition: itkCommand.h:241
TConstMemberFunctionPointer m_ConstMemberFunction
Definition: itkCommand.h:144
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:343
void(T::* TMemberFunctionPointer)()
Definition: itkCommand.h:237
void SetCallbackFunction(const T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:316
void SetCallback(FunctionPointer f)
Definition: itkCommand.h:392
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:143
void(T::* TMemberFunctionPointer)(const EventObject &)
Definition: itkCommand.h:171
SmartPointer< Self > Pointer
Definition: itkCommand.h:96
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:277
A Command subclass that calls a pointer to a C function.
Definition: itkCommand.h:367
void Execute(const Object *caller, const EventObject &event)
Definition: itkCommand.h:413
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:214
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:85
virtual void Execute(Object *caller, const EventObject &event)
Definition: itkCommand.h:121
void SetClientData(void *cd)
Definition: itkCommand.h:389
virtual void Execute(const Object *, const EventObject &)
Definition: itkCommand.h:267
virtual void Execute(const Object *caller, const EventObject &event)
Definition: itkCommand.h:131
virtual ~ReceptorMemberCommand()
Definition: itkCommand.h:216
virtual ~MemberCommand()
Definition: itkCommand.h:150
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:106
virtual void Execute(const Object *, const EventObject &event)
Definition: itkCommand.h:203
ConstFunctionPointer m_ConstCallback
Definition: itkCommand.h:439
SmartPointer< Self > Pointer
Definition: itkCommand.h:379
void operator=(const Self &)
SmartPointer< Self > Pointer
Definition: itkCommand.h:50
void(* DeleteDataFunctionPointer)(void *)
Definition: itkCommand.h:374
void SetCallbackFunction(T *object, TConstMemberFunctionPointer memberFunction)
Definition: itkCommand.h:113
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:167
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:233
void Execute(Object *caller, const EventObject &event)
Definition: itkCommand.h:403
virtual void Execute(Object *, const EventObject &)
Definition: itkCommand.h:324
void(* FunctionPointer)(Object *, const EventObject &, void *)
Definition: itkCommand.h:371
DeleteDataFunctionPointer m_ClientDataDeleteCallback
Definition: itkCommand.h:440
virtual ~SimpleMemberCommand()
Definition: itkCommand.h:282
SmartPointer< Self > Pointer
Definition: itkCommand.h:307
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:299
Command Self
Definition: itkCommand.h:48
SmartPointer< Self > Pointer
Definition: itkCommand.h:175
virtual void Execute(const Object *, const EventObject &)
Definition: itkCommand.h:333
virtual void Execute(Object *, const EventObject &event)
Definition: itkCommand.h:193
Base class for most ITK classes.
Definition: itkObject.h:57
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:250
void operator=(const Self &)
Superclass for callback/observer methods.
Definition: itkCommand.h:44
void(T::* TConstMemberFunctionPointer)(const Object *, const EventObject &)
Definition: itkCommand.h:90
void SetClientDataDeleteCallback(DeleteDataFunctionPointer f)
Definition: itkCommand.h:399
SimpleConstMemberCommand Self
Definition: itkCommand.h:306
void(* ConstFunctionPointer)(const Object *, const EventObject &, void *)
Definition: itkCommand.h:372
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:185
void operator=(const Self &)
CStyleCommand Self
Definition: itkCommand.h:378