/* Luwra * Minimal-overhead Lua wrapper for C++ * * Copyright (C) 2015, Ole Krüger */ #ifndef LUWRA_FUNCTIONS_H_ #define LUWRA_FUNCTIONS_H_ #include "common.hpp" #include "types.hpp" #include "stack.hpp" LUWRA_NS_BEGIN namespace internal { template struct FunctionWrapper { static_assert( sizeof(T) == -1, "The FunctionWrapper template expects a function signature as parameter" ); }; template <> struct FunctionWrapper { template static inline int invoke(State*) { function_pointer(); return 0; } }; template struct FunctionWrapper { template static inline int invoke(State* state) { return push(state, function_pointer()); } }; template struct FunctionWrapper { template static inline int invoke(State* state) { apply(state, function_pointer); return 0; } }; template struct FunctionWrapper { template static inline int invoke(State* state) { return push( state, apply(state, function_pointer) ); } }; } /** * Assuming its parameters can be retrieved from the Lua stack, ordinary functions can be wrapped * using the `wrap_function` instance in order to produce a C function which can be used by the * Lua VM. * * Assuming your function has the following signature: * * R my_fun(A0, A1 ... An); * * Generate a Lua-compatible like so: * * CFunction wrapped_fun = wrap_function; */ template < typename S, S* function_pointer > constexpr CFunction wrap_function = &internal::FunctionWrapper::template invoke; LUWRA_NS_END #endif