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