]> git.tdb.fi Git - builder.git/blob - source/task.h
Handle directory creation and unlinking in Task
[builder.git] / source / task.h
1 #ifndef TASK_H_
2 #define TASK_H_
3
4 #include <string>
5 #include <sigc++/signal.h>
6 #include <msp/fs/path.h>
7
8 /**
9 Tasks are used to manage other programs and worker threads involved in the
10 build process.  They are run asynchronously by default, but a wait() method is
11 provided should it be necessary to wait for a task to finish.
12 */
13 class Task
14 {
15 public:
16         enum Status
17         {
18                 RUNNING,
19                 SUCCESS,
20                 ERROR
21         };
22
23         sigc::signal<void, bool> signal_finished;
24
25         Msp::FS::Path file;
26         bool unlink;
27
28 protected:
29         Task();
30 public:
31         virtual ~Task() { }
32
33         /** Associate the task with a file. */
34         void set_file(const Msp::FS::Path &);
35
36         /** If set to true, the associated file is removed before the task is
37         started. */
38         void set_unlink(bool = true);
39
40         /** Returns the command being executed for this task.  Only makes sense if
41         an external command is involved. */
42         virtual std::string get_command() const = 0;
43
44         /// Starts the task.
45         virtual void start() = 0;
46
47 protected:
48         /** Ensures that the output directory exists and removes the file if
49         necessary. */
50         void prepare();
51
52 public:
53         /// Checks the status of the task and immediately returns.
54         virtual Status check() = 0;
55
56         /// Waits for the task to finish and returns its final status.
57         virtual Status wait() = 0;
58 };
59
60 #endif