Changeset 345 for trunk/nv/lua/lua_dispatch.hh
- Timestamp:
- 11/17/14 17:55:38 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/lua/lua_dispatch.hh
r319 r345 29 29 namespace detail 30 30 { 31 template <typename...> 32 struct types {}; 33 31 34 template < typename R > 32 struct dispatcher35 struct call_and_push 33 36 { 34 static int call( lua_State* L, int, R (*func)() ) 37 template <typename... Args, typename... Vs> 38 static void do_call(lua_State* L, R(*func)(Args...), Vs&&... vs) 35 39 { 36 push_value( L, func() ); return 1;40 push_value(L, func(std::forward<Vs>(vs)...)); 37 41 } 38 template < typename T1>39 static int call( lua_State* L, int index, R (*func)( T1 ))42 template <class C, typename... Args, typename... Vs> 43 static void do_call(lua_State* L, C& c, R(C::*func)(Args...), Vs&&... vs) 40 44 { 41 push_value( L, func( get_value<T1>( L, index + 0 ) ) ); return 1; 42 } 43 template < typename T1, typename T2 > 44 static int call( lua_State* L, int index, R (*func)( T1, T2 ) ) 45 { 46 push_value( L, func( get_value<T1>( L, index + 0 ), get_value<T2>( L, index + 1 ) ) ); return 1; 47 } 48 template < typename T1, typename T2, typename T3 > 49 static int call( lua_State* L, int index, R (*func)( T1, T2, T3 ) ) 50 { 51 push_value( L, func( get_value<T1>( L, index + 0 ), get_value<T2>( L, index + 1 ), get_value<T3>( L, index + 2 ) ) ); return 1; 52 } 53 template < typename T1, typename T2, typename T3, typename T4 > 54 static int call( lua_State* L, int index, R (*func)( T1, T2, T3, T4 ) ) 55 { 56 push_value( L, func( get_value<T1>( L, index + 0 ), get_value<T2>( L, index + 1 ), get_value<T3>( L, index + 2 ), get_value<T4>( L, index + 3 ) ) ); return 1; 57 } 58 template < class C > 59 static int call( lua_State* L, int, C& c, R (C::*func)() ) 60 { 61 push_value( L, (c.*func)() ); return 1; 62 } 63 template < class C, typename T1 > 64 static int call( lua_State* L, int index, C& c, R (C::*func)( T1 ) ) 65 { 66 push_value( L, (c.*func)( get_value<T1>( L, index + 0 ) ) ); return 1; 67 } 68 template < class C, typename T1, typename T2 > 69 static int call( lua_State* L, int index, C& c, R (C::*func)( T1, T2 ) ) 70 { 71 push_value( L, (c.*func)( get_value<T1>( L, index + 0 ), get_value<T2>( L, index + 1 ) ) ); return 1; 72 } 73 template < class C, typename T1, typename T2, typename T3 > 74 static int call( lua_State* L, int index, C& c, R (C::*func)( T1, T2, T3 ) ) 75 { 76 push_value( L, (c.*func)( get_value<T1>( L, index + 0 ), get_value<T2>( L, index + 1 ), get_value<T3>( L, index + 2 ) ) ); return 1; 77 } 78 template < class C, typename T1, typename T2, typename T3, typename T4 > 79 static int call( lua_State* L, int index, C& c, R (C::*func)( T1, T2, T3, T4 ) ) 80 { 81 push_value( L, (c.*func)( get_value<T1>( L, index + 0 ), get_value<T2>( L, index + 1 ), get_value<T3>( L, index + 2 ), get_value<T4>( L, index + 3 ) ) ); return 1; 45 push_value(L, (c.*func)(std::forward<Vs>(vs)...)); 82 46 } 83 47 }; 84 48 85 49 template <> 86 struct dispatcher< void>50 struct call_and_push<void> 87 51 { 88 static int call( lua_State*, int, void (*func)() ) 52 template <typename... Args, typename... Vs> 53 static void do_call(lua_State*, void(*func)(Args...), Vs&&... vs) 89 54 { 90 func( ); return 0;55 func(std::forward<Vs>(vs)...); 91 56 } 92 template < typename T1>93 static int call( lua_State* L, int index, void (*func)( T1 ))57 template <class C, typename... Args, typename... Vs> 58 static void do_call(lua_State*, C& c, void(C::*func)(Args...), Vs&&... vs) 94 59 { 95 func( get_value<T1>( L, index + 0 ) ); return 0;60 (c.*func)(std::forward<Vs>(vs)...); 96 61 } 97 template < typename T1, typename T2 > 98 static int call( lua_State* L, int index, void (*func)( T1, T2 ) ) 99 { 100 func( get_value<T1>( L, index + 0 ), get_value<T2>( L, index + 1 ) ); return 0; 101 } 102 template < typename T1, typename T2, typename T3 > 103 static int call( lua_State* L, int index, void (*func)( T1, T2, T3 ) ) 104 { 105 func( get_value<T1>( L, index + 0 ), get_value<T2>( L, index + 1 ), get_value<T3>( L, index + 2 ) ); return 0; 106 } 107 template < typename T1, typename T2, typename T3, typename T4 > 108 static int call( lua_State* L, int index, void (*func)( T1, T2, T3, T4 ) ) 109 { 110 func( get_value<T1>( L, index + 0 ), get_value<T2>( L, index + 1 ), get_value<T3>( L, index + 2 ), get_value<T4>( L, index + 3 ) ); return 0; 111 } 112 template < class C > 113 static int call( lua_State*, int, C& c, void (C::*func)() ) 114 { 115 (c.*func)(); return 0; 116 } 117 template < class C, typename T1 > 118 static int call( lua_State* L, int index, C& c, void (C::*func)( T1 ) ) 119 { 120 (c.*func)( get_value<T1>( L, index + 0 ) ); return 0; 121 } 122 template < class C, typename T1, typename T2 > 123 static int call( lua_State* L, int index, C& c, void (C::*func)( T1, T2 ) ) 124 { 125 (c.*func)( get_value<T1>( L, index + 0 ), get_value<T2>( L, index + 1 ) ); return 0; 126 } 127 template < class C, typename T1, typename T2, typename T3 > 128 static int call( lua_State* L, int index, C& c, void (C::*func)( T1, T2, T3 ) ) 129 { 130 (c.*func)( get_value<T1>( L, index + 0 ), get_value<T2>( L, index + 1 ), get_value<T3>( L, index + 2 ) ); return 0; 131 } 132 template < class C, typename T1, typename T2, typename T3, typename T4 > 133 static int call( lua_State* L, int index, C& c, void (C::*func)( T1, T2, T3, T4 ) ) 134 { 135 (c.*func)( get_value<T1>( L, index + 0 ), get_value<T2>( L, index + 1 ), get_value<T3>( L, index + 2 ), get_value<T4>( L, index + 3 ) ); return 0; 136 } 62 }; 137 63 138 }; 64 template <typename R, typename... Args, typename Head, typename ...Tail, typename... Vs> 65 int do_call(lua_State* L, int index, R(*func)(Args...), types<Head, Tail...>, Vs&&... vs) 66 { 67 do_call(L, index + 1, func, types < Tail... > {}, std::forward<Vs>(vs)..., get_value<Head>(L, index)); return 1; 68 } 69 70 template <typename R, class C, typename... Args, typename Head, typename ...Tail, typename... Vs> 71 int do_call(lua_State* L, int index, C& c, R(C::*func)(Args...), types<Head, Tail...>, Vs&&... vs) 72 { 73 do_call(L, index + 1, c, func, types < Tail... > {}, std::forward<Vs>(vs)..., get_value<Head>(L, index)); return 1; 74 } 75 76 template <typename R, typename... Args, typename... Vs> 77 int do_call(lua_State* L, int, R(*func)(Args...), types<>, Vs&&... vs) 78 { 79 call_and_push<R>::do_call(L, func, std::forward<Vs>(vs)...); 80 return 1; 81 } 82 83 template <typename R, class C, typename... Args, typename... Vs> 84 int do_call(lua_State* L, int, C& c, R(C::*func)(Args...), types<>, Vs&&... vs) 85 { 86 call_and_push<R>::do_call(L, c, func, std::forward<Vs>(vs)...); 87 return 1; 88 } 89 90 template < typename R, typename... Args > 91 int dispatch(lua_State* L, int index, R(*func)(Args...)) 92 { 93 do_call(L, index, func, types < Args... > {}); 94 return 1; 95 } 96 template < typename R, class C, typename... Args > 97 int dispatch(lua_State* L, int index, C& c, R(C::*func)(Args...)) 98 { 99 do_call(L, index, c, func, types < Args... > {}); 100 return 1; 101 } 139 102 140 103 template < typename F, F f > 141 104 int function_wrapper( lua_State* L ) 142 105 { 143 return dispatch er<typename return_type<F>::type>::call( L, 1, f);106 return dispatch(L, 1, f); 144 107 } 145 108 … … 148 111 { 149 112 C* c = (C*)to_ref_object( L, 1 ); 150 return dispatch er<typename return_type<F>::type>::call( L, 2, *c, f );113 return dispatch( L, 2, *c, f ); 151 114 } 152 115 … … 155 118 { 156 119 C* c = (C*)to_pointer( L, nv::lua::detail::upvalue_index(1) ); 157 return dispatch er<typename return_type<F>::type>::call( L, 1, *c, f );120 return dispatch( L, 1, *c, f ); 158 121 } 159 122 160 template< typename F >161 int call( lua_State* L, int index, F func )162 {163 return dispatcher<typename return_type<F>::type>::call( L, index, func );164 }165 166 template< class C, typename F >167 int call( lua_State* L, int index, C& c, F func )168 {169 return dispatcher<typename return_type<F>::type>::call( L, index, c, func );170 }171 172 123 } 173 124
Note: See TracChangeset
for help on using the changeset viewer.