mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Merge remote-tracking branch 'root/classes'
Conflicts: sol/demangle.hpp sol/deprecate.hpp sol/lua_function.hpp sol/userdata.hpp
This commit is contained in:
commit
bf19dd9084
|
@ -24,57 +24,40 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
/*#include <windows.h>
|
||||
#include <DbgHelp.h>
|
||||
*/
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
#include <cxxabi.h>
|
||||
#endif
|
||||
|
||||
namespace sol {
|
||||
namespace detail {
|
||||
|
||||
|
||||
std::string demangle(const std::type_info& id) {
|
||||
/*std::string realname(2048, '\0');
|
||||
DWORD result = UnDecorateSymbolName(id.raw_name(), &realname[0],
|
||||
static_cast<DWORD>(realname.size()), UNDNAME_NAME_ONLY | UNDNAME_32_BIT_DECODE);
|
||||
if (result == 0)
|
||||
return "";
|
||||
realname.resize(result);
|
||||
*/
|
||||
const static std::string removals[ 2 ] = {
|
||||
"struct ",
|
||||
"class "
|
||||
};
|
||||
#ifdef _MSC_VER
|
||||
std::string demangle(const std::type_info& id) {
|
||||
const static std::string removals[2] = { "struct ", "class " };
|
||||
std::string realname = id.name();
|
||||
for (std::size_t r = 0; r < 2; ++r) {
|
||||
auto found = realname.find(removals[ r ]);
|
||||
if (found == std::string::npos)
|
||||
for(std::size_t r = 0; r < 2; ++r) {
|
||||
auto found = realname.find(removals[r]);
|
||||
|
||||
if(found == std::string::npos) {
|
||||
continue;
|
||||
}
|
||||
|
||||
realname.erase(found, removals[r].size());
|
||||
}
|
||||
return realname;
|
||||
}
|
||||
}
|
||||
|
||||
#elif __GNUC__ || __clang__
|
||||
#include <cxxabi.h>
|
||||
|
||||
namespace sol {
|
||||
namespace detail {
|
||||
|
||||
std::string demangle(const std::type_info& id) {
|
||||
#elif defined(__GNUC__) || defined(__clang__)
|
||||
std::string demangle(const std::type_info& id) {
|
||||
int status;
|
||||
char* unmangled = abi::__cxa_demangle(id.name(), 0, 0, &status);
|
||||
std::string realname = unmangled;
|
||||
free(unmangled);
|
||||
return realname;
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
namespace sol {
|
||||
namespace detail {
|
||||
|
||||
#error Implement demangling for this platform
|
||||
#endif // VC++ || GCC || Others
|
||||
|
||||
#error Compiler not supported for demangling
|
||||
#endif // compilers
|
||||
} // detail
|
||||
} // sol
|
||||
|
||||
|
|
|
@ -22,14 +22,14 @@
|
|||
#ifndef SOL_DEPRECATE_HPP
|
||||
#define SOL_DEPRECATE_HPP
|
||||
|
||||
#ifndef DEPRECATE
|
||||
#ifdef _MSC_VER
|
||||
#define DEPRECATE __declspec(deprecated)
|
||||
#elif __GNUC__
|
||||
#define DEPRECATE __attribute__((deprecated))
|
||||
#else
|
||||
#define DEPRECATE [[deprecated]]
|
||||
#endif // VC++ || GCC || Others
|
||||
#endif // DEPRECATE
|
||||
#ifndef SOL_DEPRECATED
|
||||
#ifdef _MSC_VER
|
||||
#define SOL_DEPRECATED __declspec(deprecated)
|
||||
#elif __GNUC__
|
||||
#define SOL_DEPRECATED __attribute__((deprecated))
|
||||
#else
|
||||
#define SOL_DEPRECATED [[deprecated]]
|
||||
#endif // compilers
|
||||
#endif // SOL_DEPRECATED
|
||||
|
||||
#endif // SOL_DEPRECATE_HPP
|
|
@ -27,24 +27,24 @@
|
|||
|
||||
namespace sol {
|
||||
|
||||
template<typename TFx>
|
||||
template<typename Function>
|
||||
struct static_lua_func {
|
||||
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t;
|
||||
typedef function_traits<fx_t> fx_traits;
|
||||
typedef typename std::remove_pointer<typename std::decay<Function>::type>::type function_type;
|
||||
typedef function_traits<function_type> traits_type;
|
||||
|
||||
template<typename... Args>
|
||||
static int typed_call(types<void>, types<Args...> t, fx_t* fx, lua_State* L) {
|
||||
static int typed_call(types<void>, types<Args...> t, function_type* fx, lua_State* L) {
|
||||
stack::pop_call(L, fx, t);
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
static int typed_call(types<>, types<Args...> t, fx_t* fx, lua_State* L) {
|
||||
static int typed_call(types<>, types<Args...> t, function_type* fx, lua_State* L) {
|
||||
return typed_call(types<void>(), t, fx, L);
|
||||
}
|
||||
|
||||
template<typename... Ret, typename... Args>
|
||||
static int typed_call(types<Ret...>, types<Args...> t, fx_t* fx, lua_State* L) {
|
||||
static int typed_call(types<Ret...>, types<Args...> t, function_type* fx, lua_State* L) {
|
||||
typedef typename multi_return<Ret...>::type return_type;
|
||||
return_type r = stack::pop_call(L, fx, t);
|
||||
stack::push(L, std::move(r));
|
||||
|
@ -52,9 +52,9 @@ struct static_lua_func {
|
|||
}
|
||||
|
||||
static int call(lua_State* L) {
|
||||
auto udata = stack::get_user<fx_t*>(L);
|
||||
fx_t* fx = udata.first;
|
||||
int r = typed_call(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), fx, L);
|
||||
auto udata = stack::get_user<function_type*>(L);
|
||||
function_type* fx = udata.first;
|
||||
int r = typed_call(tuple_types<typename traits_type::return_type>(), typename traits_type::args_type(), fx, L);
|
||||
return r;
|
||||
}
|
||||
|
||||
|
@ -63,25 +63,25 @@ struct static_lua_func {
|
|||
}
|
||||
};
|
||||
|
||||
template<typename T, typename TFx>
|
||||
template<typename T, typename Function>
|
||||
struct static_object_lua_func {
|
||||
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t;
|
||||
typedef function_traits<fx_t> fx_traits;
|
||||
typedef typename std::remove_pointer<typename std::decay<Function>::type>::type function_type;
|
||||
typedef function_traits<function_type> traits_type;
|
||||
|
||||
template<typename... Args>
|
||||
static int typed_call(types<void>, types<Args...>, T& item, fx_t& ifx, lua_State* L) {
|
||||
static int typed_call(types<void>, types<Args...>, T& item, function_type& ifx, lua_State* L) {
|
||||
auto fx = [&item, &ifx](Args&&... args) -> void { (item.*ifx)(std::forward<Args>(args)...); };
|
||||
stack::pop_call(L, fx, types<Args...>());
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
static int typed_call(types<>, types<Args...> t, T& item, fx_t& ifx, lua_State* L) {
|
||||
static int typed_call(types<>, types<Args...> t, T& item, function_type& ifx, lua_State* L) {
|
||||
return typed_call(types<void>(), t, item, ifx, L);
|
||||
}
|
||||
|
||||
template<typename... Ret, typename... Args>
|
||||
static int typed_call(types<Ret...>, types<Args...>, T& item, fx_t& ifx, lua_State* L) {
|
||||
static int typed_call(types<Ret...>, types<Args...>, T& item, function_type& ifx, lua_State* L) {
|
||||
typedef typename multi_return<Ret...>::type return_type;
|
||||
auto fx = [&item, &ifx](Args&&... args) -> return_type { return (item.*ifx)(std::forward<Args>(args)...); };
|
||||
return_type r = stack::pop_call(L, fx, types<Args...>());
|
||||
|
@ -90,11 +90,11 @@ struct static_object_lua_func {
|
|||
}
|
||||
|
||||
static int call(lua_State* L) {
|
||||
auto memberdata = stack::get_user<fx_t>(L, 1);
|
||||
auto memberdata = stack::get_user<function_type>(L, 1);
|
||||
auto objdata = stack::get_user<T*>(L, memberdata.second);
|
||||
fx_t& memfx = memberdata.first;
|
||||
function_type& memfx = memberdata.first;
|
||||
T& obj = *objdata.first;
|
||||
int r = typed_call(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), obj, memfx, L);
|
||||
int r = typed_call(tuple_types<typename traits_type::return_type>(), typename traits_type::args_type(), obj, memfx, L);
|
||||
return r;
|
||||
}
|
||||
|
||||
|
@ -107,8 +107,9 @@ struct lua_func {
|
|||
static int call(lua_State* L) {
|
||||
void** pinheritancedata = static_cast<void**>(stack::get<lightuserdata_t>(L, 1).value);
|
||||
void* inheritancedata = *pinheritancedata;
|
||||
if (inheritancedata == nullptr)
|
||||
if (inheritancedata == nullptr) {
|
||||
throw sol_error("call from Lua to C++ function has null data");
|
||||
}
|
||||
lua_func* pfx = static_cast<lua_func*>(inheritancedata);
|
||||
lua_func& fx = *pfx;
|
||||
int r = fx(L);
|
||||
|
@ -131,11 +132,11 @@ struct lua_func {
|
|||
virtual ~lua_func() {}
|
||||
};
|
||||
|
||||
template<typename TFx>
|
||||
template<typename Function>
|
||||
struct functor_lua_func : public lua_func {
|
||||
typedef decltype(&TFx::operator()) fx_t;
|
||||
typedef function_traits<fx_t> fx_traits;
|
||||
TFx fx;
|
||||
typedef decltype(&Function::operator()) function_type;
|
||||
typedef function_traits<function_type> traits_type;
|
||||
Function fx;
|
||||
|
||||
template<typename... FxArgs>
|
||||
functor_lua_func(FxArgs&&... fxargs): fx(std::forward<FxArgs>(fxargs)...) {}
|
||||
|
@ -160,15 +161,15 @@ struct functor_lua_func : public lua_func {
|
|||
}
|
||||
|
||||
virtual int operator()(lua_State* L) override {
|
||||
return (*this)(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), L);
|
||||
return (*this)(tuple_types<typename traits_type::return_type>(), typename traits_type::args_type(), L);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename TFx, typename T = TFx, bool is_member_pointer = std::is_member_function_pointer<TFx>::value>
|
||||
template<typename Function, typename T = Function, bool is_member_pointer = std::is_member_function_pointer<Function>::value>
|
||||
struct function_lua_func : public lua_func {
|
||||
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t;
|
||||
typedef function_traits<fx_t> fx_traits;
|
||||
fx_t fx;
|
||||
typedef typename std::remove_pointer<typename std::decay<Function>::type>::type function_type;
|
||||
typedef function_traits<function_type> traits_type;
|
||||
function_type fx;
|
||||
|
||||
template<typename... FxArgs>
|
||||
function_lua_func(FxArgs&&... fxargs): fx(std::forward<FxArgs>(fxargs)...) {}
|
||||
|
@ -193,23 +194,23 @@ struct function_lua_func : public lua_func {
|
|||
}
|
||||
|
||||
virtual int operator()(lua_State* L) override {
|
||||
return (*this)(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), L);
|
||||
return (*this)(tuple_types<typename traits_type::return_type>(), typename traits_type::args_type(), L);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename TFx, typename T>
|
||||
struct function_lua_func<TFx, T, true> : public lua_func {
|
||||
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t;
|
||||
typedef function_traits<fx_t> fx_traits;
|
||||
template<typename Function, typename T>
|
||||
struct function_lua_func<Function, T, true> : public lua_func {
|
||||
typedef typename std::remove_pointer<typename std::decay<Function>::type>::type function_type;
|
||||
typedef function_traits<function_type> traits_type;
|
||||
struct functor {
|
||||
T member;
|
||||
fx_t invocation;
|
||||
function_type invocation;
|
||||
|
||||
template<typename... FxArgs>
|
||||
functor(T m, FxArgs&&... fxargs): member(std::move(m)), invocation(std::forward<FxArgs>(fxargs)...) {}
|
||||
|
||||
template<typename... Args>
|
||||
typename fx_traits::return_type operator()(Args&&... args) {
|
||||
typename traits_type::return_type operator()(Args&&... args) {
|
||||
return (member.*invocation)(std::forward<Args>(args)...);
|
||||
}
|
||||
} fx;
|
||||
|
@ -237,17 +238,17 @@ struct function_lua_func<TFx, T, true> : public lua_func {
|
|||
}
|
||||
|
||||
virtual int operator()(lua_State* L) override {
|
||||
return (*this)(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), L);
|
||||
return (*this)(tuple_types<typename traits_type::return_type>(), typename traits_type::args_type(), L);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename TFx, typename T>
|
||||
template<typename Function, typename T>
|
||||
struct class_lua_func : public lua_func {
|
||||
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t;
|
||||
typedef function_traits<fx_t> fx_traits;
|
||||
typedef typename std::remove_pointer<typename std::decay<Function>::type>::type function_type;
|
||||
typedef function_traits<function_type> traits_type;
|
||||
struct functor {
|
||||
T* item;
|
||||
fx_t invocation;
|
||||
function_type invocation;
|
||||
|
||||
template<typename... FxArgs>
|
||||
functor(FxArgs&&... fxargs): item(nullptr), invocation(std::forward<FxArgs>(fxargs)...) {}
|
||||
|
@ -258,7 +259,7 @@ struct class_lua_func : public lua_func {
|
|||
}
|
||||
|
||||
template<typename... Args>
|
||||
typename fx_traits::return_type operator()(Args&&... args) {
|
||||
typename traits_type::return_type operator()(Args&&... args) {
|
||||
T& member = *item;
|
||||
return (member.*invocation)(std::forward<Args>(args)...);
|
||||
}
|
||||
|
@ -288,7 +289,7 @@ struct class_lua_func : public lua_func {
|
|||
|
||||
virtual int operator()(lua_State* L) override {
|
||||
fx.pre_call(L);
|
||||
return (*this)(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), L);
|
||||
return (*this)(tuple_types<typename traits_type::return_type>(), typename traits_type::args_type(), L);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -22,12 +22,18 @@
|
|||
#ifndef SOL_USERDATA_HPP
|
||||
#define SOL_USERDATA_HPP
|
||||
|
||||
#include <sol/state.hpp>
|
||||
#include <sol/lua_function.hpp>
|
||||
#include <sol/demangle.hpp>
|
||||
#include "state.hpp"
|
||||
#include "lua_function.hpp"
|
||||
#include "demangle.hpp"
|
||||
#include <vector>
|
||||
|
||||
namespace sol {
|
||||
namespace detail {
|
||||
template<typename T, typename... Args>
|
||||
inline std::unique_ptr<T> make_unique(Args&&... args) {
|
||||
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
|
||||
}
|
||||
} // detail
|
||||
|
||||
template <typename T>
|
||||
class userdata {
|
||||
|
@ -70,12 +76,15 @@ private:
|
|||
}
|
||||
};
|
||||
|
||||
template <std::size_t n>
|
||||
template<std::size_t n>
|
||||
struct destructor {
|
||||
static int destruct(lua_State* L) {
|
||||
for (std::size_t i = 0; i < n; ++i) {
|
||||
for(std::size_t i = 0; i < n; ++i) {
|
||||
lightuserdata_t luserdata = stack::get<lightuserdata_t>(L, i);
|
||||
// make warnings shut up
|
||||
(void)luserdata;
|
||||
}
|
||||
|
||||
userdata_t userdata = stack::get<userdata_t>(L, 0);
|
||||
T* obj = static_cast<T*>(userdata.value);
|
||||
std::allocator<T> alloc{};
|
||||
|
@ -108,29 +117,23 @@ private:
|
|||
void build_function_tables(Ret(T::* func)(MArgs...), std::string name, Args&&... args) {
|
||||
typedef typename std::decay<decltype(func)>::type fx_t;
|
||||
functionnames.push_back(std::move(name));
|
||||
functions.emplace_back(std::make_unique<class_lua_func<fx_t, T>>(std::move(func)));
|
||||
functions.emplace_back(detail::make_unique<class_lua_func<fx_t, T>>(std::move(func)));
|
||||
functiontable.push_back({ functionnames.back().c_str(), &class_func<n>::call });
|
||||
build_function_tables<n + 1>(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
public:
|
||||
template <typename... Args>
|
||||
userdata(Args&&... args) : userdata(classname, std::forward<Args>(args)...) {
|
||||
|
||||
}
|
||||
userdata(Args&&... args) : userdata(classname, std::forward<Args>(args)...) {}
|
||||
|
||||
template <typename... Args>
|
||||
userdata(std::string name, Args&&... args) : userdata(name, constructors<>(), std::forward<Args>(args)...) {
|
||||
|
||||
}
|
||||
userdata(std::string name, Args&&... args) : userdata(name, constructors<>(), std::forward<Args>(args)...) {}
|
||||
|
||||
template <typename... Args, typename... CArgs>
|
||||
userdata(constructors<CArgs...> c, Args&&... args) : userdata(classname, std::move(c), std::forward<Args>(args)...) {
|
||||
|
||||
}
|
||||
userdata(constructors<CArgs...> c, Args&&... args) : userdata(classname, std::move(c), std::forward<Args>(args)...) {}
|
||||
|
||||
template <typename... Args, typename... CArgs>
|
||||
userdata(std::string name, constructors<CArgs...> c, Args&&... args) : luaname(std::move(name)) {
|
||||
userdata(std::string name, constructors<CArgs...>, Args&&... args) : luaname(std::move(name)) {
|
||||
functionnames.reserve(sizeof...(args));
|
||||
functiontable.reserve(sizeof...(args));
|
||||
functions.reserve(sizeof...(args));
|
||||
|
@ -145,9 +148,7 @@ public:
|
|||
metatable.push_back({ nullptr, nullptr });
|
||||
}
|
||||
|
||||
void register_into(const table& s) {
|
||||
|
||||
}
|
||||
void register_into(const table& s) { }
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
|
@ -158,4 +159,4 @@ const std::string userdata<T>::meta = std::string("sol.stateful.").append(classn
|
|||
|
||||
}
|
||||
|
||||
#endif SOL_USERDATA_HPP
|
||||
#endif // SOL_USERDATA_HPP
|
||||
|
|
Loading…
Reference in New Issue
Block a user