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

Last change on this file since 76 was 76, checked in by Marek Kurdej, 11 years ago

Added: automated license updating lines:
%pacpus:license{
%pacpus:license}

  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1// %pacpus:license{
2// This file is part of the PACPUS framework distributed under the
3// CECILL-C License, Version 1.0.
4// %pacpus:license}
5/// @file
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.
13
14#ifndef DEF_PACPUS_ASYNC_WORKER_BASE_H
15#define DEF_PACPUS_ASYNC_WORKER_BASE_H
16
17#include <boost/noncopyable.hpp>
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 boost::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 Q_SIGNALS:
61 /// @todo Documentation
62 void finished();
63 /// @todo Documentation
64 void stopped();
65
66 protected:
67 /// @todo Documentation
68 virtual void setup();
69 /// @todo Documentation
70 virtual void process();
71 /// @todo Documentation
72 virtual void cleanup();
73
74 /** Returns true if the worker is stopping. */
75 bool isStopping() const { return stopping_; }
76
77 private Q_SLOTS:
78 /// @todo Documentation
79 void doSetup();
80 /// @todo Documentation
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
98#endif // DEF_PACPUS_ASYNC_WORKER_BASE_H
Note: See TracBrowser for help on using the repository browser.