[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 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.
|
---|
[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 |
|
---|
| 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
|
---|
[44] | 33 | , private boost::noncopyable
|
---|
[3] | 34 | {
|
---|
| 35 | Q_OBJECT
|
---|
| 36 | public:
|
---|
| 37 | /** Constructor. */
|
---|
| 38 | AsyncWorkerBase();
|
---|
| 39 |
|
---|
| 40 | /** Destructor. */
|
---|
| 41 | virtual ~AsyncWorkerBase();
|
---|
| 42 |
|
---|
| 43 | /** Starts the worker by creating a new thread for it, and calling the setup() virtual method. */
|
---|
| 44 | void start();
|
---|
| 45 |
|
---|
| 46 | /** Terminates the worker as soon as there are no more requests pending and its current processing
|
---|
| 47 | * is finished.
|
---|
| 48 | *
|
---|
| 49 | * This method tries to end the worker gracefully. All pending signals sent before the stop() request will
|
---|
| 50 | * be honored.
|
---|
| 51 | * It is safe to call this method from any thread.
|
---|
| 52 | *
|
---|
| 53 | * @param autoDelete if true, deletes the worker as soon as its event loop has finished processing.
|
---|
| 54 | */
|
---|
| 55 | void stop(bool autoDelete);
|
---|
| 56 |
|
---|
| 57 | /** Returns true if the worker is active. */
|
---|
| 58 | bool isActive() const { return active_; }
|
---|
| 59 |
|
---|
[67] | 60 | Q_SIGNALS:
|
---|
| 61 | /// @todo Documentation
|
---|
| 62 | void finished();
|
---|
| 63 | /// @todo Documentation
|
---|
| 64 | void stopped();
|
---|
[3] | 65 |
|
---|
| 66 | protected:
|
---|
[67] | 67 | /// @todo Documentation
|
---|
[3] | 68 | virtual void setup();
|
---|
[67] | 69 | /// @todo Documentation
|
---|
[3] | 70 | virtual void process();
|
---|
[67] | 71 | /// @todo Documentation
|
---|
[3] | 72 | virtual void cleanup();
|
---|
| 73 |
|
---|
| 74 | /** Returns true if the worker is stopping. */
|
---|
| 75 | bool isStopping() const { return stopping_; }
|
---|
| 76 |
|
---|
[67] | 77 | private Q_SLOTS:
|
---|
| 78 | /// @todo Documentation
|
---|
[3] | 79 | void doSetup();
|
---|
[67] | 80 | /// @todo Documentation
|
---|
[3] | 81 | void doFinish();
|
---|
| 82 |
|
---|
| 83 | private:
|
---|
| 84 | /*! \brief Ends the worker, asking the underlying thread to terminate
|
---|
| 85 | *
|
---|
| 86 | * This method signals the end of processing and requests the underlying thread to terminate. Actual termination
|
---|
| 87 | * will occur as soon as all pending signals (including those that may come from other workers during the
|
---|
| 88 | * finish request handling) have been processed.
|
---|
| 89 | */
|
---|
| 90 | void finish();
|
---|
| 91 |
|
---|
| 92 | // Attributes
|
---|
| 93 | bool active_;
|
---|
| 94 | bool stopping_;
|
---|
| 95 | };
|
---|
| 96 | } // namespace pacpus
|
---|
| 97 |
|
---|
[31] | 98 | #endif // DEF_PACPUS_ASYNC_WORKER_BASE_H
|
---|