From 8ed913e8c1e375a5610e0ebbf4b1e16b086559e2 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Thu, 28 Nov 2013 22:51:51 -0500 Subject: [PATCH] Allow specification of index in get function --- sol/stack.hpp | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/sol/stack.hpp b/sol/stack.hpp index 35fb4112..42d62601 100644 --- a/sol/stack.hpp +++ b/sol/stack.hpp @@ -30,37 +30,37 @@ namespace sol { namespace stack { namespace detail { template -inline T get_unsigned(lua_State* L, std::true_type) { - return lua_tounsigned(L, -1); +inline T get_unsigned(lua_State* L, std::true_type, int index = -1) { + return lua_tounsigned(L, index); } template -inline T get_unsigned(lua_State* L, std::false_type) { - return lua_tointeger(L, -1); +inline T get_unsigned(lua_State* L, std::false_type, int index = -1) { + return lua_tointeger(L, index); } template -inline T get_arithmetic(lua_State* L, std::false_type) { +inline T get_arithmetic(lua_State* L, std::false_type, int index = -1) { // T is a floating point - return lua_tonumber(L, -1); + return lua_tonumber(L, index); } template -inline T get_arithmetic(lua_State* L, std::true_type) { +inline T get_arithmetic(lua_State* L, std::true_type, int index = -1) { // T is an integral return get_unsigned(L, std::is_unsigned{}); } template -inline T get_helper(lua_State* L, std::true_type) { +inline T get_helper(lua_State* L, std::true_type, int index = -1) { // T is a class type - return T(L, -1); + return T(L, index); } template -inline T get_helper(lua_State* L, std::false_type) { +inline T get_helper(lua_State* L, std::false_type, int index = -1) { // T is a fundamental type - return get_arithmetic(L, std::is_integral{}); + return get_arithmetic(L, std::is_integral{}, index); } template @@ -87,25 +87,25 @@ inline void push_arithmetic(lua_State* L, T x, std::false_type) { } // detail template -inline T get(lua_State* L) { - return detail::get_helper(L, std::is_class{}); +inline T get(lua_State* L, int index = -1) { + return detail::get_helper(L, std::is_class{}, index); } template<> -inline bool get(lua_State* L) { - return lua_toboolean(L, -1) != 0; +inline bool get(lua_State* L, int index) { + return lua_toboolean(L, index) != 0; } template<> -inline std::string get(lua_State* L) { +inline std::string get(lua_State* L, int index) { std::string::size_type len; - auto str = lua_tolstring(L, -1, &len); + auto str = lua_tolstring(L, index, &len); return { str, len }; } template<> -inline const char* get(lua_State* L) { - return lua_tostring(L, -1); +inline const char* get(lua_State* L, int index) { + return lua_tostring(L, index); } template