ITK  4.9.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 &) ITK_DELETE_FUNCTION;
70  void operator=(const Self &) ITK_DELETE_FUNCTION;
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;
142  TMemberFunctionPointer m_MemberFunction;
143  TConstMemberFunctionPointer m_ConstMemberFunction;
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 &) ITK_DELETE_FUNCTION;
155  void operator=(const Self &) ITK_DELETE_FUNCTION;
156 };
157 
167 template< typename T >
168 class ReceptorMemberCommand:public Command
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;
215  TMemberFunctionPointer m_MemberFunction;
216 
218  m_This( ITK_NULLPTR ),
219  m_MemberFunction( ITK_NULLPTR )
220  {}
221 
223 
224 private:
225  ReceptorMemberCommand(const Self &) ITK_DELETE_FUNCTION;
226  void operator=(const Self &) ITK_DELETE_FUNCTION;
227 };
228 
238 template< typename T >
239 class SimpleMemberCommand:public Command
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;
283  TMemberFunctionPointer m_MemberFunction;
284 
286  m_This( ITK_NULLPTR ),
287  m_MemberFunction( ITK_NULLPTR )
288  {}
289 
290  virtual ~SimpleMemberCommand() {}
291 
292 private:
293  SimpleMemberCommand(const Self &) ITK_DELETE_FUNCTION;
294  void operator=(const Self &) ITK_DELETE_FUNCTION;
295 };
296 
306 template< typename T >
307 class SimpleConstMemberCommand:public Command
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;
351  TMemberFunctionPointer m_MemberFunction;
352 
354  m_This( ITK_NULLPTR ),
355  m_MemberFunction( ITK_NULLPTR )
356  {}
357 
359 
360 private:
361  SimpleConstMemberCommand(const Self &) ITK_DELETE_FUNCTION;
362  void operator=(const Self &) ITK_DELETE_FUNCTION;
363 };
364 
377 class ITKCommon_EXPORT 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);
400 
402  void SetCallback(FunctionPointer f);
403  void SetConstCallback(ConstFunctionPointer f);
405 
407  void SetClientDataDeleteCallback(DeleteDataFunctionPointer f);
408 
410  virtual void Execute(Object *caller, const EventObject & event) ITK_OVERRIDE;
411 
413  virtual void Execute(const Object *caller, const EventObject & event) ITK_OVERRIDE;
414 
415 protected:
416  CStyleCommand();
417  ~CStyleCommand();
418 
419  void * m_ClientData;
420  FunctionPointer m_Callback;
421  ConstFunctionPointer m_ConstCallback;
422  DeleteDataFunctionPointer m_ClientDataDeleteCallback;
423 };
424 } // end namespace itk
425 
426 #endif
FunctionPointer m_Callback
Definition: itkCommand.h:420
MemberCommand Self
Definition: itkCommand.h:95
virtual void Execute(Object *, const EventObject &event) override
Definition: itkCommand.h:194
ReceptorMemberCommand Self
Definition: itkCommand.h:175
SimpleMemberCommand Self
Definition: itkCommand.h:246
SmartPointer< Self > Pointer
Definition: itkCommand.h:247
TConstMemberFunctionPointer m_ConstMemberFunction
Definition: itkCommand.h:143
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:351
void SetCallbackFunction(const T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:324
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:142
SmartPointer< Self > Pointer
Definition: itkCommand.h:96
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(Object *, const EventObject &) override
Definition: itkCommand.h:332
virtual ~ReceptorMemberCommand()
Definition: itkCommand.h:222
virtual void Execute(Object *, const EventObject &) override
Definition: itkCommand.h:264
virtual ~MemberCommand()
Definition: itkCommand.h:151
virtual void Execute(const Object *, const EventObject &) override
Definition: itkCommand.h:341
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:106
ConstFunctionPointer m_ConstCallback
Definition: itkCommand.h:421
virtual void Execute(const Object *, const EventObject &event) override
Definition: itkCommand.h:204
SmartPointer< Self > Pointer
Definition: itkCommand.h:389
SmartPointer< Self > Pointer
Definition: itkCommand.h:50
void SetCallbackFunction(T *object, TConstMemberFunctionPointer memberFunction)
Definition: itkCommand.h:113
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
DeleteDataFunctionPointer m_ClientDataDeleteCallback
Definition: itkCommand.h:422
virtual ~SimpleMemberCommand()
Definition: itkCommand.h:290
virtual void Execute(Object *caller, const EventObject &event) override
Definition: itkCommand.h:121
SmartPointer< Self > Pointer
Definition: itkCommand.h:315
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:307
virtual void Execute(const Object *, const EventObject &) override
Definition: itkCommand.h:273
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
virtual void Execute(const Object *caller, const EventObject &event) override
Definition: itkCommand.h:131
Superclass for callback/observer methods.
Definition: itkCommand.h:44
SimpleConstMemberCommand Self
Definition: itkCommand.h:314
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:186
CStyleCommand Self
Definition: itkCommand.h:388