fw4spl
Signal.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_SIGNAL_HPP__
8 #define __FWCOM_SIGNAL_HPP__
9 
10 #include "fwCom/SignalBase.hpp"
11 #include <fwCom/SlotConnection.hpp>
12 
13 #include <fwCore/mt/types.hpp>
14 
15 #include <list>
16 #include <map>
17 #include <type_traits>
18 #include <utility>
19 
20 namespace fwCom
21 {
22 
23 struct SlotConnectionBase;
24 struct SlotBase;
25 
26 template < typename F >
27 struct Signal;
28 
29 template < typename F >
30 struct SlotRun;
31 
36 template < typename R, typename ... A >
37 struct Signal< R(A ...) > : SignalBase
38 {
42  typedef R SignatureType (A ...);
43 
45 
46  typedef SPTR ( SelfType ) sptr;
47  typedef WPTR ( SelfType ) wptr;
48 
50  typedef SPTR ( SlotRunType ) SlotSptr;
51 
52  typedef std::pair< bool, SlotRunType* > PairType;
53  typedef std::list< PairType* > SlotContainerType;
54 
55  typedef std::map< WPTR( SlotBase ), WPTR( SlotConnectionBase ),
56  std::owner_less< WPTR( SlotBase ) > > ConnectionMapType;
59  static sptr New();
61 
64  {
65  this->disconnectAll();
66  }
67 
75  Connection connect( SPTR( SlotBase ) slot );
76 
81  void disconnect( SPTR( SlotBase ) slot );
82 
84  void disconnectAll();
85 
87  void emit( A ... a ) const;
88 
90  void asyncEmit( A ... a ) const;
91 
93  size_t getNumberOfConnections() const
94  {
95  ::fwCore::mt::ReadLock lock(m_connectionsMutex);
96  return m_slots.size();
97  }
98 
103  Connection getConnection( SPTR( SlotBase ) slot, bool throws = false );
104 
105  protected:
106 
107  template < typename F >
108  friend struct SlotConnection;
118  template< typename FROM_F >
119  Connection connect( SPTR( SlotBase ) slot );
120 
122  SlotContainerType m_slots;
123 
125  ConnectionMapType m_connections;
126 
127  mutable ::fwCore::mt::ReadWriteMutex m_connectionsMutex;
128 
129  private:
130  BOOST_STATIC_ASSERT( (std::is_same<void, R>::value) );
131 
132 };
133 
134 } // namespace fwCom
135 
136 #endif /* __FWCOM_SIGNAL_HPP__ */
137 
#define SPTR(_cls_)
Namespace containing fw4spl communication tools.
Definition: DumpEditor.hpp:30
#define WPTR(_cls_)
SlotContainerType m_slots
Connected slots.
Definition: Signal.hpp:122
Class managing Signal-Slot connections.
Definition: Connection.hpp:17
Base class for Slot connection implementation. This class is for internal use purpose.
size_t getNumberOfConnections() const
Returns number of connected slots.
Definition: Signal.hpp:93
ConnectionMapType m_connections
Container of current connections.
Definition: Signal.hpp:125
::boost::shared_lock< ReadWriteMutex > ReadLock
Defines a lock of read type for read/write mutex.
~Signal()
Destructor : disconnects all remaining connections.
Definition: Signal.hpp:63
Signal base class.
Definition: SignalBase.hpp:23
Base class for Slot implementations.
Definition: SlotBase.hpp:46