TaskManager interface template
This commit is contained in:
@@ -134,18 +134,20 @@ class TaskTimer {
|
|||||||
std::chrono::steady_clock::time_point start_time_;
|
std::chrono::steady_clock::time_point start_time_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename TFunc>
|
||||||
class TaskManager {
|
class TaskManager {
|
||||||
public:
|
public:
|
||||||
|
TaskManager() = default;
|
||||||
|
~TaskManager() = default;
|
||||||
using TaskFunction = std::function<void(int)>;
|
using TaskFunction = std::function<void(int)>;
|
||||||
|
|
||||||
TaskManager(int totalTasks, int timeoutSeconds, const TaskFunction &taskFunc)
|
TaskManager(int totalTasks, int timeoutSeconds)
|
||||||
: total_tasks_(totalTasks),
|
: total_tasks_(totalTasks),
|
||||||
timeout_seconds_(timeoutSeconds),
|
timeout_seconds_(timeoutSeconds),
|
||||||
task_function_(taskFunc),
|
|
||||||
task_index_(0),
|
task_index_(0),
|
||||||
task_complete_(false) {}
|
task_complete_(false) {}
|
||||||
|
|
||||||
void ExecuteTasks() {
|
void ExecuteTasks(const TFunc &taskFunc) {
|
||||||
if (task_complete_) {
|
if (task_complete_) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -153,7 +155,7 @@ class TaskManager {
|
|||||||
StartTimer();
|
StartTimer();
|
||||||
|
|
||||||
for (; task_index_ < total_tasks_; ++task_index_) {
|
for (; task_index_ < total_tasks_; ++task_index_) {
|
||||||
task_function_(task_index_);
|
taskFunc(task_index_);
|
||||||
|
|
||||||
if (ShouldFinishTask()) {
|
if (ShouldFinishTask()) {
|
||||||
break;
|
break;
|
||||||
@@ -170,7 +172,6 @@ class TaskManager {
|
|||||||
private:
|
private:
|
||||||
int total_tasks_;
|
int total_tasks_;
|
||||||
int timeout_seconds_;
|
int timeout_seconds_;
|
||||||
TaskFunction task_function_;
|
|
||||||
int task_index_;
|
int task_index_;
|
||||||
bool task_complete_;
|
bool task_complete_;
|
||||||
std::chrono::steady_clock::time_point start_time_;
|
std::chrono::steady_clock::time_point start_time_;
|
||||||
|
|||||||
Reference in New Issue
Block a user