]> git.tdb.fi Git - builder.git/blob - source/chainedtask.cpp
Replace basic for loops with range-based loops or algorithms
[builder.git] / source / chainedtask.cpp
1 #include <msp/strings/utils.h>
2 #include "chainedtask.h"
3
4 using namespace std;
5 using namespace Msp;
6
7 ChainedTask::ChainedTask(Task *t):
8         current(0),
9         final_status(RUNNING)
10 {
11         add_task(t);
12 }
13
14 ChainedTask::~ChainedTask()
15 {
16         for(Task *t: tasks)
17                 delete t;
18 }
19
20 void ChainedTask::add_task(Task *t)
21 {
22         tasks.push_back(t);
23 }
24
25 string ChainedTask::get_command() const
26 {
27         string cmd;
28         for(Task *t: tasks)
29                 append(cmd, "\n", t->get_command());
30         return cmd;
31 }
32
33 void ChainedTask::start()
34 {
35         prepare();
36
37         current = 0;
38         tasks[current]->start();
39 }
40
41 Task::Status ChainedTask::check()
42 {
43         while(current<tasks.size() && !process(tasks[current]->check())) ;
44
45         return final_status;
46 }
47
48 Task::Status ChainedTask::wait()
49 {
50         while(current<tasks.size() && !process(tasks[current]->wait())) ;
51
52         return final_status;
53 }
54
55 bool ChainedTask::process(Status sub_status)
56 {
57         if(sub_status==SUCCESS && current+1<tasks.size())
58         {
59                 // The task succeeded and there's more to run
60                 ++current;
61                 tasks[current]->start();
62                 return true;
63         }
64
65         if(sub_status!=RUNNING)
66         {
67                 // The task is not running anymore and either failed or was the last one
68                 current = tasks.size();
69                 final_status = sub_status;
70         }
71
72         return false;
73 }