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:
ThePhD 2014-04-25 20:58:55 -04:00
commit bf19dd9084
4 changed files with 101 additions and 116 deletions

View File

@ -24,57 +24,40 @@
#include <string> #include <string>
#ifdef _MSC_VER #if defined(__GNUC__) || defined(__clang__)
/*#include <windows.h>
#include <DbgHelp.h>
*/
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 "
};
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)
continue;
realname.erase(found, removals[r].size());
}
return realname;
}
#elif __GNUC__ || __clang__
#include <cxxabi.h> #include <cxxabi.h>
#endif
namespace sol { namespace sol {
namespace detail { namespace detail {
#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]);
std::string demangle(const std::type_info& id) { if(found == std::string::npos) {
int status; continue;
char* unmangled = abi::__cxa_demangle(id.name(), 0, 0, &status); }
std::string realname = unmangled;
free(unmangled); realname.erase(found, removals[r].size());
return realname;
} }
return realname;
}
#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 #else
namespace sol { #error Compiler not supported for demangling
namespace detail { #endif // compilers
#error Implement demangling for this platform
#endif // VC++ || GCC || Others
} // detail } // detail
} // sol } // sol

View File

@ -22,14 +22,14 @@
#ifndef SOL_DEPRECATE_HPP #ifndef SOL_DEPRECATE_HPP
#define SOL_DEPRECATE_HPP #define SOL_DEPRECATE_HPP
#ifndef DEPRECATE #ifndef SOL_DEPRECATED
#ifdef _MSC_VER #ifdef _MSC_VER
#define DEPRECATE __declspec(deprecated) #define SOL_DEPRECATED __declspec(deprecated)
#elif __GNUC__ #elif __GNUC__
#define DEPRECATE __attribute__((deprecated)) #define SOL_DEPRECATED __attribute__((deprecated))
#else #else
#define DEPRECATE [[deprecated]] #define SOL_DEPRECATED [[deprecated]]
#endif // VC++ || GCC || Others #endif // compilers
#endif // DEPRECATE #endif // SOL_DEPRECATED
#endif // SOL_DEPRECATE_HPP #endif // SOL_DEPRECATE_HPP

View File

@ -27,24 +27,24 @@
namespace sol { namespace sol {
template<typename TFx> template<typename Function>
struct static_lua_func { struct static_lua_func {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t; typedef typename std::remove_pointer<typename std::decay<Function>::type>::type function_type;
typedef function_traits<fx_t> fx_traits; typedef function_traits<function_type> traits_type;
template<typename... Args> 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); stack::pop_call(L, fx, t);
return 0; return 0;
} }
template<typename... Args> 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); return typed_call(types<void>(), t, fx, L);
} }
template<typename... Ret, typename... Args> 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; typedef typename multi_return<Ret...>::type return_type;
return_type r = stack::pop_call(L, fx, t); return_type r = stack::pop_call(L, fx, t);
stack::push(L, std::move(r)); stack::push(L, std::move(r));
@ -52,9 +52,9 @@ struct static_lua_func {
} }
static int call(lua_State* L) { static int call(lua_State* L) {
auto udata = stack::get_user<fx_t*>(L); auto udata = stack::get_user<function_type*>(L);
fx_t* fx = udata.first; function_type* fx = udata.first;
int r = typed_call(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), fx, L); int r = typed_call(tuple_types<typename traits_type::return_type>(), typename traits_type::args_type(), fx, L);
return r; 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 { struct static_object_lua_func {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t; typedef typename std::remove_pointer<typename std::decay<Function>::type>::type function_type;
typedef function_traits<fx_t> fx_traits; typedef function_traits<function_type> traits_type;
template<typename... Args> 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)...); }; auto fx = [&item, &ifx](Args&&... args) -> void { (item.*ifx)(std::forward<Args>(args)...); };
stack::pop_call(L, fx, types<Args...>()); stack::pop_call(L, fx, types<Args...>());
return 0; return 0;
} }
template<typename... Args> 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); return typed_call(types<void>(), t, item, ifx, L);
} }
template<typename... Ret, typename... Args> 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; typedef typename multi_return<Ret...>::type return_type;
auto fx = [&item, &ifx](Args&&... args) -> return_type { return (item.*ifx)(std::forward<Args>(args)...); }; 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...>()); 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) { 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); auto objdata = stack::get_user<T*>(L, memberdata.second);
fx_t& memfx = memberdata.first; function_type& memfx = memberdata.first;
T& obj = *objdata.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; return r;
} }
@ -107,8 +107,9 @@ struct lua_func {
static int call(lua_State* L) { static int call(lua_State* L) {
void** pinheritancedata = static_cast<void**>(stack::get<lightuserdata_t>(L, 1).value); void** pinheritancedata = static_cast<void**>(stack::get<lightuserdata_t>(L, 1).value);
void* inheritancedata = *pinheritancedata; void* inheritancedata = *pinheritancedata;
if (inheritancedata == nullptr) if (inheritancedata == nullptr) {
throw sol_error("call from Lua to C++ function has null data"); throw sol_error("call from Lua to C++ function has null data");
}
lua_func* pfx = static_cast<lua_func*>(inheritancedata); lua_func* pfx = static_cast<lua_func*>(inheritancedata);
lua_func& fx = *pfx; lua_func& fx = *pfx;
int r = fx(L); int r = fx(L);
@ -131,11 +132,11 @@ struct lua_func {
virtual ~lua_func() {} virtual ~lua_func() {}
}; };
template<typename TFx> template<typename Function>
struct functor_lua_func : public lua_func { struct functor_lua_func : public lua_func {
typedef decltype(&TFx::operator()) fx_t; typedef decltype(&Function::operator()) function_type;
typedef function_traits<fx_t> fx_traits; typedef function_traits<function_type> traits_type;
TFx fx; Function fx;
template<typename... FxArgs> template<typename... FxArgs>
functor_lua_func(FxArgs&&... fxargs): fx(std::forward<FxArgs>(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 { 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 { struct function_lua_func : public lua_func {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t; typedef typename std::remove_pointer<typename std::decay<Function>::type>::type function_type;
typedef function_traits<fx_t> fx_traits; typedef function_traits<function_type> traits_type;
fx_t fx; function_type fx;
template<typename... FxArgs> template<typename... FxArgs>
function_lua_func(FxArgs&&... fxargs): fx(std::forward<FxArgs>(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 { 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 function_lua_func<TFx, T, true> : public lua_func { struct function_lua_func<Function, T, true> : public lua_func {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t; typedef typename std::remove_pointer<typename std::decay<Function>::type>::type function_type;
typedef function_traits<fx_t> fx_traits; typedef function_traits<function_type> traits_type;
struct functor { struct functor {
T member; T member;
fx_t invocation; function_type invocation;
template<typename... FxArgs> template<typename... FxArgs>
functor(T m, FxArgs&&... fxargs): member(std::move(m)), invocation(std::forward<FxArgs>(fxargs)...) {} functor(T m, FxArgs&&... fxargs): member(std::move(m)), invocation(std::forward<FxArgs>(fxargs)...) {}
template<typename... Args> 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)...); return (member.*invocation)(std::forward<Args>(args)...);
} }
} fx; } fx;
@ -237,17 +238,17 @@ struct function_lua_func<TFx, T, true> : public lua_func {
} }
virtual int operator()(lua_State* L) override { 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 { struct class_lua_func : public lua_func {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t; typedef typename std::remove_pointer<typename std::decay<Function>::type>::type function_type;
typedef function_traits<fx_t> fx_traits; typedef function_traits<function_type> traits_type;
struct functor { struct functor {
T* item; T* item;
fx_t invocation; function_type invocation;
template<typename... FxArgs> template<typename... FxArgs>
functor(FxArgs&&... fxargs): item(nullptr), invocation(std::forward<FxArgs>(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> template<typename... Args>
typename fx_traits::return_type operator()(Args&&... args) { typename traits_type::return_type operator()(Args&&... args) {
T& member = *item; T& member = *item;
return (member.*invocation)(std::forward<Args>(args)...); 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 { virtual int operator()(lua_State* L) override {
fx.pre_call(L); 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);
} }
}; };

View File

@ -22,12 +22,18 @@
#ifndef SOL_USERDATA_HPP #ifndef SOL_USERDATA_HPP
#define SOL_USERDATA_HPP #define SOL_USERDATA_HPP
#include <sol/state.hpp> #include "state.hpp"
#include <sol/lua_function.hpp> #include "lua_function.hpp"
#include <sol/demangle.hpp> #include "demangle.hpp"
#include <vector> #include <vector>
namespace sol { 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> template <typename T>
class userdata { class userdata {
@ -70,12 +76,15 @@ private:
} }
}; };
template <std::size_t n> template<std::size_t n>
struct destructor { struct destructor {
static int destruct(lua_State* L) { 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); lightuserdata_t luserdata = stack::get<lightuserdata_t>(L, i);
// make warnings shut up
(void)luserdata;
} }
userdata_t userdata = stack::get<userdata_t>(L, 0); userdata_t userdata = stack::get<userdata_t>(L, 0);
T* obj = static_cast<T*>(userdata.value); T* obj = static_cast<T*>(userdata.value);
std::allocator<T> alloc{}; std::allocator<T> alloc{};
@ -108,29 +117,23 @@ private:
void build_function_tables(Ret(T::* func)(MArgs...), std::string name, Args&&... args) { void build_function_tables(Ret(T::* func)(MArgs...), std::string name, Args&&... args) {
typedef typename std::decay<decltype(func)>::type fx_t; typedef typename std::decay<decltype(func)>::type fx_t;
functionnames.push_back(std::move(name)); 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 }); functiontable.push_back({ functionnames.back().c_str(), &class_func<n>::call });
build_function_tables<n + 1>(std::forward<Args>(args)...); build_function_tables<n + 1>(std::forward<Args>(args)...);
} }
public: public:
template <typename... Args> template <typename... Args>
userdata(Args&&... args) : userdata(classname, std::forward<Args>(args)...) { userdata(Args&&... args) : userdata(classname, std::forward<Args>(args)...) {}
}
template <typename... 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> 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> 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)); functionnames.reserve(sizeof...(args));
functiontable.reserve(sizeof...(args)); functiontable.reserve(sizeof...(args));
functions.reserve(sizeof...(args)); functions.reserve(sizeof...(args));
@ -145,9 +148,7 @@ public:
metatable.push_back({ nullptr, nullptr }); metatable.push_back({ nullptr, nullptr });
} }
void register_into(const table& s) { void register_into(const table& s) { }
}
}; };
template <typename T> 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