source: pacpusframework/trunk/include/Pacpus/PacpusTools/AsyncWorkerBase.h@ 18

Last change on this file since 18 was 3, checked in by sgosseli, 12 years ago
  • Add the existing Pacpus files from pacpusdev and pacpuscore.
  • Provide a clean build system based on multiple CMake files.
File size: 2.4 KB
Line 
1#ifndef __ASYNCWORKER_BASE_H__
2#define __ASYNCWORKER_BASE_H__
3
4// Includes, pacpus.
5#include <PacpusTools/utility/NonCopyable.h>
6
7// Includes, qt.
8#include <QObject>
9
10namespace pacpus
11{
12 /**
13 * @brief A simple base class for asynchronous workers able to partake in the
14 * Qt slot / signal mechanism
15 *
16 * This class represents an asynchronous event-based worker able to receive and
17 * send Qt signals to other workers or threads. The rationale is to be able to
18 * define stateful objects that perform calculations asynchronously, triggered
19 * by external user-defined messages.
20 */
21 class AsyncWorkerBase
22 : public QObject
23 , private NonCopyable
24 {
25 Q_OBJECT
26 public:
27 /** Constructor. */
28 AsyncWorkerBase();
29
30 /** Destructor. */
31 virtual ~AsyncWorkerBase();
32
33 /** Starts the worker by creating a new thread for it, and calling the setup() virtual method. */
34 void start();
35
36 /** Terminates the worker as soon as there are no more requests pending and its current processing
37 * is finished.
38 *
39 * This method tries to end the worker gracefully. All pending signals sent before the stop() request will
40 * be honored.
41 * It is safe to call this method from any thread.
42 *
43 * @param autoDelete if true, deletes the worker as soon as its event loop has finished processing.
44 */
45 void stop(bool autoDelete);
46
47 /** Returns true if the worker is active. */
48 bool isActive() const { return active_; }
49
50 signals:
51 void finished();
52 void stopped();
53
54 protected:
55 virtual void setup();
56 virtual void process();
57 virtual void cleanup();
58
59 /** Returns true if the worker is stopping. */
60 bool isStopping() const { return stopping_; }
61
62 private slots:
63 void doSetup();
64 void doFinish();
65
66 private:
67 /*! \brief Ends the worker, asking the underlying thread to terminate
68 *
69 * This method signals the end of processing and requests the underlying thread to terminate. Actual termination
70 * will occur as soon as all pending signals (including those that may come from other workers during the
71 * finish request handling) have been processed.
72 */
73 void finish();
74
75 // Attributes
76 bool active_;
77 bool stopping_;
78 };
79} // namespace pacpus
80
81#endif
Note: See TracBrowser for help on using the repository browser.