fw4spl
Observer.cpp
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 #include "fwJobs/Observer.hpp"
8 
9 namespace fwJobs
10 {
11 
12 //------------------------------------------------------------------------------
13 
14 Observer::sptr Observer::New(const std::string& name, std::uint64_t workUnits)
15 {
16  return std::make_shared<Observer>( name, workUnits );
17 }
18 
19 //------------------------------------------------------------------------------
20 
21 Observer::Observer(const std::string& name, std::uint64_t workUnits) :
22  IJob(name)
23 {
24  m_finishTask = PackagedTask([this]()
25  {
27  this->finishNoLock();
28  });
29  m_totalWorkUnits = workUnits;
30 
31  this->addSimpleCancelHook( [this]()
32  {
33  this->finish();
34  } );
35  this->run();
36 }
37 
38 //------------------------------------------------------------------------------
39 
41 {
42  return [this](std::uint64_t doneWork)
43  {
44  this->doneWork(doneWork);
45  };
46 }
47 
48 //------------------------------------------------------------------------------
49 
51 {
53  if( m_state == RUNNING || m_state == CANCELING )
54  {
55  lock.unlock();
56  m_finishTask();
57  }
58 }
59 
60 //------------------------------------------------------------------------------
61 
63 {
65  return m_finishTask.get_future();
66 }
67 
68 } //namespace fwJobs
static FWJOBS_API sptr New(const std::string &name, std::uint64_t workUnits=100)
Create a new Observer sptr with a name.
Definition: Observer.cpp:14
std::uint64_t m_totalWorkUnits
Number of work units to reach to complete the job.
Definition: IJob.hpp:376
std::function< void(std::uint64_t) > ProgressCallback
Progress callback type.
Definition: Observer.hpp:32
virtual FWJOBS_API SharedFuture runImpl()
Runs the task.
Definition: Observer.cpp:62
State m_state
Job&#39;s state.
Definition: IJob.hpp:403
FWJOBS_API void doneWork(std::uint64_t units)
Setter on done work units.
Definition: IJob.cpp:379
This class is an interface for class managing job.
Definition: IJob.hpp:28
std::shared_future< void > SharedFuture
Future type.
Definition: IJob.hpp:109
::boost::unique_lock< ReadWriteMutex > WriteLock
Defines a lock of write type for read/write mutex.
PackagedTask m_finishTask
Task observed.
Definition: Observer.hpp:88
std::packaged_task< void() > PackagedTask
Task type.
Definition: Observer.hpp:85
FWJOBS_API void finish()
Call for finishing observer progress.
Definition: Observer.cpp:50
FWJOBS_API Observer(const std::string &name, std::uint64_t workUnits=100)
Constructor.
Definition: Observer.cpp:21
FWJOBS_API void addSimpleCancelHook(CancelHook callback)
Add cancel callback to sequence for cancel hook.
Definition: IJob.cpp:142
mutable::fwCore::mt::ReadWriteMutex m_mutex
Mutex to protect object access.
Definition: IJob.hpp:364
::boost::shared_lock< ReadWriteMutex > ReadLock
Defines a lock of read type for read/write mutex.
FWJOBS_API void finishNoLock()
Finish the job without mutex lock: set the state to finished or canceled.
Definition: IJob.cpp:271
std::shared_ptr< ::fwJobs::Observer > sptr
Progress callback type.
Definition: Observer.hpp:28
FWJOBS_API SharedFuture run()
Run the current job.
Definition: IJob.cpp:199
This namespace fwJobs provides jobs management.
FWJOBS_API ProgressCallback progressCallback()
return a progress Callback function. This function takes work units in parameter. ...
Definition: Observer.cpp:40