ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
rtkProgressCommands.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright RTK 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 * 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
19#ifndef rtkProgressCommands_h
20#define rtkProgressCommands_h
21
22#include <itkCommand.h>
23
24namespace rtk
25{
26
36template <typename TCaller>
37class ITK_TEMPLATE_EXPORT ProgressCommand : public itk::Command
38{
39public:
44
45 void
46 Execute(itk::Object * caller, const itk::EventObject & event) override
47 {
48 Execute((const itk::Object *)caller, event);
49 }
50
51 void
52 Execute(const itk::Object * caller, const itk::EventObject & event) override
53 {
54 const auto * cCaller = dynamic_cast<const TCaller *>(caller);
55 if (cCaller)
56 {
57 if (itk::EndEvent().CheckEvent(&event))
58 {
59 End(cCaller);
60 return;
61 }
62 if (itk::ProgressEvent().CheckEvent(&event))
63 {
64 Run(cCaller);
65 }
66 } // TODO fail when cast fails
67 }
68
69
70protected:
72 virtual void
73 Run(const TCaller * caller) = 0;
74
76 virtual void
77 End(const TCaller * itkNotUsed(caller))
78 { /* Default implementation: do nothing */
79 }
80};
81
90template <typename TCaller>
91class ITK_TEMPLATE_EXPORT PercentageProgressCommand : public ProgressCommand<TCaller>
92{
93public:
98 itkNewMacro(Self);
99
100 int percentage = -1;
101
102protected:
103 void
104 Run(const TCaller * caller) override
105 {
106 int newPercentage = (int)(caller->GetProgress() * 100.);
107 if (newPercentage > percentage)
108 {
109 // TODO allow string personnalization?
110 std::cout << "\r" << caller->GetNameOfClass() << " " << newPercentage << "% completed." << std::flush;
111 percentage = newPercentage;
112 }
113 }
114
115 void
116 End(const TCaller * itkNotUsed(caller)) override
117 {
118 std::cout << std::endl; // new line when execution ends
119 }
120};
121
122} // end namespace rtk
123
124#endif
Superclass for callback/observer methods.
Definition itkCommand.h:46
Abstraction of the Events used to communicating among filters and with GUIs.
Base class for most ITK classes.
Definition itkObject.h:62
Implements transparent reference counting.
Outputs every time a filter progresses by at least one percent.
PercentageProgressCommand Self
void End(const TCaller *caller) override
itk::SmartPointer< Self > Pointer
ProgressCommand< TCaller > Superclass
void Run(const TCaller *caller) override
Abstract superclass to all progress callbacks. Derived classes must implement the Run() method.
virtual void Run(const TCaller *caller)=0
itk::SmartPointer< Self > Pointer
void Execute(const itk::Object *caller, const itk::EventObject &event) override
virtual void End(const TCaller *caller)
void Execute(itk::Object *caller, const itk::EventObject &event) override