1 | // This file is part of the PACPUS framework distributed under the
|
---|
2 | // CECILL-C License, Version 1.0.
|
---|
3 | //
|
---|
4 | /// @author Samuel Gosselin <firstname.surname@utc.fr>
|
---|
5 | /// @date December, 2012
|
---|
6 | /// @version $Id$
|
---|
7 | /// @copyright Copyright (c) UTC/CNRS Heudiasyc 2006 - 2013. All rights reserved.
|
---|
8 | /// @brief Async worker base class
|
---|
9 | ///
|
---|
10 | /// Detailed description.
|
---|
11 |
|
---|
12 | #ifndef DEF_PACPUS_ASYNC_WORKER_BASE_H
|
---|
13 | #define DEF_PACPUS_ASYNC_WORKER_BASE_H
|
---|
14 |
|
---|
15 | // Includes, boost.
|
---|
16 | #include <boost/noncopyable.hpp>
|
---|
17 |
|
---|
18 | // Includes, qt.
|
---|
19 | #include <QObject>
|
---|
20 |
|
---|
21 | namespace pacpus
|
---|
22 | {
|
---|
23 | /**
|
---|
24 | * @brief A simple base class for asynchronous workers able to partake in the
|
---|
25 | * Qt slot / signal mechanism
|
---|
26 | *
|
---|
27 | * This class represents an asynchronous event-based worker able to receive and
|
---|
28 | * send Qt signals to other workers or threads. The rationale is to be able to
|
---|
29 | * define stateful objects that perform calculations asynchronously, triggered
|
---|
30 | * by external user-defined messages.
|
---|
31 | */
|
---|
32 | class AsyncWorkerBase
|
---|
33 | : public QObject
|
---|
34 | , private boost::noncopyable
|
---|
35 | {
|
---|
36 | Q_OBJECT
|
---|
37 | public:
|
---|
38 | /** Constructor. */
|
---|
39 | AsyncWorkerBase();
|
---|
40 |
|
---|
41 | /** Destructor. */
|
---|
42 | virtual ~AsyncWorkerBase();
|
---|
43 |
|
---|
44 | /** Starts the worker by creating a new thread for it, and calling the setup() virtual method. */
|
---|
45 | void start();
|
---|
46 |
|
---|
47 | /** Terminates the worker as soon as there are no more requests pending and its current processing
|
---|
48 | * is finished.
|
---|
49 | *
|
---|
50 | * This method tries to end the worker gracefully. All pending signals sent before the stop() request will
|
---|
51 | * be honored.
|
---|
52 | * It is safe to call this method from any thread.
|
---|
53 | *
|
---|
54 | * @param autoDelete if true, deletes the worker as soon as its event loop has finished processing.
|
---|
55 | */
|
---|
56 | void stop(bool autoDelete);
|
---|
57 |
|
---|
58 | /** Returns true if the worker is active. */
|
---|
59 | bool isActive() const { return active_; }
|
---|
60 |
|
---|
61 | signals:
|
---|
62 | void finished();
|
---|
63 | void stopped();
|
---|
64 |
|
---|
65 | protected:
|
---|
66 | virtual void setup();
|
---|
67 | virtual void process();
|
---|
68 | virtual void cleanup();
|
---|
69 |
|
---|
70 | /** Returns true if the worker is stopping. */
|
---|
71 | bool isStopping() const { return stopping_; }
|
---|
72 |
|
---|
73 | private slots:
|
---|
74 | void doSetup();
|
---|
75 | void doFinish();
|
---|
76 |
|
---|
77 | private:
|
---|
78 | /*! \brief Ends the worker, asking the underlying thread to terminate
|
---|
79 | *
|
---|
80 | * This method signals the end of processing and requests the underlying thread to terminate. Actual termination
|
---|
81 | * will occur as soon as all pending signals (including those that may come from other workers during the
|
---|
82 | * finish request handling) have been processed.
|
---|
83 | */
|
---|
84 | void finish();
|
---|
85 |
|
---|
86 | // Attributes
|
---|
87 | bool active_;
|
---|
88 | bool stopping_;
|
---|
89 | };
|
---|
90 | } // namespace pacpus
|
---|
91 |
|
---|
92 | #endif // DEF_PACPUS_ASYNC_WORKER_BASE_H
|
---|