fork download
  1. #include <iostream>
  2.  
  3. template<typename T> struct Type {};
  4. typedef double lua_Number;
  5. struct lua_State {};
  6. int luaL_checkint( lua_State*, int index) {
  7. return 7+index;
  8. }
  9. lua_Number luaL_checknumber( lua_State*, int index) {
  10. return 3.14+index;
  11. }
  12. bool lua_toboolean( lua_State*, int index ) {
  13. return index%2 == 0;
  14. }
  15. namespace lua_to_cpp {
  16. inline int get(Type<int>, lua_State *l, const int index) {
  17. return luaL_checkint(l, index);
  18. };
  19.  
  20. inline lua_Number get(Type<lua_Number>, lua_State *l, const int index) {
  21. return luaL_checknumber(l, index);
  22. }
  23.  
  24. inline bool get(Type<bool>, lua_State *l, const int index) {
  25. return lua_toboolean(l, index) != 0;
  26. }
  27. }
  28. template<unsigned...>struct indexes{typedef indexes type;};
  29. template<unsigned Max, unsigned...Is>struct make_indexes:make_indexes<Max-1, Max-1, Is...>{};
  30. template<unsigned... Is>struct make_indexes<0, Is...>:indexes<Is...>{};
  31. template<unsigned Max>using make_indexes_t=typename make_indexes<Max>::type;
  32.  
  33. template<typename... Args, unsigned... Is>
  34. void invoke( void(*func)(Args...), lua_State* state, indexes<Is...>) {
  35. return func( lua_to_cpp::get( Type<Args>{}, state, Is+2 )... );
  36. }
  37. template<typename... Args>
  38. void invoke( void(*func)(Args...), lua_State* state ) {
  39. invoke( func, state, make_indexes_t<sizeof...(Args)>{} );
  40. }
  41.  
  42. void foo( int, lua_Number ) {
  43. std::cout << "called!\n";
  44. }
  45.  
  46. void call_foo( lua_State* state ) {
  47. invoke(foo, state);
  48. }
  49.  
  50.  
  51. int main() {
  52. call_foo(nullptr);
  53. // your code goes here
  54. return 0;
  55. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
called!