ITK  4.4.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< class 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  virtual ~MemberCommand(){}
147 
148 private:
149  MemberCommand(const Self &); //purposely not implemented
150  void operator=(const Self &); //purposely not implemented
151 };
152 
162 template< class T >
164 {
165 public:
167  typedef void ( T::*TMemberFunctionPointer )(const EventObject &);
168 
172 
174  itkNewMacro(Self);
175 
177  itkTypeMacro(ReceptorMemberCommand, Command);
178 
181  void SetCallbackFunction(T *object,
182  TMemberFunctionPointer memberFunction)
183  {
184  m_This = object;
185  m_MemberFunction = memberFunction;
186  }
187 
189  virtual void Execute(Object *, const EventObject & event)
190  {
191  if ( m_MemberFunction )
192  {
193  ( ( *m_This ).*( m_MemberFunction ) )( event );
194  }
195  }
197 
199  virtual void Execute(const Object *, const EventObject & event)
200  {
201  if ( m_MemberFunction )
202  {
203  ( ( *m_This ).*( m_MemberFunction ) )( event );
204  }
205  }
207 
208 protected:
209  T * m_This;
213 
214 private:
215  ReceptorMemberCommand(const Self &); //purposely not implemented
216  void operator=(const Self &); //purposely not implemented
217 };
218 
228 template< class T >
230 {
231 public:
233  typedef void ( T::*TMemberFunctionPointer )();
234 
238 
240  itkTypeMacro(SimpleMemberCommand, Command);
241 
243  itkNewMacro(Self);
244 
246  void SetCallbackFunction(T *object,
247  TMemberFunctionPointer memberFunction)
248  {
249  m_This = object;
250  m_MemberFunction = memberFunction;
251  }
252 
254  virtual void Execute(Object *, const EventObject &)
255  {
256  if ( m_MemberFunction )
257  {
258  ( ( *m_This ).*( m_MemberFunction ) )( );
259  }
260  }
262 
263  virtual void Execute(const Object *, const EventObject &)
264  {
265  if ( m_MemberFunction )
266  {
267  ( ( *m_This ).*( m_MemberFunction ) )( );
268  }
269  }
270 
271 protected:
272  T * m_This;
275  virtual ~SimpleMemberCommand() {}
276 
277 private:
278  SimpleMemberCommand(const Self &); //purposely not implemented
279  void operator=(const Self &); //purposely not implemented
280 };
281 
291 template< class T >
293 {
294 public:
296  typedef void ( T::*TMemberFunctionPointer )() const;
297 
301 
303  itkTypeMacro(SimpleConstMemberCommand, Command);
304 
306  itkNewMacro(Self);
307 
309  void SetCallbackFunction(const T *object,
310  TMemberFunctionPointer memberFunction)
311  {
312  m_This = object;
313  m_MemberFunction = memberFunction;
314  }
315 
317  virtual void Execute(Object *, const EventObject &)
318  {
319  if ( m_MemberFunction )
320  {
321  ( ( *m_This ).*( m_MemberFunction ) )( );
322  }
323  }
325 
326  virtual void Execute(const Object *, const EventObject &)
327  {
328  if ( m_MemberFunction )
329  {
330  ( ( *m_This ).*( m_MemberFunction ) )( );
331  }
332  }
333 
334 protected:
335  const T * m_This;
339 
340 private:
341  SimpleConstMemberCommand(const Self &); //purposely not implemented
342  void operator=(const Self &); //purposely not implemented
343 };
344 
357 class CStyleCommand:public Command
358 {
359 public:
361  typedef void ( *FunctionPointer )(Object *, const EventObject &, void *);
362  typedef void ( *ConstFunctionPointer )(const Object *,
363  const EventObject &, void *);
364  typedef void ( *DeleteDataFunctionPointer )(void *);
366 
370 
372  itkTypeMacro(CStyleCommand, Command);
373 
375  itkNewMacro(Self);
376 
379  void SetClientData(void *cd) { m_ClientData = cd; }
380 
383  { m_Callback = f; }
385  { m_ConstCallback = f; }
387 
391 
393  void Execute(Object *caller, const EventObject & event)
394  {
395  if ( m_Callback )
396  {
397  m_Callback(caller, event, m_ClientData);
398  }
399  }
401 
403  void Execute(const Object *caller, const EventObject & event)
404  {
405  if ( m_ConstCallback )
406  {
407  m_ConstCallback(caller, event, m_ClientData);
408  }
409  }
411 
412 protected:
415  {
416  // not implemented
417  }
418 
420  {
422  {
424  }
425  }
426 
427  void * m_ClientData;
431 };
432 } // end namespace itk
433 
434 #endif
435