NorMIT-nav  22.09
An IGT application
cxThreadedTimedAlgorithm.h
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #ifndef CXTHREADEDTIMEDALGORITHM_H_
13 #define CXTHREADEDTIMEDALGORITHM_H_
14 
15 #include "cxResourceExport.h"
16 
17 #include <QFutureWatcher>
18 #include <QtConcurrent/QtConcurrentRun>
19 
20 #include "cxTimedAlgorithm.h"
21 
22 #include "vtkForwardDeclarations.h"
23 #include "cxForwardDeclarations.h"
24 
25 namespace cx
26 {
27 
36 template <class T>
37 class cxResource_EXPORT ThreadedTimedAlgorithm : public TimedBaseAlgorithm
38 {
39 public:
40  ThreadedTimedAlgorithm(QString product, int secondsBetweenAnnounce) :
41  TimedBaseAlgorithm(product, secondsBetweenAnnounce)
42  {
43  connect(&mWatcher, SIGNAL(finished()), this, SLOT(finishedSlot()));
44  connect(&mWatcher, SIGNAL(finished()), this, SLOT(postProcessingSlot()));
45  connect(&mWatcher, SIGNAL(finished()), this, SIGNAL(finished()));
46  }
48 
49  virtual void execute()
50  {
51  emit aboutToStart();
52  this->preProcessingSlot();
53  this->generate();
54  }
55  virtual bool isFinished() const { return mWatcher.isFinished(); }
56  virtual bool isRunning() const { return mWatcher.isRunning(); }
57 
58 
59 protected:
60  virtual void preProcessingSlot() {}
61  virtual void postProcessingSlot() = 0;
62 
63 protected:
64  virtual T calculate() = 0;
65 
66  void generate()
67  {
69  emit started(0); // TODO move to started signal from qtconcurrent??
70 
71  mFutureResult = QtConcurrent::run(this, &ThreadedTimedAlgorithm<T>::calculate);
72  mWatcher.setFuture(mFutureResult);
73  }
74  T getResult()
75  {
76  T result = mWatcher.future().result();
77  return result;
78  }
79 
80 private:
81  void finishedSlot()
82  {
84  }
85 
86  QFuture<T> mFutureResult;
87  QFutureWatcher<T> mWatcher;
88 };
89 
90 //template specicalizations
91 template<>
92 cxResource_EXPORT void ThreadedTimedAlgorithm<void>::getResult();
93 
94 //---------------------------------------------------------------------------------------------------------------------
102 class Example : public ThreadedTimedAlgorithm<QString>
103 {
104  Q_OBJECT
105 public:
106  Example();
107  virtual ~Example();
108 
109 private slots:
110  virtual void postProcessingSlot();
111 
112 private:
113  virtual QString calculate();
114 };
115 
116 }//namespace
117 
118 
119 #endif /* CXTHREADEDTIMEDALGORITHM_H_ */
cx::ThreadedTimedAlgorithm::isRunning
virtual bool isRunning() const
Definition: cxThreadedTimedAlgorithm.h:56
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cxForwardDeclarations.h
cx::ThreadedTimedAlgorithm::~ThreadedTimedAlgorithm
virtual ~ThreadedTimedAlgorithm()
Definition: cxThreadedTimedAlgorithm.h:47
cx::ThreadedTimedAlgorithm::getResult
T getResult()
Definition: cxThreadedTimedAlgorithm.h:74
cx::ThreadedTimedAlgorithm
Base class for algorithms that wants to thread and time their execution. T is the return type of the ...
Definition: cxThreadedTimedAlgorithm.h:37
cx::TimedBaseAlgorithm::stopTiming
void stopTiming()
Definition: cxTimedAlgorithm.cpp:46
cx::ThreadedTimedAlgorithm::isFinished
virtual bool isFinished() const
Definition: cxThreadedTimedAlgorithm.h:55
cx::ThreadedTimedAlgorithm::ThreadedTimedAlgorithm
ThreadedTimedAlgorithm(QString product, int secondsBetweenAnnounce)
Definition: cxThreadedTimedAlgorithm.h:40
cx::ThreadedTimedAlgorithm::execute
virtual void execute()
Definition: cxThreadedTimedAlgorithm.h:49
cx::Example::~Example
virtual ~Example()
Definition: cxThreadedTimedAlgorithm.cpp:49
vtkForwardDeclarations.h
cx::TimedBaseAlgorithm::startTiming
void startTiming()
Definition: cxTimedAlgorithm.cpp:38
cx::Example::Example
Example()
Definition: cxThreadedTimedAlgorithm.cpp:42
cx::ThreadedTimedAlgorithm::generate
void generate()
Definition: cxThreadedTimedAlgorithm.h:66
cx::TimedBaseAlgorithm
Base class for algorithms that wants to time their execution.
Definition: cxTimedAlgorithm.h:40
cxTimedAlgorithm.h
cx::Example
Implementation of ThreadedTimedAlgorithm that shows the minimum implementation of this class.
Definition: cxThreadedTimedAlgorithm.h:102