ITK  5.0.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:
47  ITK_DISALLOW_COPY_AND_ASSIGN(Command);
48 
50  using Self = Command;
51  using Superclass = Object;
54 
56  itkTypeMacro(Command, Object);
57 
59  virtual void Execute(Object *caller, const EventObject & event) = 0;
60 
64  virtual void Execute(const Object *caller, const EventObject & event) = 0;
65 
66 protected:
67  Command();
68  ~Command() override;
69 };
70 
71 // some implementations for several callback types
72 
82 template< typename T >
83 class ITK_TEMPLATE_EXPORT MemberCommand:public Command
84 {
85 public:
86  ITK_DISALLOW_COPY_AND_ASSIGN(MemberCommand);
87 
89  using TMemberFunctionPointer = void (T::*)(Object *, const EventObject &);
90  using TConstMemberFunctionPointer = void (T::*)(const Object *, 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  void Execute(Object *caller, const EventObject & event) override
121  {
122  if ( m_MemberFunction )
123  {
124  ( ( *m_This ).*( m_MemberFunction ) )( caller, event );
125  }
126  }
128 
130  void Execute(const Object *caller, const EventObject & event) override
131  {
132  if ( m_ConstMemberFunction )
133  {
134  ( ( *m_This ).*( m_ConstMemberFunction ) )( caller, event );
135  }
136  }
138 
139 protected:
140  T * m_This;
143 
145  m_This( nullptr ),
146  m_MemberFunction( nullptr ),
147  m_ConstMemberFunction( nullptr )
148  {}
149 
150  ~MemberCommand() override = default;
151 };
152 
162 template< typename T >
163 class ITK_TEMPLATE_EXPORT ReceptorMemberCommand:public Command
164 {
165 public:
166  ITK_DISALLOW_COPY_AND_ASSIGN(ReceptorMemberCommand);
167 
169  using TMemberFunctionPointer = void (T::*)(const EventObject &);
170 
174 
176  itkNewMacro(Self);
177 
179  itkTypeMacro(ReceptorMemberCommand, Command);
180 
183  void SetCallbackFunction(T *object,
184  TMemberFunctionPointer memberFunction)
185  {
186  m_This = object;
187  m_MemberFunction = memberFunction;
188  }
189 
191  void Execute(Object *, const EventObject & event) override
192  {
193  if ( m_MemberFunction )
194  {
195  ( ( *m_This ).*( m_MemberFunction ) )( event );
196  }
197  }
199 
201  void Execute(const Object *, const EventObject & event) override
202  {
203  if ( m_MemberFunction )
204  {
205  ( ( *m_This ).*( m_MemberFunction ) )( event );
206  }
207  }
209 
210 protected:
211  T * m_This;
213 
215  m_This( nullptr ),
216  m_MemberFunction( nullptr )
217  {}
218 
219  ~ReceptorMemberCommand() override = default;
220 };
221 
231 template< typename T >
232 class ITK_TEMPLATE_EXPORT SimpleMemberCommand:public Command
233 {
234 public:
235  ITK_DISALLOW_COPY_AND_ASSIGN(SimpleMemberCommand);
236 
238  using TMemberFunctionPointer = void (T::*)();
239 
243 
245  itkTypeMacro(SimpleMemberCommand, Command);
246 
248  itkNewMacro(Self);
249 
251  void SetCallbackFunction(T *object,
252  TMemberFunctionPointer memberFunction)
253  {
254  m_This = object;
255  m_MemberFunction = memberFunction;
256  }
257 
259  void Execute(Object *, const EventObject &) override
260  {
261  if ( m_MemberFunction )
262  {
263  ( ( *m_This ).*( m_MemberFunction ) )( );
264  }
265  }
267 
268  void Execute(const Object *, const EventObject &) override
269  {
270  if ( m_MemberFunction )
271  {
272  ( ( *m_This ).*( m_MemberFunction ) )( );
273  }
274  }
275 
276 protected:
277  T * m_This;
278  TMemberFunctionPointer m_MemberFunction;
279 
281  m_This( nullptr ),
282  m_MemberFunction( nullptr )
283  {}
284 
285  ~SimpleMemberCommand() override = default;
286 };
287 
297 template< typename T >
298 class ITK_TEMPLATE_EXPORT SimpleConstMemberCommand:public Command
299 {
300 public:
301  ITK_DISALLOW_COPY_AND_ASSIGN(SimpleConstMemberCommand);
302 
304  using TMemberFunctionPointer = void (T::*)() const;
305 
309 
311  itkTypeMacro(SimpleConstMemberCommand, Command);
312 
314  itkNewMacro(Self);
315 
317  void SetCallbackFunction(const T *object,
318  TMemberFunctionPointer memberFunction)
319  {
320  m_This = object;
321  m_MemberFunction = memberFunction;
322  }
323 
325  void Execute(Object *, const EventObject &) override
326  {
327  if ( m_MemberFunction )
328  {
329  ( ( *m_This ).*( m_MemberFunction ) )( );
330  }
331  }
333 
334  void Execute(const Object *, const EventObject &) override
335  {
336  if ( m_MemberFunction )
337  {
338  ( ( *m_This ).*( m_MemberFunction ) )( );
339  }
340  }
341 
342 protected:
343  const T * m_This;
345 
347  m_This( nullptr ),
348  m_MemberFunction( nullptr )
349  {}
350 
351  ~SimpleConstMemberCommand() override = default;
352 };
353 
366 class ITKCommon_EXPORT CStyleCommand:public Command
367 {
368 public:
370  using FunctionPointer = void (*)(Object *, const EventObject &, void *);
371  using ConstFunctionPointer = void (*)(const Object *, const EventObject &, void *);
372  using DeleteDataFunctionPointer = void (*)(void *);
374 
378 
380  itkTypeMacro(CStyleCommand, Command);
381 
383  itkNewMacro(Self);
384 
387  void SetClientData(void *cd);
388 
390  void SetCallback(FunctionPointer f);
391  void SetConstCallback(ConstFunctionPointer f);
393 
395  void SetClientDataDeleteCallback(DeleteDataFunctionPointer f);
396 
398  void Execute(Object *caller, const EventObject & event) override;
399 
401  void Execute(const Object *caller, const EventObject & event) override;
402 
403 protected:
404  CStyleCommand();
405  ~CStyleCommand() override;
406 
407  void * m_ClientData{ nullptr };
408  FunctionPointer m_Callback{ nullptr };
409  ConstFunctionPointer m_ConstCallback{ nullptr };
410  DeleteDataFunctionPointer m_ClientDataDeleteCallback{ nullptr };
411 };
412 } // end namespace itk
413 
414 #endif
void Execute(Object *, const EventObject &event) override
Definition: itkCommand.h:191
TConstMemberFunctionPointer m_ConstMemberFunction
Definition: itkCommand.h:142
void(*)(void *) DeleteDataFunctionPointer
Definition: itkCommand.h:372
void Execute(Object *caller, const EventObject &event) override
Definition: itkCommand.h:120
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:344
void SetCallbackFunction(const T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:317
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:141
void Execute(const Object *, const EventObject &) override
Definition: itkCommand.h:334
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:278
A Command subclass that calls a pointer to a C function.
Definition: itkCommand.h:366
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:212
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:83
void Execute(const Object *, const EventObject &) override
Definition: itkCommand.h:268
void(*)(Object *, const EventObject &, void *) FunctionPointer
Definition: itkCommand.h:370
void Execute(Object *, const EventObject &) override
Definition: itkCommand.h:259
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:105
void(T::*)() const TMemberFunctionPointer
Definition: itkCommand.h:304
void Execute(Object *, const EventObject &) override
Definition: itkCommand.h:325
void(T::*)(const EventObject &) TMemberFunctionPointer
Definition: itkCommand.h:169
void SetCallbackFunction(T *object, TConstMemberFunctionPointer memberFunction)
Definition: itkCommand.h:112
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:163
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:232
void(T::*)(const Object *, const EventObject &) TConstMemberFunctionPointer
Definition: itkCommand.h:90
void(*)(const Object *, const EventObject &, void *) ConstFunctionPointer
Definition: itkCommand.h:371
void Execute(const Object *, const EventObject &event) override
Definition: itkCommand.h:201
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:298
class ITK_FORWARD_EXPORT Command
Definition: itkObject.h:41
Base class for most ITK classes.
Definition: itkObject.h:60
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:251
Superclass for callback/observer methods.
Definition: itkCommand.h:44
void(T::*)(Object *, const EventObject &) TMemberFunctionPointer
Definition: itkCommand.h:89
void Execute(const Object *caller, const EventObject &event) override
Definition: itkCommand.h:130
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:183