ITK  6.0.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  * https://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_MOVE(Command);
49 
51  using Self = Command;
52  using Superclass = Object;
55 
57  itkOverrideGetNameOfClassMacro(Command);
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_MOVE(MemberCommand);
90 
92  using TMemberFunctionPointer = void (T::*)(Object *, const EventObject &);
93  using TConstMemberFunctionPointer = void (T::*)(const Object *, const EventObject &);
99 
101  itkNewMacro(Self);
102 
104  itkOverrideGetNameOfClassMacro(MemberCommand);
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  }
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  }
144 protected:
145  T * m_This{ nullptr };
146  TMemberFunctionPointer m_MemberFunction{ nullptr };
147  TConstMemberFunctionPointer m_ConstMemberFunction{ nullptr };
148 
149  MemberCommand() = default;
150 
151  ~MemberCommand() override = default;
152 };
153 
163 template <typename T>
164 class ITK_TEMPLATE_EXPORT ReceptorMemberCommand : public Command
165 {
166 public:
167  ITK_DISALLOW_COPY_AND_MOVE(ReceptorMemberCommand);
168 
170  using TMemberFunctionPointer = void (T::*)(const EventObject &);
171 
175 
177  itkNewMacro(Self);
178 
180  itkOverrideGetNameOfClassMacro(ReceptorMemberCommand);
181 
184  void
185  SetCallbackFunction(T * object, TMemberFunctionPointer memberFunction)
186  {
187  m_This = object;
188  m_MemberFunction = memberFunction;
189  }
190 
192  void
193  Execute(Object *, const EventObject & event) override
194  {
195  if (m_MemberFunction)
196  {
197  (m_This->*(m_MemberFunction))(event);
198  }
199  }
203  void
204  Execute(const Object *, const EventObject & event) override
205  {
206  if (m_MemberFunction)
207  {
208  (m_This->*(m_MemberFunction))(event);
209  }
210  }
213 protected:
214  T * m_This{ nullptr };
215  TMemberFunctionPointer m_MemberFunction{ nullptr };
216 
217  ReceptorMemberCommand() = default;
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_MOVE(SimpleMemberCommand);
236 
238  using TMemberFunctionPointer = void (T::*)();
239 
243 
245  itkOverrideGetNameOfClassMacro(SimpleMemberCommand);
246 
248  itkNewMacro(Self);
249 
251  void
252  SetCallbackFunction(T * object, TMemberFunctionPointer memberFunction)
253  {
254  m_This = object;
255  m_MemberFunction = memberFunction;
256  }
257 
259  void
260  Execute(Object *, const EventObject &) override
261  {
262  if (m_MemberFunction)
263  {
264  (m_This->*(m_MemberFunction))();
265  }
266  }
269  void
270  Execute(const Object *, const EventObject &) override
271  {
272  if (m_MemberFunction)
273  {
274  (m_This->*(m_MemberFunction))();
275  }
276  }
277 
278 protected:
279  T * m_This{ nullptr };
280  TMemberFunctionPointer m_MemberFunction{ nullptr };
281 
282  SimpleMemberCommand() = default;
283 
284  ~SimpleMemberCommand() override = default;
285 };
286 
296 template <typename T>
297 class ITK_TEMPLATE_EXPORT SimpleConstMemberCommand : public Command
298 {
299 public:
300  ITK_DISALLOW_COPY_AND_MOVE(SimpleConstMemberCommand);
301 
303  using TMemberFunctionPointer = void (T::*)() const;
304 
308 
310  itkOverrideGetNameOfClassMacro(SimpleConstMemberCommand);
311 
313  itkNewMacro(Self);
314 
316  void
317  SetCallbackFunction(const T * object, TMemberFunctionPointer memberFunction)
318  {
319  m_This = object;
320  m_MemberFunction = memberFunction;
321  }
322 
324  void
325  Execute(Object *, const EventObject &) override
326  {
327  if (m_MemberFunction)
328  {
329  (m_This->*(m_MemberFunction))();
330  }
331  }
334  void
335  Execute(const Object *, const EventObject &) override
336  {
337  if (m_MemberFunction)
338  {
339  (m_This->*(m_MemberFunction))();
340  }
341  }
342 
343 protected:
344  const T * m_This{ nullptr };
345  TMemberFunctionPointer m_MemberFunction{ nullptr };
346 
347  SimpleConstMemberCommand() = default;
348 
349  ~SimpleConstMemberCommand() override = default;
350 };
351 
364 class ITKCommon_EXPORT CStyleCommand : public Command
365 {
366 public:
368  using FunctionPointer = void (*)(Object *, const EventObject &, void *);
369  using ConstFunctionPointer = void (*)(const Object *, const EventObject &, void *);
370  using DeleteDataFunctionPointer = void (*)(void *);
376 
378  itkOverrideGetNameOfClassMacro(CStyleCommand);
379 
381  itkNewMacro(Self);
382 
385  void
386  SetClientData(void * cd);
387 
389  void
390  SetCallback(FunctionPointer f);
391  void
392  SetConstCallback(ConstFunctionPointer f);
396  void
397  SetClientDataDeleteCallback(DeleteDataFunctionPointer f);
398 
400  void
401  Execute(Object * caller, const EventObject & event) override;
402 
404  void
405  Execute(const Object * caller, const EventObject & event) override;
406 
407 protected:
408  CStyleCommand();
409  ~CStyleCommand() override;
410 
411  void * m_ClientData{ nullptr };
412  FunctionPointer m_Callback{ nullptr };
413  ConstFunctionPointer m_ConstCallback{ nullptr };
414  DeleteDataFunctionPointer m_ClientDataDeleteCallback{ nullptr };
415 };
416 
417 
427 class ITKCommon_EXPORT FunctionCommand : public Command
428 {
429 public:
433 
434  using FunctionObjectType = std::function<void(const EventObject &)>;
435 
437  itkOverrideGetNameOfClassMacro(FunctionCommand);
438 
440  itkNewMacro(Self);
441 
443  void
444  SetCallback(FunctionObjectType f);
445 
447  void
448  Execute(Object *, const EventObject & event) override;
449 
451  void
452  Execute(const Object *, const EventObject & event) override;
453 
454 protected:
455  FunctionCommand();
456  ~FunctionCommand() override;
457 
458 
459  FunctionObjectType m_FunctionObject{};
460 };
461 
462 } // end namespace itk
463 
464 #endif
itkObjectFactory.h
itk::SimpleMemberCommand::TMemberFunctionPointer
void(T::*)() TMemberFunctionPointer
Definition: itkCommand.h:238
itk::FunctionCommand
A Command subclass that calls a std::function object.
Definition: itkCommand.h:427
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:297
itk::SimpleConstMemberCommand::SetCallbackFunction
void SetCallbackFunction(const T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:317
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:260
itk::SimpleMemberCommand
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:232
itk::CStyleCommand::ConstFunctionPointer
void(*)(const Object *, const EventObject &, void *) ConstFunctionPointer
Definition: itkCommand.h:369
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:164
itk::SmartPointer< Self >
itk::SimpleConstMemberCommand::Execute
void Execute(const Object *, const EventObject &) override
Definition: itkCommand.h:335
itk::CStyleCommand::DeleteDataFunctionPointer
void(*)(void *) DeleteDataFunctionPointer
Definition: itkCommand.h:370
itk::ReceptorMemberCommand::Execute
void Execute(Object *, const EventObject &event) override
Definition: itkCommand.h:193
itk::FunctionCommand::FunctionObjectType
std::function< void(const EventObject &)> FunctionObjectType
Definition: itkCommand.h:434
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:204
itk::MemberCommand::Execute
void Execute(Object *caller, const EventObject &event) override
Definition: itkCommand.h:124
itk::Command
class ITK_FORWARD_EXPORT Command
Definition: itkObject.h:42
itk::SimpleMemberCommand::Execute
void Execute(const Object *, const EventObject &) override
Definition: itkCommand.h:270
itk::SimpleConstMemberCommand::Execute
void Execute(Object *, const EventObject &) override
Definition: itkCommand.h:325
itk::SimpleMemberCommand::SetCallbackFunction
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:252
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:185
itk::SimpleConstMemberCommand::TMemberFunctionPointer
void(T::*)() const TMemberFunctionPointer
Definition: itkCommand.h:303
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
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:368
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:61
itk::EventObject
Abstraction of the Events used to communicating among filters and with GUIs.
Definition: itkEventObject.h:58
itk::CStyleCommand
A Command subclass that calls a pointer to a C function.
Definition: itkCommand.h:364
itk::ReceptorMemberCommand::TMemberFunctionPointer
void(T::*)(const EventObject &) TMemberFunctionPointer
Definition: itkCommand.h:170
itk::MemberCommand::TMemberFunctionPointer
void(T::*)(Object *, const EventObject &) TMemberFunctionPointer
Definition: itkCommand.h:92