Changeset 345
- Timestamp:
- 11/17/14 17:55:38 (11 years ago)
- Location:
- trunk/nv
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/core/type_traits.hh
r319 r345 79 79 }; 80 80 81 template < typename R, typename T1>82 struct function_traits_impl < R (*)( T1) >81 template < typename R, typename... Args > 82 struct function_traits_impl < R(*)(Args...) > 83 83 { 84 typedef R (*type)();84 typedef R(*type)( Args... ); 85 85 typedef R return_type; 86 86 typedef void class_type; 87 static const int arg_count = 1; 88 }; 89 90 template < typename R, typename T1, typename T2 > 91 struct function_traits_impl< R (*)( T1, T2 ) > 92 { 93 typedef R (*type)(); 94 typedef R return_type; 95 typedef void class_type; 96 static const int arg_count = 2; 97 }; 98 99 template < typename R, typename T1, typename T2, typename T3 > 100 struct function_traits_impl< R (*)( T1, T2, T3 ) > 101 { 102 typedef R (*type)(); 103 typedef R return_type; 104 typedef void class_type; 105 static const int arg_count = 3; 106 }; 107 108 template < typename R, typename T1, typename T2, typename T3, typename T4 > 109 struct function_traits_impl< R (*)( T1, T2, T3, T4 ) > 110 { 111 typedef R (*type)(); 112 typedef R return_type; 113 typedef void class_type; 114 static const int arg_count = 4; 87 static const int arg_count = sizeof...(Args); 115 88 }; 116 89 … … 124 97 }; 125 98 126 template < class C, typename R, typename T1>127 struct function_traits_impl < R (C::*)( T1) >99 template < class C, typename R, typename... Args > 100 struct function_traits_impl < R(C::*)(Args...) > 128 101 { 129 typedef R (*type)(); 130 typedef R return_type; 131 typedef C class_type; 132 static const int arg_count = 1; 133 }; 134 135 template < class C, typename R, typename T1, typename T2 > 136 struct function_traits_impl< R (C::*)( T1, T2 ) > 137 { 138 typedef R (*type)(); 139 typedef R return_type; 140 typedef C class_type; 141 static const int arg_count = 2; 142 }; 143 144 template < class C, typename R, typename T1, typename T2, typename T3 > 145 struct function_traits_impl< R (C::*)( T1, T2, T3 ) > 146 { 147 typedef R (*type)(); 148 typedef R return_type; 149 typedef C class_type; 150 static const int arg_count = 3; 151 }; 152 153 template < class C, typename R, typename T1, typename T2, typename T3, typename T4 > 154 struct function_traits_impl< R (C::*)( T1, T2, T3, T4 ) > 155 { 156 typedef R (*type)(); 157 typedef R return_type; 158 typedef C class_type; 159 static const int arg_count = 4; 102 typedef R(C::*type)(Args...); 103 typedef R return_type; 104 typedef C class_type; 105 static const int arg_count = sizeof...(Args); 160 106 }; 161 107 -
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 -
trunk/nv/lua/lua_function.hh
r319 r345 51 51 } 52 52 53 template <typename T1>54 R operator()( const T1& p1)53 template <typename... Args> 54 R operator()( Args&&... args ) 55 55 { 56 56 retrieve(); 57 detail::push_value ( L, p1);58 return call_result<R>( 1);57 detail::push_values(L, std::forward<Args>(args)...); 58 return call_result<R>(sizeof...(Args)); 59 59 } 60 60 61 template <typename T1, typename T2>62 R operator()(const T1& p1, const T2& p2)63 {64 retrieve();65 detail::push_values( L, p1, p2 );66 return call_result<R>(2);67 }68 69 template <typename T1, typename T2, typename T3>70 R operator()(const T1& p1, const T2& p2, const T3& p3)71 {72 retrieve();73 detail::push_values( L, p1, p2, p3 );74 return call_result<R>(3);75 }76 61 protected: 77 62 template< typename RR > -
trunk/nv/lua/lua_path.hh
r319 r345 89 89 bool resolve( lua_State* L, bool global = true ) const; 90 90 private: 91 91 92 void parse(); 92 93 void push( size_t start, size_t length ); -
trunk/nv/lua/lua_state.hh
r341 r345 55 55 return R(); 56 56 } 57 template < typename R, typename T1>58 R call( const path& p, const T1& p1)59 { 60 if ( push_function( p, m_global ))61 { 62 detail::push_value ( m_state, p1);63 if ( call_function( 1, detail::return_count< R >::value ) == 0)57 template < typename R, typename... Args > 58 R call(const path& p, Args&&... args ) 59 { 60 if (push_function(p, m_global)) 61 { 62 detail::push_values(m_state, std::forward<Args>(args)...); 63 if (call_function(sizeof...(Args), detail::return_count< R >::value) == 0) 64 64 { 65 return detail::pop_return_value<R>( m_state ); 66 } 67 } 68 return R(); 69 } 70 template < typename R, typename T1, typename T2 > 71 R call( const path& p, const T1& p1, const T2& p2 ) 72 { 73 if ( push_function( p, m_global ) ) 74 { 75 detail::push_values( m_state, p1, p2 ); 76 if ( call_function( 2, detail::return_count< R >::value ) == 0 ) 77 { 78 return detail::pop_return_value<R>( m_state ); 79 } 80 } 81 return R(); 82 } 83 template < typename R, typename T1, typename T2, typename T3 > 84 R call( const path& p, const T1& p1, const T2& p2, const T3& p3 ) 85 { 86 if ( push_function( p, m_global ) ) 87 { 88 detail::push_values( m_state, p1, p2, p3 ); 89 if ( call_function( 3, detail::return_count< R >::value ) == 0 ) 90 { 91 return detail::pop_return_value<R>( m_state ); 92 } 93 } 94 return R(); 95 } 96 template < typename R, typename T1, typename T2, typename T3, typename T4 > 97 R call( const path& p, const T1& p1, const T2& p2, const T3& p3, const T4& p4 ) 98 { 99 if ( push_function( p, m_global ) ) 100 { 101 detail::push_value( m_state, p1, p2, p3, p4 ); 102 if ( call_function( 4, detail::return_count< R >::value ) == 0 ) 103 { 104 return detail::pop_return_value<R>( m_state ); 65 return detail::pop_return_value<R>(m_state); 105 66 } 106 67 } … … 306 267 return R(); 307 268 } 308 template < typename R, typename T1>309 R call( ref table, const path& p, const T1& p1)269 template < typename R, typename... Args > 270 R call( ref table, const path& p, Args&&... args ) 310 271 { 311 272 stack_guard guard( this ); // TODO: remove … … 313 274 if ( push_function( p, false ) ) 314 275 { 315 detail::push_value( m_state, p1 ); 316 if ( call_function( 1, detail::return_count< R >::value ) == 0 ) 317 { 318 return detail::pop_return_value<R>( m_state ); 319 } 320 } 321 return R(); 322 } 323 template < typename R, typename T1, typename T2 > 324 R call( ref table, const path& p, const T1& p1, const T2& p2 ) 325 { 326 stack_guard guard( this ); // TODO: remove 327 detail::push_ref_object( m_state, table ); 328 if ( push_function( p, false ) ) 329 { 330 detail::push_values( m_state, p1, p2 ); 331 if ( call_function( 2, detail::return_count< R >::value ) == 0 ) 332 { 333 return detail::pop_return_value<R>( m_state ); 334 } 335 } 336 return R(); 337 } 338 template < typename R, typename T1, typename T2, typename T3 > 339 R call( ref table, const path& p, const T1& p1, const T2& p2, const T3& p3 ) 340 { 341 stack_guard guard( this ); // TODO: remove 342 detail::push_ref_object( m_state, table ); 343 if ( push_function( p, false ) ) 344 { 345 detail::push_values( m_state, p1, p2, p3 ); 346 if ( call_function( 3, detail::return_count< R >::value ) == 0 ) 347 { 348 return detail::pop_return_value<R>( m_state ); 349 } 350 } 351 return R(); 352 } 353 template < typename R, typename T1, typename T2, typename T3, typename T4 > 354 R call( ref table, const path& p, const T1& p1, const T2& p2, const T3& p3, const T4& p4 ) 355 { 356 stack_guard guard( this ); // TODO: remove 357 detail::push_ref_object( m_state, table ); 358 if ( push_function( p, false ) ) 359 { 360 detail::push_value( m_state, p1, p2, p3, p4 ); 361 if ( call_function( 4, detail::return_count< R >::value ) == 0 ) 276 detail::push_values( m_state, std::forward<Args>(args)... ); 277 if (call_function(sizeof...(Args), detail::return_count< R >::value) == 0) 362 278 { 363 279 return detail::pop_return_value<R>( m_state ); -
trunk/nv/lua/lua_values.hh
r319 r345 213 213 } 214 214 215 template < typename T1 > 216 void push_values( lua_State *L, const T1& p1 ) { push_value( L, p1 ); } 217 template < typename T1, typename T2 > 218 void push_values( lua_State *L, const T1& p1, const T2& p2 ) { push_value( L, p1 ); push_value( L, p2 ); } 219 template < typename T1, typename T2, typename T3 > 220 void push_values( lua_State *L, const T1& p1, const T2& p2, const T3& p3 ) { push_value( L, p1 ); push_value( L, p2 ); push_value( L, p3 ); } 221 template < typename T1, typename T2, typename T3, typename T4 > 222 void push_values( lua_State *L, const T1& p1, const T2& p2, const T3& p3, const T4& p4 ) { push_value( L, p1 ); push_value( L, p2 ); push_value( L, p3 ); push_value( L, p4 ); } 223 template < typename T1, typename T2, typename T3, typename T4, typename T5 > 224 void push_values( lua_State *L, const T1& p1, const T2& p2, const T3& p3, const T4& p4, const T5& p5 ) { push_value( L, p1 ); push_value( L, p2 ); push_value( L, p3 ); push_value( L, p4 ); push_value( L, p5 ); } 215 template < typename T > 216 void push_values(lua_State *L, T&& p) 217 { 218 push_value( L, std::forward<T>(p) ); 219 } 220 template < typename T, typename ...Ts > 221 void push_values(lua_State *L, T&& p, Ts&& ...ps) 222 { 223 push_value(L, std::forward<T>(p)); 224 push_values(L, std::forward<Ts>(ps)...); 225 } 226 225 227 226 228 template < typename T >
Note: See TracChangeset
for help on using the changeset viewer.