Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #ifndef CXTHREADEDTIMEDALGORITHM_H_
34 #define CXTHREADEDTIMEDALGORITHM_H_
35 
36 #include "cxResourceExport.h"
37 
38 #include <QFutureWatcher>
39 #include <QtConcurrent/QtConcurrentRun>
40 
41 #include "cxTimedAlgorithm.h"
42 
43 #include "vtkForwardDeclarations.h"
44 #include "cxForwardDeclarations.h"
45 
46 namespace cx
47 {
48 
57 template <class T>
58 class cxResource_EXPORT ThreadedTimedAlgorithm : public TimedBaseAlgorithm
59 {
60 public:
61  ThreadedTimedAlgorithm(QString product, int secondsBetweenAnnounce) :
62  TimedBaseAlgorithm(product, secondsBetweenAnnounce)
63  {
64  connect(&mWatcher, SIGNAL(finished()), this, SLOT(finishedSlot()));
65  connect(&mWatcher, SIGNAL(finished()), this, SLOT(postProcessingSlot()));
66  connect(&mWatcher, SIGNAL(finished()), this, SIGNAL(finished()));
67  }
69 
70  virtual void execute()
71  {
72  emit aboutToStart();
73  this->preProcessingSlot();
74  this->generate();
75  }
76  virtual bool isFinished() const { return mWatcher.isFinished(); }
77  virtual bool isRunning() const { return mWatcher.isRunning(); }
78 
79 
80 protected:
81  virtual void preProcessingSlot() {}
82  virtual void postProcessingSlot() = 0;
83 
84 protected:
85  virtual T calculate() = 0;
86 
87  void generate()
88  {
90  emit started(0); // TODO move to started signal from qtconcurrent??
91 
92  mFutureResult = QtConcurrent::run(this, &ThreadedTimedAlgorithm<T>::calculate);
93  mWatcher.setFuture(mFutureResult);
94  }
95  T getResult()
96  {
97  T result = mWatcher.future().result();
98  return result;
99  }
100 
101 private:
102  void finishedSlot()
103  {
105  }
106 
107  QFuture<T> mFutureResult;
108  QFutureWatcher<T> mWatcher;
109 };
110 
111 //template specicalizations
112 template<>
113 cxResource_EXPORT void ThreadedTimedAlgorithm<void>::getResult();
114 
115 //---------------------------------------------------------------------------------------------------------------------
123 class Example : public ThreadedTimedAlgorithm<QString>
124 {
125  Q_OBJECT
126 public:
127  Example();
128  virtual ~Example();
129 
130 private slots:
131  virtual void postProcessingSlot();
132 
133 private:
134  virtual QString calculate();
135 };
136 
137 }//namespace
138 
139 
140 #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...