]> git.tdb.fi Git - builder.git/blob - source/lib/task.h
b0f56f470399446d7339b1508300c2f0fd89e59f
[builder.git] / source / lib / 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 #include "libbuilder_api.h"
8
9 /**
10 Tasks are used to manage other programs and worker threads involved in the
11 build process.  They are run asynchronously by default, but a wait() method is
12 provided should it be necessary to wait for a task to finish.
13 */
14 class LIBBUILDER_API Task
15 {
16 public:
17         enum Status
18         {
19                 RUNNING,
20                 SUCCESS,
21                 ERROR
22         };
23
24         sigc::signal<void, bool> signal_finished;
25
26 protected:
27         std::vector<Msp::FS::Path> files;
28         bool unlink = false;
29
30         Task() = default;
31 public:
32         virtual ~Task() { }
33
34         /** Associate the task with a file. */
35         void add_file(const Msp::FS::Path &);
36
37         /** If set to true, the associated files are removed before the task is
38         started. */
39         void set_unlink(bool = true);
40
41         /** Returns the command being executed for this task.  Only makes sense if
42         an external command is involved. */
43         virtual std::string get_command() const = 0;
44
45         /// Starts the task.
46         virtual void start() = 0;
47
48 protected:
49         /// Ensures that the output directory exists and removes files if 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