fw4spl
SlotBase.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_SLOTBASE_HXX__
7 #define __FWCOM_SLOTBASE_HXX__
8 
9 #ifndef __FWCOM_SLOTBASE_HPP__
10 #error fwCom/SlotBase.hpp not included
11 #endif
12 
13 #include "fwCom/exception/BadCall.hpp"
14 #include "fwCom/Slot.hpp"
15 #include "fwCom/Slot.hxx"
16 #include "fwCom/SlotRun.hpp"
17 #include "fwCom/SlotRun.hxx"
18 
19 #include "fwCore/exceptionmacros.hpp"
20 
21 #include <boost/function_types/function_arity.hpp>
22 
23 #include <functional>
24 
25 namespace fwCom
26 {
27 
28 //------------------------------------------------------------------------------
29 
30 template< typename A1, typename A2, typename A3 >
31 void SlotBase::run(A1 a1, A2 a2, A3 a3) const
32 {
33  typedef SlotRun< void (A1, A2, A3) > SlotFuncType;
34  const SlotFuncType* fun = dynamic_cast< const SlotFuncType* >(this);
35  if(fun)
36  {
37  fun->run(a1, a2, a3);
38  }
39  else
40  {
41  OSLM_ERROR( "failed to run : " << m_signature << " with " << SlotBase::getTypeName< void(A1, A2, A3) >() );
42  this->run(a1, a2);
43  }
44 }
45 
46 //-----------------------------------------------------------------------------
47 
48 template< typename A1, typename A2 >
49 void SlotBase::run(A1 a1, A2 a2) const
50 {
51  typedef SlotRun< void (A1, A2) > SlotFuncType;
52  const SlotFuncType* fun = dynamic_cast< const SlotFuncType* >(this);
53  if(fun)
54  {
55  fun->run(a1, a2);
56  }
57  else
58  {
59  OSLM_ERROR( "failed to run : " << m_signature << " with " << SlotBase::getTypeName< void(A1, A2) >() );
60  this->run(a1);
61  }
62 }
63 
64 //-----------------------------------------------------------------------------
65 
66 template< typename A1 >
67 void SlotBase::run(A1 a1) const
68 {
69  typedef SlotRun< void (A1) > SlotFuncType;
70  const SlotFuncType* fun = dynamic_cast< const SlotFuncType* >(this);
71  if(fun)
72  {
73  return fun->run(a1);
74  }
75  else
76  {
77  OSLM_ERROR( "failed to run : " << m_signature << " with " << SlotBase::getTypeName< void(A1) >() );
78  return this->run();
79  }
80 }
81 
82 //------------------------------------------------------------------------------
83 
84 template< typename R, typename A1, typename A2, typename A3 >
85 R SlotBase::call(A1 a1, A2 a2, A3 a3) const
86 {
87  typedef Slot< R(A1, A2, A3) > SlotFuncType;
88  const SlotFuncType* fun = dynamic_cast< const SlotFuncType* >(this);
89  if(fun)
90  {
91  return fun->call(a1, a2, a3);
92  }
93  else
94  {
95  OSLM_ERROR( "failed to call : " << m_signature << " with " << SlotBase::getTypeName< R(A1, A2, A3) >() );
96  return this->call<R>(a1, a2);
97  }
98 }
99 
100 //-----------------------------------------------------------------------------
101 
102 template< typename R, typename A1, typename A2 >
103 R SlotBase::call(A1 a1, A2 a2) const
104 {
105  typedef Slot< R(A1, A2) > SlotFuncType;
106  const SlotFuncType* fun = dynamic_cast< const SlotFuncType* >(this);
107  if(fun)
108  {
109  return fun->call(a1, a2);
110  }
111  else
112  {
113  OSLM_ERROR( "failed to call : " << m_signature << " with " << SlotBase::getTypeName< R(A1, A2) >() );
114  return this->call<R>(a1);
115  }
116 }
117 
118 //-----------------------------------------------------------------------------
119 
120 template< typename R, typename A1 >
121 R SlotBase::call(A1 a1) const
122 {
123  typedef Slot< R(A1) > SlotFuncType;
124  const SlotFuncType* fun = dynamic_cast< const SlotFuncType* >(this);
125  if(fun)
126  {
127  return fun->call(a1);
128  }
129  else
130  {
131  OSLM_ERROR( "failed to call : " << m_signature << " with " << SlotBase::getTypeName< R(A1) >() );
132  return this->call<R>();
133  }
134 }
135 
136 //-----------------------------------------------------------------------------
137 
138 template< typename R >
139 R SlotBase::call() const
140 {
141  typedef Slot< R() > SlotFuncType;
142  const SlotFuncType* fun = dynamic_cast< const SlotFuncType* >(this);
143  if(fun)
144  {
145  return fun->call();
146  }
147  OSLM_ERROR( "failed to call : " + m_signature + " with " + SlotBase::getTypeName< R() >() );
148  FW_RAISE_EXCEPTION( ::fwCom::exception::BadCall( "Failed to find right signature for call" ) );
149 }
150 
151 //------------------------------------------------------------------------------
152 
153 template< typename A1, typename A2, typename A3 >
154 SlotBase::VoidSharedFutureType SlotBase::asyncRun(A1 a1, A2 a2, A3 a3) const
155 {
156  typedef SlotRun< void (A1, A2, A3) > SlotFuncType;
157  const SlotFuncType* fun = dynamic_cast< const SlotFuncType* >(this);
158  if(fun)
159  {
160  return fun->asyncRun(a1, a2, a3);
161  }
162  else
163  {
164  OSLM_ERROR( "failed to asyncRun : " << m_signature << " with " <<
165  SlotBase::getTypeName< void(A1, A2, A3) >() );
166  return this->asyncRun(a1, a2);
167  }
168 }
169 
170 //-----------------------------------------------------------------------------
171 
172 template< typename A1, typename A2 >
173 SlotBase::VoidSharedFutureType SlotBase::asyncRun(A1 a1, A2 a2) const
174 {
175  typedef SlotRun< void (A1, A2) > SlotFuncType;
176  const SlotFuncType* fun = dynamic_cast< const SlotFuncType* >(this);
177  if(fun)
178  {
179  return fun->asyncRun(a1, a2);
180  }
181  else
182  {
183  OSLM_ERROR( "failed to asyncRun : " << m_signature << " with " << SlotBase::getTypeName< void(A1, A2) >() );
184  return this->asyncRun(a1);
185  }
186 }
187 
188 //-----------------------------------------------------------------------------
189 
190 template< typename A1 >
191 SlotBase::VoidSharedFutureType SlotBase::asyncRun(A1 a1) const
192 {
193  typedef SlotRun< void (A1) > SlotFuncType;
194  const SlotFuncType* fun = dynamic_cast< const SlotFuncType* >(this);
195  if(fun)
196  {
197  return fun->asyncRun(a1);
198  }
199  else
200  {
201  OSLM_ERROR( "failed to asyncRun : " << m_signature << " with " << SlotBase::getTypeName< void(A1) >() );
202  return this->asyncRun();
203  }
204 }
205 
206 //------------------------------------------------------------------------------
207 
208 template< typename R, typename A1, typename A2, typename A3 >
209 std::shared_future< R > SlotBase::asyncCall(A1 a1, A2 a2, A3 a3) const
210 {
211  typedef Slot< R(A1, A2, A3) > SlotFuncType;
212  const SlotFuncType* fun = dynamic_cast< const SlotFuncType* >(this);
213  if(fun)
214  {
215  return fun->asyncCall(a1, a2, a3);
216  }
217  else
218  {
219  OSLM_ERROR( "failed to asyncCall : " << m_signature << " with " << SlotBase::getTypeName< R(A1, A2, A3) >() );
220  return this->asyncCall<R>(a1, a2);
221  }
222 }
223 
224 //-----------------------------------------------------------------------------
225 
226 template< typename R, typename A1, typename A2 >
227 std::shared_future< R > SlotBase::asyncCall(A1 a1, A2 a2) const
228 {
229  typedef Slot< R(A1, A2) > SlotFuncType;
230  const SlotFuncType* fun = dynamic_cast< const SlotFuncType* >(this);
231  if(fun)
232  {
233  return fun->asyncCall(a1, a2);
234  }
235  else
236  {
237  OSLM_ERROR( "failed to asyncCall : " << m_signature << " with " << SlotBase::getTypeName< R(A1, A2) >() );
238  return this->asyncCall<R>(a1);
239  }
240 }
241 
242 //-----------------------------------------------------------------------------
243 
244 template< typename R, typename A1 >
245 std::shared_future< R > SlotBase::asyncCall(A1 a1) const
246 {
247  typedef Slot< R(A1) > SlotFuncType;
248  const SlotFuncType* fun = dynamic_cast< const SlotFuncType* >(this);
249  if(fun)
250  {
251  return fun->asyncCall(a1);
252  }
253  else
254  {
255  OSLM_ERROR( "failed to asyncCall : " << m_signature << " with " << SlotBase::getTypeName< R(A1) >() );
256  return this->asyncCall<R>();
257  }
258 }
259 
260 //-----------------------------------------------------------------------------
261 
262 template< typename R >
263 std::shared_future< R > SlotBase::asyncCall() const
264 {
265  typedef Slot< R() > SlotFuncType;
266  const SlotFuncType* fun = dynamic_cast< const SlotFuncType* >(this);
267  if(fun)
268  {
269  return fun->asyncCall();
270  }
271  else
272  {
273  OSLM_ERROR( "failed to asyncCall : " + m_signature + " with " + SlotBase::getTypeName< R() >() );
274  FW_RAISE_EXCEPTION( ::fwCom::exception::BadCall( "Failed to find right signature for asyncCall" ) );
275  }
276 }
277 
278 //-----------------------------------------------------------------------------
279 
280 } // namespace fwCom
281 
282 #endif /* __FWCOM_SLOTBASE_HXX__ */
283 
Namespace containing fw4spl communication tools.
Definition: DumpEditor.hpp:30
std::string getTypeName() const
Returns F typeid name.
Definition: SlotBase.hpp:205
#define OSLM_ERROR(message)
Definition: spyLog.hpp:274
Bad call exception.
Definition: BadCall.hpp:20
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