// The MIT License (MIT) // Copyright (c) 2013-2016 Rapptz and contributors // 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_USERTYPE_HPP #define SOL_USERTYPE_HPP #include "state.hpp" #include "function_types.hpp" #include "usertype_traits.hpp" #include "default_construct.hpp" #include "deprecate.hpp" #include #include #include namespace sol { const std::array meta_variable_names = {{ "__index", "__newindex" }}; const std::array meta_function_names = {{ "__index", "__newindex", "__mode", "__call", "__metatable", "__tostring", "__len", "__unm", "__add", "__sub", "__mul", "__div", "__mod", "__pow", "__concat", "__eq", "__lt", "__le", "__gc", }}; enum class meta_function { index, new_index, mode, call, metatable, to_string, length, unary_minus, addition, subtraction, multiplication, division, modulus, power_of, involution = power_of, concatenation, equal_to, less_than, less_than_or_equal_to, }; template class usertype { private: typedef std::unordered_map, bool>> function_map_t; function_map_t indexmetafunctions, newindexmetafunctions; std::vector functionnames; std::vector> metafunctions; std::vector metafunctiontable; std::vector ptrmetafunctiontable; lua_CFunction cleanup; template struct constructor { template static void do_constructor(lua_State* L, T* obj, call_syntax syntax, int, types) { default_construct fx{}; stack::call(L, -1 + static_cast(syntax), types(), types(), fx, 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) { const auto& meta = usertype_traits::metatable; call_syntax syntax = stack::get_call_syntax(L, meta); int argcount = lua_gettop(L); T** referencepointer = reinterpret_cast(lua_newuserdata(L, sizeof(T*) + sizeof(T))); T*& referencereference = *referencepointer; T* obj = reinterpret_cast(referencepointer + 1); referencereference = obj; match_constructor(L, obj, syntax, argcount - static_cast(syntax), identity_t()...); if(luaL_newmetatable(L, std::addressof(meta[0])) == 1) { lua_pop(L, 1); std::string err = "Unable to get usertype metatable for "; err += meta; throw error(err); } lua_setmetatable(L, -2); return 1; } }; static int destruct(lua_State* L) { userdata udata = stack::get(L, 1); // The first sizeof(T*) bytes are the reference: the rest is // the actual data itself (if there is a reference at all) T** pobj = reinterpret_cast(udata.value); T*& obj = *pobj; std::allocator alloc{}; alloc.destroy(obj); return 0; } template void build_cleanup() { cleanup = &base_function::usertype::gc; } template void build_function_tables(function_map_t*& index, function_map_t*& newindex) { int extracount = 0; if(!indexmetafunctions.empty()) { if(index == nullptr) { auto idxptr = std::make_unique>("__index", nullptr); index = &(idxptr->functions); functionnames.emplace_back("__index"); metafunctions.emplace_back(std::move(idxptr)); std::string& name = functionnames.back(); metafunctiontable.push_back({ name.c_str(), &base_function::usertype::call }); ptrmetafunctiontable.push_back({ name.c_str(), &base_function::usertype::ref_call }); ++extracount; } auto& idx = *index; for(auto&& namedfunc : indexmetafunctions) { idx.emplace(std::move(namedfunc.first), std::move(namedfunc.second)); } } if(!newindexmetafunctions.empty()) { if(newindex == nullptr) { auto idxptr = std::make_unique>("__newindex", nullptr); newindex = &(idxptr->functions); functionnames.emplace_back("__newindex"); metafunctions.emplace_back(std::move(idxptr)); std::string& name = functionnames.back(); if(extracount > 0) { metafunctiontable.push_back({ name.c_str(), &base_function::usertype::call }); ptrmetafunctiontable.push_back({ name.c_str(), &base_function::usertype::ref_call }); } else { metafunctiontable.push_back({ name.c_str(), &base_function::usertype::call }); ptrmetafunctiontable.push_back({ name.c_str(), &base_function::usertype::ref_call }); } ++extracount; } auto& idx = *newindex; for(auto&& namedfunc : newindexmetafunctions) { idx.emplace(std::move(namedfunc.first), std::move(namedfunc.second)); } } switch(extracount) { case 2: build_cleanup(); break; case 1: build_cleanup(); break; case 0: default: build_cleanup(); break; } } template bool build_function(std::true_type, function_map_t*&, function_map_t*&, std::string funcname, Ret Base::* func) { static_assert(std::is_base_of::value, "Any registered function must be part of the class"); typedef std::decay_t function_type; indexmetafunctions.emplace(funcname, std::make_pair(std::make_unique>(func), false)); newindexmetafunctions.emplace(funcname, std::make_pair(std::make_unique>(func), false)); return false; } template std::unique_ptr make_function(const std::string&, overload_set func) { return std::make_unique>(func); } template std::unique_ptr make_function(const std::string&, Ret(*func)(Arg, Args...)) { typedef Unqualified> Argu; static_assert(std::is_base_of::value, "Any non-member-function must have a first argument which is covariant with the desired userdata type."); typedef std::decay_t function_type; return std::make_unique>(func); } template std::unique_ptr make_variable_function(std::true_type, const std::string&, Ret Base::* func) { static_assert(std::is_base_of::value, "Any registered function must be part of the class"); typedef std::decay_t function_type; return std::make_unique>(func); } template std::unique_ptr make_variable_function(std::false_type, const std::string&, Ret Base::* func) { static_assert(std::is_base_of::value, "Any registered function must be part of the class"); typedef std::decay_t function_type; return std::make_unique>(func); } template std::unique_ptr make_function(const std::string& name, Ret Base::* func) { typedef std::decay_t function_type; return make_variable_function(std::is_member_object_pointer(), name, func); } template std::unique_ptr make_function(const std::string&, Fx&& func) { typedef Unqualified Fxu; typedef std::tuple_element_t<0, typename function_traits::args_tuple_type> Arg0; typedef Unqualified> Argu; static_assert(std::is_base_of::value, "Any non-member-function must have a first argument which is covariant with the desired usertype."); typedef std::decay_t function_type; return std::make_unique>(func); } template bool build_function(std::false_type, function_map_t*& index, function_map_t*& newindex, std::string funcname, Fx&& func) { typedef std::decay_t function_type; auto metamethod = std::find(meta_function_names.begin(), meta_function_names.end(), funcname); if(metamethod != meta_function_names.end()) { functionnames.push_back(std::move(funcname)); std::string& name = functionnames.back(); auto indexmetamethod = std::find(meta_variable_names.begin(), meta_variable_names.end(), name); std::unique_ptr baseptr(nullptr); if(indexmetamethod != meta_variable_names.end()) { auto idxptr = std::make_unique>(name, func); std::ptrdiff_t idxvalue = std::distance(meta_variable_names.begin(), indexmetamethod); switch(idxvalue) { case 0: index = &(idxptr->functions); break; case 1: newindex = &(idxptr->functions); break; default: break; } baseptr = std::move(idxptr); } else { baseptr = make_function(funcname, std::forward(func)); } metafunctions.emplace_back(std::move(baseptr)); metafunctiontable.push_back( { name.c_str(), &base_function::usertype::call } ); ptrmetafunctiontable.push_back( { name.c_str(), &base_function::usertype::ref_call } ); return true; } indexmetafunctions.emplace(funcname, std::make_pair(make_function(funcname, std::forward(func)), true)); return false; } template void build_function_tables(function_map_t*& index, function_map_t*& newindex, std::string funcname, Fx&& func, Args&&... args) { typedef std::is_member_object_pointer> is_variable; static const std::size_t V = static_cast(!is_variable::value); if(build_function(is_variable(), index, newindex, std::move(funcname), std::forward(func))) { build_function_tables(index, newindex, std::forward(args)...); } else { build_function_tables(index, newindex, std::forward(args)...); } } template void build_function_tables(function_map_t*& index, function_map_t*& newindex, meta_function metafunc, Ret Base::* func, Args&&... args) { std::size_t idx = static_cast(metafunc); const std::string& funcname = meta_function_names[idx]; build_function_tables(index, newindex, funcname, std::move(func), std::forward(args)...); } public: template usertype(Args&&... args): usertype(default_constructor, std::forward(args)...) {} template SOL_DEPRECATED usertype(std::string, std::string, Args&&... args): usertype(default_constructor, std::forward(args)...) {} template SOL_DEPRECATED usertype(const char*, std::string, Args&&... args): usertype(default_constructor, std::forward(args)...) {} template SOL_DEPRECATED usertype(std::string, constructors c, Args&&... args) : usertype(std::move(c), std::forward(args)...) {} template SOL_DEPRECATED usertype(const char*, constructors c, Args&&... args) : usertype(std::move(c), std::forward(args)...) {} template usertype(constructors, Args&&... args) { functionnames.reserve(sizeof...(args) + 2); metafunctiontable.reserve(sizeof...(args)); ptrmetafunctiontable.reserve(sizeof...(args)); function_map_t* index = nullptr; function_map_t* newindex = nullptr; build_function_tables<0>(index, newindex, std::forward(args)...); indexmetafunctions.clear(); newindexmetafunctions.clear(); functionnames.push_back("new"); metafunctiontable.push_back({ functionnames.back().c_str(), &constructor::construct }); functionnames.push_back("__gc"); metafunctiontable.push_back({ functionnames.back().c_str(), 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 metafunctiontable.push_back({ nullptr, nullptr }); ptrmetafunctiontable.push_back({ nullptr, nullptr }); } int push(lua_State* L) { // push pointer tables first, // but leave the regular T table on last // so it can be linked to a type for usage with `.new(...)` or `:new(...)` push_metatable(L, usertype_traits::metatable, metafunctions, ptrmetafunctiontable); lua_pop(L, 1); push_metatable(L, usertype_traits::metatable, metafunctions, metafunctiontable); set_global_deleter(L); return 1; } private: template static void push_metatable(lua_State* L, Meta&& metakey, MetaFuncs&& metafuncs, MetaFuncTable&& metafunctable) { luaL_newmetatable(L, std::addressof(metakey[0])); if(metafunctable.size() > 1) { // regular functions accessed through __index semantics int up = push_upvalues(L, metafuncs); luaL_setfuncs(L, metafunctable.data(), up); } } void set_global_deleter(lua_State* L) { // Automatic deleter table -- stays alive until lua VM dies // even if the user calls collectgarbage(), weirdly enough lua_createtable(L, 0, 0); lua_createtable(L, 0, 1); int up = push_upvalues(L, metafunctions); lua_pushcclosure(L, cleanup, up); lua_setfield(L, -2, "__gc"); lua_setmetatable(L, -2); // gctable name by default has ♻ part of it lua_setglobal(L, std::addressof(usertype_traits::gctable[0])); } template static int push_upvalues(lua_State* L, TCont&& cont) { int n = 0; for(auto& c : cont) { if(release) { stack::push(L, c.release()); } else { stack::push(L, c.get()); } ++n; } return n; } }; namespace stack { template struct pusher> { static int push(lua_State* L, usertype& user) { return user.push(L); } }; } // stack } // sol #endif // SOL_USERTYPE_HPP