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

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

Documentation: file info.
Fixed: problem with includes in PacpusPluginInterface.h.

  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1// This file is part of the PACPUS framework distributed under the
2// CECILL-C License, Version 1.0.
3//
4/// @file
5/// @author Samuel Gosselin <firstname.surname@utc.fr>
6/// @date December, 2012
7/// @version $Id: AsyncWorkerBase.h 67 2013-01-09 18:17:44Z kurdejma $
8/// @copyright Copyright (c) UTC/CNRS Heudiasyc 2006 - 2013. All rights reserved.
9/// @brief Async worker base class
10///
11/// Detailed description.
12
13#ifndef DEF_PACPUS_ASYNC_WORKER_BASE_H
14#define DEF_PACPUS_ASYNC_WORKER_BASE_H
15
16#include <boost/noncopyable.hpp>
17#include <QObject>
18
19namespace pacpus
20{
21 /**
22 * @brief A simple base class for asynchronous workers able to partake in the
23 * Qt slot / signal mechanism
24 *
25 * This class represents an asynchronous event-based worker able to receive and
26 * send Qt signals to other workers or threads. The rationale is to be able to
27 * define stateful objects that perform calculations asynchronously, triggered
28 * by external user-defined messages.
29 */
30 class AsyncWorkerBase
31 : public QObject
32 , private boost::noncopyable
33 {
34 Q_OBJECT
35 public:
36 /** Constructor. */
37 AsyncWorkerBase();
38
39 /** Destructor. */
40 virtual ~AsyncWorkerBase();
41
42 /** Starts the worker by creating a new thread for it, and calling the setup() virtual method. */
43 void start();
44
45 /** Terminates the worker as soon as there are no more requests pending and its current processing
46 * is finished.
47 *
48 * This method tries to end the worker gracefully. All pending signals sent before the stop() request will
49 * be honored.
50 * It is safe to call this method from any thread.
51 *
52 * @param autoDelete if true, deletes the worker as soon as its event loop has finished processing.
53 */
54 void stop(bool autoDelete);
55
56 /** Returns true if the worker is active. */
57 bool isActive() const { return active_; }
58
59 Q_SIGNALS:
60 /// @todo Documentation
61 void finished();
62 /// @todo Documentation
63 void stopped();
64
65 protected:
66 /// @todo Documentation
67 virtual void setup();
68 /// @todo Documentation
69 virtual void process();
70 /// @todo Documentation
71 virtual void cleanup();
72
73 /** Returns true if the worker is stopping. */
74 bool isStopping() const { return stopping_; }
75
76 private Q_SLOTS:
77 /// @todo Documentation
78 void doSetup();
79 /// @todo Documentation
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
97#endif // DEF_PACPUS_ASYNC_WORKER_BASE_H
Note: See TracBrowser for help on using the repository browser.