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