PARP Research Group University of Murcia, Spain


src/qvcore/qvworker.h

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007, 2008. PARP Research Group.
00003  *      <http://perception.inf.um.es>
00004  *      University of Murcia, Spain.
00005  *
00006  *      This file is part of the QVision library.
00007  *
00008  *      QVision is free software: you can redistribute it and/or modify
00009  *      it under the terms of the GNU Lesser General Public License as
00010  *      published by the Free Software Foundation, version 3 of the License.
00011  *
00012  *      QVision is distributed in the hope that it will be useful,
00013  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *      GNU Lesser General Public License for more details.
00016  *
00017  *      You should have received a copy of the GNU Lesser General Public
00018  *      License along with QVision. If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00024 
00025 #ifndef QVWORKER_H
00026 #define QVWORKER_H
00027 
00028 #include <QStringList>
00029 #include <QThread>
00030 #include <QTime>
00031 
00032 #include <qvutils/qvcpustatcontroler.h>
00033 #include <QVPropertyContainer>
00034 
00045 class QVWorker: public QThread, public QVPropertyContainer
00046         {
00047         Q_OBJECT
00048 
00049         public:
00051                 typedef enum
00052                         {
00056                         Running,
00059                         RunningOneStep,
00063                         Paused,
00067                         Stoped,
00069                         Finished
00070                         } TWorkerStatus;
00071 
00076                 QVWorker(const QString name = QString());
00077 
00082                 QVWorker(const QVWorker &other);
00083 
00085                 ~QVWorker();
00086 
00091                 virtual void iterate() { std::cerr << "WARNING: redefinition of worker() is deprecated. Redefine iterate() instead." << std::endl; worker(); };
00092 
00096                 virtual void worker() {};
00097 
00109                 void setMinimumDelay(int ms) { minms = ms; };
00110 
00113                 bool isFinished() const { return status == Finished; }
00114 
00117                 bool isPaused() const { return status == Paused; }
00118 
00121                 bool isStoped() const { return status == Stoped; }
00122 
00125                 bool isRunning() const { return status == Running; }
00126 
00129                 TWorkerStatus getStatus() const { return status; }
00130 /*
00134                 void unlink();
00135 */
00138                 int getIteration() const { return numIterations; }
00139 
00142                 bool isStatsEnabled() const { return statsEnabled; }
00143 
00154                 QVStat getCpuStat() const {
00155                         if (statsEnabled) return cpuStatControler->value();
00156                         else return QVStat();
00157                 }
00158 
00166                 void setPrintStatsFrequency(int freq) {
00167                         if (statsEnabled) {
00168                                 cpuStatControler->setFreq(freq);
00169                                 cpuStatControler->setWorkerName(getName());
00170                         }
00171                 }
00172 
00179                 void printStats()
00180                         {
00181                         if (statsEnabled)
00182                                 cpuStatControler->printStats();
00183                         }
00184 
00203                 void addTrigger(QString name)                   { triggerList.push_back(name); }
00204 
00206                 const QStringList getTriggerList() const        { return triggerList; }
00207 
00208         public slots:
00211                 void pause() { qDebug() << "QVWorker::pause()"; status = Paused; emit statusUpdate(status); }
00212 
00215                 void unPause() { qDebug() << "QVWorker::unPause()"; status = Running; emit statusUpdate(status); }
00216 
00219                 void step() { qDebug() << "QVWorker::step()"; status = RunningOneStep; emit statusUpdate(status); }
00220 
00223                 void stop() { qDebug() << "QVWorker::stop()"; status = Stoped; emit statusUpdate(status); }
00224 
00227                 void finish() { qDebug() << "QVWorker::finish()"; status = Finished; emit statusUpdate(status); }
00228 
00243                 virtual void processTrigger(QString name) { Q_UNUSED(name); }
00244 
00245         signals:
00248                 void startIteration();
00249 
00252                 void endIteration(uint id, int iteration);
00253 
00256                 void statusUpdate(QVWorker::TWorkerStatus);
00257 
00258         private:
00259                 bool statsEnabled;
00260                 QVStatControler *cpuStatControler;
00261                 int numIterations, maxIterations;
00262                 TWorkerStatus status;
00263                 QStringList triggerList;
00264                 QTime iterationTime;
00265                 int curms,minms;
00266 
00267         protected:
00268                 void run();
00269 
00282                 void timeFlag(const QString flag)
00283                         {
00284                         qDebug() << "QVWorker::timeFlag("<< flag <<")";
00285                         if (statsEnabled) cpuStatControler->setFlag(flag);
00286                         }
00287 
00288                 void workerIterate();
00289         };
00290 
00291 Q_DECLARE_METATYPE(QVStat);
00292 #endif



QVision framework. PARP research group, copyright 2007, 2008.