Skip to content

Commit ff28fc4

Browse files
committed
refactor Task module and tests: enforce explicit constructors, replace PipelineStage enums with k-prefixed constants, and optimize type-to-string mappings for readability and maintainability
1 parent 4c5032b commit ff28fc4

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

modules/core/task/include/task.hpp

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,25 @@ enum TypeOfTask : uint8_t {
3939
kUnknown
4040
};
4141

42-
inline std::string TypeOfTaskToString(TypeOfTask type) {
43-
constexpr std::pair<TypeOfTask, std::string> kTypeOfTaskStrings[] = {
44-
{TypeOfTask::kALL, "all"}, {TypeOfTask::kMPI, "mpi"}, {TypeOfTask::kOMP, "omp"},
45-
{TypeOfTask::kSEQ, "seq"}, {TypeOfTask::kSTL, "stl"}, {TypeOfTask::kTBB, "tbb"},
46-
};
42+
using TaskMapping = std::pair<TypeOfTask, std::string>;
43+
using TaskMappingArray = std::array<TaskMapping, 6>;
44+
45+
constexpr TaskMappingArray kTaskTypeMappings = {{{TypeOfTask::kALL, "all"},
46+
{TypeOfTask::kMPI, "mpi"},
47+
{TypeOfTask::kOMP, "omp"},
48+
{TypeOfTask::kSEQ, "seq"},
49+
{TypeOfTask::kSTL, "stl"},
50+
{TypeOfTask::kTBB, "tbb"}}};
4751

48-
for (const auto &[key, value] : kTypeOfTaskStrings) {
49-
if (key == type) return value;
52+
inline std::string TypeOfTaskToString(TypeOfTask type) {
53+
for (const auto &[key, value] : kTaskTypeMappings) {
54+
if (key == type) {
55+
return value;
56+
}
5057
}
5158
return "unknown";
5259
}
5360

54-
enum class PipelineStage { None, Validation, PreProcessing, Run, Done };
55-
5661
/// @brief Indicates whether a task is enabled or disabled.
5762
enum StatusOfTask : uint8_t {
5863
/// Task is enabled and should be executed
@@ -104,17 +109,17 @@ class Task {
104109
/// @brief Validates input data and task attributes before execution.
105110
/// @return True if validation is successful.
106111
virtual bool Validation() final {
107-
if (stage_ == PipelineStage::None) {
108-
stage_ = PipelineStage::Validation;
112+
if (stage_ == PipelineStage::kNone) {
113+
stage_ = PipelineStage::kValidation;
109114
}
110115
return ValidationImpl();
111116
}
112117

113118
/// @brief Performs preprocessing on the input data.
114119
/// @return True if preprocessing is successful.
115120
virtual bool PreProcessing() final {
116-
if (stage_ == PipelineStage::Validation) {
117-
stage_ = PipelineStage::PreProcessing;
121+
if (stage_ == PipelineStage::kValidation) {
122+
stage_ = PipelineStage::kPreProcessing;
118123
}
119124
if (state_of_testing_ == StateOfTesting::kFunc) {
120125
InternalTimeTest();
@@ -125,17 +130,17 @@ class Task {
125130
/// @brief Executes the main logic of the task.
126131
/// @return True if execution is successful.
127132
virtual bool Run() final {
128-
if (stage_ == PipelineStage::PreProcessing || stage_ == PipelineStage::Run) {
129-
stage_ = PipelineStage::Run;
133+
if (stage_ == PipelineStage::kPreProcessing || stage_ == PipelineStage::kRun) {
134+
stage_ = PipelineStage::kRun;
130135
}
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-
if (stage_ == PipelineStage::Run) {
138-
stage_ = PipelineStage::Done;
142+
if (stage_ == PipelineStage::kRun) {
143+
stage_ = PipelineStage::kDone;
139144
}
140145
if (state_of_testing_ == StateOfTesting::kFunc) {
141146
InternalTimeTest();
@@ -172,10 +177,10 @@ class Task {
172177
OutType &GetOutput() { return output_; }
173178

174179
/// @brief Destructor. Verifies that the pipeline was executed in the correct order.
175-
/// @note Terminates the program if pipeline order is incorrect or incomplete.
180+
/// @note Terminates the program if the pipeline order is incorrect or incomplete.
176181
virtual ~Task() {
177-
if (stage_ != PipelineStage::Done) {
178-
std::cerr << "ORDER OF FUNCTIONS IS NOT RIGHT" << std::endl;
182+
if (stage_ != PipelineStage::kDone) {
183+
std::cerr << "ORDER OF FUNCTIONS IS NOT RIGHT" << '\n';
179184
std::terminate();
180185
}
181186
#if _OPENMP >= 201811
@@ -185,14 +190,13 @@ class Task {
185190

186191
protected:
187192
/// @brief Measures execution time between preprocessing and postprocessing steps.
188-
/// @param str Name of the method being timed.
189193
/// @throws std::runtime_error If execution exceeds the allowed time limit.
190194
virtual void InternalTimeTest() final {
191-
if (stage_ == PipelineStage::PreProcessing) {
195+
if (stage_ == PipelineStage::kPreProcessing) {
192196
tmp_time_point_ = std::chrono::high_resolution_clock::now();
193197
}
194198

195-
if (stage_ == PipelineStage::Done) {
199+
if (stage_ == PipelineStage::kDone) {
196200
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now() -
197201
tmp_time_point_)
198202
.count();
@@ -234,7 +238,7 @@ class Task {
234238
StatusOfTask status_of_task_ = kEnabled;
235239
static constexpr double kMaxTestTime = 1.0;
236240
std::chrono::high_resolution_clock::time_point tmp_time_point_;
237-
PipelineStage stage_ = PipelineStage::None;
241+
enum class PipelineStage : uint8_t { kNone, kValidation, kPreProcessing, kRun, kDone } stage_ = PipelineStage::kNone;
238242
};
239243

240244
/// @brief Smart pointer alias for Task.
@@ -247,7 +251,7 @@ using TaskPtr = std::shared_ptr<Task<InType, OutType>>;
247251
/// @tparam TaskType Type of the task to create.
248252
/// @tparam InType Type of the input.
249253
/// @param in Input to pass to the task constructor.
250-
/// @return Shared pointer to the newly created task.
254+
/// @return Shared a pointer to the newly created task.
251255
template <typename TaskType, typename InType>
252256
std::shared_ptr<TaskType> TaskGetter(InType in) {
253257
return std::make_shared<TaskType>(in);

modules/core/task/tests/task_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ TEST(TaskTest, TaskDestructor_ThrowsIfStageIncomplete) {
153153
auto destroy_func = [] {
154154
std::vector<int32_t> in(20, 1);
155155
struct LocalTask : ppc::core::Task<std::vector<int32_t>, int32_t> {
156-
LocalTask(const std::vector<int32_t>& in) { this->GetInput() = in; }
156+
explicit LocalTask(const std::vector<int32_t>& in) { this->GetInput() = in; }
157157
bool ValidationImpl() override { return true; }
158158
bool PreProcessingImpl() override { return true; }
159159
bool RunImpl() override { return true; }
@@ -166,7 +166,7 @@ TEST(TaskTest, TaskDestructor_ThrowsIfStageIncomplete) {
166166

167167
TEST(TaskTest, InternalTimeTest_ThrowsIfTimeoutExceeded) {
168168
struct SlowTask : ppc::core::Task<std::vector<int32_t>, int32_t> {
169-
SlowTask(const std::vector<int32_t>& in) { this->GetInput() = in; }
169+
explicit SlowTask(const std::vector<int32_t>& in) { this->GetInput() = in; }
170170
bool ValidationImpl() override { return true; }
171171
bool PreProcessingImpl() override {
172172
std::this_thread::sleep_for(std::chrono::seconds(2));

0 commit comments

Comments
 (0)