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 
70  UnknownException& operator=(const UnknownException& other) throw()
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 );
138  void stopCurrentOperation();
140  void run();
146  void quit();
148  bool operationRunning();
149 
150 signals:
166  void exceptionDuringOperation(BaseException *e);
167 
168 private:
170  struct ThreadOperationInfo
171  {
172  ThreadOperationInfo() :
173  operation(NULL),
174  deleteAtEnd(false)
175  {
176  }
177 
178  ThreadOperationInfo(ThreadOperation* o, bool d) :
179  operation(o),
180  deleteAtEnd(d)
181  {
182  }
183 
184  ThreadOperation* operation;
185  bool deleteAtEnd;
186  };
188  QQueue<ThreadOperationInfo> operations;
190  QMutex mutex;
192  QWaitCondition waitForOperationsToDo;
194  ThreadOperationInfo operation;
196  bool quitRequested;
201  QList<BaseException *> exceptions;
202 };
203 
204 } // end namespace farsa
205 
206 #endif