7 #ifndef __FWCOM_SIGNAL_HXX__ 8 #define __FWCOM_SIGNAL_HXX__ 10 #ifndef __FWCOM_SIGNAL_HPP__ 11 #error fwCom/Signal.hpp not included 14 #include "fwCom/exception/AlreadyConnected.hpp" 15 #include "fwCom/exception/BadSlot.hpp" 16 #include "fwCom/Slot.hpp" 17 #include "fwCom/Slot.hxx" 18 #include "fwCom/SlotConnection.hpp" 19 #include "fwCom/SlotConnection.hxx" 20 #include "fwCom/util/remove_last_arg.hpp" 22 #include <boost/function_types/function_arity.hpp> 27 template <
typename R,
typename ... A >
30 return std::make_shared<
Signal< R (A ...) > > ();
35 template <
typename R,
typename ... A >
38 return this->connect< SignatureType >(slot);
43 template <
typename R,
typename ... A >
44 void Signal< R (A ...) >::disconnect( SlotBase::sptr slot )
48 ConnectionMapType::const_iterator iter = m_connections.find(slot);
50 if (iter != m_connections.end())
52 SlotConnectionBase::sptr connection ( iter->second.lock() );
53 SLM_ASSERT(
"Connection has been previously destroyed", connection );
57 connection->disconnectWeakLock();
69 template <
typename R,
typename ... A >
70 void Signal< R (A ...) >::disconnectAll()
74 ConnectionMapType connections = m_connections;
76 for(
const typename ConnectionMapType::value_type &conn : connections )
78 SlotConnectionBase::sptr connection( conn.second.lock() );
82 connection->disconnectWeakLock();
89 template <
typename R,
typename ... A >
90 void Signal< R (A ...) >::emit( A ...
a )
const 93 typename SlotContainerType::const_iterator iter;
94 typename SlotContainerType::const_iterator end = m_slots.end();
95 for ( iter = m_slots.begin(); iter != end; ++iter )
99 (*iter)->second->run(a ...);
106 template <
typename R,
typename ... A >
107 void Signal< R (A ...) >::asyncEmit( A ...
a )
const 110 typename SlotContainerType::const_iterator iter;
111 typename SlotContainerType::const_iterator end = m_slots.end();
112 for ( iter = m_slots.begin(); iter != end; ++iter )
116 (*iter)->second->asyncRun(a ...);
123 template <
typename R,
typename ... A >
124 template<
typename FROM_F >
130 if(m_connections.find( slot ) != m_connections.end())
139 unsigned int sigArity = ::boost::function_types::function_arity< SignatureType >::value;
140 if ( sigArity == slot->arity() )
142 SlotSptr slotToConnect = std::dynamic_pointer_cast<
SlotRunType >(slot);
146 typename Signal< R( A ... ) >::sptr sig =
147 std::dynamic_pointer_cast <
Signal< R( A ... ) > > ( this->shared_from_this() );
148 typename ConnectionType::sptr slotConnection = ConnectionType::New( sig, slotToConnect);
149 slot->m_connections.insert(slotConnection);
150 m_connections.insert(
typename ConnectionMapType::value_type( slot, slotConnection ) );
151 slotConnection->connectNoLock();
159 else if ( sigArity > slot->arity() )
163 typename SPTR(WrappedSlotRunType) wrappedSlot = std::dynamic_pointer_cast< WrappedSlotRunType >(slot);
168 SlotSptr slotToConnect =
Slot <
Slot < void (A ...) > >::New(wrappedSlot);
169 typename Signal< R( A ... ) >::sptr sig =
170 std::dynamic_pointer_cast <
Signal< R( A ... ) > > ( this->shared_from_this() );
171 typename ConnectionType::sptr slotConnection = ConnectionType::New( sig, slot, slotToConnect );
172 slot->m_connections.insert(slotConnection);
173 m_connections.insert(
typename ConnectionMapType::value_type( slot, slotConnection ) );
174 slotConnection->connectNoLock();
179 connection = this->connect< typename ::fwCom::util::remove_last_arg< FROM_F >::type >( slot );
193 template <
typename R,
typename ... A >
199 ConnectionMapType::const_iterator iter = m_connections.find(slot);
201 if (iter == m_connections.end() )
210 SlotConnectionBase::sptr slotConnection (iter->second);
::boost::upgrade_lock< ReadWriteMutex > ReadToWriteLock
Defines an upgradable lock type for read/write mutex.
::boost::upgrade_to_unique_lock< ReadWriteMutex > UpgradeToWriteLock
Defines a write lock upgraded from ReadToWriteLock.
Namespace containing fw4spl communication tools.
::boost::unique_lock< ReadWriteMutex > WriteLock
Defines a lock of write type for read/write mutex.
Class managing Signal-Slot connections.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
::boost::shared_lock< ReadWriteMutex > ReadLock
Defines a lock of read type for read/write mutex.