]> git.tdb.fi Git - builder.git/blob - source/lib/task.h
Report timings of the build
[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 <msp/time/timedelta.h>
8 #include <msp/time/timestamp.h>
9 #include "libbuilder_api.h"
10
11 class FileTarget;
12
13 /**
14 Tasks are used to manage other programs and worker threads involved in the
15 build process.  They are run asynchronously by default, but a wait() method is
16 provided should it be necessary to wait for a task to finish.
17 */
18 class LIBBUILDER_API Task
19 {
20 public:
21         enum Status
22         {
23                 RUNNING,
24                 SUCCESS,
25                 ERROR
26         };
27
28         sigc::signal<void, bool> signal_finished;
29
30 protected:
31         std::vector<const FileTarget *> targets;
32         bool unlink = false;
33         Msp::Time::TimeStamp start_time;
34         Msp::Time::TimeDelta duration;
35
36         Task() = default;
37 public:
38         virtual ~Task() { }
39
40         /** Associate the task with a target. */
41         void add_target(const FileTarget &);
42
43         /** If set to true, the associated files are removed before the task is
44         started. */
45         void set_unlink(bool = true);
46
47         const std::vector<const FileTarget *> &get_targets() const { return targets; }
48
49         /** Returns the command being executed for this task.  Only makes sense if
50         an external command is involved. */
51         virtual std::string get_command() const = 0;
52
53         /// Starts the task.
54         virtual void start() = 0;
55
56 protected:
57         /// Ensures that the output directory exists and removes files if necessary.
58         void prepare();
59
60         void finished(bool);
61
62 public:
63         /// Checks the status of the task and immediately returns.
64         virtual Status check() = 0;
65
66         /// Waits for the task to finish and returns its final status.
67         virtual Status wait() = 0;
68
69         /** Returns the amount of time completing the task took.  Only available
70         after the task has finished. */
71         const Msp::Time::TimeDelta &get_duration() const { return duration; }
72 };
73
74 #endif