diff --git a/sol/function.hpp b/sol/function.hpp index d349f9c9..cbdf5109 100644 --- a/sol/function.hpp +++ b/sol/function.hpp @@ -26,7 +26,7 @@ #include "tuple.hpp" #include "stack.hpp" #include "function_types.hpp" -#include "userdata_traits.hpp" +#include "usertype_traits.hpp" #include "resolve.hpp" #include #include @@ -191,7 +191,7 @@ struct pusher> { template static void set_fx(lua_State* L, std::unique_ptr luafunc) { - auto&& metakey = userdata_traits>::metatable; + auto&& metakey = usertype_traits>::metatable; const char* metatablename = std::addressof(metakey[0]); base_function* target = luafunc.release(); void* userdata = reinterpret_cast(target); diff --git a/sol/function_types.hpp b/sol/function_types.hpp index d121c8c6..8000033a 100644 --- a/sol/function_types.hpp +++ b/sol/function_types.hpp @@ -248,7 +248,7 @@ struct base_function { } template - struct userdata { + struct usertype { static int call(lua_State* L) { // Zero-based template parameter, but upvalues start at 1 return base_call(L, stack::get(L, I + 1)); @@ -384,7 +384,7 @@ struct member_function : public base_function { }; template -struct userdata_function_core : public base_function { +struct usertype_function_core : public base_function { typedef typename std::remove_pointer::type T; typedef typename std::remove_pointer::type>::type function_type; typedef detail::functor fx_t; @@ -395,7 +395,7 @@ struct userdata_function_core : public base_function { fx_t fx; template - userdata_function_core(FxArgs&&... fxargs): fx(std::forward(fxargs)...) {} + usertype_function_core(FxArgs&&... fxargs): fx(std::forward(fxargs)...) {} template> typename std::enable_if::value, void>::type push(lua_State* L, Return&& r) { @@ -443,15 +443,15 @@ struct userdata_function_core : public base_function { }; template -struct userdata_function : public userdata_function_core { - typedef userdata_function_core base_t; +struct usertype_function : public usertype_function_core { + typedef usertype_function_core base_t; typedef typename std::remove_pointer::type T; typedef typename base_t::traits_type traits_type; typedef typename base_t::args_type args_type; typedef typename base_t::return_type return_type; template - userdata_function(FxArgs&&... fxargs): base_t(std::forward(fxargs)...) {} + usertype_function(FxArgs&&... fxargs): base_t(std::forward(fxargs)...) {} template int fx_call(lua_State* L) { @@ -472,15 +472,15 @@ struct userdata_function : public userdata_function_core { }; template -struct userdata_variable_function : public userdata_function_core { - typedef userdata_function_core base_t; +struct usertype_variable_function : public usertype_function_core { + typedef usertype_function_core base_t; typedef typename std::remove_pointer::type T; typedef typename base_t::traits_type traits_type; typedef typename base_t::args_type args_type; typedef typename base_t::return_type return_type; template - userdata_variable_function(FxArgs&&... fxargs): base_t(std::forward(fxargs)...) {} + usertype_variable_function(FxArgs&&... fxargs): base_t(std::forward(fxargs)...) {} template int fx_call(lua_State* L) { @@ -511,8 +511,8 @@ struct userdata_variable_function : public userdata_function_core }; template -struct userdata_indexing_function : public userdata_function_core { - typedef userdata_function_core base_t; +struct usertype_indexing_function : public usertype_function_core { + typedef usertype_function_core base_t; typedef typename std::remove_pointer::type T; typedef typename base_t::traits_type traits_type; typedef typename base_t::args_type args_type; @@ -522,7 +522,7 @@ struct userdata_indexing_function : public userdata_function_core std::unordered_map, bool>> functions; template - userdata_indexing_function(std::string name, FxArgs&&... fxargs): base_t(std::forward(fxargs)...), name(std::move(name)) {} + usertype_indexing_function(std::string name, FxArgs&&... fxargs): base_t(std::forward(fxargs)...), name(std::move(name)) {} template int fx_call(lua_State* L) { @@ -532,10 +532,10 @@ struct userdata_indexing_function : public userdata_function_core if(function->second.second) { stack::push(L, function->second.first.get()); if(std::is_same::value) { - stack::push(L, &base_function::userdata<0>::ref_call, 1); + stack::push(L, &base_function::usertype<0>::ref_call, 1); } else { - stack::push(L, &base_function::userdata<0>::call, 1); + stack::push(L, &base_function::usertype<0>::call, 1); } return 1; } diff --git a/sol/stack.hpp b/sol/stack.hpp index 5ad445d9..4b8ec1f1 100644 --- a/sol/stack.hpp +++ b/sol/stack.hpp @@ -26,7 +26,7 @@ #include "reference.hpp" #include "tuple.hpp" #include "traits.hpp" -#include "userdata_traits.hpp" +#include "usertype_traits.hpp" #include #include #include @@ -259,19 +259,19 @@ struct pusher { template, EnableIf>, Not>, Not>, Not>> = 0> static void push(lua_State* L, T& t) { - detail::push_userdata(L, userdata_traits::metatable, t); + detail::push_userdata(L, usertype_traits::metatable, t); } template, EnableIf>, Not>, Not>, Not>> = 0> static void push(lua_State* L, T&& t) { - detail::push_userdata(L, userdata_traits::metatable, std::move(t)); + detail::push_userdata(L, usertype_traits::metatable, std::move(t)); } }; template struct pusher { static void push(lua_State* L, T* obj) { - detail::push_userdata(L, userdata_traits::metatable, obj); + detail::push_userdata(L, usertype_traits::metatable, obj); } }; diff --git a/sol/state.hpp b/sol/state.hpp index b7401888..77a11ed8 100644 --- a/sol/state.hpp +++ b/sol/state.hpp @@ -148,26 +148,26 @@ public: } template - state& set_userdata(userdata& user) { - return set_userdata(user.name(), user); + state& set_usertype(usertype& user) { + return set_usertype(usertype_traits::name, user); } template - state& set_userdata(Key&& key, userdata& user) { - global.set_userdata(std::forward(key), user); + state& set_usertype(Key&& key, usertype& user) { + global.set_usertype(std::forward(key), user); return *this; } template - state& new_userdata(const std::string& name, Args&&... args) { + state& new_usertype(const std::string& name, Args&&... args) { constructors> ctor{}; - return new_userdata(name, ctor, std::forward(args)...); + return new_usertype(name, ctor, std::forward(args)...); } template - state& new_userdata(const std::string& name, constructors ctor, Args&&... args) { - userdata udata(name, ctor, std::forward(args)...); - global.set_userdata(udata); + state& new_usertype(const std::string& name, constructors ctor, Args&&... args) { + usertype utype(ctor, std::forward(args)...); + set_usertype(name, utype); return *this; } diff --git a/sol/table.hpp b/sol/table.hpp index 1420855b..04290db9 100644 --- a/sol/table.hpp +++ b/sol/table.hpp @@ -25,7 +25,7 @@ #include "proxy.hpp" #include "stack.hpp" #include "function_types.hpp" -#include "userdata.hpp" +#include "usertype.hpp" namespace sol { class table : public reference { @@ -84,12 +84,12 @@ public: } template - table& set_userdata(userdata& user) { - return set_userdata(user.name(), user); + table& set_usertype(usertype& user) { + return set_userdata(usertype_traits::name, user); } template - table& set_userdata(Key&& key, userdata& user) { + table& set_usertype(Key&& key, usertype& user) { push(); stack::push(state(), std::forward(key)); stack::push(state(), user); diff --git a/sol/userdata.hpp b/sol/usertype.hpp similarity index 85% rename from sol/userdata.hpp rename to sol/usertype.hpp index 48ddb023..f49806bc 100644 --- a/sol/userdata.hpp +++ b/sol/usertype.hpp @@ -20,11 +20,11 @@ // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #ifndef SOL_USERDATA_HPP -#define SOL_USERDATA_HPP +#define SOL_USERTYPE_HPP #include "state.hpp" #include "function_types.hpp" -#include "userdata_traits.hpp" +#include "usertype_traits.hpp" #include "default_construct.hpp" #include #include @@ -88,7 +88,7 @@ enum class meta_function { }; template -class userdata { +class usertype { private: typedef std::unordered_map, bool>> function_map_t; function_map_t indexmetafunctions, newindexmetafunctions; @@ -97,7 +97,6 @@ private: std::vector metafunctiontable; std::vector ptrmetafunctiontable; lua_CFunction cleanup; - std::string luaname; template struct constructor { @@ -121,7 +120,7 @@ private: } static int construct(lua_State* L) { - auto&& meta = userdata_traits::metatable; + auto&& meta = usertype_traits::metatable; call_syntax syntax = stack::get_call_syntax(L, meta); int argcount = lua_gettop(L); @@ -131,7 +130,7 @@ private: if(luaL_newmetatable(L, std::addressof(meta[0])) == 1) { lua_pop(L, 1); - std::string err = "Unable to get userdata metatable for "; + std::string err = "Unable to get usertype metatable for "; err += meta; throw error(err); } @@ -153,7 +152,7 @@ private: template void build_cleanup() { - cleanup = &base_function::userdata::gc; + cleanup = &base_function::usertype::gc; } template @@ -161,13 +160,13 @@ private: int extracount = 0; if(!indexmetafunctions.empty()) { if(index == nullptr) { - auto idxptr = detail::make_unique>("__index", nullptr); + auto idxptr = detail::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::userdata::call }); - ptrmetafunctiontable.push_back({ name.c_str(), &base_function::userdata::ref_call }); + 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; @@ -177,18 +176,18 @@ private: } if(!newindexmetafunctions.empty()) { if(newindex == nullptr) { - auto idxptr = detail::make_unique>("__newindex", nullptr); + auto idxptr = detail::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::userdata::call }); - ptrmetafunctiontable.push_back({ name.c_str(), &base_function::userdata::ref_call }); + 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::userdata::call }); - ptrmetafunctiontable.push_back({ name.c_str(), &base_function::userdata::ref_call }); + metafunctiontable.push_back({ name.c_str(), &base_function::usertype::call }); + ptrmetafunctiontable.push_back({ name.c_str(), &base_function::usertype::ref_call }); } ++extracount; } @@ -215,8 +214,8 @@ private: 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 typename std::decay::type function_type; - indexmetafunctions.emplace(funcname, std::make_pair(detail::make_unique>(func), false)); - newindexmetafunctions.emplace(funcname, std::make_pair(detail::make_unique>(func), false)); + indexmetafunctions.emplace(funcname, std::make_pair(detail::make_unique>(func), false)); + newindexmetafunctions.emplace(funcname, std::make_pair(detail::make_unique>(func), false)); return false; } @@ -225,21 +224,21 @@ private: 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 typename std::decay::type function_type; - return detail::make_unique>(func); + return detail::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 typename std::decay::type function_type; - return detail::make_unique>(func); + return detail::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 typename std::decay::type function_type; - return detail::make_unique>(func); + return detail::make_unique>(func); } template @@ -255,7 +254,7 @@ private: typedef Unqualified TArgu; 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 typename std::decay::type function_type; - return detail::make_unique>(func); + return detail::make_unique>(func); } template @@ -268,7 +267,7 @@ private: auto indexmetamethod = std::find(meta_variable_names.begin(), meta_variable_names.end(), name); std::unique_ptr ptr(nullptr); if(indexmetamethod != meta_variable_names.end()) { - auto idxptr = detail::make_unique>(name, func); + auto idxptr = detail::make_unique>(name, func); switch(std::distance(indexmetamethod, meta_variable_names.end())) { case 0: index = &(idxptr->functions); @@ -285,8 +284,8 @@ private: ptr = make_function(funcname, std::forward(func)); } metafunctions.emplace_back(std::move(ptr)); - metafunctiontable.push_back( { name.c_str(), &base_function::userdata::call } ); - ptrmetafunctiontable.push_back( { name.c_str(), &base_function::userdata::ref_call } ); + 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)); @@ -314,13 +313,10 @@ private: public: template - userdata(Args&&... args): userdata(userdata_traits::name, default_constructor, std::forward(args)...) {} + usertype(Args&&... args): usertype(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)) { + usertype(constructors, Args&&... args) { functionnames.reserve(sizeof...(args) + 2); metafunctiontable.reserve(sizeof...(args)); ptrmetafunctiontable.reserve(sizeof...(args)); @@ -343,23 +339,15 @@ public: 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::string& name() const { - return luaname; - } - void 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, userdata_traits::metatable, + push_metatable(L, usertype_traits::metatable, metafunctions, ptrmetafunctiontable); lua_pop(L, 1); - push_metatable(L, userdata_traits::metatable, + push_metatable(L, usertype_traits::metatable, metafunctions, metafunctiontable); set_global_deleter(L); } @@ -386,7 +374,7 @@ private: lua_setfield(L, -2, "__gc"); lua_setmetatable(L, -2); // gctable name by default has ♻ part of it - lua_setglobal(L, std::addressof(userdata_traits::gctable[0])); + lua_setglobal(L, std::addressof(usertype_traits::gctable[0])); } template @@ -407,12 +395,12 @@ private: namespace stack { template -struct pusher> { - static void push(lua_State* L, userdata& user) { +struct pusher> { + static void push(lua_State* L, usertype& user) { user.push(L); } }; } // stack } // sol -#endif // SOL_USERDATA_HPP +#endif // SOL_USERTYPE_HPP diff --git a/sol/userdata_traits.hpp b/sol/usertype_traits.hpp similarity index 80% rename from sol/userdata_traits.hpp rename to sol/usertype_traits.hpp index 18d28811..34871f55 100644 --- a/sol/userdata_traits.hpp +++ b/sol/usertype_traits.hpp @@ -19,29 +19,29 @@ // 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_TRAITS_HPP -#define SOL_USERDATA_TRAITS_HPP +#ifndef SOL_USERTYPE_TRAITS_HPP +#define SOL_USERTYPE_TRAITS_HPP #include "demangle.hpp" namespace sol { template -struct userdata_traits { +struct usertype_traits { static const std::string name; static const std::string metatable; static const std::string gctable; }; template -const std::string userdata_traits::name = detail::demangle(typeid(T)); +const std::string usertype_traits::name = detail::demangle(typeid(T)); template -const std::string userdata_traits::metatable = std::string("sol.").append(detail::demangle(typeid(T))); +const std::string usertype_traits::metatable = std::string("sol.").append(detail::demangle(typeid(T))); template -const std::string userdata_traits::gctable = std::string("sol.").append(detail::demangle(typeid(T))).append(".\xE2\x99\xBB"); +const std::string usertype_traits::gctable = std::string("sol.").append(detail::demangle(typeid(T))).append(".\xE2\x99\xBB"); } -#endif // SOL_USERDATA_TRAITS_HPP +#endif // SOL_USERTYPE_TRAITS_HPP