fw4spl
SlotCall.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_SLOTCALL_HXX__
7 #define __FWCOM_SLOTCALL_HXX__
8 
9 #ifndef __FWCOM_SLOTCALL_HPP__
10 #error fwCom/SlotCall.hpp not included
11 #endif
12 
13 #include "fwCom/exception/NoWorker.hpp"
14 #include "fwCom/SlotRun.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 R, typename ... A >
30 std::function< R() > SlotCall< R(A ...) >::bindCall( A ... args ) const
31 {
32  return std::bind( ( R(SlotCall< R(A ...) >::*)( A ... ) const ) &SlotCall< R(A ...) >::call, this,
33  args ... );
34 }
35 
36 //-----------------------------------------------------------------------------
37 
38 template< typename R, typename ... A >
39 typename SlotCall< R(A ...) >::SharedFutureType SlotCall< R(A ...) >::asyncCall(
40  const ::fwThread::Worker::sptr& worker, A ... args ) const
41 {
42  if(!worker)
43  {
44  FW_RAISE_EXCEPTION( ::fwCom::exception::NoWorker("No valid worker.") );
45  }
46 
47  return postWeakCall(
48  worker,
50  std::dynamic_pointer_cast< const SlotBase >(this->shared_from_this()),
51  this->bindCall( args ... )
52  )
53  );
54 }
55 
56 //-----------------------------------------------------------------------------
57 
58 template< typename R, typename ... A >
59 typename SlotCall< R(A ...) >::SharedFutureType SlotCall< R(A ...) >::asyncCall(A ... args) const
60 {
61  ::fwCore::mt::ReadLock lock(this->m_workerMutex);
62 
63  if(!this->m_worker)
64  {
65  FW_RAISE_EXCEPTION( ::fwCom::exception::NoWorker("Slot has no worker set.") );
66  }
67 
68  return postWeakCall(
69  this->m_worker,
71  std::dynamic_pointer_cast< const SlotBase >(this->shared_from_this()),
72  this->bindCall( args ... ),
73  this->m_worker
74  )
75  );
76 }
77 
78 //-----------------------------------------------------------------------------
79 
80 template< typename R, typename ... A >
81 template< typename WEAKCALL >
82 std::shared_future< R > SlotCall< R(A ...) >::postWeakCall( const ::fwThread::Worker::sptr& worker, WEAKCALL f )
83 {
84  std::packaged_task< R() > task( f );
85  std::future< R > ufuture = task.get_future();
86 
87  std::function< void() > ftask = ::fwThread::moveTaskIntoFunction(task);
88 
89  worker->post(ftask);
90 
91  return std::move(ufuture);
92 }
93 
94 } // namespace fwCom
95 
96 #endif /* __FWCOM_SLOTCALL_HXX__ */
97 
Namespace containing fw4spl communication tools.
Definition: DumpEditor.hpp:30
WeakCall< T, R > weakcall(const std::shared_ptr< T const > &ptr, std::function< R() > f)
Returns weak call from given object and function.
Definition: WeakCall.hpp:94
No worker exception.
Definition: NoWorker.hpp:19
::boost::shared_lock< ReadWriteMutex > ReadLock
Defines a lock of read type for read/write mutex.