ITK  5.2.0
Insight Toolkit
itkCommand.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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 #include <functional>
24 
25 namespace itk
26 {
44 // The superclass that all commands should be subclasses of
45 class ITKCommon_EXPORT Command : public Object
46 {
47 public:
48  ITK_DISALLOW_COPY_AND_ASSIGN(Command);
49 
51  using Self = Command;
52  using Superclass = Object;
55 
57  itkTypeMacro(Command, Object);
58 
60  virtual void
61  Execute(Object * caller, const EventObject & event) = 0;
62 
66  virtual void
67  Execute(const Object * caller, const EventObject & event) = 0;
68 
69 protected:
70  Command();
71  ~Command() override;
72 };
73 
74 // some implementations for several callback types
75 
85 template <typename T>
86 class ITK_TEMPLATE_EXPORT MemberCommand : public Command
87 {
88 public:
89  ITK_DISALLOW_COPY_AND_ASSIGN(MemberCommand);
90 
92  using TMemberFunctionPointer = void (T::*)(Object *, const EventObject &);
93  using TConstMemberFunctionPointer = void (T::*)(const Object *, const EventObject &);
95 
99 
101  itkNewMacro(Self);
102 
104  itkTypeMacro(MemberCommand, Command);
105 
108  void
109  SetCallbackFunction(T * object, TMemberFunctionPointer memberFunction)
110  {
111  m_This = object;
112  m_MemberFunction = memberFunction;
113  }
114 
115  void
117  {
118  m_This = object;
119  m_ConstMemberFunction = memberFunction;
120  }
121 
123  void
124  Execute(Object * caller, const EventObject & event) override
125  {
126  if (m_MemberFunction)
127  {
128  ((*m_This).*(m_MemberFunction))(caller, event);
129  }
130  }
132 
134  void
135  Execute(const Object * caller, const EventObject & event) override
136  {
137  if (m_ConstMemberFunction)
138  {
139  ((*m_This).*(m_ConstMemberFunction))(caller, event);
140  }
141  }
143 
144 protected:
145  T * m_This;
148 
150  : m_This(nullptr)
151  , m_MemberFunction(nullptr)
152  , m_ConstMemberFunction(nullptr)
153  {}
154 
155  ~MemberCommand() override = default;
156 };
157 
167 template <typename T>
168 class ITK_TEMPLATE_EXPORT ReceptorMemberCommand : public Command
169 {
170 public:
171  ITK_DISALLOW_COPY_AND_ASSIGN(ReceptorMemberCommand);
172 
174  using TMemberFunctionPointer = void (T::*)(const EventObject &);
175 
179 
181  itkNewMacro(Self);
182 
184  itkTypeMacro(ReceptorMemberCommand, Command);
185 
188  void
189  SetCallbackFunction(T * object, TMemberFunctionPointer memberFunction)
190  {
191  m_This = object;
192  m_MemberFunction = memberFunction;
193  }
194 
196  void
197  Execute(Object *, const EventObject & event) override
198  {
199  if (m_MemberFunction)
200  {
201  ((*m_This).*(m_MemberFunction))(event);
202  }
203  }
205 
207  void
208  Execute(const Object *, const EventObject & event) override
209  {
210  if (m_MemberFunction)
211  {
212  ((*m_This).*(m_MemberFunction))(event);
213  }
214  }
216 
217 protected:
218  T * m_This;
220 
222  : m_This(nullptr)
223  , m_MemberFunction(nullptr)
224  {}
225 
226  ~ReceptorMemberCommand() override = default;
227 };
228 
238 template <typename T>
239 class ITK_TEMPLATE_EXPORT SimpleMemberCommand : public Command
240 {
241 public:
242  ITK_DISALLOW_COPY_AND_ASSIGN(SimpleMemberCommand);
243 
245  using TMemberFunctionPointer = void (T::*)();
246 
250 
252  itkTypeMacro(SimpleMemberCommand, Command);
253 
255  itkNewMacro(Self);
256 
258  void
259  SetCallbackFunction(T * object, TMemberFunctionPointer memberFunction)
260  {
261  m_This = object;
262  m_MemberFunction = memberFunction;
263  }
264 
266  void
267  Execute(Object *, const EventObject &) override
268  {
269  if (m_MemberFunction)
270  {
271  ((*m_This).*(m_MemberFunction))();
272  }
273  }
275 
276  void
277  Execute(const Object *, const EventObject &) override
278  {
279  if (m_MemberFunction)
280  {
281  ((*m_This).*(m_MemberFunction))();
282  }
283  }
284 
285 protected:
286  T * m_This;
288 
290  : m_This(nullptr)
291  , m_MemberFunction(nullptr)
292  {}
293 
294  ~SimpleMemberCommand() override = default;
295 };
296 
306 template <typename T>
307 class ITK_TEMPLATE_EXPORT SimpleConstMemberCommand : public Command
308 {
309 public:
310  ITK_DISALLOW_COPY_AND_ASSIGN(SimpleConstMemberCommand);
311 
313  using TMemberFunctionPointer = void (T::*)() const;
314 
318 
320  itkTypeMacro(SimpleConstMemberCommand, Command);
321 
323  itkNewMacro(Self);
324 
326  void
327  SetCallbackFunction(const T * object, TMemberFunctionPointer memberFunction)
328  {
329  m_This = object;
330  m_MemberFunction = memberFunction;
331  }
332 
334  void
335  Execute(Object *, const EventObject &) override
336  {
337  if (m_MemberFunction)
338  {
339  ((*m_This).*(m_MemberFunction))();
340  }
341  }
343 
344  void
345  Execute(const Object *, const EventObject &) override
346  {
347  if (m_MemberFunction)
348  {
349  ((*m_This).*(m_MemberFunction))();
350  }
351  }
352 
353 protected:
354  const T * m_This;
356 
358  : m_This(nullptr)
359  , m_MemberFunction(nullptr)
360  {}
361 
362  ~SimpleConstMemberCommand() override = default;
363 };
364 
377 class ITKCommon_EXPORT CStyleCommand : public Command
378 {
379 public:
381  using FunctionPointer = void (*)(Object *, const EventObject &, void *);
382  using ConstFunctionPointer = void (*)(const Object *, const EventObject &, void *);
383  using DeleteDataFunctionPointer = void (*)(void *);
385 
389 
391  itkTypeMacro(CStyleCommand, Command);
392 
394  itkNewMacro(Self);
395 
398  void
399  SetClientData(void * cd);
400 
402  void
403  SetCallback(FunctionPointer f);
404  void
405  SetConstCallback(ConstFunctionPointer f);
407 
409  void
410  SetClientDataDeleteCallback(DeleteDataFunctionPointer f);
411 
413  void
414  Execute(Object * caller, const EventObject & event) override;
415 
417  void
418  Execute(const Object * caller, const EventObject & event) override;
419 
420 protected:
421  CStyleCommand();
422  ~CStyleCommand() override;
423 
424  void * m_ClientData{ nullptr };
425  FunctionPointer m_Callback{ nullptr };
426  ConstFunctionPointer m_ConstCallback{ nullptr };
427  DeleteDataFunctionPointer m_ClientDataDeleteCallback{ nullptr };
428 };
429 
430 
440 class ITKCommon_EXPORT FunctionCommand : public Command
441 {
442 public:
446 
447  using FunctionObjectType = std::function<void(const EventObject &)>;
448 
450  itkTypeMacro(FunctionCommand, Command);
451 
453  itkNewMacro(Self);
454 
456  void
457  SetCallback(FunctionObjectType f);
458 
460  void
461  Execute(Object *, const EventObject & event) override;
462 
464  void
465  Execute(const Object *, const EventObject & event) override;
466 
467 protected:
468  FunctionCommand();
469  ~FunctionCommand() override;
470 
471 
473 };
474 
475 } // end namespace itk
476 
477 #endif
itk::ReceptorMemberCommand::m_This
T * m_This
Definition: itkCommand.h:218
itkObjectFactory.h
itk::SimpleMemberCommand::TMemberFunctionPointer
void(T::*)() TMemberFunctionPointer
Definition: itkCommand.h:245
itk::FunctionCommand
A Command subclass that calls a std::function object.
Definition: itkCommand.h:440
itk::MemberCommand::SetCallbackFunction
void SetCallbackFunction(T *object, TConstMemberFunctionPointer memberFunction)
Definition: itkCommand.h:116
itk::SimpleConstMemberCommand
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:307
itk::SimpleConstMemberCommand::SetCallbackFunction
void SetCallbackFunction(const T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:327
itk::SimpleMemberCommand::m_MemberFunction
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:287
itk::MemberCommand::TConstMemberFunctionPointer
void(T::*)(const Object *, const EventObject &) TConstMemberFunctionPointer
Definition: itkCommand.h:93
itk::SimpleMemberCommand::Execute
void Execute(Object *, const EventObject &) override
Definition: itkCommand.h:267
itk::SimpleMemberCommand
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:239
itk::MemberCommand::m_This
T * m_This
Definition: itkCommand.h:145
itk::CStyleCommand::ConstFunctionPointer
void(*)(const Object *, const EventObject &, void *) ConstFunctionPointer
Definition: itkCommand.h:382
itk::MemberCommand::Execute
void Execute(const Object *caller, const EventObject &event) override
Definition: itkCommand.h:135
itk::ReceptorMemberCommand
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:168
itk::ReceptorMemberCommand::ReceptorMemberCommand
ReceptorMemberCommand()
Definition: itkCommand.h:221
itk::SmartPointer< Self >
itk::SimpleConstMemberCommand::Execute
void Execute(const Object *, const EventObject &) override
Definition: itkCommand.h:345
itk::CStyleCommand::DeleteDataFunctionPointer
void(*)(void *) DeleteDataFunctionPointer
Definition: itkCommand.h:383
itk::SimpleConstMemberCommand::m_This
const T * m_This
Definition: itkCommand.h:354
itk::ReceptorMemberCommand::Execute
void Execute(Object *, const EventObject &event) override
Definition: itkCommand.h:197
itk::FunctionCommand::FunctionObjectType
std::function< void(const EventObject &)> FunctionObjectType
Definition: itkCommand.h:447
itk::SimpleMemberCommand::m_This
T * m_This
Definition: itkCommand.h:286
itk::Command
Superclass for callback/observer methods.
Definition: itkCommand.h:45
itk::ReceptorMemberCommand::Execute
void Execute(const Object *, const EventObject &event) override
Definition: itkCommand.h:208
itk::FunctionCommand::m_FunctionObject
FunctionObjectType m_FunctionObject
Definition: itkCommand.h:472
itk::MemberCommand::Execute
void Execute(Object *caller, const EventObject &event) override
Definition: itkCommand.h:124
itk::SimpleConstMemberCommand::SimpleConstMemberCommand
SimpleConstMemberCommand()
Definition: itkCommand.h:357
itk::SimpleMemberCommand::SimpleMemberCommand
SimpleMemberCommand()
Definition: itkCommand.h:289
itk::Command
class ITK_FORWARD_EXPORT Command
Definition: itkObject.h:43
itk::SimpleMemberCommand::Execute
void Execute(const Object *, const EventObject &) override
Definition: itkCommand.h:277
itk::SimpleConstMemberCommand::m_MemberFunction
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:355
itk::MemberCommand::m_ConstMemberFunction
TConstMemberFunctionPointer m_ConstMemberFunction
Definition: itkCommand.h:147
itk::SimpleConstMemberCommand::Execute
void Execute(Object *, const EventObject &) override
Definition: itkCommand.h:335
itk::SimpleMemberCommand::SetCallbackFunction
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:259
itkObject.h
itk::MemberCommand
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:86
itk::ReceptorMemberCommand::SetCallbackFunction
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:189
itk::ReceptorMemberCommand::m_MemberFunction
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:219
itk::SimpleConstMemberCommand::TMemberFunctionPointer
void(T::*)() const TMemberFunctionPointer
Definition: itkCommand.h:313
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::MemberCommand::SetCallbackFunction
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:109
itk::CStyleCommand::FunctionPointer
void(*)(Object *, const EventObject &, void *) FunctionPointer
Definition: itkCommand.h:381
itk::MemberCommand::m_MemberFunction
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:146
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:62
itk::MemberCommand::MemberCommand
MemberCommand()
Definition: itkCommand.h:149
itk::EventObject
Abstraction of the Events used to communicating among filters and with GUIs.
Definition: itkEventObject.h:57
itk::CStyleCommand
A Command subclass that calls a pointer to a C function.
Definition: itkCommand.h:377
itk::ReceptorMemberCommand::TMemberFunctionPointer
void(T::*)(const EventObject &) TMemberFunctionPointer
Definition: itkCommand.h:174
itk::MemberCommand::TMemberFunctionPointer
void(T::*)(Object *, const EventObject &) TMemberFunctionPointer
Definition: itkCommand.h:92