source: pacpusframework/trunk/include/Pacpus/PacpusTools/AsyncWorkerBase.h@ 31

Last change on this file since 31 was 31, checked in by sgosseli, 11 years ago

Huge commit: use the new includes style in all the files, add the license header in all the headers, and in some cpp.

File size: 2.7 KB
Line 
1/**
2 *
3 * Distributed under the UTC Heudiascy Pacpus License, Version 1.0.
4 * Copyright (c) UTC Heudiasyc 2010 - 2013. All rights reserved.
5 *
6 * See the LICENSE file for more information or a copy at:
7 * http://www.hds.utc.fr/~kurdejma/LICENSE_1_0.txt
8 *
9 */
10
11#ifndef DEF_PACPUS_ASYNC_WORKER_BASE_H
12#define DEF_PACPUS_ASYNC_WORKER_BASE_H
13
14// Includes, pacpus.
15#include <PacpusTools/utility/NonCopyable.h>
16
17// Includes, qt.
18#include <QObject>
19
20namespace 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 NonCopyable
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
60 signals:
61 void finished();
62 void stopped();
63
64 protected:
65 virtual void setup();
66 virtual void process();
67 virtual void cleanup();
68
69 /** Returns true if the worker is stopping. */
70 bool isStopping() const { return stopping_; }
71
72 private slots:
73 void doSetup();
74 void doFinish();
75
76 private:
77 /*! \brief Ends the worker, asking the underlying thread to terminate
78 *
79 * This method signals the end of processing and requests the underlying thread to terminate. Actual termination
80 * will occur as soon as all pending signals (including those that may come from other workers during the
81 * finish request handling) have been processed.
82 */
83 void finish();
84
85 // Attributes
86 bool active_;
87 bool stopping_;
88 };
89} // namespace pacpus
90
91#endif // DEF_PACPUS_ASYNC_WORKER_BASE_H
Note: See TracBrowser for help on using the repository browser.