ITK  5.1.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 
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
60  Execute(Object * caller, const EventObject & event) = 0;
61 
65  virtual void
66  Execute(const Object * caller, const EventObject & event) = 0;
67 
68 protected:
69  Command();
70  ~Command() override;
71 };
72 
73 // some implementations for several callback types
74 
84 template <typename T>
85 class ITK_TEMPLATE_EXPORT MemberCommand : public Command
86 {
87 public:
88  ITK_DISALLOW_COPY_AND_ASSIGN(MemberCommand);
89 
91  using TMemberFunctionPointer = void (T::*)(Object *, const EventObject &);
92  using TConstMemberFunctionPointer = void (T::*)(const Object *, const EventObject &);
94 
98 
100  itkNewMacro(Self);
101 
103  itkTypeMacro(MemberCommand, Command);
104 
107  void
108  SetCallbackFunction(T * object, TMemberFunctionPointer memberFunction)
109  {
110  m_This = object;
111  m_MemberFunction = memberFunction;
112  }
113 
114  void
116  {
117  m_This = object;
118  m_ConstMemberFunction = memberFunction;
119  }
120 
122  void
123  Execute(Object * caller, const EventObject & event) override
124  {
125  if (m_MemberFunction)
126  {
127  ((*m_This).*(m_MemberFunction))(caller, event);
128  }
129  }
131 
133  void
134  Execute(const Object * caller, const EventObject & event) override
135  {
136  if (m_ConstMemberFunction)
137  {
138  ((*m_This).*(m_ConstMemberFunction))(caller, event);
139  }
140  }
142 
143 protected:
144  T * m_This;
147 
149  : m_This(nullptr)
150  , m_MemberFunction(nullptr)
151  , m_ConstMemberFunction(nullptr)
152  {}
153 
154  ~MemberCommand() override = default;
155 };
156 
166 template <typename T>
167 class ITK_TEMPLATE_EXPORT ReceptorMemberCommand : public Command
168 {
169 public:
170  ITK_DISALLOW_COPY_AND_ASSIGN(ReceptorMemberCommand);
171 
173  using TMemberFunctionPointer = void (T::*)(const EventObject &);
174 
178 
180  itkNewMacro(Self);
181 
183  itkTypeMacro(ReceptorMemberCommand, Command);
184 
187  void
188  SetCallbackFunction(T * object, TMemberFunctionPointer memberFunction)
189  {
190  m_This = object;
191  m_MemberFunction = memberFunction;
192  }
193 
195  void
196  Execute(Object *, const EventObject & event) override
197  {
198  if (m_MemberFunction)
199  {
200  ((*m_This).*(m_MemberFunction))(event);
201  }
202  }
204 
206  void
207  Execute(const Object *, const EventObject & event) override
208  {
209  if (m_MemberFunction)
210  {
211  ((*m_This).*(m_MemberFunction))(event);
212  }
213  }
215 
216 protected:
217  T * m_This;
219 
221  : m_This(nullptr)
222  , m_MemberFunction(nullptr)
223  {}
224 
225  ~ReceptorMemberCommand() override = default;
226 };
227 
237 template <typename T>
238 class ITK_TEMPLATE_EXPORT SimpleMemberCommand : public Command
239 {
240 public:
241  ITK_DISALLOW_COPY_AND_ASSIGN(SimpleMemberCommand);
242 
244  using TMemberFunctionPointer = void (T::*)();
245 
249 
251  itkTypeMacro(SimpleMemberCommand, Command);
252 
254  itkNewMacro(Self);
255 
257  void
258  SetCallbackFunction(T * object, TMemberFunctionPointer memberFunction)
259  {
260  m_This = object;
261  m_MemberFunction = memberFunction;
262  }
263 
265  void
266  Execute(Object *, const EventObject &) override
267  {
268  if (m_MemberFunction)
269  {
270  ((*m_This).*(m_MemberFunction))();
271  }
272  }
274 
275  void
276  Execute(const Object *, const EventObject &) override
277  {
278  if (m_MemberFunction)
279  {
280  ((*m_This).*(m_MemberFunction))();
281  }
282  }
283 
284 protected:
285  T * m_This;
287 
289  : m_This(nullptr)
290  , m_MemberFunction(nullptr)
291  {}
292 
293  ~SimpleMemberCommand() override = default;
294 };
295 
305 template <typename T>
306 class ITK_TEMPLATE_EXPORT SimpleConstMemberCommand : public Command
307 {
308 public:
309  ITK_DISALLOW_COPY_AND_ASSIGN(SimpleConstMemberCommand);
310 
312  using TMemberFunctionPointer = void (T::*)() const;
313 
317 
319  itkTypeMacro(SimpleConstMemberCommand, Command);
320 
322  itkNewMacro(Self);
323 
325  void
326  SetCallbackFunction(const T * object, TMemberFunctionPointer memberFunction)
327  {
328  m_This = object;
329  m_MemberFunction = memberFunction;
330  }
331 
333  void
334  Execute(Object *, const EventObject &) override
335  {
336  if (m_MemberFunction)
337  {
338  ((*m_This).*(m_MemberFunction))();
339  }
340  }
342 
343  void
344  Execute(const Object *, const EventObject &) override
345  {
346  if (m_MemberFunction)
347  {
348  ((*m_This).*(m_MemberFunction))();
349  }
350  }
351 
352 protected:
353  const T * m_This;
355 
357  : m_This(nullptr)
358  , m_MemberFunction(nullptr)
359  {}
360 
361  ~SimpleConstMemberCommand() override = default;
362 };
363 
376 class ITKCommon_EXPORT CStyleCommand : public Command
377 {
378 public:
380  using FunctionPointer = void (*)(Object *, const EventObject &, void *);
381  using ConstFunctionPointer = void (*)(const Object *, const EventObject &, void *);
382  using DeleteDataFunctionPointer = void (*)(void *);
384 
388 
390  itkTypeMacro(CStyleCommand, Command);
391 
393  itkNewMacro(Self);
394 
397  void
398  SetClientData(void * cd);
399 
401  void
402  SetCallback(FunctionPointer f);
403  void
404  SetConstCallback(ConstFunctionPointer f);
406 
408  void
409  SetClientDataDeleteCallback(DeleteDataFunctionPointer f);
410 
412  void
413  Execute(Object * caller, const EventObject & event) override;
414 
416  void
417  Execute(const Object * caller, const EventObject & event) override;
418 
419 protected:
420  CStyleCommand();
421  ~CStyleCommand() override;
422 
423  void * m_ClientData{ nullptr };
424  FunctionPointer m_Callback{ nullptr };
425  ConstFunctionPointer m_ConstCallback{ nullptr };
426  DeleteDataFunctionPointer m_ClientDataDeleteCallback{ nullptr };
427 };
428 } // end namespace itk
429 
430 #endif
itk::ReceptorMemberCommand::m_This
T * m_This
Definition: itkCommand.h:217
itkObjectFactory.h
itk::SimpleMemberCommand::TMemberFunctionPointer
void(T::*)() TMemberFunctionPointer
Definition: itkCommand.h:244
itk::MemberCommand::SetCallbackFunction
void SetCallbackFunction(T *object, TConstMemberFunctionPointer memberFunction)
Definition: itkCommand.h:115
itk::SimpleConstMemberCommand
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:306
itk::SimpleConstMemberCommand::SetCallbackFunction
void SetCallbackFunction(const T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:326
itk::SimpleMemberCommand::m_MemberFunction
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:286
itk::MemberCommand::TConstMemberFunctionPointer
void(T::*)(const Object *, const EventObject &) TConstMemberFunctionPointer
Definition: itkCommand.h:92
itk::SimpleMemberCommand::Execute
void Execute(Object *, const EventObject &) override
Definition: itkCommand.h:266
itk::SimpleMemberCommand
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:238
itk::MemberCommand::m_This
T * m_This
Definition: itkCommand.h:144
itk::CStyleCommand::ConstFunctionPointer
void(*)(const Object *, const EventObject &, void *) ConstFunctionPointer
Definition: itkCommand.h:381
itk::MemberCommand::Execute
void Execute(const Object *caller, const EventObject &event) override
Definition: itkCommand.h:134
itk::ReceptorMemberCommand
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:167
itk::ReceptorMemberCommand::ReceptorMemberCommand
ReceptorMemberCommand()
Definition: itkCommand.h:220
itk::SmartPointer< Self >
itk::SimpleConstMemberCommand::Execute
void Execute(const Object *, const EventObject &) override
Definition: itkCommand.h:344
itk::CStyleCommand::DeleteDataFunctionPointer
void(*)(void *) DeleteDataFunctionPointer
Definition: itkCommand.h:382
itk::SimpleConstMemberCommand::m_This
const T * m_This
Definition: itkCommand.h:353
itk::ReceptorMemberCommand::Execute
void Execute(Object *, const EventObject &event) override
Definition: itkCommand.h:196
itk::SimpleMemberCommand::m_This
T * m_This
Definition: itkCommand.h:285
itk::Command
Superclass for callback/observer methods.
Definition: itkCommand.h:44
itk::ReceptorMemberCommand::Execute
void Execute(const Object *, const EventObject &event) override
Definition: itkCommand.h:207
itk::MemberCommand::Execute
void Execute(Object *caller, const EventObject &event) override
Definition: itkCommand.h:123
itk::SimpleConstMemberCommand::SimpleConstMemberCommand
SimpleConstMemberCommand()
Definition: itkCommand.h:356
itk::SimpleMemberCommand::SimpleMemberCommand
SimpleMemberCommand()
Definition: itkCommand.h:288
itk::Command
class ITK_FORWARD_EXPORT Command
Definition: itkObject.h:41
itk::SimpleMemberCommand::Execute
void Execute(const Object *, const EventObject &) override
Definition: itkCommand.h:276
itk::SimpleConstMemberCommand::m_MemberFunction
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:354
itk::MemberCommand::m_ConstMemberFunction
TConstMemberFunctionPointer m_ConstMemberFunction
Definition: itkCommand.h:146
itk::SimpleConstMemberCommand::Execute
void Execute(Object *, const EventObject &) override
Definition: itkCommand.h:334
itk::SimpleMemberCommand::SetCallbackFunction
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:258
itkObject.h
itk::MemberCommand
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:85
itk::ReceptorMemberCommand::SetCallbackFunction
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:188
itk::ReceptorMemberCommand::m_MemberFunction
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:218
itk::SimpleConstMemberCommand::TMemberFunctionPointer
void(T::*)() const TMemberFunctionPointer
Definition: itkCommand.h:312
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
itk::MemberCommand::SetCallbackFunction
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:108
itk::CStyleCommand::FunctionPointer
void(*)(Object *, const EventObject &, void *) FunctionPointer
Definition: itkCommand.h:380
itk::MemberCommand::m_MemberFunction
TMemberFunctionPointer m_MemberFunction
Definition: itkCommand.h:145
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:60
itk::MemberCommand::MemberCommand
MemberCommand()
Definition: itkCommand.h:148
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:376
itk::ReceptorMemberCommand::TMemberFunctionPointer
void(T::*)(const EventObject &) TMemberFunctionPointer
Definition: itkCommand.h:173
itk::MemberCommand::TMemberFunctionPointer
void(T::*)(Object *, const EventObject &) TMemberFunctionPointer
Definition: itkCommand.h:91