ITK  4.2.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 private:
68  Command(const Self &); //purposely not implemented
69  void operator=(const Self &); //purposely not implemented
70 };
71 
72 // some implementations for several callback types
73 
83 template< class T >
84 class MemberCommand:public Command
85 {
86 public:
88  typedef void ( T::*TMemberFunctionPointer )(Object *, const EventObject &);
89  typedef void ( T::*TConstMemberFunctionPointer )(const Object *,
90  const EventObject &);
92 
96 
98  itkNewMacro(Self);
99 
101  itkTypeMacro(MemberCommand, Command);
102 
105  void SetCallbackFunction(T *object,
106  TMemberFunctionPointer memberFunction)
107  {
108  m_This = object;
109  m_MemberFunction = memberFunction;
110  }
111 
112  void SetCallbackFunction(T *object,
113  TConstMemberFunctionPointer memberFunction)
114  {
115  m_This = object;
116  m_ConstMemberFunction = memberFunction;
117  }
118 
120  virtual void Execute(Object *caller, const EventObject & event)
121  {
122  if ( m_MemberFunction )
123  {
124  ( ( *m_This ).*( m_MemberFunction ) )( caller, event );
125  }
126  }
128 
130  virtual void Execute(const Object *caller, const EventObject & event)
131  {
132  if ( m_ConstMemberFunction )
133  {
134  ( ( *m_This ).*( m_ConstMemberFunction ) )( caller, event );
135  }
136  }
138 
139 protected:
140 
141  T * m_This;
145  virtual ~MemberCommand(){}
146 private:
147  MemberCommand(const Self &); //purposely not implemented
148  void operator=(const Self &); //purposely not implemented
149 };
150 
160 template< class T >
162 {
163 public:
165  typedef void ( T::*TMemberFunctionPointer )(const EventObject &);
166 
170 
172  itkNewMacro(Self);
173 
175  itkTypeMacro(ReceptorMemberCommand, Command);
176 
179  void SetCallbackFunction(T *object,
180  TMemberFunctionPointer memberFunction)
181  {
182  m_This = object;
183  m_MemberFunction = memberFunction;
184  }
185 
187  virtual void Execute(Object *, const EventObject & event)
188  {
189  if ( m_MemberFunction )
190  {
191  ( ( *m_This ).*( m_MemberFunction ) )( event );
192  }
193  }
195 
197  virtual void Execute(const Object *, const EventObject & event)
198  {
199  if ( m_MemberFunction )
200  {
201  ( ( *m_This ).*( m_MemberFunction ) )( event );
202  }
203  }
205 
206 protected:
207  T * m_This;
211 private:
212  ReceptorMemberCommand(const Self &); //purposely not implemented
213  void operator=(const Self &); //purposely not implemented
214 };
215 
225 template< class T >
227 {
228 public:
230  typedef void ( T::*TMemberFunctionPointer )();
231 
235 
237  itkTypeMacro(SimpleMemberCommand, Command);
238 
240  itkNewMacro(Self);
241 
243  void SetCallbackFunction(T *object,
244  TMemberFunctionPointer memberFunction)
245  {
246  m_This = object;
247  m_MemberFunction = memberFunction;
248  }
249 
251  virtual void Execute(Object *, const EventObject &)
252  {
253  if ( m_MemberFunction )
254  {
255  ( ( *m_This ).*( m_MemberFunction ) )( );
256  }
257  }
259 
260  virtual void Execute(const Object *, const EventObject &)
261  {
262  if ( m_MemberFunction )
263  {
264  ( ( *m_This ).*( m_MemberFunction ) )( );
265  }
266  }
267 
268 protected:
269  T * m_This;
272  virtual ~SimpleMemberCommand() {}
273 private:
274  SimpleMemberCommand(const Self &); //purposely not implemented
275  void operator=(const Self &); //purposely not implemented
276 };
277 
287 template< class T >
289 {
290 public:
292  typedef void ( T::*TMemberFunctionPointer )() const;
293 
297 
299  itkTypeMacro(SimpleConstMemberCommand, Command);
300 
302  itkNewMacro(Self);
303 
305  void SetCallbackFunction(const T *object,
306  TMemberFunctionPointer memberFunction)
307  {
308  m_This = object;
309  m_MemberFunction = memberFunction;
310  }
311 
313  virtual void Execute(Object *, const EventObject &)
314  {
315  if ( m_MemberFunction )
316  {
317  ( ( *m_This ).*( m_MemberFunction ) )( );
318  }
319  }
321 
322  virtual void Execute(const Object *, const EventObject &)
323  {
324  if ( m_MemberFunction )
325  {
326  ( ( *m_This ).*( m_MemberFunction ) )( );
327  }
328  }
329 
330 protected:
331  const T * m_This;
335 private:
336  SimpleConstMemberCommand(const Self &); //purposely not implemented
337  void operator=(const Self &); //purposely not implemented
338 };
339 
352 class CStyleCommand:public Command
353 {
354 public:
356  typedef void ( *FunctionPointer )(Object *, const EventObject &, void *);
357  typedef void ( *ConstFunctionPointer )(const Object *,
358  const EventObject &, void *);
359  typedef void ( *DeleteDataFunctionPointer )(void *);
361 
365 
367  itkTypeMacro(CStyleCommand, Command);
368 
370  itkNewMacro(Self);
371 
374  void SetClientData(void *cd) { m_ClientData = cd; }
375 
378  { m_Callback = f; }
380  { m_ConstCallback = f; }
382 
386 
388  void Execute(Object *caller, const EventObject & event)
389  {
390  if ( m_Callback )
391  {
392  m_Callback(caller, event, m_ClientData);
393  }
394  }
396 
398  void Execute(const Object *caller, const EventObject & event)
399  {
400  if ( m_ConstCallback )
401  {
402  m_ConstCallback(caller, event, m_ClientData);
403  }
404  }
406 
407 protected:
410  {
411  // not implemented
412  }
413 
415  {
417  {
419  }
420  }
421 
422  void * m_ClientData;
426 };
427 } // end namespace itk
428 
429 #endif
430