workerthread.h
1 /********************************************************************************
2  * FARSA Experiments Library *
3  * Copyright (C) 2007-2012 *
4  * Gianluca Massera <emmegian@yahoo.it> *
5  * Stefano Nolfi <stefano.nolfi@istc.cnr.it> *
6  * Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
21  ********************************************************************************/
22 
23 #ifndef WORKERTHREAD_H
24 #define WORKERTHREAD_H
25 
26 #include "utilitiesconfig.h"
27 #include <QThread>
28 #include <QMutex>
29 #include <QWaitCondition>
30 #include <QQueue>
31 #include <QList>
32 #include "baseexception.h"
33 
34 namespace farsa {
35 
44 class FARSA_UTIL_TEMPLATE UnknownException : public BaseException
45 {
46 public:
50  UnknownException() throw() :
51  BaseException()
52  {
53  }
54 
60  UnknownException(const UnknownException& other) throw() :
61  BaseException(other)
62  {
63  }
64 
71  {
72  if (&other == this) {
73  return *this;
74  }
75 
76  BaseException::operator=(other);
77 
78  return *this;
79  }
80 
84  virtual ~UnknownException() throw()
85  {
86  }
87 
93  virtual const char *what() const throw()
94  {
95  return "Unknown exception thrown by a ThreadOperation, cannot provide further information";
96  }
97 
102  EXCEPTION_HELPER_FUNCTIONS(UnknownException)
103 };
104 
108 class FARSA_UTIL_TEMPLATE ThreadOperation {
109 public:
110  virtual ~ThreadOperation() { };
115  virtual void stop() = 0;
120  virtual void run() = 0;
121 };
122 
126 class FARSA_UTIL_API WorkerThread : public QThread {
127  Q_OBJECT
128 
129 public:
131  WorkerThread( QObject* parent );
133  ~WorkerThread();
136  void addOperation( ThreadOperation* operation, bool deleteAtEnd = true );
140  void stopCurrentOperation(bool wait);
142  void run();
148  void quit();
150  bool operationRunning();
151 
152 signals:
168  void exceptionDuringOperation(farsa::BaseException *e);
169 
170 private:
172  struct ThreadOperationInfo
173  {
174  ThreadOperationInfo() :
175  operation(NULL),
176  deleteAtEnd(false)
177  {
178  }
179 
180  ThreadOperationInfo(ThreadOperation* o, bool d) :
181  operation(o),
182  deleteAtEnd(d)
183  {
184  }
185 
186  ThreadOperation* operation;
187  bool deleteAtEnd;
188  };
190  QQueue<ThreadOperationInfo> operations;
192  QMutex mutex;
194  QWaitCondition waitForOperationsToDo;
196  QWaitCondition waitForOperationsToFinish;
198  ThreadOperationInfo operation;
200  bool quitRequested;
205  QList<BaseException *> exceptions;
206 };
207 
208 } // end namespace farsa
209 
210 #endif
This file contains the common type defitions used on the whole framework.
UnknownException & operator=(const UnknownException &other)
Copy operator.
Definition: workerthread.h:70
virtual const char * what() const
Returns a C string describing the exception.
Definition: workerthread.h:93
A macro to deprecate functions.
virtual ~UnknownException()
Destructor.
Definition: workerthread.h:84
UnknownException(const UnknownException &other)
Copy constructor.
Definition: workerthread.h:60
interface for describing an operation to do for the WorkerThread
Definition: workerthread.h:108
The exception stored when an unknown exception is thrown by a ThreadOperation.
Definition: workerthread.h:44
UnknownException()
Constructor.
Definition: workerthread.h:50
the supporting thread in order to run operations on a different thread instead of the GUI thread ...
Definition: workerthread.h:126