From 95ddf46b55d2314133e1ba5421210f925838810a Mon Sep 17 00:00:00 2001 From: OrfeasZ Date: Sat, 17 Sep 2016 18:22:19 +0300 Subject: [PATCH] Introduced a new simple_usertype class which derives from usertype. Exposed an "add" function for it in order to allow adding members one at a time before registration. Introduced state_view.create_simple_usertype() methods for creating simple_usertypes. --- sol/state_view.hpp | 17 ++++++++++++++++- sol/table_core.hpp | 22 ++++++++++++++++++++-- sol/usertype.hpp | 18 +++++++++++++++++- 3 files changed, 53 insertions(+), 4 deletions(-) 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> {