Make createEnumTable to replace direct Lua API usage

Co-authored-by: Nerixyz <nerixdev@outlook.de>
This commit is contained in:
Mm2PL 2024-10-03 16:24:54 +02:00
parent 944e447886
commit b525473d79
No known key found for this signature in database
GPG key ID: 94AC9B80EFA15ED9

View file

@ -308,6 +308,7 @@ bool pop(lua_State *L, T *out, StackIdx idx = -1)
* Values in this table may change. * Values in this table may change.
* *
* @returns stack index of newly created table * @returns stack index of newly created table
* @deprecated
*/ */
template <typename T> template <typename T>
StackIdx pushEnumTable(lua_State *L) StackIdx pushEnumTable(lua_State *L)
@ -326,6 +327,29 @@ StackIdx pushEnumTable(lua_State *L)
return out; return out;
} }
/**
* @brief Creates a table mapping enum names to unique values.
*
* Values in this table may change.
*
* @returns Sol reference to the table
*/
template <typename T>
requires std::is_enum_v<T>
sol::table createEnumTable(sol::state_view &lua)
{
constexpr auto values = magic_enum::enum_values<T>();
auto out = lua.create_table(0, values.size());
for (const T v : values)
{
std::string_view name = magic_enum::enum_name<T>(v);
std::string str(name);
out.raw_set(str, v);
}
return out;
}
// Represents a Lua function on the stack // Represents a Lua function on the stack
template <typename ReturnType, typename... Args> template <typename ReturnType, typename... Args>
class CallbackFunction class CallbackFunction