diff --git a/sol/state_view.hpp b/sol/state_view.hpp index 3212fe6b..9270b67f 100644 --- a/sol/state_view.hpp +++ b/sol/state_view.hpp @@ -355,13 +355,28 @@ namespace sol { global.new_simple_usertype(name, std::forward(args)...); return *this; } - + template state_view& new_simple_usertype(const std::string& name, constructors ctor, Args&&... args) { global.new_simple_usertype(name, ctor, std::forward(args)...); return *this; } + template + simple_usertype create_simple_usertype(Args&&... args) { + return global.create_simple_usertype(std::forward(args)...); + } + + template + simple_usertype create_simple_usertype(Args&&... args) { + return global.create_simple_usertype(std::forward(args)...); + } + + template + simple_usertype create_simple_usertype(constructors ctor, Args&&... args) { + return global.create_simple_usertype(ctor, std::forward(args)...); + } + template state_view& new_enum(const std::string& name, Args&&... args) { global.new_enum(name, std::forward(args)...); diff --git a/sol/table_core.hpp b/sol/table_core.hpp index 663f6181..a8800dae 100644 --- a/sol/table_core.hpp +++ b/sol/table_core.hpp @@ -274,7 +274,7 @@ namespace sol { template basic_table_core& new_simple_usertype(const std::string& name, Args&&... args) { - usertype utype(simple, base_t::lua_state(), std::forward(args)...); + simple_usertype utype(base_t::lua_state(), std::forward(args)...); set_usertype(name, utype); return *this; } @@ -287,11 +287,29 @@ namespace sol { template basic_table_core& new_simple_usertype(const std::string& name, constructors ctor, Args&&... args) { - usertype utype(simple, base_t::lua_state(), ctor, std::forward(args)...); + simple_usertype utype(base_t::lua_state(), ctor, std::forward(args)...); set_usertype(name, utype); return *this; } + template + simple_usertype create_simple_usertype(Args&&... args) { + simple_usertype utype(base_t::lua_state(), std::forward(args)...); + return utype; + } + + template + simple_usertype create_simple_usertype(Args&&... args) { + constructors> ctor{}; + return create_simple_usertype(ctor, std::forward(args)...); + } + + template + simple_usertype create_simple_usertype(constructors ctor, Args&&... args) { + simple_usertype utype(base_t::lua_state(), ctor, std::forward(args)...); + return utype; + } + template basic_table_core& new_enum(const std::string& name, Args&&... args) { if (read_only) { diff --git a/sol/usertype.hpp b/sol/usertype.hpp index dfe9cd0a..c841184b 100644 --- a/sol/usertype.hpp +++ b/sol/usertype.hpp @@ -32,7 +32,7 @@ namespace sol { template class usertype { - private: + protected: std::unique_ptr metatableregister; template @@ -63,6 +63,22 @@ namespace sol { } }; + template + class simple_usertype : public usertype { + protected: + lua_State* state; + + public: + template + simple_usertype(lua_State* L, Args&&... args) : state(L), usertype(simple, L, std::forward(args)...) {} + + template + void add(N&& n, F&& f) { + auto meta = (simple_usertype_metatable*) metatableregister.get(); + meta->add(state, n, f); + } + }; + namespace stack { template struct pusher> {