fw4spl
SigSlotConnection.cpp
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 #include "fwCom/helper/SigSlotConnection.hpp"
8 
9 #include <fwCom/SignalBase.hpp>
10 #include <fwCom/exception/AlreadyConnected.hpp>
11 #include <fwCom/exception/BadSlot.hpp>
12 
13 #include <fwCore/spyLog.hpp>
14 
15 #include <fwTools/Object.hpp>
16 
17 #include <boost/foreach.hpp>
18 
19 namespace fwCom
20 {
21 namespace helper
22 {
23 
24 //-----------------------------------------------------------------------------
25 
27 {
28 }
29 
30 //-----------------------------------------------------------------------------
31 
33 {
34  this->disconnect();
35 }
36 
37 //-----------------------------------------------------------------------------
38 
39 void SigSlotConnection::connect(const ::fwCom::HasSignals::csptr& hasSignals, ::fwCom::Signals::SignalKeyType signalKey,
40  const ::fwCom::HasSlots::csptr& hasSlots, ::fwCom::Slots::SlotKeyType slotKey )
41 {
42  try
43  {
44  ::fwCom::Connection connection = hasSignals->signal( signalKey )->connect( hasSlots->slot( slotKey ) );
45  m_connections.push_back(connection);
46  }
47  catch (::fwCom::exception::BadSlot& e)
48  {
49  OSLM_ERROR("Can't connect signal '" + signalKey + "' with slot '" + slotKey + "' : " << e.what() << ".");
50  }
52  {
53  const ::fwTools::Object::csptr source = ::fwTools::Object::dynamicCast(hasSignals);
54  auto sourceID = source ? source->getID() : "";
55 
56  const ::fwTools::Object::csptr target = ::fwTools::Object::dynamicCast(hasSlots);
57  auto targetID = target ? target->getID() : "";
58 
59  OSLM_ERROR("Can't connect signal '" + sourceID + "/" + signalKey + "' with slot '"
60  + targetID + "/" + slotKey + "' : " << e.what() << ".");
61  }
62 }
63 
64 //-----------------------------------------------------------------------------
65 
66 void SigSlotConnection::connect(const ::fwCom::HasSignals::csptr& hasSignals,
67  const ::fwCom::HasSlots::csptr& hasSlots,
68  const KeyConnectionsType& keyConnections )
69 {
70  SLM_ASSERT("Signal source is NULL", hasSignals);
71  SLM_ASSERT("Slot destination is NULL", hasSlots);
72 
73  for( const KeyConnectionType& keys : keyConnections )
74  {
75  auto signal = hasSignals->signal( keys.first );
76  SLM_ASSERT("Signal '" + keys.first + "' not found.", signal);
77  auto slot = hasSlots->slot( keys.second );
78  SLM_ASSERT("Slot '" + keys.second + "' not found.", slot);
79 
80  try
81  {
82  ::fwCom::Connection connection = signal->connect( slot );
83  m_connections.push_back(connection);
84  }
85  catch (::fwCom::exception::BadSlot& e)
86  {
87  OSLM_ERROR("Can't connect signal '" + keys.first + "' with slot '" + keys.second + "' : "
88  << e.what() << ".");
89  }
91  {
92  const ::fwTools::Object::csptr source = ::fwTools::Object::dynamicCast(hasSignals);
93  auto sourceID = source ? source->getID() : "";
94 
95  const ::fwTools::Object::csptr target = ::fwTools::Object::dynamicCast(hasSlots);
96  auto targetID = target ? target->getID() : "";
97 
98  OSLM_ERROR("Can't connect signal '" + sourceID + "/" + keys.first + "' with slot '"
99  + targetID + "/" + keys.second + "' : " << e.what() << ".");
100  }
101  }
102 }
103 
104 //-----------------------------------------------------------------------------
105 
107 {
108  m_connections.push_back(connection);
109 }
110 
111 //-----------------------------------------------------------------------------
112 
114 {
115  BOOST_REVERSE_FOREACH( ::fwCom::Connection& connection, m_connections )
116  {
117  connection.disconnect();
118  }
119  m_connections.clear();
120 }
121 
122 //-----------------------------------------------------------------------------
123 
124 } // end namespace helper
125 } // end namespace fwCom
ConnectionContainerType m_connections
Connections storage.
FWCOM_API SigSlotConnection()
Constructor, do nothing.
FWCOM_API void disconnect()
Disconnect all registered connections and clear m_connections.
Namespace containing fw4spl communication tools.
Definition: DumpEditor.hpp:30
FWCOM_API void addConnection(::fwCom::Connection connection)
Add a connection.
virtual FWCOM_API ~SigSlotConnection()
Destructor, call disconnect()
Class managing Signal-Slot connections.
Definition: Connection.hpp:17
Bad slot exception.
Definition: BadSlot.hpp:20
#define OSLM_ERROR(message)
Definition: spyLog.hpp:274
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
FWCOM_API void connect(const ::fwCom::HasSignals::csptr &hasSignals,::fwCom::Signals::SignalKeyType signalKey, const ::fwCom::HasSlots::csptr &hasSlots,::fwCom::Slots::SlotKeyType slotKey)
Connect signal to slot, and register this new connection in m_connections.
void disconnect()
Disconnect related Connection.
Definition: Connection.hpp:50
This file defines SpyLog macros. These macros are used to log messages to a file or to the console du...