fw4spl
SlotRun.hxx
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 #ifndef __FWCOM_SLOTRUN_HXX__
7 #define __FWCOM_SLOTRUN_HXX__
8 
9 #ifndef __FWCOM_SLOTRUN_HPP__
10 #error fwCom/SlotRun.hpp not included
11 #endif
12 
13 #include "fwCom/exception/NoWorker.hpp"
14 #include "fwCom/SlotBase.hxx"
15 #include "fwCom/util/WeakCall.hpp"
16 
17 #include <fwCore/mt/types.hpp>
18 
19 #include <fwThread/TaskHandler.hpp>
20 #include <fwThread/Worker.hpp>
21 
22 #include <future>
23 
24 namespace fwCom
25 {
26 
27 //------------------------------------------------------------------------------
28 
29 template< typename ... A >
30 inline std::function< void() > SlotRun< void (A ...) >::bindRun( A ... args ) const
31 {
32  return std::bind( ( void (SelfType::*)( A ... ) const ) &SelfType::run, this, args ... );
33 }
34 
35 //-----------------------------------------------------------------------------
36 
37 template< typename ... A >
38 inline SlotBase::VoidSharedFutureType SlotRun< void (A ...) >::asyncRun(
39  const ::fwThread::Worker::sptr& worker, A ... args ) const
40 {
41  if(!worker)
42  {
43  FW_RAISE_EXCEPTION( ::fwCom::exception::NoWorker("No valid worker.") );
44  }
45 
46  return postWeakCall< void >(
47  worker,
48  ::fwCom::util::weakcall(
49  std::dynamic_pointer_cast< const SlotBase >(this->shared_from_this()),
50  this->bindRun( args ... )
51  )
52  );
53 }
54 
55 //-----------------------------------------------------------------------------
56 
57 template< typename ... A >
58 inline SlotBase::VoidSharedFutureType SlotRun< void (A ...) >::asyncRun(A ... args) const
59 {
60  ::fwCore::mt::ReadLock lock(this->m_workerMutex);
61 
62  if(!this->m_worker)
63  {
64  FW_RAISE_EXCEPTION( ::fwCom::exception::NoWorker("Slot has no worker set.") );
65  }
66 
67  return postWeakCall< void >(
68  m_worker,
69  ::fwCom::util::weakcall(
70  std::dynamic_pointer_cast< const SlotBase >(this->shared_from_this()),
71  this->bindRun( args ... ),
72  this->m_worker
73  )
74  );
75 }
76 
77 //-----------------------------------------------------------------------------
78 
79 // Copied from fwThread::Worker because of issues with gcc 4.2 and template
80 // keyword
81 template< typename ... A >
82 template< typename R, typename WEAKCALL >
83 std::shared_future< R > SlotRun< void (A ...) >::postWeakCall( const ::fwThread::Worker::sptr& worker, WEAKCALL f )
84 {
85  std::packaged_task< R() > task( f );
86  std::future< R > ufuture = task.get_future();
87 
88  std::function< void() > ftask = ::fwThread::moveTaskIntoFunction(task);
89 
90  worker->post(ftask);
91 
92  return std::move(ufuture);
93 }
94 
95 //-----------------------------------------------------------------------------
96 
97 } // namespace fwCom
98 
99 #endif /* __FWCOM_SLOTRUN_HXX__ */
100 
Namespace containing fw4spl communication tools.
Definition: DumpEditor.hpp:30
No worker exception.
Definition: NoWorker.hpp:19
::boost::shared_lock< ReadWriteMutex > ReadLock
Defines a lock of read type for read/write mutex.
std::shared_future< void > VoidSharedFutureType
SlotBase::asyncRun return type.
Definition: SlotBase.hpp:62