]> git.tdb.fi Git - builder.git/blob - source/chainedtask.cpp
Add utility class for executing chains of tasks
[builder.git] / source / chainedtask.cpp
1 #include "chainedtask.h"
2
3 using namespace std;
4
5 ChainedTask::ChainedTask(Task *t):
6         current(0),
7         final_status(RUNNING)
8 {
9         add_task(t);
10 }
11
12 ChainedTask::~ChainedTask()
13 {
14         for(vector<Task *>::iterator i=tasks.begin(); i!=tasks.end(); ++i)
15                 delete *i;
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(vector<Task *>::const_iterator i=tasks.begin(); i!=tasks.end(); ++i)
27         {
28                 if(i!=tasks.begin())
29                         cmd += '\n';
30                 cmd += (*i)->get_command();
31         }
32         return cmd;
33 }
34
35 void ChainedTask::start()
36 {
37         prepare();
38
39         current = 0;
40         tasks[current]->start();
41 }
42
43 Task::Status ChainedTask::check()
44 {
45         while(current<tasks.size() && !process(tasks[current]->check())) ;
46
47         return final_status;
48 }
49
50 Task::Status ChainedTask::wait()
51 {
52         while(current<tasks.size() && !process(tasks[current]->wait())) ;
53
54         return final_status;
55 }
56
57 bool ChainedTask::process(Status sub_status)
58 {
59         if(sub_status==SUCCESS && current+1<tasks.size())
60         {
61                 // The task succeeded and there's more to run
62                 ++current;
63                 tasks[current]->start();
64                 return true;
65         }
66
67         if(sub_status!=RUNNING)
68         {
69                 // The task is not running anymore and either failed or was the last one
70                 current = tasks.size();
71                 final_status = sub_status;
72         }
73
74         return false;
75 }