Switched pop to get and wrote a new pop<T> function

This commit is contained in:
Rapptz 2013-11-28 18:27:27 -05:00
parent f49e35a105
commit 796fd6903e

View File

@ -30,37 +30,37 @@ namespace sol {
namespace stack { namespace stack {
namespace detail { namespace detail {
template<typename T> template<typename T>
inline T pop_unsigned(lua_State* L, std::true_type) { inline T get_unsigned(lua_State* L, std::true_type) {
return lua_tounsigned(L, -1); return lua_tounsigned(L, -1);
} }
template<typename T> template<typename T>
inline T pop_unsigned(lua_State* L, std::false_type) { inline T get_unsigned(lua_State* L, std::false_type) {
return lua_tointeger(L, -1); return lua_tointeger(L, -1);
} }
template<typename T> template<typename T>
inline T pop_arithmetic(lua_State* L, std::false_type) { inline T get_arithmetic(lua_State* L, std::false_type) {
// T is a floating point // T is a floating point
return lua_tonumber(L, -1); return lua_tonumber(L, -1);
} }
template<typename T> template<typename T>
inline T pop_arithmetic(lua_State* L, std::true_type) { inline T get_arithmetic(lua_State* L, std::true_type) {
// T is an integral // T is an integral
return pop_unsigned<T>(L, std::is_unsigned<T>{}); return get_unsigned<T>(L, std::is_unsigned<T>{});
} }
template<typename T> template<typename T>
inline T pop_helper(lua_State* L, std::true_type) { inline T get_helper(lua_State* L, std::true_type) {
// T is a class type // T is a class type
return T(L, -1); return T(L, -1);
} }
template<typename T> template<typename T>
inline T pop_helper(lua_State* L, std::false_type) { inline T get_helper(lua_State* L, std::false_type) {
// T is a fundamental type // T is a fundamental type
return pop_arithmetic<T>(L, std::is_integral<T>{}); return get_arithmetic<T>(L, std::is_integral<T>{});
} }
template<typename T> template<typename T>
@ -87,32 +87,32 @@ inline void push_arithmetic(lua_State* L, T x, std::false_type) {
} // detail } // detail
template<typename T> template<typename T>
inline T pop(lua_State* L) { inline T get(lua_State* L) {
auto result = detail::pop_helper<T>(L, std::is_class<T>{}); return detail::get_helper<T>(L, std::is_class<T>{});
lua_pop(L, 1);
return result;
} }
template<> template<>
inline bool pop<bool>(lua_State* L) { inline bool get<bool>(lua_State* L) {
bool result = lua_toboolean(L, -1) != 0; return lua_toboolean(L, -1) != 0;
lua_pop(L, 1);
return result;
} }
template<> template<>
inline std::string pop<std::string>(lua_State* L) { inline std::string get<std::string>(lua_State* L) {
std::string::size_type len; std::string::size_type len;
auto str = lua_tolstring(L, -1, &len); auto str = lua_tolstring(L, -1, &len);
lua_pop(L, 1);
return { str, len }; return { str, len };
} }
template<> template<>
inline const char* pop<const char*>(lua_State* L) { inline const char* get<const char*>(lua_State* L) {
auto result = lua_tostring(L, -1); return lua_tostring(L, -1);
}
template<typename T>
inline T pop(lua_State* L) {
auto r = get<T>(L);
lua_pop(L, 1); lua_pop(L, 1);
return result; return r;
} }
template<typename T> template<typename T>