fw4spl
AutoBind.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_UTIL_AUTOBIND_HXX__
7 #define __FWCOM_UTIL_AUTOBIND_HXX__
8 
9 #ifndef __FWCOM_UTIL_AUTOBIND_HPP__
10 #error fwCom/util/AutoBind.hpp not included
11 #endif
12 
13 #include <boost/function_types/function_arity.hpp>
14 
15 namespace fwCom
16 {
17 
18 namespace stdplh = std::placeholders;
19 
20 namespace util
21 {
22 
23 //------------------------------------------------------------------------------
24 
25 template< typename F >
26 template< typename W, typename ... A >
27 std::function< typename AutoBind< F, 0 >::FunctionType > AutoBind< F, 0 >::wrap( W f, A ... a )
28 {
29  return std::bind( f, a ... );
30 }
31 
32 //-----------------------------------------------------------------------------
33 
34 template< typename F >
35 template< typename W, typename ... A >
36 std::function< typename AutoBind< F, 1 >::FunctionType > AutoBind< F, 1 >::wrap( W f, A ... a )
37 {
38  return std::bind( f, a ..., stdplh::_1 );
39 }
40 
41 //-----------------------------------------------------------------------------
42 
43 template< typename F >
44 template< typename W, typename ... A >
45 std::function< typename AutoBind< F, 2 >::FunctionType > AutoBind< F, 2 >::wrap( W f, A ... a )
46 {
47  return std::bind( f, a ..., stdplh::_1, stdplh::_2 );
48 }
49 
50 //-----------------------------------------------------------------------------
51 
52 template< typename F >
53 template< typename W, typename ... A >
54 std::function< typename AutoBind< F, 3 >::FunctionType > AutoBind< F, 3 >::wrap( W f, A ... a )
55 {
56  return std::bind( f, a ..., stdplh::_1, stdplh::_2, stdplh::_3 );
57 }
58 
59 //-----------------------------------------------------------------------------
60 
61 template< typename F >
62 template< typename W, typename ... A >
63 std::function< typename AutoBind< F, 4 >::FunctionType > AutoBind< F, 4 >::wrap( W f, A ... a )
64 {
65  return std::bind( f, a ..., stdplh::_1, stdplh::_2, stdplh::_3, stdplh::_4 );
66 }
67 
68 //-----------------------------------------------------------------------------
69 
70 template< typename F >
71 template< typename W, typename ... A >
72 std::function< typename AutoBind< F, 5 >::FunctionType > AutoBind< F, 5 >::wrap( W f, A ... a )
73 {
74  return std::bind( f, a ..., stdplh::_1, stdplh::_2, stdplh::_3, stdplh::_4, stdplh::_5 );
75 }
76 
77 //-----------------------------------------------------------------------------
78 
79 template< typename F >
80 template< typename W, typename ... A >
81 std::function< typename AutoBind< F, 6 >::FunctionType > AutoBind< F, 6 >::wrap( W f, A ... a )
82 {
83  return std::bind( f, a ..., stdplh::_1, stdplh::_2, stdplh::_3, stdplh::_4, stdplh::_5, stdplh::_6 );
84 }
85 
86 //-----------------------------------------------------------------------------
87 
88 template< typename F >
89 template< typename W, typename ... A >
90 std::function< typename AutoBind< F, 7 >::FunctionType > AutoBind< F, 7 >::wrap( W f, A ... a )
91 {
92  return std::bind( f, a ..., stdplh::_1, stdplh::_2, stdplh::_3, stdplh::_4, stdplh::_5, stdplh::_6,
93  stdplh::_7 );
94 }
95 
96 //-----------------------------------------------------------------------------
97 
98 template< typename F >
99 template< typename W, typename ... A >
100 std::function< typename AutoBind< F, 8 >::FunctionType > AutoBind< F, 8 >::wrap( W f, A ... a )
101 {
102  return std::bind( f, a ..., stdplh::_1, stdplh::_2, stdplh::_3, stdplh::_4, stdplh::_5, stdplh::_6, stdplh::_7,
103  stdplh::_8 );
104 }
105 
106 //-----------------------------------------------------------------------------
107 
108 template< typename F >
109 template< typename W, typename ... A >
110 std::function< typename AutoBind< F, 9 >::FunctionType > AutoBind< F, 9 >::wrap( W f, A ... a )
111 {
112  return std::bind( f, a ..., stdplh::_1, stdplh::_2, stdplh::_3, stdplh::_4, stdplh::_5, stdplh::_6, stdplh::_7,
113  stdplh::_8, stdplh::_9 );
114 }
115 
116 //-----------------------------------------------------------------------------
117 
118 template <typename F, typename ... A >
119 std::function< typename convert_function_type< F >::type > autobind(F f, A ... a)
120 {
121  typedef typename convert_function_type< F >::type FunctionType;
122  const int arity = ::boost::function_types::function_arity< FunctionType >::value;
123  return AutoBind< F, arity >::wrap(f, a ...);
124 }
125 
126 } //namespace util
127 
128 } //namespace fwCom
129 
130 #endif /* __FWCOM_UTIL_AUTOBIND_HXX__ */
131 
Namespace containing fw4spl communication tools.
Definition: DumpEditor.hpp:30
Automatic binding class. This class allow to bind automatically a function/method with the right numb...
Definition: AutoBind.hpp:33
std::function< typename convert_function_type< F >::type > autobind(F f, A...a)
Automatic bind of given function.
Definition: AutoBind.hxx:119