fw4spl
SlotBase.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 __FWCOM_SLOTBASE_HPP__
8 #define __FWCOM_SLOTBASE_HPP__
9 
10 #include "fwCom/config.hpp"
11 #include "fwCom/util/convert_function_type.hpp"
12 
13 #include <fwCore/BaseObject.hpp>
14 #include <fwCore/mt/types.hpp>
15 #include <fwCore/spyLog.hpp>
16 
17 #include <future>
18 #include <queue>
19 #include <set>
20 
21 namespace fwThread
22 {
23 class Worker;
24 }
25 
26 namespace fwCom
27 {
28 
29 namespace util
30 {
31 template< typename T, typename R >
32 struct WeakCall;
33 }
34 
35 template< typename F >
36 struct SlotRun;
37 
38 template< typename F >
39 class Slot;
40 
41 struct SlotConnectionBase;
42 
46 struct FWCOM_CLASS_API SlotBase : virtual fwCore::BaseObject
47 {
48 
53  typedef SPTR ( SlotBase ) sptr;
54  typedef WPTR ( SlotBase ) wptr;
55  typedef SPTR ( SlotBase const ) csptr;
56  typedef WPTR ( SlotBase const ) cwptr;
57 
58  typedef std::string IDType;
61  typedef std::shared_future< void > VoidSharedFutureType;
63 
65  typedef std::set< CSPTR( SlotConnectionBase ) > ConnectionSetType;
66 
67  virtual ~SlotBase()
68  {
69  }
70 
75  unsigned int arity() const
76  {
77  return m_arity;
78  }
79 
81  void setWorker(const SPTR(::fwThread::Worker)& worker)
82  {
83  ::fwCore::mt::WriteLock lock(m_workerMutex);
84 
85  m_worker = worker;
86  }
87 
89  SPTR(::fwThread::Worker) getWorker() const
90  {
91  ::fwCore::mt::ReadLock lock(m_workerMutex);
92  return m_worker;
93  }
94 
102  template< typename A1, typename A2, typename A3 >
103  void run(A1 a1, A2 a2, A3 a3) const;
104 
105  template< typename A1, typename A2 >
106  void run(A1 a1, A2 a2) const;
107 
108  template< typename A1 >
109  void run(A1 a1) const;
110 
111  FWCOM_API void run() const;
121  template< typename R, typename A1, typename A2, typename A3 >
122  R call(A1 a1, A2 a2, A3 a3) const;
123 
124  template< typename R, typename A1, typename A2 >
125  R call(A1 a1, A2 a2) const;
126 
127  template< typename R, typename A1 >
128  R call(A1 a1) const;
129 
130  template< typename R >
131  R call() const;
142  template< typename A1, typename A2, typename A3 >
143  VoidSharedFutureType asyncRun(A1 a1, A2 a2, A3 a3) const;
144 
145  template< typename A1, typename A2 >
146  VoidSharedFutureType asyncRun(A1 a1, A2 a2) const;
147 
148  template< typename A1 >
149  VoidSharedFutureType asyncRun(A1 a1) const;
150 
151  FWCOM_API VoidSharedFutureType asyncRun() const;
162  template< typename R, typename A1, typename A2, typename A3 >
163  std::shared_future< R > asyncCall(A1 a1, A2 a2, A3 a3) const;
164 
165  template< typename R, typename A1, typename A2 >
166  std::shared_future< R > asyncCall(A1 a1, A2 a2) const;
167 
168  template< typename R, typename A1 >
169  std::shared_future< R > asyncCall(A1 a1) const;
170 
171  template< typename R >
172  std::shared_future< R > asyncCall() const;
175  size_t getNumberOfConnections() const
177  {
178  ::fwCore::mt::ReadLock lock(m_connectionsMutex);
179  return m_connections.size();
180  }
181  protected:
182 
184  SlotBase( const SlotBase& );
185 
187  SlotBase& operator=( const SlotBase& );
188 
192  template < typename F >
193  friend struct SlotConnection;
194 
195  template < typename F >
196  friend struct Signal;
197 
198  template< typename T, typename R >
199  friend struct util::WeakCall;
200 
203  template < typename F >
205  std::string getTypeName() const
206  {
207  std::string signature = std::string("function_type(") + typeid(F).name() + ")";
208  return signature;
209  }
210 
211  SlotBase(unsigned int arity) :
212  m_arity(arity)
213  {
214  }
215 
217  std::string m_signature;
218 
220  const unsigned int m_arity;
221 
223  SPTR(::fwThread::Worker) m_worker;
224 
226  ConnectionSetType m_connections;
227 
228  mutable ::fwCore::mt::ReadWriteMutex m_connectionsMutex;
229  mutable ::fwCore::mt::ReadWriteMutex m_workerMutex;
230 };
231 
232 } // namespace fwCom
233 
234 #endif /* __FWCOM_SLOTBASE_HPP__ */
235 
#define SPTR(_cls_)
Base class for all FW4SPL&#39;s classes.
Definition: BaseObject.hpp:22
unsigned int arity() const
Returns Slot&#39;s arity. The arity defines the number of parameters in Slot signature.
Definition: SlotBase.hpp:75
Namespace containing fw4spl communication tools.
Definition: DumpEditor.hpp:30
#define WPTR(_cls_)
This namespace fwCore provides common foundations for FW4SPL.
Definition: BaseObject.hpp:16
::boost::unique_lock< ReadWriteMutex > WriteLock
Defines a lock of write type for read/write mutex.
Base class for Slot connection implementation. This class is for internal use purpose.
std::string getTypeName() const
Returns F typeid name.
Definition: SlotBase.hpp:205
void setWorker(const std::shared_ptr< ::fwThread::Worker > &worker)
Sets Slot&#39;s Worker.
Definition: SlotBase.hpp:81
::boost::shared_lock< ReadWriteMutex > ReadLock
Defines a lock of read type for read/write mutex.
std::set< std::shared_ptr< const SlotConnectionBase > > ConnectionSetType
Connections container type.
Definition: SlotBase.hpp:65
Weak functor class. Store a zero-arg-function and a weak pointer on an object. The WeakCall is callab...
Definition: SlotBase.hpp:32
Base class for Slot implementations.
Definition: SlotBase.hpp:46
This class creates and manages a task loop. The default implementation create a loop in a new thread...
Definition: Worker.hpp:32
This namespace fwThread provides few tools to execute asynchronous tasks on different threads...
const unsigned int m_arity
Slot&#39;s arity.
Definition: SlotBase.hpp:220
This file defines SpyLog macros. These macros are used to log messages to a file or to the console du...
std::shared_future< void > VoidSharedFutureType
SlotBase::asyncRun return type.
Definition: SlotBase.hpp:62
std::string m_signature
Slot&#39;s signature based on typeid.
Definition: SlotBase.hpp:217