Quark  0.1
Task.h++
Go to the documentation of this file.
1 #ifndef __libquark_util_Task_hxx
2 #define __libquark_util_Task_hxx
3 
4 #include <QAtomicInt>
5 #include <QObject>
6 
7 #include <quark/Quark.h++>
8 
9 namespace quark {
10 namespace util {
11 
12 class TaskProcessor;
13 
20 class LIBQUARK_API Task : public QObject
21 {
22  Q_OBJECT;
23 
24  friend class TaskProcessor;
25 
26  public:
27 
29  virtual ~Task();
30 
32  inline int id() const
33  { return(_id); }
34 
36  inline bool isRunning() const
37  { return(_running); }
38 
40  inline bool isCancelling() const
41  { return(_cancelling); }
42 
44  inline bool isCancelled() const
45  { return(_cancelled); }
46 
48  inline bool isFailed() const
49  { return(_failed); }
50 
52  inline bool isBackground() const
53  { return(_background); }
54 
56  inline QString error() const
57  { return(_error); }
58 
60  void setTimeout(int seconds);
61 
66  qint64 executionTime() const;
67 
69  static const int DEFAULT_TIMEOUT;
70 
71  signals:
72 
74  void statusUpdated(const QString& status);
75 
77  void finished();
78 
80  void cancelled();
81 
82  protected:
83 
89  Task(bool background = false);
90 
95  virtual void execute() = 0;
96 
104  virtual bool cancel();
105 
107  void setCancelled();
108 
110  void fail(QString error = QString());
111 
113  void updateStatus(const QString& text);
114 
115  private:
116 
117  void process();
118  bool tryCancel();
119  void notifyFinished();
120  void notifyCancelled();
121  bool isTimedOut(const qint64 &now) const;
122 
123  inline void setRunning(bool running)
124  { _running = running; }
125 
126  int _id;
127  bool _background;
128  bool _running;
129  bool _cancelling;
130  bool _cancelled;
131  bool _failed;
132  qint64 _startTime;
133  qint64 _endTime;
134  int _timeout;
135  QString _error;
136 
137  static QAtomicInt _idCounter;
138 
139  Q_DISABLE_COPY(Task);
140 };
141 
142 } // namespace util
143 } // namespace quark
144 
145 #endif // __libquark_util_Task_hxx
bool isCancelled() const
Tests if the task has been cancelled.
Definition: Task.h++:44
bool isFailed() const
Tests if the task has failed.
Definition: Task.h++:48
bool isCancelling() const
Tests if the task is currently cancelling.
Definition: Task.h++:40
bool isBackground() const
Tests if the task is a background task.
Definition: Task.h++:52
static const int DEFAULT_TIMEOUT
The default timeout interval, in seconds.
Definition: Task.h++:69
An object that executes Tasks in a queue on a dedicated thread.
Definition: TaskProcessor.h++:23
Definition: BarChartView.h++:6
An abstract base class for objects that represent tasks to be executed by a TaskProcessor.
Definition: Task.h++:20
int id() const
Returns the unique ID that was assigned to this task.
Definition: Task.h++:32
#define LIBQUARK_API
Definition: Quark.h++:18
bool isRunning() const
Tests if the task is currently executing.
Definition: Task.h++:36
QString error() const
Returns the error message reported by this task upon failure, if any.
Definition: Task.h++:56