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