You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With sol2, you can query the metatable of tables and table proxys using my_table[sol::metatable_key]. Is there any way to query the metatable of a sol::protected_function directly using the sol API? It doesn't implement operator[] and I found no way to convert it to a table or proxy type. A generic function like sol::getmetatable would be cool.
My use case is that I want to attach metadata to my C++ functions like so:
Now I want to query this metadata in a context (function body of foobar), where I don't have g["bar"], but rather a sol::protected_function. I managed to retrieve the metatable with sol::state::script and one line of LUA code, but it would be nice to have something in the C++ API (which might boost performance as well?):
template <typename Table, typename Key>
autoget_metadata(sol::state_view& lua, Table&& t, Key&& key)
{
// This works, but I was hoping for a cleaner solution
lua["_tmp"] = t;
lua.script(R"( _tmp = getmetatable(_tmp))");
sol::object ret = lua["_tmp"][std::forward<Key>(key)];
return ret;
}
voidfoobar(sol::state_view& lua, sol::protected_function const& f)
{
// in this context, I don't have access to f as a proxy or table
std::cout << get_metadata(lua, f, "foo").as<std::string>() << "\n";
}
Thanks a lot for this amazing library!
With sol2, you can query the metatable of tables and table proxys using
my_table[sol::metatable_key]
. Is there any way to query the metatable of asol::protected_function
directly using the sol API? It doesn't implementoperator[]
and I found no way to convert it to a table or proxy type. A generic function likesol::getmetatable
would be cool.My use case is that I want to attach metadata to my C++ functions like so:
Now I want to query this metadata in a context (function body of
foobar
), where I don't haveg["bar"]
, but rather asol::protected_function
. I managed to retrieve the metatable withsol::state::script
and one line of LUA code, but it would be nice to have something in the C++ API (which might boost performance as well?):Run this on godbolt
The text was updated successfully, but these errors were encountered: