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