// The MIT License (MIT) // Copyright (c) 2013-2016 Rapptz, ThePhD 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_STACK_GET_HPP #define SOL_STACK_GET_HPP #include "stack_core.hpp" #include "usertype_traits.hpp" #include "inheritance.hpp" #include "overload.hpp" #include "error.hpp" #include #include #include namespace sol { namespace stack { template struct getter { static T& get(lua_State* L, int index = -1) { return getter{}.get(L, index); } }; template struct getter::value>> { static T get(lua_State* L, int index = -1) { return static_cast(lua_tonumber(L, index)); } }; template struct getter, std::is_signed>::value>> { static T get(lua_State* L, int index = -1) { return static_cast(lua_tointeger(L, index)); } }; template struct getter, std::is_unsigned>::value>> { static T get(lua_State* L, int index = -1) { return static_cast(lua_tointeger(L, index)); } }; template struct getter::value>> { static T get(lua_State* L, int index = -1) { return static_cast(lua_tointegerx(L, index, nullptr)); } }; template struct getter::value || std::is_base_of::value>> { static T get(lua_State* L, int index = -1) { return T(L, index); } }; template<> struct getter { static userdata_value get(lua_State* L, int index = -1) { return userdata_value( lua_touserdata(L, index) ); } }; template<> struct getter { static lightuserdata_value get(lua_State* L, int index = -1) { return lightuserdata_value( lua_touserdata(L, index) ); } }; template struct getter> { static light get(lua_State* L, int index = -1) { return light( static_cast(lua_touserdata(L, index)) ); } }; template struct getter> { static T& get(lua_State* L, int index = -1) { return *static_cast(lua_touserdata(L, index)); } }; template struct getter> { static T* get(lua_State* L, int index = -1) { return static_cast(lua_touserdata(L, index)); } }; template<> struct getter { static type get(lua_State *L, int index){ return static_cast(lua_type(L, index)); } }; template<> struct getter { static bool get(lua_State* L, int index) { return lua_toboolean(L, index) != 0; } }; template<> struct getter { static std::string get(lua_State* L, int index = -1) { std::size_t len; auto str = lua_tolstring(L, index, &len); return { str, len }; } }; template <> struct getter { string_detail::string_shim get(lua_State* L, int index) { size_t len; const char* p = lua_tolstring(L, index, &len); return string_detail::string_shim(p, len); } }; template<> struct getter { static const char* get(lua_State* L, int index = -1) { return lua_tostring(L, index); } }; template<> struct getter { static meta_function get(lua_State *L, int index) { const char* name = getter{}.get(L, index); for (std::size_t i = 0; i < meta_function_names.size(); ++i) if (meta_function_names[i] == name) return static_cast(i); return meta_function::construct; } }; template<> struct getter { static char get(lua_State* L, int index = -1) { size_t len; auto str = lua_tolstring(L, index, &len); return len > 0 ? str[0] : '\0'; } }; #if 0 template<> struct getter { static std::wstring get(lua_State* L, int index = -1) { return {}; } }; template<> struct getter { static std::u16string get(lua_State* L, int index = -1) { return {}; } }; template<> struct getter { static std::u32string get(lua_State* L, int index = -1) { return {}; } }; template<> struct getter { static wchar_t get(lua_State* L, int index = -1) { auto str = getter{}.get(L, index); return str.size() > 0 ? str[0] : '\0'; } }; template<> struct getter { static char get(lua_State* L, int index = -1) { auto str = getter{}.get(L, index); return str.size() > 0 ? str[0] : '\0'; } }; template<> struct getter { static char32_t get(lua_State* L, int index = -1) { auto str = getter{}.get(L, index); return str.size() > 0 ? str[0] : '\0'; } }; #endif // For a distant future template<> struct getter { static nil_t get(lua_State*, int = -1) { return nil; } }; template<> struct pusher { static std::nullptr_t get(lua_State*, int = -1) { return nullptr; } }; template<> struct getter { static nullopt_t get(lua_State*, int = -1) { return nullopt; } }; template<> struct getter { static this_state get(lua_State* L, int = -1) { return this_state{L}; } }; template<> struct getter { static lua_CFunction get(lua_State* L, int index = -1) { return lua_tocfunction(L, index); } }; template<> struct getter { static c_closure get(lua_State* L, int index = -1) { return c_closure(lua_tocfunction(L, index), -1); } }; template<> struct getter { static error get(lua_State* L, int index = -1) { size_t sz = 0; const char* err = lua_tolstring(L, index, &sz); if (err == nullptr) { return error(detail::direct_error, ""); } return error(detail::direct_error, std::string(err, sz)); } }; template<> struct getter { static void* get(lua_State* L, int index = -1) { return lua_touserdata(L, index); } }; template struct getter { static T* get_no_nil(lua_State* L, int index = -1) { void** pudata = static_cast(lua_touserdata(L, index)); void* udata = *pudata; return get_no_nil_from(L, udata, index); } static T* get_no_nil_from(lua_State* L, void* udata, int index = -1) { #ifndef SOL_NO_EXCEPTIONS if (has_bases::value && luaL_getmetafield(L, index, &detail::base_class_check_key()[0]) != 0) { void* basecastdata = lua_touserdata(L, -1); detail::throw_cast basecast = (detail::throw_cast)basecastdata; // use the casting function to properly adjust the pointer for the desired T udata = detail::catch_cast(udata, basecast); lua_pop(L, 1); } #elif !defined(SOL_NO_RTTI) if (has_bases::value && luaL_getmetafield(L, index, &detail::base_class_cast_key()[0]) != 0) { void* basecastdata = lua_touserdata(L, -1); detail::inheritance_cast_function ic = (detail::inheritance_cast_function)basecastdata; // use the casting function to properly adjust the pointer for the desired T udata = ic(udata, typeid(T)); lua_pop(L, 1); } #else // Lol, you motherfucker if (has_bases::value && luaL_getmetafield(L, index, &detail::base_class_cast_key()[0]) != 0) { void* basecastdata = lua_touserdata(L, -1); detail::inheritance_cast_function ic = (detail::inheritance_cast_function)basecastdata; // use the casting function to properly adjust the pointer for the desired T udata = ic(udata, detail::id_for::value); lua_pop(L, 1); } #endif // No Runtime Type Information || Exceptions T* obj = static_cast(udata); return obj; } static T* get(lua_State* L, int index = -1) { type t = type_of(L, index); if (t == type::nil) return nullptr; return get_no_nil(L, index); } }; template struct getter> { static T* get(lua_State* L, int index = -1) { return getter::get_no_nil(L, index); } }; template struct getter { static T& get(lua_State* L, int index = -1) { return *getter::get_no_nil(L, index); } }; template struct getter::value>> { typedef typename unique_usertype_traits::type P; typedef typename unique_usertype_traits::actual_type Real; static Real& get(lua_State* L, int index = -1) { P** pref = static_cast(lua_touserdata(L, index)); detail::special_destruct_func* fx = static_cast(static_cast(pref + 1)); Real* mem = static_cast(static_cast(fx + 1)); return *mem; } }; template struct getter> { static T& get(lua_State* L, int index = -1) { return getter{}.get(L, index); } }; template struct getter> { template static decltype(auto) apply(std::index_sequence, lua_State* L, int index = -1) { index = index < 0 ? lua_absindex(L, index) - ( sizeof...(I) - 1 ) : index; return std::tuple(L, index + I))...>(stack::get(L, index + I)...); } static decltype(auto) get(lua_State* L, int index = -1) { return apply(std::make_index_sequence(), L, index); } }; template struct getter> { static decltype(auto) get(lua_State* L, int index = -1) { index = index < 0 ? lua_absindex(L, index) - 1 : index; return std::pair(L, index)), decltype(stack::get(L, index))>(stack::get(L, index), stack::get(L, index + 1)); } }; } // stack } // sol #endif // SOL_STACK_GET_HPP