Skip to content

Commit

Permalink
function_array_helper: handle __COUNTER__ not being 0
Browse files Browse the repository at this point in the history
  • Loading branch information
edo9300 committed Jan 3, 2024
1 parent da45f91 commit c8399f0
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions function_array_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

namespace {
namespace Detail {
static constexpr auto COUNTER_OFFSET = __COUNTER__ + 1;
template<std::size_t N>
struct LuaFunction;

Expand All @@ -39,7 +40,7 @@ constexpr auto make_lua_functions_array(std::index_sequence<I...> seq) {
#define LUA_STATIC_FUNCTION(name) \
static LUA_INLINE int32_t MAKE_LUA_NAME(LUA_MODULE,name)(lua_State*, duel*); \
template<> \
struct Detail::LuaFunction<__COUNTER__> { \
struct Detail::LuaFunction<__COUNTER__ - Detail::COUNTER_OFFSET> { \
static int32_t call(lua_State* L) { \
return MAKE_LUA_NAME(LUA_MODULE,name)(L, lua_get<duel*>(L)); \
} \
Expand All @@ -50,7 +51,7 @@ static LUA_INLINE int32_t MAKE_LUA_NAME(LUA_MODULE,name)([[maybe_unused]] lua_St
#define LUA_FUNCTION(name) \
static LUA_INLINE int32_t MAKE_LUA_NAME(LUA_MODULE,name)(lua_State*, LUA_CLASS*, duel*); \
template<> \
struct Detail::LuaFunction<__COUNTER__> { \
struct Detail::LuaFunction<__COUNTER__ - Detail::COUNTER_OFFSET> { \
static int32_t call(lua_State* L) { \
return MAKE_LUA_NAME(LUA_MODULE,name)(L, lua_get<LUA_CLASS*, true>(L, 1), lua_get<duel*>(L)); \
} \
Expand All @@ -59,20 +60,20 @@ struct Detail::LuaFunction<__COUNTER__> { \
static LUA_INLINE int32_t MAKE_LUA_NAME(LUA_MODULE,name)([[maybe_unused]] lua_State* L, [[maybe_unused]] LUA_CLASS* self, [[maybe_unused]] duel* pduel)

#define GET_LUA_FUNCTIONS_ARRAY() \
Detail::make_lua_functions_array(std::make_index_sequence<__COUNTER__>())
Detail::make_lua_functions_array(std::make_index_sequence<__COUNTER__ - Detail::COUNTER_OFFSET>())


#define LUA_FUNCTION_EXISTING(name,...) \
template<> \
struct Detail::LuaFunction<__COUNTER__> { \
struct Detail::LuaFunction<__COUNTER__ - Detail::COUNTER_OFFSET> { \
static constexpr luaL_Reg elem{#name,__VA_ARGS__}; \
}

#define LUA_FUNCTION_ALIAS(name) LUA_DECLARE_ALIAS_INT(name, __COUNTER__)
#define LUA_DECLARE_ALIAS_INT(name, COUNTER) \
template<> \
struct Detail::LuaFunction<COUNTER> { \
static constexpr luaL_Reg elem{#name,Detail::LuaFunction<COUNTER-1>::elem.func}; \
struct Detail::LuaFunction<COUNTER - Detail::COUNTER_OFFSET> { \
static constexpr luaL_Reg elem{#name,Detail::LuaFunction<COUNTER - Detail::COUNTER_OFFSET-1>::elem.func}; \
}
#else
#define LUA_FUNCTION(name) static int32_t MAKE_LUA_NAME(LUA_MODULE,name)([[maybe_unused]] lua_State* L, [[maybe_unused]] LUA_CLASS* self, [[maybe_unused]] duel* pduel)
Expand Down

0 comments on commit c8399f0

Please sign in to comment.