From f4434393ce820dca0bcbc3a5e74bd3100dbe51b9 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Sun, 2 Apr 2017 16:30:14 -0400 Subject: [PATCH] add single --- single/sol/sol.hpp | 486 ++++++++++++++++++++++++++++++--------------- 1 file changed, 330 insertions(+), 156 deletions(-) diff --git a/single/sol/sol.hpp b/single/sol/sol.hpp index 7df12ae4..bc4a43d7 100644 --- a/single/sol/sol.hpp +++ b/single/sol/sol.hpp @@ -20,8 +20,8 @@ // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // This file was generated with a script. -// Generated 2017-04-01 02:39:08.628086 UTC -// This header was generated with sol v2.16.0 (revision 91aff61) +// Generated 2017-04-02 20:29:59.118420 UTC +// This header was generated with sol v2.16.0 (revision 34c7b74) // https://github.com/ThePhD/sol2 #ifndef SOL_SINGLE_INCLUDE_HPP @@ -799,7 +799,7 @@ namespace sol { #include #else #include -#endif // C++-compiler Lua +#endif // C++ Mangming for Lua #if defined(_WIN32) || defined(_MSC_VER) #ifndef SOL_CODECVT_SUPPORT @@ -890,9 +890,8 @@ namespace sol { #ifndef SOL_NO_COMPAT -#ifdef __cplusplus -extern "C" { -#endif +#ifdef SOL_USING_CXX_LUA + // beginning of sol/compatibility/5.1.0.h #ifndef SOL_5_1_0_H @@ -1812,10 +1811,17 @@ inline void luaL_pushresult(luaL_Buffer_52 *B) { #endif // SOL_5_X_X_INL // end of sol/compatibility/5.x.x.inl +#else + +#ifdef __cplusplus +extern "C" { +#endif #ifdef __cplusplus } #endif +#endif // C++ Mangling for Lua + #endif // SOL_NO_COMPAT // end of sol/compatibility.hpp @@ -3083,6 +3089,9 @@ namespace sol { return std::addressof(item); } }; + + struct unchecked_t {}; + const unchecked_t unchecked = unchecked_t{}; } // detail struct lua_nil_t {}; @@ -3550,10 +3559,16 @@ namespace sol { using table_core = basic_table_core; template using stack_table_core = basic_table_core; + template + using basic_table = basic_table_core; typedef table_core table; typedef table_core global_table; typedef stack_table_core stack_table; typedef stack_table_core stack_global_table; + template + struct basic_environment; + using environment = basic_environment; + using stack_environment = basic_environment; template class basic_function; template @@ -3974,6 +3989,22 @@ namespace sol { inline bool operator!= (const stack_reference& l, const stack_reference& r) { return !operator==(l, r); } + + inline bool operator==(const stack_reference& lhs, const lua_nil_t&) { + return !lhs.valid(); + } + + inline bool operator==(const lua_nil_t&, const stack_reference& rhs) { + return !rhs.valid(); + } + + inline bool operator!=(const stack_reference& lhs, const lua_nil_t&) { + return lhs.valid(); + } + + inline bool operator!=(const lua_nil_t&, const stack_reference& rhs) { + return rhs.valid(); + } } // sol // end of sol/stack_reference.hpp @@ -4142,6 +4173,22 @@ namespace sol { inline bool operator!= (const reference& l, const reference& r) { return !operator==(l, r); } + + inline bool operator==(const reference& lhs, const lua_nil_t&) { + return !lhs.valid(); + } + + inline bool operator==(const lua_nil_t&, const reference& rhs) { + return !rhs.valid(); + } + + inline bool operator!=(const reference& lhs, const lua_nil_t&) { + return lhs.valid(); + } + + inline bool operator!=(const lua_nil_t&, const reference& rhs) { + return rhs.valid(); + } } // sol // end of sol/reference.hpp @@ -4150,81 +4197,6 @@ namespace sol { // beginning of sol/stack_core.hpp -// beginning of sol/userdata.hpp - -namespace sol { - template - class basic_userdata : public base_t { - public: - basic_userdata() noexcept = default; - template , basic_userdata>>, meta::neg>, std::is_base_of>> = meta::enabler> - basic_userdata(T&& r) noexcept : base_t(std::forward(r)) { -#ifdef SOL_CHECK_ARGUMENTS - if (!is_userdata>::value) { - auto pp = stack::push_pop(*this); - type_assert(base_t::lua_state(), -1, type::userdata); - } -#endif // Safety - } - basic_userdata(const basic_userdata&) = default; - basic_userdata(basic_userdata&&) = default; - basic_userdata& operator=(const basic_userdata&) = default; - basic_userdata& operator=(basic_userdata&&) = default; - basic_userdata(const stack_reference& r) : basic_userdata(r.lua_state(), r.stack_index()) {} - basic_userdata(stack_reference&& r) : basic_userdata(r.lua_state(), r.stack_index()) {} - template >>, meta::neg>> = meta::enabler> - basic_userdata(lua_State* L, T&& r) : basic_userdata(L, sol::ref_index(r.registry_index())) {} - basic_userdata(lua_State* L, int index = -1) : base_t(L, index) { -#ifdef SOL_CHECK_ARGUMENTS - type_assert(L, index, type::userdata); -#endif // Safety - } - basic_userdata(lua_State* L, ref_index index) : base_t(L, index) { -#ifdef SOL_CHECK_ARGUMENTS - auto pp = stack::push_pop(*this); - type_assert(L, -1, type::userdata); -#endif // Safety - } - }; - - template - class basic_lightuserdata : public base_t { - public: - basic_lightuserdata() noexcept = default; - template , basic_lightuserdata>>, meta::neg>, std::is_base_of>> = meta::enabler> - basic_lightuserdata(T&& r) noexcept : base_t(std::forward(r)) { -#ifdef SOL_CHECK_ARGUMENTS - if (!is_userdata>::value) { - auto pp = stack::push_pop(*this); - type_assert(base_t::lua_state(), -1, type::lightuserdata); - } -#endif // Safety - } - basic_lightuserdata(const basic_lightuserdata&) = default; - basic_lightuserdata(basic_lightuserdata&&) = default; - basic_lightuserdata& operator=(const basic_lightuserdata&) = default; - basic_lightuserdata& operator=(basic_lightuserdata&&) = default; - basic_lightuserdata(const stack_reference& r) : basic_lightuserdata(r.lua_state(), r.stack_index()) {} - basic_lightuserdata(stack_reference&& r) : basic_lightuserdata(r.lua_state(), r.stack_index()) {} - template >>, meta::neg>> = meta::enabler> - basic_lightuserdata(lua_State* L, T&& r) : basic_lightuserdata(L, sol::ref_index(r.registry_index())) {} - basic_lightuserdata(lua_State* L, int index = -1) : base_t(L, index) { -#ifdef SOL_CHECK_ARGUMENTS - type_assert(L, index, type::lightuserdata); -#endif // Safety - } - basic_lightuserdata(lua_State* L, ref_index index) : base_t(L, index) { -#ifdef SOL_CHECK_ARGUMENTS - auto pp = stack::push_pop(*this); - type_assert(L, -1, type::lightuserdata); -#endif // Safety - } - }; - -} // sol - -// end of sol/userdata.hpp - // beginning of sol/tie.hpp namespace sol { @@ -7323,6 +7295,140 @@ namespace sol { // end of sol/stack.hpp +// beginning of sol/object_base.hpp + +namespace sol { + + template + class basic_object_base : public base_t { + private: + template + decltype(auto) as_stack(std::true_type) const { + return stack::get(base_t::lua_state(), base_t::stack_index()); + } + + template + decltype(auto) as_stack(std::false_type) const { + base_t::push(); + return stack::pop(base_t::lua_state()); + } + + template + bool is_stack(std::true_type) const { + return stack::check(base_t::lua_state(), base_t::stack_index(), no_panic); + } + + template + bool is_stack(std::false_type) const { + int r = base_t::registry_index(); + if (r == LUA_REFNIL) + return meta::any_same, lua_nil_t, nullopt_t, std::nullptr_t>::value ? true : false; + if (r == LUA_NOREF) + return false; + auto pp = stack::push_pop(*this); + return stack::check(base_t::lua_state(), -1, no_panic); + } + + public: + basic_object_base() noexcept = default; + basic_object_base(const basic_object_base&) = default; + basic_object_base(basic_object_base&&) = default; + basic_object_base& operator=(const basic_object_base&) = default; + basic_object_base& operator=(basic_object_base&&) = default; + template , basic_object_base>>> = meta::enabler> + basic_object_base(T&& arg, Args&&... args) : base_t(std::forward(arg), std::forward(args)...) { } + + template + decltype(auto) as() const { + return as_stack(std::is_same()); + } + + template + bool is() const { + return is_stack(std::is_same()); + } + }; +} // sol + +// end of sol/object_base.hpp + +// beginning of sol/userdata.hpp + +namespace sol { + template + class basic_userdata : public basic_table { + typedef basic_table base_t; + public: + basic_userdata() noexcept = default; + template , basic_userdata>>, meta::neg>, std::is_base_of>> = meta::enabler> + basic_userdata(T&& r) noexcept : base_t(std::forward(r)) { +#ifdef SOL_CHECK_ARGUMENTS + if (!is_userdata>::value) { + auto pp = stack::push_pop(*this); + type_assert(base_t::lua_state(), -1, type::userdata); + } +#endif // Safety + } + basic_userdata(const basic_userdata&) = default; + basic_userdata(basic_userdata&&) = default; + basic_userdata& operator=(const basic_userdata&) = default; + basic_userdata& operator=(basic_userdata&&) = default; + basic_userdata(const stack_reference& r) : basic_userdata(r.lua_state(), r.stack_index()) {} + basic_userdata(stack_reference&& r) : basic_userdata(r.lua_state(), r.stack_index()) {} + template >>, meta::neg, ref_index>>> = meta::enabler> + basic_userdata(lua_State* L, T&& r) : basic_userdata(L, sol::ref_index(r.registry_index())) {} + basic_userdata(lua_State* L, int index = -1) : base_t(L, index) { +#ifdef SOL_CHECK_ARGUMENTS + type_assert(L, index, type::userdata); +#endif // Safety + } + basic_userdata(lua_State* L, ref_index index) : base_t(L, index) { +#ifdef SOL_CHECK_ARGUMENTS + auto pp = stack::push_pop(*this); + type_assert(L, -1, type::userdata); +#endif // Safety + } + }; + + template + class basic_lightuserdata : public basic_object_base< base_type > { + typedef basic_object_base base_t; + public: + basic_lightuserdata() noexcept = default; + template , basic_lightuserdata>>, meta::neg>, std::is_base_of>> = meta::enabler> + basic_lightuserdata(T&& r) noexcept : base_t(std::forward(r)) { +#ifdef SOL_CHECK_ARGUMENTS + if (!is_lightuserdata>::value) { + auto pp = stack::push_pop(*this); + type_assert(base_t::lua_state(), -1, type::lightuserdata); + } +#endif // Safety + } + basic_lightuserdata(const basic_lightuserdata&) = default; + basic_lightuserdata(basic_lightuserdata&&) = default; + basic_lightuserdata& operator=(const basic_lightuserdata&) = default; + basic_lightuserdata& operator=(basic_lightuserdata&&) = default; + basic_lightuserdata(const stack_reference& r) : basic_lightuserdata(r.lua_state(), r.stack_index()) {} + basic_lightuserdata(stack_reference&& r) : basic_lightuserdata(r.lua_state(), r.stack_index()) {} + template >>, meta::neg, ref_index>>> = meta::enabler> + basic_lightuserdata(lua_State* L, T&& r) : basic_lightuserdata(L, sol::ref_index(r.registry_index())) {} + basic_lightuserdata(lua_State* L, int index = -1) : base_t(L, index) { +#ifdef SOL_CHECK_ARGUMENTS + type_assert(L, index, type::lightuserdata); +#endif // Safety + } + basic_lightuserdata(lua_State* L, ref_index index) : base_t(L, index) { +#ifdef SOL_CHECK_ARGUMENTS + auto pp = stack::push_pop(*this); + type_assert(L, -1, type::lightuserdata); +#endif // Safety + } + }; + +} // sol + +// end of sol/userdata.hpp + // beginning of sol/as_args.hpp namespace sol { @@ -9987,35 +10093,10 @@ namespace sol { return r; } - template - class basic_object : public base_t { + template + class basic_object : public basic_object_base { private: - template - decltype(auto) as_stack(std::true_type) const { - return stack::get(base_t::lua_state(), base_t::stack_index()); - } - - template - decltype(auto) as_stack(std::false_type) const { - base_t::push(); - return stack::pop(base_t::lua_state()); - } - - template - bool is_stack(std::true_type) const { - return stack::check(base_t::lua_state(), base_t::stack_index(), no_panic); - } - - template - bool is_stack(std::false_type) const { - int r = base_t::registry_index(); - if (r == LUA_REFNIL) - return meta::any_same, lua_nil_t, nullopt_t, std::nullptr_t>::value ? true : false; - if (r == LUA_NOREF) - return false; - auto pp = stack::push_pop(*this); - return stack::check(base_t::lua_state(), -1, no_panic); - } + typedef basic_object_base base_t; template basic_object(std::integral_constant, lua_State* L, int index = -1) noexcept : base_t(L, index) { @@ -10026,7 +10107,7 @@ namespace sol { public: basic_object() noexcept = default; - template , basic_object>>, meta::neg>, std::is_base_of>> = meta::enabler> + template , basic_object>>, meta::neg>, std::is_base_of>> = meta::enabler> basic_object(T&& r) : base_t(std::forward(r)) {} basic_object(lua_nil_t r) : base_t(r) {} basic_object(const basic_object&) = default; @@ -10045,22 +10126,12 @@ namespace sol { basic_object(lua_State* L, in_place_t, T&& arg, Args&&... args) noexcept : basic_object(L, in_place, std::forward(arg), std::forward(args)...) {} basic_object& operator=(const basic_object&) = default; basic_object& operator=(basic_object&&) = default; - basic_object& operator=(const base_t& b) { base_t::operator=(b); return *this; } - basic_object& operator=(base_t&& b) { base_t::operator=(std::move(b)); return *this; } + basic_object& operator=(const base_type& b) { base_t::operator=(b); return *this; } + basic_object& operator=(base_type&& b) { base_t::operator=(std::move(b)); return *this; } template basic_object& operator=(const proxy_base& r) { this->operator=(r.operator basic_object()); return *this; } template basic_object& operator=(proxy_base&& r) { this->operator=(r.operator basic_object()); return *this; } - - template - decltype(auto) as() const { - return as_stack(std::is_same()); - } - - template - bool is() const { - return is_stack(std::is_same()); - } }; template @@ -10072,22 +10143,6 @@ namespace sol { object make_object(lua_State* L, Args&&... args) { return make_reference(L, std::forward(args)...); } - - inline bool operator==(const object& lhs, const lua_nil_t&) { - return !lhs.valid(); - } - - inline bool operator==(const lua_nil_t&, const object& rhs) { - return !rhs.valid(); - } - - inline bool operator!=(const object& lhs, const lua_nil_t&) { - return lhs.valid(); - } - - inline bool operator!=(const lua_nil_t&, const object& rhs) { - return rhs.valid(); - } } // sol // end of sol/object.hpp @@ -12151,8 +12206,24 @@ namespace sol { } } - template - class basic_table_core : public base_t { + struct new_table { + int sequence_hint = 0; + int map_hint = 0; + + new_table() = default; + new_table(const new_table&) = default; + new_table(new_table&&) = default; + new_table& operator=(const new_table&) = default; + new_table& operator=(new_table&&) = default; + + new_table(int sequence_hint, int map_hint = 0) : sequence_hint(sequence_hint), map_hint(map_hint) {} + }; + + const new_table create = new_table{}; + + template + class basic_table_core : public basic_object_base { + typedef basic_object_base base_t; friend class state; friend class state_view; @@ -12265,30 +12336,26 @@ namespace sol { traverse_set_deep(std::forward(keys)...); } - basic_table_core(lua_State* L, detail::global_tag t) noexcept : reference(L, t) { } - + basic_table_core(lua_State* L, detail::global_tag t) noexcept : base_t(L, t) { } + public: - typedef basic_table_iterator iterator; + typedef basic_table_iterator iterator; typedef iterator const_iterator; - basic_table_core() noexcept : base_t() { } - template , basic_table_core>>, meta::neg>, std::is_base_of>> = meta::enabler> - basic_table_core(T&& r) noexcept : base_t(std::forward(r)) { -#ifdef SOL_CHECK_ARGUMENTS - if (!is_table>::value) { - auto pp = stack::push_pop(*this); - stack::check(base_t::lua_state(), -1, type_panic); - } -#endif // Safety - } + basic_table_core() noexcept = default; basic_table_core(const basic_table_core&) = default; basic_table_core(basic_table_core&&) = default; basic_table_core& operator=(const basic_table_core&) = default; basic_table_core& operator=(basic_table_core&&) = default; basic_table_core(const stack_reference& r) : basic_table_core(r.lua_state(), r.stack_index()) {} basic_table_core(stack_reference&& r) : basic_table_core(r.lua_state(), r.stack_index()) {} - template >>, meta::neg>> = meta::enabler> + template >>, meta::neg, ref_index>>> = meta::enabler> basic_table_core(lua_State* L, T&& r) : basic_table_core(L, sol::ref_index(r.registry_index())) {} + basic_table_core(lua_State* L, new_table nt) : base_t(L, (lua_createtable(L, nt.sequence_hint, nt.map_hint), -1)) { + if (!std::is_base_of::value) { + lua_pop(L, 1); + } + } basic_table_core(lua_State* L, int index = -1) : base_t(L, index) { #ifdef SOL_CHECK_ARGUMENTS stack::check(L, index, type_panic); @@ -12300,6 +12367,15 @@ namespace sol { stack::check(L, -1, type_panic); #endif // Safety } + template , basic_table_core>>, meta::neg>, std::is_base_of>> = meta::enabler> + basic_table_core(T&& r) noexcept : base_t(std::forward(r)) { +#ifdef SOL_CHECK_ARGUMENTS + if (!is_table>::value) { + auto pp = stack::push_pop(*this); + stack::check(base_t::lua_state(), -1, type_panic); + } +#endif // Safety + } iterator begin() const { return iterator(*this); @@ -12593,6 +12669,56 @@ namespace sol { // end of sol/table.hpp +// beginning of sol/environment.hpp + +namespace sol { + + template + struct basic_environment : basic_table { + private: + typedef basic_table table_t; + public: + basic_environment() noexcept = default; + basic_environment(const basic_environment&) = default; + basic_environment(basic_environment&&) = default; + basic_environment& operator=(const basic_environment&) = default; + basic_environment& operator=(basic_environment&&) = default; + + basic_environment(lua_State* L, sol::new_table t, const sol::reference& fallback) : table_t(L, std::move(t)) { + sol::stack_table mt(L, sol::new_table(0, 1)); + mt.set(sol::meta_function::index, fallback); + this->set(metatable_key, mt); + mt.pop(); + } + template , basic_environment>>, meta::boolean...>::value)>> = meta::enabler> + basic_environment(T&& arg, Args&&... args) : table_t(std::forward(arg), std::forward(args)...) { } + }; + + template + void set_environment(const reference& target, const basic_environment& env) { + lua_State* L = target.lua_state(); +#if SOL_LUA_VERSION < 502 + // Use lua_setfenv + target.push(); + env.push(); + lua_setfenv(L, -2); + env.pop(); + target.pop(); +#else + // Use upvalues as explained in Lua 5.2 and beyond's manual + target.push(); + env.push(); + if (lua_setupvalue(L, -2, 1) == nullptr) { + env.pop(); + } + target.pop(); +#endif + } + +} // sol + +// end of sol/environment.hpp + // beginning of sol/load_result.hpp namespace sol { @@ -12935,6 +13061,28 @@ namespace sol { return require_core(key, [this, &filename]() {stack::script_file(L, filename); }, create_global); } + template + protected_function_result do_string(const std::string& code, const basic_environment& env) { + load_status x = static_cast(luaL_loadstring(L, code.c_str())); + if (x != load_status::ok) { + return protected_function_result(L, -1, 0, 1, static_cast(x)); + } + protected_function pf(L, -1); + set_environment(pf, env); + return pf(); + } + + template + protected_function_result do_file(const std::string& filename, const basic_environment& env) { + load_status x = static_cast(luaL_loadfile(L, filename.c_str())); + if (x != load_status::ok) { + return protected_function_result(L, -1, 0, 1, static_cast(x)); + } + protected_function pf(L, -1); + set_environment(pf, env); + return pf(); + } + protected_function_result do_string(const std::string& code) { load_status x = static_cast(luaL_loadstring(L, code.c_str())); if (x != load_status::ok) { @@ -12953,7 +13101,15 @@ namespace sol { return pf(); } - template + protected_function_result script(const std::string& code, const environment& env) { + return script(code, env, sol::default_on_error); + } + + protected_function_result script_file(const std::string& filename, const environment& env) { + return script_file(filename, env, sol::default_on_error); + } + + template >> = meta::enabler> protected_function_result script(const std::string& code, Fx&& on_error) { protected_function_result pfr = do_string(code); if (!pfr.valid()) { @@ -12962,7 +13118,7 @@ namespace sol { return pfr; } - template + template >> = meta::enabler> protected_function_result script_file(const std::string& filename, Fx&& on_error) { protected_function_result pfr = do_file(filename); if (!pfr.valid()) { @@ -12971,6 +13127,24 @@ namespace sol { return pfr; } + template + protected_function_result script(const std::string& code, const basic_environment& env, Fx&& on_error) { + protected_function_result pfr = do_string(code, env); + if (!pfr.valid()) { + return on_error(L, std::move(pfr)); + } + return pfr; + } + + template + protected_function_result script_file(const std::string& filename, const basic_environment& env, Fx&& on_error) { + protected_function_result pfr = do_file(filename, env); + if (!pfr.valid()) { + return on_error(L, std::move(pfr)); + } + return pfr; + } + function_result script(const std::string& code) { int index = lua_gettop(L); stack::script(L, code);