Skip to content

Commit 5093418

Browse files
committed
refactor Task module: replace switch-case in GetStringTaskType with TypeOfTaskToString utility for improved readability and maintainability
1 parent c6d74f8 commit 5093418

1 file changed

Lines changed: 22 additions & 20 deletions

File tree

modules/core/task/include/task.hpp

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

42+
constexpr std::pair<TypeOfTask, std::string_view> kTypeOfTaskStrings[] = {
43+
{TypeOfTask::kALL, "all"},
44+
{TypeOfTask::kMPI, "mpi"},
45+
{TypeOfTask::kOMP, "omp"},
46+
{TypeOfTask::kSEQ, "seq"},
47+
{TypeOfTask::kSTL, "stl"},
48+
{TypeOfTask::kTBB, "tbb"},
49+
};
50+
51+
inline std::string_view TypeOfTaskToString(TypeOfTask type) {
52+
for (const auto& [key, value] : kTypeOfTaskStrings) {
53+
if (key == type) return value;
54+
}
55+
return "unknown";
56+
}
57+
4258
enum class PipelineStage { None, Validation, PreProcessing, Run, Done };
4359

4460
/// @brief Indicates whether a task is enabled or disabled.
@@ -64,7 +80,7 @@ inline std::string GetStringTaskStatus(StatusOfTask status_of_task) {
6480
/// @param settings_file_path Path to the JSON file containing task type strings.
6581
/// @return Formatted string combining the task type and its corresponding value from the file.
6682
/// @throws std::runtime_error If the file cannot be opened.
67-
inline std::string GetStringTaskType(TypeOfTask type_of_task, const std::string &settings_file_path) {
83+
inline std::string GetStringTaskType(TypeOfTask type_of_task, const std::string& settings_file_path) {
6884
std::ifstream file(settings_file_path);
6985
if (!file.is_open()) {
7086
throw std::runtime_error("Failed to open " + settings_file_path);
@@ -73,26 +89,12 @@ inline std::string GetStringTaskType(TypeOfTask type_of_task, const std::string
7389
auto list_settings = ppc::util::InitJSONPtr();
7490
file >> *list_settings;
7591

76-
auto to_type_str = [&](const std::string &type) -> std::string {
77-
return type + "_" + std::string((*list_settings)["tasks"][type]);
78-
};
79-
80-
switch (type_of_task) {
81-
case TypeOfTask::kALL:
82-
return to_type_str("all");
83-
case TypeOfTask::kSTL:
84-
return to_type_str("stl");
85-
case TypeOfTask::kOMP:
86-
return to_type_str("omp");
87-
case TypeOfTask::kMPI:
88-
return to_type_str("mpi");
89-
case TypeOfTask::kTBB:
90-
return to_type_str("tbb");
91-
case TypeOfTask::kSEQ:
92-
return to_type_str("seq");
93-
default:
94-
return "unknown";
92+
std::string type_str = std::string(TypeOfTaskToString(type_of_task));
93+
if (type_str == "unknown") {
94+
return "unknown";
9595
}
96+
97+
return type_str + "_" + std::string((*list_settings)["tasks"][type_str]);
9698
}
9799

98100
enum StateOfTesting : uint8_t { kFunc, kPerf };

0 commit comments

Comments
 (0)