Skip to content

Commit

Permalink
Change from variant to conditional type for registered functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Foereaper committed Aug 5, 2024
1 parent df442f3 commit 7dce466
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions ElunaTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ template<typename T = void>
struct ElunaRegister
{
const char* name;
std::variant<std::monostate, int(*)(Eluna*, T*), int(*)(Eluna*)> mfunc;
typename std::conditional<std::is_same_v<T, void>, int(*)(Eluna*), int(*)(Eluna*, T*)>::type mfunc;
MethodRegisterState regState;

// constructor for non-globals (with T*)
Expand All @@ -169,7 +169,7 @@ struct ElunaRegister

// constructor for nullptr functions and METHOD_REG_NONE (unimplemented methods)
ElunaRegister(const char* name = nullptr, MethodRegisterState state = METHOD_REG_NONE)
: name(name), mfunc(std::monostate{}), regState(state) {}
: name(name), mfunc(nullptr), regState(state) {}
};

template<typename T = void>
Expand Down Expand Up @@ -277,17 +277,16 @@ class ElunaTemplate
lua_pop(E->L, 1);
}

template<typename C = void>
template<typename C>
static void SetMethods(Eluna* E, ElunaRegister<C>* methodTable)
{
ASSERT(E);
ASSERT(methodTable);

// determine if the method table functions are global or non-global
const auto& firstMethod = methodTable[0];
bool isGlobal = std::holds_alternative<int(*)(Eluna*)>(firstMethod.mfunc);
constexpr bool isGlobal = std::is_same_v<T, void>;

if (isGlobal)
if constexpr (isGlobal)
{
lua_pushglobaltable(E->L);
}
Expand Down Expand Up @@ -428,17 +427,9 @@ class ElunaTemplate

int expected = 0;
if constexpr (isGlobal)
{
auto func = std::get_if<int(*)(Eluna*)>(&l->mfunc);
if (func)
expected = (*func)(E);
}
expected = l->mfunc(E); // global method
else
{
auto func = std::get_if<int(*)(Eluna*, T*)>(&l->mfunc);
if (func)
expected = (*func)(E, obj);
}
expected = l->mfunc(E, obj); // non-global method

int args = lua_gettop(L) - top;
if (args < 0 || args > expected)
Expand Down

0 comments on commit 7dce466

Please sign in to comment.