2016-03-25 03:45:44 +08:00
|
|
|
// 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 <memory>
|
|
|
|
#include <functional>
|
|
|
|
#include <utility>
|
2016-08-13 01:08:59 +08:00
|
|
|
#ifdef SOL_CODECVT_SUPPORT
|
2016-08-13 00:30:18 +08:00
|
|
|
#include <codecvt>
|
|
|
|
#include <locale>
|
2016-08-13 01:08:59 +08:00
|
|
|
#endif
|
2016-03-25 03:45:44 +08:00
|
|
|
|
|
|
|
namespace sol {
|
2016-06-20 05:59:40 +08:00
|
|
|
namespace stack {
|
|
|
|
|
|
|
|
template<typename T, typename>
|
|
|
|
struct getter {
|
2016-07-29 12:57:47 +08:00
|
|
|
static T& get(lua_State* L, int index, record& tracking) {
|
2016-10-24 07:17:33 +08:00
|
|
|
return getter<sol::detail::as_value_tag<T>>{}.get(L, index, tracking);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct getter<T, std::enable_if_t<std::is_floating_point<T>::value>> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static T get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
return static_cast<T>(lua_tonumber(L, index));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct getter<T, std::enable_if_t<meta::all<std::is_integral<T>, std::is_signed<T>>::value>> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static T get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
return static_cast<T>(lua_tointeger(L, index));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct getter<T, std::enable_if_t<meta::all<std::is_integral<T>, std::is_unsigned<T>>::value>> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static T get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
return static_cast<T>(lua_tointeger(L, index));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct getter<T, std::enable_if_t<std::is_enum<T>::value>> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static T get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
return static_cast<T>(lua_tointegerx(L, index, nullptr));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-18 03:05:26 +08:00
|
|
|
template<typename T>
|
2016-08-24 09:42:27 +08:00
|
|
|
struct getter<as_table_t<T>, std::enable_if_t<!meta::has_key_value_pair<meta::unqualified_t<T>>::value>> {
|
2016-08-18 03:05:26 +08:00
|
|
|
static T get(lua_State* L, int index, record& tracking) {
|
|
|
|
typedef typename T::value_type V;
|
|
|
|
tracking.use(1);
|
|
|
|
|
|
|
|
index = lua_absindex(L, index);
|
2016-08-20 09:08:03 +08:00
|
|
|
T arr;
|
|
|
|
get_field<false, true>(L, static_cast<lua_Integer>(-1), index);
|
2016-08-20 09:38:19 +08:00
|
|
|
int isnum;
|
|
|
|
std::size_t sizehint = static_cast<std::size_t>(lua_tointegerx(L, -1, &isnum));
|
2016-08-24 09:42:27 +08:00
|
|
|
if (isnum != 0) {
|
2016-08-20 09:38:19 +08:00
|
|
|
detail::reserve(arr, sizehint);
|
2016-08-20 09:08:03 +08:00
|
|
|
}
|
2016-08-20 09:38:19 +08:00
|
|
|
lua_pop(L, 1);
|
2016-08-19 11:51:41 +08:00
|
|
|
#if SOL_LUA_VERSION >= 503
|
|
|
|
// This method is HIGHLY performant over regular table iteration thanks to the Lua API changes in 5.3
|
2016-08-24 09:42:27 +08:00
|
|
|
for (lua_Integer i = 0; ; i += lua_size<V>::value, lua_pop(L, lua_size<V>::value)) {
|
|
|
|
for (int vi = 0; vi < lua_size<V>::value; ++vi) {
|
|
|
|
type t = static_cast<type>(lua_geti(L, index, i + vi));
|
|
|
|
if (t == type::nil) {
|
|
|
|
if (i == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lua_pop(L, (vi + 1));
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
}
|
2016-08-18 03:05:26 +08:00
|
|
|
}
|
2016-08-24 09:42:27 +08:00
|
|
|
arr.push_back(stack::get<V>(L, -lua_size<V>::value));
|
2016-08-18 03:05:26 +08:00
|
|
|
}
|
2016-08-19 11:51:41 +08:00
|
|
|
#else
|
|
|
|
// Zzzz slower but necessary thanks to the lower version API and missing functions qq
|
2016-08-24 09:42:27 +08:00
|
|
|
for (lua_Integer i = 0; ; i += lua_size<V>::value, lua_pop(L, lua_size<V>::value)) {
|
|
|
|
for (int vi = 0; vi < lua_size<V>::value; ++vi) {
|
|
|
|
lua_pushinteger(L, i);
|
|
|
|
lua_gettable(L, index);
|
|
|
|
type t = type_of(L, -1);
|
|
|
|
if (t == type::nil) {
|
|
|
|
if (i == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lua_pop(L, (vi + 1));
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
}
|
2016-08-19 11:51:41 +08:00
|
|
|
}
|
|
|
|
arr.push_back(stack::get<V>(L, -1));
|
|
|
|
}
|
|
|
|
#endif
|
2016-08-18 03:05:26 +08:00
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2016-08-24 09:42:27 +08:00
|
|
|
struct getter<as_table_t<T>, std::enable_if_t<meta::has_key_value_pair<meta::unqualified_t<T>>::value>> {
|
2016-08-18 03:05:26 +08:00
|
|
|
static T get(lua_State* L, int index, record& tracking) {
|
|
|
|
typedef typename T::value_type P;
|
|
|
|
typedef typename P::first_type K;
|
|
|
|
typedef typename P::second_type V;
|
|
|
|
tracking.use(1);
|
|
|
|
|
|
|
|
T associative;
|
|
|
|
index = lua_absindex(L, index);
|
|
|
|
lua_pushnil(L);
|
|
|
|
while (lua_next(L, index) != 0) {
|
2016-08-18 03:47:18 +08:00
|
|
|
decltype(auto) key = stack::check_get<K>(L, -2);
|
2016-08-18 03:05:26 +08:00
|
|
|
if (!key) {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
continue;
|
|
|
|
}
|
2016-08-18 03:47:18 +08:00
|
|
|
associative.emplace(std::forward<decltype(*key)>(*key), stack::get<V>(L, -1));
|
2016-08-18 03:05:26 +08:00
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
return associative;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-20 05:59:40 +08:00
|
|
|
template<typename T>
|
|
|
|
struct getter<T, std::enable_if_t<std::is_base_of<reference, T>::value || std::is_base_of<stack_reference, T>::value>> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static T get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
return T(L, index);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct getter<userdata_value> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static userdata_value get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
return userdata_value(lua_touserdata(L, index));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct getter<lightuserdata_value> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static lightuserdata_value get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
return lightuserdata_value(lua_touserdata(L, index));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct getter<light<T>> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static light<T> get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
return light<T>(static_cast<T*>(lua_touserdata(L, index)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct getter<user<T>> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static T& get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
return *static_cast<T*>(lua_touserdata(L, index));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct getter<user<T*>> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static T* get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
return static_cast<T*>(lua_touserdata(L, index));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct getter<type> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static type get(lua_State *L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
return static_cast<type>(lua_type(L, index));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct getter<bool> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static bool get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
return lua_toboolean(L, index) != 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct getter<std::string> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static std::string get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
std::size_t len;
|
|
|
|
auto str = lua_tolstring(L, index, &len);
|
2016-08-11 21:16:23 +08:00
|
|
|
return std::string( str, len );
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct getter<string_detail::string_shim> {
|
2016-07-29 12:57:47 +08:00
|
|
|
string_detail::string_shim get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
size_t len;
|
|
|
|
const char* p = lua_tolstring(L, index, &len);
|
|
|
|
return string_detail::string_shim(p, len);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct getter<const char*> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static const char* get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
return lua_tostring(L, index);
|
|
|
|
}
|
|
|
|
};
|
2016-08-13 00:30:18 +08:00
|
|
|
|
2016-06-20 05:59:40 +08:00
|
|
|
template<>
|
|
|
|
struct getter<char> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static char get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
size_t len;
|
|
|
|
auto str = lua_tolstring(L, index, &len);
|
|
|
|
return len > 0 ? str[0] : '\0';
|
|
|
|
}
|
|
|
|
};
|
2016-05-19 09:29:17 +08:00
|
|
|
|
2016-08-13 01:08:59 +08:00
|
|
|
#ifdef SOL_CODECVT_SUPPORT
|
2016-06-20 05:59:40 +08:00
|
|
|
template<>
|
|
|
|
struct getter<std::wstring> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static std::wstring get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-08-13 00:30:18 +08:00
|
|
|
size_t len;
|
|
|
|
auto str = lua_tolstring(L, index, &len);
|
|
|
|
if (len < 1)
|
|
|
|
return std::wstring();
|
|
|
|
if (sizeof(wchar_t) == 2) {
|
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> convert;
|
|
|
|
std::wstring r = convert.from_bytes(str, str + len);
|
2016-08-13 03:57:53 +08:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
// Fuck you, MinGW, and fuck you libstdc++ for introducing this absolutely asinine bug
|
|
|
|
// https://sourceforge.net/p/mingw-w64/bugs/538/
|
|
|
|
// http://chat.stackoverflow.com/transcript/message/32271369#32271369
|
|
|
|
for (auto& c : r) {
|
|
|
|
uint8_t* b = reinterpret_cast<uint8_t*>(&c);
|
|
|
|
std::swap(b[0], b[1]);
|
|
|
|
}
|
|
|
|
#endif
|
2016-08-13 00:30:18 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
|
|
|
|
std::wstring r = convert.from_bytes(str, str + len);
|
|
|
|
return r;
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct getter<std::u16string> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static std::u16string get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-08-13 00:30:18 +08:00
|
|
|
size_t len;
|
|
|
|
auto str = lua_tolstring(L, index, &len);
|
|
|
|
if (len < 1)
|
|
|
|
return std::u16string();
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<int16_t>, int16_t> convert;
|
|
|
|
auto intd = convert.from_bytes(str, str + len);
|
|
|
|
std::u16string r(intd.size(), '\0');
|
|
|
|
std::memcpy(&r[0], intd.data(), intd.size() * sizeof(char16_t));
|
|
|
|
#else
|
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
|
|
|
|
std::u16string r = convert.from_bytes(str, str + len);
|
|
|
|
#endif // VC++ is a shit
|
|
|
|
return r;
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct getter<std::u32string> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static std::u32string get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-08-13 00:30:18 +08:00
|
|
|
size_t len;
|
|
|
|
auto str = lua_tolstring(L, index, &len);
|
|
|
|
if (len < 1)
|
|
|
|
return std::u32string();
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
std::wstring_convert<std::codecvt_utf8<int32_t>, int32_t> convert;
|
|
|
|
auto intd = convert.from_bytes(str, str + len);
|
|
|
|
std::u32string r(intd.size(), '\0');
|
|
|
|
std::memcpy(&r[0], intd.data(), r.size() * sizeof(char32_t));
|
|
|
|
#else
|
|
|
|
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> convert;
|
|
|
|
std::u32string r = convert.from_bytes(str, str + len);
|
|
|
|
#endif // VC++ is a shit
|
|
|
|
return r;
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct getter<wchar_t> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static wchar_t get(lua_State* L, int index, record& tracking) {
|
2016-08-13 00:30:18 +08:00
|
|
|
auto str = getter<std::wstring>{}.get(L, index, tracking);
|
2016-09-29 15:29:48 +08:00
|
|
|
return str.size() > 0 ? str[0] : wchar_t(0);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct getter<char16_t> {
|
2016-09-29 15:29:48 +08:00
|
|
|
static char16_t get(lua_State* L, int index, record& tracking) {
|
2016-08-13 00:30:18 +08:00
|
|
|
auto str = getter<std::u16string>{}.get(L, index, tracking);
|
2016-09-29 15:29:48 +08:00
|
|
|
return str.size() > 0 ? str[0] : char16_t(0);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct getter<char32_t> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static char32_t get(lua_State* L, int index, record& tracking) {
|
2016-08-13 00:30:18 +08:00
|
|
|
auto str = getter<std::u32string>{}.get(L, index, tracking);
|
2016-09-29 15:29:48 +08:00
|
|
|
return str.size() > 0 ? str[0] : char32_t(0);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
2016-08-13 03:57:53 +08:00
|
|
|
#endif // codecvt header support
|
2016-05-19 09:29:17 +08:00
|
|
|
|
2016-08-13 00:30:18 +08:00
|
|
|
template<>
|
|
|
|
struct getter<meta_function> {
|
|
|
|
static meta_function get(lua_State *L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
|
|
|
const char* name = getter<const char*>{}.get(L, index, tracking);
|
|
|
|
for (std::size_t i = 0; i < meta_function_names.size(); ++i)
|
|
|
|
if (meta_function_names[i] == name)
|
|
|
|
return static_cast<meta_function>(i);
|
|
|
|
return meta_function::construct;
|
|
|
|
}
|
|
|
|
};
|
2016-05-19 14:15:42 +08:00
|
|
|
|
2016-06-20 05:59:40 +08:00
|
|
|
template<>
|
|
|
|
struct getter<nil_t> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static nil_t get(lua_State*, int, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2016-07-26 05:31:31 +08:00
|
|
|
struct getter<std::nullptr_t> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static std::nullptr_t get(lua_State*, int, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct getter<nullopt_t> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static nullopt_t get(lua_State*, int, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
return nullopt;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct getter<this_state> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static this_state get(lua_State* L, int, record& tracking) {
|
|
|
|
tracking.use(0);
|
2016-06-20 05:59:40 +08:00
|
|
|
return this_state{ L };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct getter<lua_CFunction> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static lua_CFunction get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
return lua_tocfunction(L, index);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct getter<c_closure> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static c_closure get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
return c_closure(lua_tocfunction(L, index), -1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct getter<error> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static error get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
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<void*> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static void* get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
return lua_touserdata(L, index);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2016-10-24 07:17:33 +08:00
|
|
|
struct getter<detail::as_value_tag<T>> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static T* get_no_nil(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
void** pudata = static_cast<void**>(lua_touserdata(L, index));
|
|
|
|
void* udata = *pudata;
|
2016-07-29 12:57:47 +08:00
|
|
|
return get_no_nil_from(L, udata, index, tracking);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2016-07-29 12:57:47 +08:00
|
|
|
static T* get_no_nil_from(lua_State* L, void* udata, int index, record&) {
|
2016-06-20 05:59:40 +08:00
|
|
|
if (detail::has_derived<T>::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<T>::value);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
T* obj = static_cast<T*>(udata);
|
|
|
|
return obj;
|
|
|
|
}
|
2016-10-24 07:17:33 +08:00
|
|
|
|
|
|
|
static T& get(lua_State* L, int index, record& tracking) {
|
|
|
|
return *get_no_nil(L, index, tracking);
|
|
|
|
}
|
|
|
|
};
|
2016-06-20 05:59:40 +08:00
|
|
|
|
2016-10-24 07:17:33 +08:00
|
|
|
template<typename T>
|
|
|
|
struct getter<detail::as_pointer_tag<T>> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static T* get(lua_State* L, int index, record& tracking) {
|
2016-06-20 05:59:40 +08:00
|
|
|
type t = type_of(L, index);
|
2016-07-29 12:57:47 +08:00
|
|
|
if (t == type::nil) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
return nullptr;
|
2016-07-29 12:57:47 +08:00
|
|
|
}
|
2016-10-24 07:17:33 +08:00
|
|
|
return getter<detail::as_value_tag<T>>::get_no_nil(L, index, tracking);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct getter<non_null<T*>> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static T* get(lua_State* L, int index, record& tracking) {
|
2016-10-24 07:17:33 +08:00
|
|
|
return getter<detail::as_value_tag<T>>::get_no_nil(L, index, tracking);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct getter<T&> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static T& get(lua_State* L, int index, record& tracking) {
|
2016-10-24 07:17:33 +08:00
|
|
|
return getter<detail::as_value_tag<T>>::get(L, index, tracking);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct getter<std::reference_wrapper<T>> {
|
|
|
|
static T& get(lua_State* L, int index, record& tracking) {
|
|
|
|
return getter<T&>{}.get(L, index, tracking);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct getter<T*> {
|
|
|
|
static T* get(lua_State* L, int index, record& tracking) {
|
|
|
|
return getter<detail::as_pointer_tag<T>>::get(L, index, tracking);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct getter<T, std::enable_if_t<is_unique_usertype<T>::value>> {
|
|
|
|
typedef typename unique_usertype_traits<T>::type P;
|
|
|
|
typedef typename unique_usertype_traits<T>::actual_type Real;
|
|
|
|
|
2016-07-29 12:57:47 +08:00
|
|
|
static Real& get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2016-06-20 05:59:40 +08:00
|
|
|
P** pref = static_cast<P**>(lua_touserdata(L, index));
|
|
|
|
detail::special_destruct_func* fx = static_cast<detail::special_destruct_func*>(static_cast<void*>(pref + 1));
|
|
|
|
Real* mem = static_cast<Real*>(static_cast<void*>(fx + 1));
|
|
|
|
return *mem;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
struct getter<std::tuple<Args...>> {
|
2016-09-26 16:02:03 +08:00
|
|
|
typedef std::tuple<decltype(stack::get<Args>(nullptr, 0))...> R;
|
|
|
|
|
|
|
|
template <typename... TArgs>
|
|
|
|
static R apply(std::index_sequence<>, lua_State*, int, record&, TArgs&&... args) {
|
|
|
|
// Fuck you too, VC++
|
|
|
|
return R{std::forward<TArgs>(args)...};
|
|
|
|
}
|
|
|
|
|
|
|
|
template <std::size_t I, std::size_t... Ix, typename... TArgs>
|
|
|
|
static R apply(std::index_sequence<I, Ix...>, lua_State* L, int index, record& tracking, TArgs&&... args) {
|
|
|
|
// Fuck you too, VC++
|
|
|
|
typedef std::tuple_element_t<I, std::tuple<Args...>> T;
|
|
|
|
return apply(std::index_sequence<Ix...>(), L, index, tracking, std::forward<TArgs>(args)..., stack::get<T>(L, index + tracking.used, tracking));
|
|
|
|
}
|
|
|
|
|
|
|
|
static R get(lua_State* L, int index, record& tracking) {
|
2016-07-29 12:57:47 +08:00
|
|
|
return apply(std::make_index_sequence<sizeof...(Args)>(), L, index, tracking);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename A, typename B>
|
|
|
|
struct getter<std::pair<A, B>> {
|
2016-07-29 12:57:47 +08:00
|
|
|
static decltype(auto) get(lua_State* L, int index, record& tracking) {
|
|
|
|
return std::pair<decltype(stack::get<A>(L, index)), decltype(stack::get<B>(L, index))>{stack::get<A>(L, index, tracking), stack::get<B>(L, index + tracking.used, tracking)};
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // stack
|
2016-03-25 03:45:44 +08:00
|
|
|
} // sol
|
|
|
|
|
|
|
|
#endif // SOL_STACK_GET_HPP
|