fw4spl
IJob.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #ifndef __FWJOBS_IJOB_HPP__
8 #define __FWJOBS_IJOB_HPP__
9 
10 #include "fwJobs/config.hpp"
11 
12 #include <fwCom/Signal.hpp>
13 
14 #include <fwCore/mt/types.hpp>
15 
16 #include <cstdint>
17 #include <functional>
18 #include <future>
19 #include <string>
20 #include <vector>
21 
22 namespace fwJobs
23 {
24 
28 class FWJOBS_CLASS_API IJob
29 {
30 public:
31 
42  enum State
43  {
44  WAITING = 0,
45  RUNNING,
46  CANCELING,
47  CANCELED,
48  FINISHED
49  };
50 
54  typedef SPTR (::fwJobs::IJob) sptr;
55  typedef WPTR (::fwJobs::IJob) wptr;
56 
58  typedef std::function< bool () > CancelRequestCallback;
59 
61  typedef std::function< void (IJob&) > JobCancelHook;
62 
64  typedef std::function< void (IJob&, const std::string&) > LogHook;
65 
67  typedef std::function< void (IJob&, std::uint64_t /*oldDoneWork*/) > DoneWorkHook;
68 
70  typedef std::function< void (IJob&, std::uint64_t /*oldTotalWorkUnits*/) > TotalWorkUnitsHook;
71 
73  typedef std::function< void () > CancelHook;
74 
76  typedef std::function< void ( State ) > StateHook;
77 
79  typedef std::vector< JobCancelHook > CancelHookSeq;
80 
82  typedef std::vector< DoneWorkHook > DoneWorkHookSeq;
83 
85  typedef std::vector< TotalWorkUnitsHook > TotalWorkUnitsHookSeq;
86 
88  typedef std::vector< LogHook > LogHookSeq;
89 
91  typedef std::vector< StateHook > StateHookSeq;
92 
94  typedef std::vector< std::string > Logs;
95 
97  typedef ::fwCom::Signal< void () > StateSignal;
98 
100  typedef ::fwCom::Signal< void () > CancelRequestedSignal;
101 
103  typedef ::fwCom::Signal< void (std::uint64_t, std::uint64_t) > DoneWorkSignal;
104 
106  typedef ::fwCom::Signal< void (std::string) > LogSignal;
107 
109  typedef std::shared_future< void > SharedFuture;
117  FWJOBS_API IJob(const std::string& name = "");
118 
120  FWJOBS_API virtual ~IJob();
121 
123  FWJOBS_API std::uint64_t getDoneWorkUnits() const;
124 
126  FWJOBS_API std::uint64_t getTotalWorkUnits() const;
127 
129  FWJOBS_API State getState() const;
130 
132  FWJOBS_API const std::string& getName() const;
133 
135  FWJOBS_API Logs getLogs() const;
136 
138  FWJOBS_API bool isCancelable() const;
139 
141  FWJOBS_API void setCancelable(bool cancel);
142 
148  FWJOBS_API SharedFuture run();
149 
157  FWJOBS_API void wait();
158 
164  FWJOBS_API const bool& cancelRequested() const;
165 
171  FWJOBS_API CancelRequestCallback cancelRequestedCallback() const;
172 
179  FWJOBS_API virtual SharedFuture cancel();
180 
186  FWJOBS_API void addSimpleCancelHook(CancelHook callback);
187 
193  FWJOBS_API void addCancelHook(JobCancelHook callback);
194 
200  FWJOBS_API void addDoneWorkHook(DoneWorkHook callback);
201 
207  FWJOBS_API void addTotalWorkUnitsHook(TotalWorkUnitsHook callback);
208 
214  FWJOBS_API void addLogHook(LogHook callback);
215 
221  FWJOBS_API void addStateHook(StateHook callback);
222 
231  FWJOBS_API void log(const std::string& message);
232 
233 protected:
234 
238  IJob(IJob&);
239  IJob& operator=(IJob&);
240  IJob(IJob&&);
241  IJob& operator=(IJob&&);
244  FWJOBS_API virtual SharedFuture runImpl() = 0;
246 
248  FWJOBS_API virtual void finish();
249 
251  FWJOBS_API void finishNoLock();
252 
258  // workaround because of vs2010 issue : http://goo.gl/WHEkQ5
259  // this is fixed in vs2012 : http://goo.gl/tvO3g7
260  // TODO : remove when compiler is up to date
261  FWJOBS_API std::function< void() > finishCallback();
262 
264  FWJOBS_API State getStateNoLock() const;
265 
267  FWJOBS_API void setState( State state );
268 
270  FWJOBS_API void setStateNoLock( State state );
271 
273  FWJOBS_API void doneWork( std::uint64_t units );
274 
282  FWJOBS_API void doneWork( std::uint64_t units, ::fwCore::mt::ReadToWriteLock& lock );
283 
285  FWJOBS_API void done();
286 
292  FWJOBS_API void setTotalWorkUnits(std::uint64_t units);
293 
301  FWJOBS_API void setTotalWorkUnitsUpgradeLock( std::uint64_t units, ::fwCore::mt::ReadToWriteLock& lock );
302 
308  FWJOBS_API void addCancelHookNoLock(JobCancelHook callback);
309 
315  FWJOBS_API void addDoneWorkHookNoLock(DoneWorkHook callback);
316 
322  FWJOBS_API void addTotalWorkUnitsHookNoLock(TotalWorkUnitsHook callback);
323 
329  FWJOBS_API void addLogHookNoLock(LogHook callback);
330 
336  FWJOBS_API void addStateHookNoLock(StateHook callback);
337 
343  FWJOBS_API void logNoLock(const std::string& message);
344 
346  SPTR(CancelRequestedSignal) m_sigCancelRequested;
347 
349  SPTR(StateSignal) m_sigCanceled;
350 
352  SPTR(StateSignal) m_sigStarted;
353 
355  SPTR(StateSignal) m_sigFinished;
356 
358  SPTR(DoneWorkSignal) m_sigDoneWork;
359 
361  SPTR(LogSignal) m_sigLogged;
362 
364  mutable ::fwCore::mt::ReadWriteMutex m_mutex;
365 
367  std::string m_name;
368 
370  Logs m_logs;
371 
373  std::uint64_t m_doneWorkUnits;
374 
376  std::uint64_t m_totalWorkUnits;
377 
379  bool m_cancelRequested;
380 
382  bool m_cancelable;
383 
385  CancelHookSeq m_cancelHooks;
386 
388  DoneWorkHookSeq m_doneWorkHooks;
389 
391  TotalWorkUnitsHookSeq m_totalWorkUnitsHooks;
392 
394  LogHookSeq m_logHooks;
395 
397  StateHookSeq m_stateHooks;
398 
400  SharedFuture m_runFuture;
401 
403  State m_state;
404 };
405 
406 } //namespace fwJobs
407 
408 #endif //__FWJOBS_IJOB_HPP__
#define SPTR(_cls_)
std::vector< StateHook > StateHookSeq
Log callback sequence type for state hook.
Definition: IJob.hpp:91
std::vector< LogHook > LogHookSeq
Log callback sequence type for log hook.
Definition: IJob.hpp:88
This class is an interface for class managing job.
Definition: IJob.hpp:28
Namespace containing fw4spl communication tools.
Definition: DumpEditor.hpp:30
#define WPTR(_cls_)
std::weak_ptr< ::fwJobs::IJob > wptr
Cancel request callback type.
Definition: IJob.hpp:55
std::vector< TotalWorkUnitsHook > TotalWorkUnitsHookSeq
Work units callback sequence type for total work unit hook.
Definition: IJob.hpp:85
std::shared_future< void > SharedFuture
Future type.
Definition: IJob.hpp:109
STL namespace.
std::vector< JobCancelHook > CancelHookSeq
Job cancel callback sequence type for cancel hook.
Definition: IJob.hpp:79
This namespace fwCore provides common foundations for FW4SPL.
Definition: BaseObject.hpp:16
std::shared_ptr< ::fwJobs::IJob > sptr
Cancel request callback type.
Definition: IJob.hpp:54
State
Job&#39;s states.
Definition: IJob.hpp:42
std::function< void(IJob &, const std::string &) > LogHook
Log callback type for log hook.
Definition: IJob.hpp:64
std::function< void(State) > StateHook
State callback type for state hook.
Definition: IJob.hpp:76
std::function< void(IJob &, std::uint64_t) > TotalWorkUnitsHook
Work units callback type for total work unit hook.
Definition: IJob.hpp:70
std::function< void(IJob &) > JobCancelHook
Job cancel callback type for cancel hook.
Definition: IJob.hpp:61
std::function< void(IJob &, std::uint64_t) > DoneWorkHook
Done work callback type for done work hook.
Definition: IJob.hpp:67
std::function< bool() > CancelRequestCallback
Cancel request callback type.
Definition: IJob.hpp:58
std::vector< DoneWorkHook > DoneWorkHookSeq
Done work callback sequence type for done work hook.
Definition: IJob.hpp:82
std::function< void() > CancelHook
Cancel callback type for cancel hook.
Definition: IJob.hpp:73
std::vector< std::string > Logs
Log container type.
Definition: IJob.hpp:94
This namespace fwJobs provides jobs management.