utilities/include/workerthread.h

00001 /********************************************************************************
00002  *  FARSA Experiments Library                                                   *
00003  *  Copyright (C) 2007-2012                                                     *
00004  *  Gianluca Massera <emmegian@yahoo.it>                                        *
00005  *  Stefano Nolfi <stefano.nolfi@istc.cnr.it>                                   *
00006  *  Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it>                         *
00007  *                                                                              *
00008  *  This program is free software; you can redistribute it and/or modify        *
00009  *  it under the terms of the GNU General Public License as published by        *
00010  *  the Free Software Foundation; either version 2 of the License, or           *
00011  *  (at your option) any later version.                                         *
00012  *                                                                              *
00013  *  This program is distributed in the hope that it will be useful,             *
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00016  *  GNU General Public License for more details.                                *
00017  *                                                                              *
00018  *  You should have received a copy of the GNU General Public License           *
00019  *  along with this program; if not, write to the Free Software                 *
00020  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
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 } // end namespace farsa
00184 
00185 #endif