CustusX  2022.05.26-dev+develop.ffefb6
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_ */
ThreadedTimedAlgorithm(QString product, int secondsBetweenAnnounce)
Base class for algorithms that wants to time their execution.
Base class for algorithms that wants to thread and time their execution. T is the return type of the ...
virtual void preProcessingSlot()
This happens before the thread (calculate) is started, here non-thread safe functions can be called...
Implementation of ThreadedTimedAlgorithm that shows the minimum implementation of this class...
Namespace for all CustusX production code.