00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef WORKERTHREAD_H
00024 #define WORKERTHREAD_H
00025
00026 #include "utilitiesconfig.h"
00027 #include <QThread>
00028 #include <QMutex>
00029 #include <QWaitCondition>
00030 #include <QQueue>
00031 #include <QList>
00032 #include "baseexception.h"
00033
00034 namespace farsa {
00035
00044 class FARSA_UTIL_TEMPLATE UnknownException : public BaseException
00045 {
00046 public:
00050 UnknownException() throw() :
00051 BaseException()
00052 {
00053 }
00054
00060 UnknownException(const UnknownException& other) throw() :
00061 BaseException(other)
00062 {
00063 }
00064
00070 UnknownException& operator=(const UnknownException& other) throw()
00071 {
00072 if (&other == this) {
00073 return *this;
00074 }
00075
00076 BaseException::operator=(other);
00077
00078 return *this;
00079 }
00080
00084 virtual ~UnknownException() throw()
00085 {
00086 }
00087
00093 virtual const char *what() const throw()
00094 {
00095 return "Unknown exception thrown by a ThreadOperation, cannot provide further information";
00096 }
00097
00102 EXCEPTION_HELPER_FUNCTIONS(UnknownException)
00103 };
00104
00108 class FARSA_UTIL_TEMPLATE ThreadOperation {
00109 public:
00110 virtual ~ThreadOperation() { };
00115 virtual void stop() = 0;
00120 virtual void run() = 0;
00121 };
00122
00126 class FARSA_UTIL_API WorkerThread : public QThread {
00127 Q_OBJECT
00128
00129 public:
00131 WorkerThread( QObject* parent );
00133 ~WorkerThread();
00135 void addOperation( ThreadOperation* operation );
00137 void stopCurrentOperation();
00139 void run();
00145 void quit();
00146
00147 signals:
00163 void exceptionDuringOperation(BaseException *e);
00164
00165 private:
00167 QQueue<ThreadOperation*> operations;
00169 QMutex mutex;
00171 QWaitCondition waitForOperationsToDo;
00173 ThreadOperation* operation;
00175 bool quitRequested;
00180 QList<BaseException *> exceptions;
00181 };
00182
00183 }
00184
00185 #endif