fw4spl
Slot.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 
7 #ifndef __FWCOM_SLOT_HXX__
8 #define __FWCOM_SLOT_HXX__
9 
10 #ifndef __FWCOM_SLOT_HPP__
11 #error fwCom/Slot.hpp not included
12 #endif
13 
14 #include "fwCom/SlotCall.hxx"
15 #include "fwCom/util/AutoBind.hpp"
16 #include "fwCom/util/AutoBind.hxx"
17 
18 #include <boost/function_types/function_arity.hpp>
19 #include <boost/function_types/result_type.hpp>
20 #include <boost/static_assert.hpp>
21 
22 #include <type_traits>
23 
24 namespace fwCom
25 {
26 
27 //-----------------------------------------------------------------------------
28 
29 template<typename R, typename ... A >
30 Slot< R( A ... ) >::Slot() :
31  SlotCall< R(A ...) >()
32 {
33  // 'this->' is needed by gcc 4.2
34  this->SlotBase::m_signature = SlotBase::getTypeName< R( A ... ) >();
35 }
36 
37 //-----------------------------------------------------------------------------
38 
39 template<typename R, typename ... A >
40 template<typename F>
41 SPTR( Slot< R( A ... ) > ) Slot< R( A ... ) >::New( F f )
42 {
43  return newSlot(f);
44 }
45 
46 //-----------------------------------------------------------------------------
47 
48 template<typename R, typename ... A >
49 template<typename F, typename O>
50 SPTR( Slot< R( A ... ) > ) Slot< R( A ... ) >::New( F f, O o )
51 {
52  return newSlot(f, o);
53 }
54 
55 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
57 
58 template<typename R, typename ... A >
59 template< typename F >
60 Slot< Slot< R( A ... ) > >::Slot( SPTR( SlotRun< F > )slot ) :
61  Slot< FunctionType >(
62  ::fwCom::util::AutoBind<
63  SignatureType,
64  ::boost::function_types::function_arity< F >::value
65  >::wrap( &SlotRun< F >::run, slot.get() ) )
66 {
67  BOOST_STATIC_ASSERT( (std::is_same<void, R>::value) );
68  this->setWorker(slot->getWorker());
69 }
70 
71 //-----------------------------------------------------------------------------
72 
73 template<typename R, typename ... A >
74 template< typename F >
75 Slot< Slot< R( A ... ) > >::Slot( SPTR( Slot< F > )slot ) :
76  Slot< FunctionType >(
77  ::fwCom::util::AutoBind<
78  SignatureType,
79  ::boost::function_types::function_arity< F >::value
80  >::wrap( &Slot< F >::call, slot.get() ) )
81 {
82  this->setWorker(slot->getWorker());
83 }
84 
85 //-----------------------------------------------------------------------------
86 
87 template<typename R, typename ... A >
88 template<typename F >
89 SPTR(Slot< R( A ... )>) Slot< Slot< R( A ... )> >::New( SPTR( SlotRun< F > ) slot )
90 {
91  assert(::boost::function_types::function_arity< F >::value <=
92  ::boost::function_types::function_arity< R( A ... ) >::value);
93  return std::make_shared< Slot< Slot< R( A ... ) > > >( slot );
94 }
95 
96 //-----------------------------------------------------------------------------
97 
98 template<typename F, typename ... BINDING>
99 SPTR(Slot< typename ::fwCom::util::convert_function_type< F >::type >) newSlot(F f, BINDING ... binding)
100 {
101  SLM_ASSERT( "Too many arguments", ( sizeof ... (binding) < 2 ) );
102  typedef std::function< typename ::fwCom::util::convert_function_type< F >::type > FunctionType;
103  FunctionType func = ::fwCom::util::autobind(f, binding ...);
104  return std::make_shared< Slot< FunctionType > > ( func );
105 }
106 
107 //-----------------------------------------------------------------------------
108 
109 } // namespace fwCom
110 
111 #endif /* __FWCOM_SLOT_HXX__ */
112 
#define SPTR(_cls_)
Namespace containing fw4spl communication tools.
Definition: DumpEditor.hpp:30
std::string getTypeName() const
Returns F typeid name.
Definition: SlotBase.hpp:205
#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
std::string m_signature
Slot&#39;s signature based on typeid.
Definition: SlotBase.hpp:217