Skip to content

Commit 9958b4b

Browse files
committed
refactor Task class: replace manual pipeline stage tracking with enum-based PipelineStage implementation and remove deprecated FuncName utility
1 parent 873c7dd commit 9958b4b

2 files changed

Lines changed: 22 additions & 63 deletions

File tree

modules/core/task/include/task.hpp

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ enum TypeOfTask : uint8_t {
3939
kUnknown
4040
};
4141

42+
enum class PipelineStage { None, Validation, PreProcessing, Run, Done };
43+
4244
/// @brief Indicates whether a task is enabled or disabled.
4345
enum StatusOfTask : uint8_t {
4446
/// Task is enabled and should be executed
@@ -104,39 +106,44 @@ template <typename InType, typename OutType>
104106
/// @tparam OutType Output data type.
105107
class Task {
106108
public:
107-
/// @brief Constructs a new Task object.
108-
explicit Task(StateOfTesting /*state_of_testing*/ = StateOfTesting::kFunc) { functions_order_.clear(); }
109-
110109
/// @brief Validates input data and task attributes before execution.
111110
/// @return True if validation is successful.
112111
virtual bool Validation() final {
113-
InternalOrderTest(ppc::util::FuncName());
112+
if (stage_ == PipelineStage::None) {
113+
stage_ = PipelineStage::Validation;
114+
}
114115
return ValidationImpl();
115116
}
116117

117118
/// @brief Performs preprocessing on the input data.
118119
/// @return True if preprocessing is successful.
119120
virtual bool PreProcessing() final {
120-
InternalOrderTest(ppc::util::FuncName());
121+
if (stage_ == PipelineStage::Validation) {
122+
stage_ = PipelineStage::PreProcessing;
123+
}
121124
if (state_of_testing_ == StateOfTesting::kFunc) {
122-
InternalTimeTest(ppc::util::FuncName());
125+
InternalTimeTest();
123126
}
124127
return PreProcessingImpl();
125128
}
126129

127130
/// @brief Executes the main logic of the task.
128131
/// @return True if execution is successful.
129132
virtual bool Run() final {
130-
InternalOrderTest(ppc::util::FuncName());
133+
if (stage_ == PipelineStage::PreProcessing || stage_ == PipelineStage::Run) {
134+
stage_ = PipelineStage::Run;
135+
}
131136
return RunImpl();
132137
}
133138

134139
/// @brief Performs postprocessing on the output data.
135140
/// @return True if postprocessing is successful.
136141
virtual bool PostProcessing() final {
137-
InternalOrderTest(ppc::util::FuncName());
142+
if (stage_ == PipelineStage::Run) {
143+
stage_ = PipelineStage::Done;
144+
}
138145
if (state_of_testing_ == StateOfTesting::kFunc) {
139-
InternalTimeTest(ppc::util::FuncName());
146+
InternalTimeTest();
140147
}
141148
return PostProcessingImpl();
142149
}
@@ -172,39 +179,25 @@ class Task {
172179
/// @brief Destructor. Verifies that the pipeline was executed in the correct order.
173180
/// @note Terminates the program if pipeline order is incorrect or incomplete.
174181
virtual ~Task() {
175-
if (!functions_order_.empty() || !was_worked_) {
176-
std::cerr << "ORDER OF FUNCTIONS IS NOT RIGHT! \n Expected - \"Validation\", \"PreProcessing\", \"Run\", "
177-
"\"PostProcessing\" \n";
182+
if (stage_ != PipelineStage::Done) {
183+
std::cerr << "ORDER OF FUNCTIONS IS NOT RIGHT" << std::endl;
178184
std::terminate();
179-
} else {
180-
functions_order_.clear();
181185
}
182186
#if _OPENMP >= 201811
183187
omp_pause_resource_all(omp_pause_soft);
184188
#endif
185189
}
186190

187191
protected:
188-
/// @brief Verifies the correct order of pipeline method calls.
189-
/// @param str Name of the method being called.
190-
virtual void InternalOrderTest(const std::string &str) final {
191-
functions_order_.push_back(str);
192-
if (str == "PostProcessing" && IsFullPipelineStage()) {
193-
functions_order_.clear();
194-
} else {
195-
was_worked_ = true;
196-
}
197-
}
198-
199192
/// @brief Measures execution time between preprocessing and postprocessing steps.
200193
/// @param str Name of the method being timed.
201194
/// @throws std::runtime_error If execution exceeds the allowed time limit.
202-
virtual void InternalTimeTest(const std::string &str) final {
203-
if (str == "PreProcessing") {
195+
virtual void InternalTimeTest() final {
196+
if (stage_ == PipelineStage::PreProcessing) {
204197
tmp_time_point_ = std::chrono::high_resolution_clock::now();
205198
}
206199

207-
if (str == "PostProcessing") {
200+
if (stage_ == PipelineStage::Done) {
208201
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now() -
209202
tmp_time_point_)
210203
.count();
@@ -244,26 +237,9 @@ class Task {
244237
StateOfTesting state_of_testing_ = kFunc;
245238
TypeOfTask type_of_task_ = kUnknown;
246239
StatusOfTask status_of_task_ = kEnabled;
247-
std::vector<std::string> functions_order_;
248-
std::vector<std::string> right_functions_order_ = {"Validation", "PreProcessing", "Run", "PostProcessing"};
249240
static constexpr double kMaxTestTime = 1.0;
250241
std::chrono::high_resolution_clock::time_point tmp_time_point_;
251-
bool was_worked_ = false;
252-
253-
bool IsFullPipelineStage() {
254-
if (functions_order_.size() < 4) {
255-
return false;
256-
}
257-
258-
auto it = std::adjacent_find(functions_order_.begin() + 2,
259-
functions_order_.begin() + static_cast<long>(functions_order_.size() - 2),
260-
std::not_equal_to<>());
261-
262-
return (functions_order_[0] == "Validation" && functions_order_[1] == "PreProcessing" &&
263-
functions_order_[2] == "Run" &&
264-
it == (functions_order_.begin() + static_cast<long>(functions_order_.size() - 2)) &&
265-
functions_order_[functions_order_.size() - 1] == "PostProcessing");
266-
}
242+
PipelineStage stage_ = PipelineStage::None;
267243
};
268244

269245
/// @brief Smart pointer alias for Task.

modules/core/util/include/util.hpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,6 @@ using NlohmannJsonTypeError = nlohmann::json::type_error;
2929

3030
namespace ppc::util {
3131

32-
/**
33-
* @brief Returns the unqualified name of the current function.
34-
*
35-
* @param loc Source location, defaults to the current function.
36-
* @return Function name without namespaces or parameters.
37-
*/
38-
inline std::string FuncName(const std::source_location& loc = std::source_location::current()) {
39-
std::string s{loc.function_name()};
40-
if (auto p = s.find('('); p != std::string::npos) {
41-
s.resize(p); // drop “(…)”
42-
}
43-
if (auto p = s.rfind("::"); p != std::string::npos) {
44-
s.erase(0, p + 2); // drop namespaces
45-
}
46-
return s;
47-
}
48-
4932
enum GTestParamIndex : uint8_t { kTaskGetter, kNameTest, kTestParams };
5033

5134
std::string GetAbsoluteTaskPath(const std::string& id_path, const std::string& relative_path);

0 commit comments

Comments
 (0)