ITK  5.4.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
25namespace itk
26{
44// The superclass that all commands should be subclasses of
45class ITKCommon_EXPORT Command : public Object
46{
47public:
48 ITK_DISALLOW_COPY_AND_MOVE(Command);
49
51 using Self = Command;
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
69protected:
71 ~Command() override;
72};
73
74// some implementations for several callback types
75
85template <typename T>
86class ITK_TEMPLATE_EXPORT MemberCommand : public Command
87{
88public:
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 }
144protected:
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
163template <typename T>
164class ITK_TEMPLATE_EXPORT ReceptorMemberCommand : public Command
165{
166public:
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 }
213protected:
214 T * m_This{ nullptr };
215 TMemberFunctionPointer m_MemberFunction{ nullptr };
216
218
219 ~ReceptorMemberCommand() override = default;
220};
221
231template <typename T>
232class ITK_TEMPLATE_EXPORT SimpleMemberCommand : public Command
233{
234public:
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
278protected:
279 T * m_This{ nullptr };
280 TMemberFunctionPointer m_MemberFunction{ nullptr };
281
283
284 ~SimpleMemberCommand() override = default;
285};
286
296template <typename T>
297class ITK_TEMPLATE_EXPORT SimpleConstMemberCommand : public Command
298{
299public:
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
343protected:
344 const T * m_This{ nullptr };
345 TMemberFunctionPointer m_MemberFunction{ nullptr };
346
348
349 ~SimpleConstMemberCommand() override = default;
350};
351
364class ITKCommon_EXPORT CStyleCommand : public Command
365{
366public:
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
391 void
396 void
398
400 void
401 Execute(Object * caller, const EventObject & event) override;
402
404 void
405 Execute(const Object * caller, const EventObject & event) override;
406
407protected:
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
427class ITKCommon_EXPORT FunctionCommand : public Command
428{
429public:
433
434 using FunctionObjectType = std::function<void(const EventObject &)>;
435
437 itkOverrideGetNameOfClassMacro(FunctionCommand);
438
440 itkNewMacro(Self);
441
443 void
445
447 void
448 Execute(Object *, const EventObject & event) override;
449
451 void
452 Execute(const Object *, const EventObject & event) override;
453
454protected:
457
458
459 FunctionObjectType m_FunctionObject{};
460};
461
462} // end namespace itk
463
464#endif
A Command subclass that calls a pointer to a C function.
Definition: itkCommand.h:365
void Execute(Object *caller, const EventObject &event) override
void(*)(void *) DeleteDataFunctionPointer
Definition: itkCommand.h:370
~CStyleCommand() override
void Execute(const Object *caller, const EventObject &event) override
void(*)(Object *, const EventObject &, void *) FunctionPointer
Definition: itkCommand.h:368
void SetCallback(FunctionPointer f)
void(*)(const Object *, const EventObject &, void *) ConstFunctionPointer
Definition: itkCommand.h:369
void SetConstCallback(ConstFunctionPointer f)
void SetClientData(void *cd)
void SetClientDataDeleteCallback(DeleteDataFunctionPointer f)
Superclass for callback/observer methods.
Definition: itkCommand.h:46
~Command() override
virtual void Execute(Object *caller, const EventObject &event)=0
virtual void Execute(const Object *caller, const EventObject &event)=0
Abstraction of the Events used to communicating among filters and with GUIs.
A Command subclass that calls a std::function object.
Definition: itkCommand.h:428
std::function< void(const EventObject &)> FunctionObjectType
Definition: itkCommand.h:434
~FunctionCommand() override
void Execute(Object *, const EventObject &event) override
void SetCallback(FunctionObjectType f)
void Execute(const Object *, const EventObject &event) override
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:87
MemberCommand()=default
void(T::*)(Object *, const EventObject &) TMemberFunctionPointer
Definition: itkCommand.h:92
void Execute(const Object *caller, const EventObject &event) override
Definition: itkCommand.h:135
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:109
void SetCallbackFunction(T *object, TConstMemberFunctionPointer memberFunction)
Definition: itkCommand.h:116
void(T::*)(const Object *, const EventObject &) TConstMemberFunctionPointer
Definition: itkCommand.h:93
void Execute(Object *caller, const EventObject &event) override
Definition: itkCommand.h:124
~MemberCommand() override=default
Base class for most ITK classes.
Definition: itkObject.h:62
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:165
~ReceptorMemberCommand() override=default
void(T::*)(const EventObject &) TMemberFunctionPointer
Definition: itkCommand.h:170
void Execute(Object *, const EventObject &event) override
Definition: itkCommand.h:193
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:185
void Execute(const Object *, const EventObject &event) override
Definition: itkCommand.h:204
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:298
~SimpleConstMemberCommand() override=default
void Execute(const Object *, const EventObject &) override
Definition: itkCommand.h:335
void Execute(Object *, const EventObject &) override
Definition: itkCommand.h:325
void(T::*)() const TMemberFunctionPointer
Definition: itkCommand.h:303
void SetCallbackFunction(const T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:317
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:233
~SimpleMemberCommand() override=default
void Execute(const Object *, const EventObject &) override
Definition: itkCommand.h:270
void SetCallbackFunction(T *object, TMemberFunctionPointer memberFunction)
Definition: itkCommand.h:252
void(T::*)() TMemberFunctionPointer
Definition: itkCommand.h:238
void Execute(Object *, const EventObject &) override
Definition: itkCommand.h:260
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
class ITK_FORWARD_EXPORT Command
Definition: itkObject.h:42