1 | // %pacpus:license{
|
---|
2 | // This file is part of the PACPUS framework distributed under the
|
---|
3 | // CECILL-C License, Version 1.0.
|
---|
4 | // %pacpus:license}
|
---|
5 | /// @file
|
---|
6 | /// @author Samuel Gosselin <firstname.surname@utc.fr>
|
---|
7 | /// @date December, 2012
|
---|
8 | /// @version $Id: AsyncWorkerBase.h 76 2013-01-10 17:05:10Z kurdejma $
|
---|
9 | /// @copyright Copyright (c) UTC/CNRS Heudiasyc 2006 - 2013. All rights reserved.
|
---|
10 | /// @brief Async worker base class
|
---|
11 | ///
|
---|
12 | /// Detailed description.
|
---|
13 |
|
---|
14 | #ifndef DEF_PACPUS_ASYNC_WORKER_BASE_H
|
---|
15 | #define DEF_PACPUS_ASYNC_WORKER_BASE_H
|
---|
16 |
|
---|
17 | //#include <boost/noncopyable.hpp>
|
---|
18 | #include <QObject>
|
---|
19 |
|
---|
20 | namespace pacpus
|
---|
21 | {
|
---|
22 | /**
|
---|
23 | * @brief A simple base class for asynchronous workers able to partake in the
|
---|
24 | * Qt slot / signal mechanism
|
---|
25 | *
|
---|
26 | * This class represents an asynchronous event-based worker able to receive and
|
---|
27 | * send Qt signals to other workers or threads. The rationale is to be able to
|
---|
28 | * define stateful objects that perform calculations asynchronously, triggered
|
---|
29 | * by external user-defined messages.
|
---|
30 | */
|
---|
31 | class AsyncWorkerBase
|
---|
32 | : public QObject
|
---|
33 | //, private boost::noncopyable
|
---|
34 | {
|
---|
35 | Q_OBJECT
|
---|
36 | Q_DISABLE_COPY(AsyncWorkerBase)
|
---|
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 | Q_SIGNALS:
|
---|
62 | /// @todo Documentation
|
---|
63 | void finished();
|
---|
64 | /// @todo Documentation
|
---|
65 | void stopped();
|
---|
66 |
|
---|
67 | protected:
|
---|
68 | /// @todo Documentation
|
---|
69 | virtual void setup();
|
---|
70 | /// @todo Documentation
|
---|
71 | virtual void process();
|
---|
72 | /// @todo Documentation
|
---|
73 | virtual void cleanup();
|
---|
74 |
|
---|
75 | /** Returns true if the worker is stopping. */
|
---|
76 | bool isStopping() const { return stopping_; }
|
---|
77 |
|
---|
78 | private Q_SLOTS:
|
---|
79 | /// @todo Documentation
|
---|
80 | void doSetup();
|
---|
81 | /// @todo Documentation
|
---|
82 | void doFinish();
|
---|
83 |
|
---|
84 | private:
|
---|
85 | /*! \brief Ends the worker, asking the underlying thread to terminate
|
---|
86 | *
|
---|
87 | * This method signals the end of processing and requests the underlying thread to terminate. Actual termination
|
---|
88 | * will occur as soon as all pending signals (including those that may come from other workers during the
|
---|
89 | * finish request handling) have been processed.
|
---|
90 | */
|
---|
91 | void finish();
|
---|
92 |
|
---|
93 | // Attributes
|
---|
94 | bool active_;
|
---|
95 | bool stopping_;
|
---|
96 | };
|
---|
97 | } // namespace pacpus
|
---|
98 |
|
---|
99 | #endif // DEF_PACPUS_ASYNC_WORKER_BASE_H
|
---|