[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 106 2013-06-05 16:01:14Z gdherbom $
|
---|
| 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 | {
|
---|
| 24 | /**
|
---|
| 25 | * @brief 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 | */
|
---|
[103] | 33 | class PACPUSTOOLS_API AsyncWorkerBase
|
---|
[3] | 34 | : public QObject
|
---|
[44] | 35 | , private boost::noncopyable
|
---|
[3] | 36 | {
|
---|
| 37 | Q_OBJECT
|
---|
| 38 | public:
|
---|
| 39 | /** Constructor. */
|
---|
| 40 | AsyncWorkerBase();
|
---|
| 41 |
|
---|
| 42 | /** Destructor. */
|
---|
| 43 | virtual ~AsyncWorkerBase();
|
---|
| 44 |
|
---|
| 45 | /** Starts the worker by creating a new thread for it, and calling the setup() virtual method. */
|
---|
| 46 | void start();
|
---|
| 47 |
|
---|
| 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 | */
|
---|
| 57 | void stop(bool autoDelete);
|
---|
| 58 |
|
---|
| 59 | /** Returns true if the worker is active. */
|
---|
| 60 | bool isActive() const { return active_; }
|
---|
| 61 |
|
---|
[67] | 62 | Q_SIGNALS:
|
---|
| 63 | /// @todo Documentation
|
---|
| 64 | void finished();
|
---|
| 65 | /// @todo Documentation
|
---|
| 66 | void stopped();
|
---|
[3] | 67 |
|
---|
| 68 | protected:
|
---|
[67] | 69 | /// @todo Documentation
|
---|
[3] | 70 | virtual void setup();
|
---|
[67] | 71 | /// @todo Documentation
|
---|
[3] | 72 | virtual void process();
|
---|
[67] | 73 | /// @todo Documentation
|
---|
[3] | 74 | virtual void cleanup();
|
---|
| 75 |
|
---|
| 76 | /** Returns true if the worker is stopping. */
|
---|
| 77 | bool isStopping() const { return stopping_; }
|
---|
| 78 |
|
---|
[67] | 79 | private Q_SLOTS:
|
---|
| 80 | /// @todo Documentation
|
---|
[3] | 81 | void doSetup();
|
---|
[67] | 82 | /// @todo Documentation
|
---|
[3] | 83 | void doFinish();
|
---|
| 84 |
|
---|
| 85 | private:
|
---|
| 86 | /*! \brief Ends the worker, asking the underlying thread to terminate
|
---|
| 87 | *
|
---|
| 88 | * This method signals the end of processing and requests the underlying thread to terminate. Actual termination
|
---|
| 89 | * will occur as soon as all pending signals (including those that may come from other workers during the
|
---|
| 90 | * finish request handling) have been processed.
|
---|
| 91 | */
|
---|
| 92 | void finish();
|
---|
| 93 |
|
---|
| 94 | // Attributes
|
---|
| 95 | bool active_;
|
---|
| 96 | bool stopping_;
|
---|
| 97 | };
|
---|
| 98 | } // namespace pacpus
|
---|
| 99 |
|
---|
[31] | 100 | #endif // DEF_PACPUS_ASYNC_WORKER_BASE_H
|
---|