// The MIT License (MIT) // Copyright (c) 2013 Danny Y., Rapptz // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of // the Software, and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #ifndef SOL_USERDATA_HPP #define SOL_USERDATA_HPP #include "state.hpp" #include "function_types.hpp" #include "userdata_traits.hpp" #include "default_construct.hpp" #include #include #include namespace sol { namespace detail { template inline std::unique_ptr make_unique(Args&&... args) { return std::unique_ptr(new T(std::forward(args)...)); } } // detail template class userdata { private: const static std::array metafunctionnames; std::string luaname; std::vector functionnames; std::vector> funcs; std::vector> ptrfuncs; std::vector> metafuncs; std::vector> ptrmetafuncs; std::vector functiontable; std::vector ptrfunctiontable; std::vector metafunctiontable; std::vector ptrmetafunctiontable; template struct constructor { template static void do_constructor(lua_State* L, T* obj, call_syntax syntax, int, types) { default_construct fx{}; stack::get_call(L, 1 + static_cast(syntax), fx, types(), obj); } static void match_constructor(lua_State*, T*, call_syntax, int) { throw error("No matching constructor for the arguments provided"); } template static void match_constructor(lua_State* L, T* obj, call_syntax syntax, int argcount, types t, Args&&... args) { if (argcount == sizeof...(CArgs)) { do_constructor(L, obj, syntax, argcount, t); return; } match_constructor(L, obj, syntax, argcount, std::forward(args)...); } static int construct(lua_State* L) { auto&& meta = userdata_traits::metatable; call_syntax syntax = stack::get_call_syntax(L, meta); int argcount = lua_gettop(L); void* udata = lua_newuserdata(L, sizeof(T)); T* obj = static_cast(udata); match_constructor(L, obj, syntax, argcount - static_cast(syntax), typename identity::type()...); if (luaL_newmetatable(L, std::addressof(meta[0])) == 1) { lua_pop(L, 1); std::string err = "Unable to get userdata metatable for "; err += meta; throw error(err); } lua_setmetatable(L, -2); return 1; } }; template struct destructor { static int destruct(lua_State* L) { userdata_t udata = stack::get(L, 1); T* obj = static_cast(udata.value); std::allocator alloc{}; alloc.destroy(obj); return 0; } }; template void build_function_tables() {} template void build_function_tables(std::string funcname, Ret TBase::* func, Args&&... args) { static_assert(std::is_base_of::value, "Any registered function must be part of the class"); typedef typename std::decay::type function_type; functionnames.push_back(std::move(funcname)); std::string& name = functionnames.back(); auto metamethod = std::find(metafunctionnames.begin(), metafunctionnames.end(), name); if (metamethod != metafunctionnames.end()) { metafuncs.emplace_back(detail::make_unique>(std::move(func))); ptrmetafuncs.emplace_back(detail::make_unique::type>>(std::move(func))); metafunctiontable.push_back({ name.c_str(), &base_function::userdata::call }); ptrmetafunctiontable.push_back({ name.c_str(), &base_function::userdata::call }); build_function_tables(std::forward(args)...); } else { funcs.emplace_back(detail::make_unique>(std::move(func))); ptrfuncs.emplace_back(detail::make_unique::type>>(std::move(func))); functiontable.push_back({ name.c_str(), &base_function::userdata::call }); ptrfunctiontable.push_back({ name.c_str(), &base_function::userdata::call }); build_function_tables(std::forward(args)...); } } public: template userdata(Args&&... args): userdata(userdata_traits::name, default_constructor, std::forward(args)...) {} template userdata(constructors c, Args&&... args): userdata(userdata_traits::name, std::move(c), std::forward(args)...) {} template userdata(std::string name, constructors, Args&&... args): luaname(std::move(name)) { functionnames.reserve(sizeof...(args) + 2); functiontable.reserve(sizeof...(args) + 2); ptrfunctiontable.reserve(sizeof...(args) + 2); metafunctiontable.reserve(sizeof...(args) + 2); ptrmetafunctiontable.reserve(sizeof...(args) + 2); funcs.reserve(sizeof...(args) + 2); ptrfuncs.reserve(sizeof...(args) + 2); metafuncs.reserve(sizeof...(args) + 2); ptrmetafuncs.reserve(sizeof...(args) + 2); build_function_tables<0, 0>(std::forward(args)...); functionnames.push_back("new"); functiontable.push_back({ functionnames.back().c_str(), &constructor::construct }); functionnames.push_back("__gc"); metafunctiontable.push_back({ functionnames.back().c_str(), &destructor::destruct }); // ptr_functions does not participate in garbage collection/new, // as all pointered types are considered // to be references. This makes returns of // `std::vector&` and `std::vector*` work functiontable.push_back({ nullptr, nullptr }); metafunctiontable.push_back({ nullptr, nullptr }); ptrfunctiontable.push_back({ nullptr, nullptr }); ptrmetafunctiontable.push_back({ nullptr, nullptr }); } template userdata(const char* name, constructors c, Args&&... args) : userdata(std::string(name), std::move(c), std::forward(args)...) {} const std::vector& function_names () const { return functionnames; } const std::vector>& functions () const { return funcs; } const std::vector>& reference_functions () const { return ptrfuncs; } const std::vector>& meta_functions () const { return metafuncs; } const std::vector>& meta_reference_functions () const { return ptrmetafuncs; } const std::vector& function_table () const { return functiontable; } const std::vector& reference_function_table () const { return ptrfunctiontable; } const std::vector& meta_function_table () const { return metafunctiontable; } const std::vector& meta_reference_function_table () const { return ptrmetafunctiontable; } const std::string& name () const { return luaname; } }; template const std::array userdata::metafunctionnames = { "__index", "__newindex", "__mode", "__call", "__metatable", "__tostring", "__len", "__gc", "__unm", "__add", "__sub", "__mul", "__div", "__mod", "__pow", "__concat", "__eq", "__lt", "__le", }; namespace stack { template struct pusher> { template static void push_metatable(lua_State* L, Meta&& meta, Funcs&& funcs, FuncTable&& functable, MetaFuncs&& metafuncs, MetaFuncTable&& metafunctable) { luaL_newmetatable(L, std::addressof(meta[0])); // regular functions accessed through __index semantics for (std::size_t u = 0; u < funcs.size(); ++u) { stack::push(L, funcs[u].get()); } luaL_setfuncs(L, functable.data(), static_cast(funcs.size())); // meta functions for (std::size_t u = 0; u < metafuncs.size(); ++u) { stack::push(L, metafuncs[u].get()); } luaL_setfuncs(L, metafunctable.data(), static_cast(metafuncs.size())); lua_pushvalue(L, -1); lua_setfield(L, -1, "__index"); } static void push (lua_State* L, userdata& user) { // push pointer tables first, // but leave the regular T table on last so it can be linked to a type for usage with `.new(...)` push_metatable(L, userdata_traits::metatable, user.reference_functions(), user.reference_function_table(), user.meta_reference_functions(), user.meta_reference_function_table()); push_metatable(L, userdata_traits::metatable, user.functions(), user.function_table(), user.meta_functions(), user.meta_function_table()); } }; } // stack } // sol #endif // SOL_USERDATA_HPP