ITK  4.13.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() ITK_OVERRIDE;
67 
68 private:
69  ITK_DISALLOW_COPY_AND_ASSIGN(Command);
70 };
71 
72 // some implementations for several callback types
73 
83 template< typename T >
84 class ITK_TEMPLATE_EXPORT 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) ITK_OVERRIDE
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) ITK_OVERRIDE
131  {
132  if ( m_ConstMemberFunction )
133  {
134  ( ( *m_This ).*( m_ConstMemberFunction ) )( caller, event );
135  }
136  }
138 
139 protected:
140  T * m_This;
141  TMemberFunctionPointer m_MemberFunction;
142  TConstMemberFunctionPointer m_ConstMemberFunction;
143 
145  m_This( ITK_NULLPTR ),
146  m_MemberFunction( ITK_NULLPTR ),
147  m_ConstMemberFunction( ITK_NULLPTR )
148  {}
149 
150  virtual ~MemberCommand() ITK_OVERRIDE {}
151 
152 private:
153  ITK_DISALLOW_COPY_AND_ASSIGN(MemberCommand);
154 };
155 
165 template< typename T >
166 class ITK_TEMPLATE_EXPORT ReceptorMemberCommand:public Command
167 {
168 public:
170  typedef void ( T::*TMemberFunctionPointer )(const EventObject &);
171 
175 
177  itkNewMacro(Self);
178 
180  itkTypeMacro(ReceptorMemberCommand, Command);
181 
184  void SetCallbackFunction(T *object,
185  TMemberFunctionPointer memberFunction)
186  {
187  m_This = object;
188  m_MemberFunction = memberFunction;
189  }
190 
192  virtual void Execute(Object *, const EventObject & event) ITK_OVERRIDE
193  {
194  if ( m_MemberFunction )
195  {
196  ( ( *m_This ).*( m_MemberFunction ) )( event );
197  }
198  }
200 
202  virtual void Execute(const Object *, const EventObject & event) ITK_OVERRIDE
203  {
204  if ( m_MemberFunction )
205  {
206  ( ( *m_This ).*( m_MemberFunction ) )( event );
207  }
208  }
210 
211 protected:
212  T * m_This;
213  TMemberFunctionPointer m_MemberFunction;
214 
216  m_This( ITK_NULLPTR ),
217  m_MemberFunction( ITK_NULLPTR )
218  {}
219 
220  virtual ~ReceptorMemberCommand() ITK_OVERRIDE {}
221 
222 private:
223  ITK_DISALLOW_COPY_AND_ASSIGN(ReceptorMemberCommand);
224 };
225 
235 template< typename T >
236 class ITK_TEMPLATE_EXPORT SimpleMemberCommand:public Command
237 {
238 public:
240  typedef void ( T::*TMemberFunctionPointer )();
241 
245 
247  itkTypeMacro(SimpleMemberCommand, Command);
248 
250  itkNewMacro(Self);
251 
253  void SetCallbackFunction(T *object,
254  TMemberFunctionPointer memberFunction)
255  {
256  m_This = object;
257  m_MemberFunction = memberFunction;
258  }
259 
261  virtual void Execute(Object *, const EventObject &) ITK_OVERRIDE
262  {
263  if ( m_MemberFunction )
264  {
265  ( ( *m_This ).*( m_MemberFunction ) )( );
266  }
267  }
269 
270  virtual void Execute(const Object *, const EventObject &) ITK_OVERRIDE
271  {
272  if ( m_MemberFunction )
273  {
274  ( ( *m_This ).*( m_MemberFunction ) )( );
275  }
276  }
277 
278 protected:
279  T * m_This;
280  TMemberFunctionPointer m_MemberFunction;
281 
283  m_This( ITK_NULLPTR ),
284  m_MemberFunction( ITK_NULLPTR )
285  {}
286 
287  virtual ~SimpleMemberCommand() ITK_OVERRIDE {}
288 
289 private:
290  ITK_DISALLOW_COPY_AND_ASSIGN(SimpleMemberCommand);
291 };
292 
302 template< typename T >
303 class ITK_TEMPLATE_EXPORT SimpleConstMemberCommand:public Command
304 {
305 public:
307  typedef void ( T::*TMemberFunctionPointer )() const;
308 
312 
314  itkTypeMacro(SimpleConstMemberCommand, Command);
315 
317  itkNewMacro(Self);
318 
320  void SetCallbackFunction(const T *object,
321  TMemberFunctionPointer memberFunction)
322  {
323  m_This = object;
324  m_MemberFunction = memberFunction;
325  }
326 
328  virtual void Execute(Object *, const EventObject &) ITK_OVERRIDE
329  {
330  if ( m_MemberFunction )
331  {
332  ( ( *m_This ).*( m_MemberFunction ) )( );
333  }
334  }
336 
337  virtual void Execute(const Object *, const EventObject &) ITK_OVERRIDE
338  {
339  if ( m_MemberFunction )
340  {
341  ( ( *m_This ).*( m_MemberFunction ) )( );
342  }
343  }
344 
345 protected:
346  const T * m_This;
347  TMemberFunctionPointer m_MemberFunction;
348 
350  m_This( ITK_NULLPTR ),
351  m_MemberFunction( ITK_NULLPTR )
352  {}
353 
355 
356 private:
357  ITK_DISALLOW_COPY_AND_ASSIGN(SimpleConstMemberCommand);
358 };
359 
372 class ITKCommon_EXPORT CStyleCommand:public Command
373 {
374 public:
376  typedef void ( *FunctionPointer )(Object *, const EventObject &, void *);
377  typedef void ( *ConstFunctionPointer )(const Object *,
378  const EventObject &, void *);
379  typedef void ( *DeleteDataFunctionPointer )(void *);
381 
385 
387  itkTypeMacro(CStyleCommand, Command);
388 
390  itkNewMacro(Self);
391 
394  void SetClientData(void *cd);
395 
397  void SetCallback(FunctionPointer f);
398  void SetConstCallback(ConstFunctionPointer f);
400 
402  void SetClientDataDeleteCallback(DeleteDataFunctionPointer f);
403 
405  virtual void Execute(Object *caller, const EventObject & event) ITK_OVERRIDE;
406 
408  virtual void Execute(const Object *caller, const EventObject & event) ITK_OVERRIDE;
409 
410 protected:
411  CStyleCommand();
412  ~CStyleCommand() ITK_OVERRIDE;
413 
414  void * m_ClientData;
415  FunctionPointer m_Callback;
416  ConstFunctionPointer m_ConstCallback;
417  DeleteDataFunctionPointer m_ClientDataDeleteCallback;
418 };
419 } // end namespace itk
420 
421 #endif
MemberCommand Self
Definition: itkCommand.h:94
virtual void Execute(Object *, const EventObject &event) override
Definition: itkCommand.h:192
ReceptorMemberCommand Self
Definition: itkCommand.h:173
SimpleMemberCommand Self
Definition: itkCommand.h:243
SmartPointer< Self > Pointer
Definition: itkCommand.h:244
TConstMemberFunctionPointer m_ConstMemberFunction
Definition: itkCommand.h:142
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:347
void SetCallbackFunction(const T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:320
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:141
SmartPointer< Self > Pointer
Definition: itkCommand.h:95
virtual ~ReceptorMemberCommand() override
Definition: itkCommand.h:220
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:280
A Command subclass that calls a pointer to a C function.
Definition: itkCommand.h:372
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:213
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:84
virtual void Execute(Object *, const EventObject &) override
Definition: itkCommand.h:328
virtual void Execute(Object *, const EventObject &) override
Definition: itkCommand.h:261
virtual void Execute(const Object *, const EventObject &) override
Definition: itkCommand.h:337
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:105
virtual void Execute(const Object *, const EventObject &event) override
Definition: itkCommand.h:202
SmartPointer< Self > Pointer
Definition: itkCommand.h:384
SmartPointer< Self > Pointer
Definition: itkCommand.h:50
virtual ~SimpleMemberCommand() override
Definition: itkCommand.h:287
void SetCallbackFunction(T *object, TConstMemberFunctionPointer memberFunction)
Definition: itkCommand.h:112
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:166
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:236
virtual ~MemberCommand() override
Definition: itkCommand.h:150
virtual void Execute(Object *caller, const EventObject &event) override
Definition: itkCommand.h:120
SmartPointer< Self > Pointer
Definition: itkCommand.h:311
SmartPointer< const Self > ConstPointer
Definition: itkCommand.h:51
Object Superclass
Definition: itkCommand.h:49
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:303
virtual void Execute(const Object *, const EventObject &) override
Definition: itkCommand.h:270
Command Self
Definition: itkCommand.h:48
SmartPointer< Self > Pointer
Definition: itkCommand.h:174
class ITK_FORWARD_EXPORT Command
Definition: itkObject.h:40
Base class for most ITK classes.
Definition: itkObject.h:59
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:253
virtual void Execute(const Object *caller, const EventObject &event) override
Definition: itkCommand.h:130
Superclass for callback/observer methods.
Definition: itkCommand.h:44
SimpleConstMemberCommand Self
Definition: itkCommand.h:310
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:184
CStyleCommand Self
Definition: itkCommand.h:383