fw4spl
Slot.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_SLOT_HPP__
8 #define __FWCOM_SLOT_HPP__
9 
10 #include "fwCom/SlotCall.hpp"
11 
12 #include <functional>
13 
14 namespace fwCom
15 {
16 
17 template< typename F >
18 class Slot;
19 
20 //-----------------------------------------------------------------------------
21 
22 template<typename R, typename ... A >
23 class Slot< R( A ... ) > : public SlotCall< R(A ...) >
24 {
25 public:
26  typedef R SignatureType (A ...);
28  typedef SPTR ( SelfType ) sptr;
29  typedef WPTR ( SelfType ) wptr;
30 
31  Slot();
32 
33  template< typename F >
34  static SPTR( Slot< R(A ...) > ) New( F f );
35 
36  template< typename F, typename O >
37  static SPTR( Slot< R(A ...) > ) New( F f, O o );
38 };
39 
40 //-----------------------------------------------------------------------------
41 
42 template<typename R, typename ... A >
43 class Slot< std::function< R( A ... ) > > : public Slot< R( A ... ) >
44 {
45 public:
46  typedef R SignatureType (A ...);
47  typedef std::function< SignatureType > FunctionType;
48 
49  template< typename FUNCTOR >
50  Slot( FUNCTOR f ) :
51  Slot< R( A ... ) >(),
52  m_func(f)
53  {
54  }
55 
56  virtual ~Slot()
57  {
58  }
59 
60  //------------------------------------------------------------------------------
61 
62  virtual void run(A ... a) const
63  {
64  m_func(a ...);
65  }
66 
67  //------------------------------------------------------------------------------
68 
69  virtual R call(A ... a) const
70  {
71  return m_func(a ...);
72  }
73 
74 protected:
75  FunctionType m_func;
76 };
77 
78 //-----------------------------------------------------------------------------
79 
80 template<typename R, typename ... A >
81 class Slot< Slot< R( A ... ) > > : public Slot< std::function < R( A ... ) > >
82 {
83 public:
84 
85  typedef R SignatureType ( A ... );
86  typedef std::function< SignatureType > FunctionType;
87 
88  template< typename F >
89  Slot( SPTR( SlotRun< F > )slot );
90 
91  template< typename F >
92  Slot( SPTR( Slot< F > )slot );
93 
94  template< typename F >
95  static SPTR( Slot< R(A ...) > ) New( SPTR( SlotRun< F > ) slot );
96 };
97 
98 //-----------------------------------------------------------------------------
99 
100 template<typename F, typename ... Bindings>
101 SPTR( Slot< typename ::fwCom::util::convert_function_type< F >::type > ) newSlot(F f, Bindings ... bindings);
102 
103 } // namespace fwCom
104 
105 #endif /* __FWCOM_SLOT_HPP__ */
106 
#define SPTR(_cls_)
virtual void run(A...a) const
Run the Slot with the given parameters.
Definition: Slot.hpp:62
Namespace containing fw4spl communication tools.
Definition: DumpEditor.hpp:30
#define WPTR(_cls_)
STL namespace.
virtual R call(A...a) const
Call the Slot with the given parameters.
Definition: Slot.hpp:69