mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
Make createEnumTable to replace direct Lua API usage
Co-authored-by: Nerixyz <nerixdev@outlook.de>
This commit is contained in:
parent
944e447886
commit
b525473d79
1 changed files with 24 additions and 0 deletions
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue