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