More style fixes and more attempts to make sure this compiles between GCC and MSVC.

This commit is contained in:
ThePhD 2013-12-02 19:15:23 -05:00
parent 29f171e124
commit bf2404bdfd
6 changed files with 110 additions and 73 deletions

View File

@ -29,20 +29,12 @@
namespace sol {
class function : virtual public reference {
private:
template<typename... Args>
void push_args(Args&&... args) const {
auto L = state();
using swallow = char[ ];
void(swallow{ (stack::push(L, std::forward<Args>(args)), '\0')... });
}
template<typename... Ret>
struct invoker {
template<typename... Args>
static std::tuple<Ret...> call(const function& ref, Args&&... args) {
ref.push();
ref.push_args(std::forward<Args>(args)...);
lua_pcall(ref.state(), sizeof...(Args), sizeof...(Ret), 0);
lua_pcall( ref.state( ), sizeof...( Args ), sizeof...( Ret ), 0 );
return std::make_tuple(stack::pop<Ret>(ref.state())...);
}
};
@ -51,9 +43,7 @@ private:
struct invoker<> {
template<typename... Args>
static void call(const function& ref, Args&&... args) {
ref.push();
ref.push_args(std::forward<Args>(args)...);
lua_pcall(ref.state(), sizeof...(Args), 0, 0);
lua_pcall( ref.state( ), sizeof...( Args ), 0, 0 );
}
};
@ -61,13 +51,32 @@ private:
struct invoker<T> {
template<typename... Args>
static T call(const function& ref, Args&&... args) {
ref.push();
ref.push_args(std::forward<Args>(args)...);
lua_pcall(ref.state(), sizeof...(Args), 1, 0);
return stack::pop<T>(ref.state());
lua_pcall( ref.state( ), sizeof...( Args ), 1, 0 );
return stack::pop<T>( ref.state( ) );
}
};
template <typename... Ret>
std::tuple<Ret...> invoke( types<Ret...>, std::size_t n ) {
lua_pcall( state( ), n, sizeof...( Ret ), 0 );
return stack::pop_call( state( ), std::make_tuple<Ret...>, types<Ret...>() );
}
template <typename Ret>
Ret invoke( types<Ret>, std::size_t n ) {
lua_pcall( state( ), n, 1, 0 );
return stack::pop<Ret>( state( ) );
}
void invoke( types<void>, std::size_t n ) {
lua_pcall( state( ), n, 0, 0 );
}
void invoke( types<>, std::size_t n ) {
lua_pcall( state( ), n, 0, 0 );
}
public:
function() : reference() {}
function(lua_State* L, int index = -1) : reference(L, index) {
@ -75,8 +84,10 @@ public:
}
template<typename... Ret, typename... Args>
auto invoke(Args&&... args) {
return invoker<Ret...>::call(*this, std::forward<Args>(args)...);
auto invoke(Args&&... args) -> decltype(invoke( types<Ret...>( ), sizeof...( Args ) )) {
push( );
stack::push_args( state( ), std::forward<Args>( args )... );
return invoke( types<Ret...>( ), sizeof...( Args ) );
}
};
} // sol

View File

@ -91,6 +91,26 @@ inline auto call(Function&& f, const Tuple& t, indices<Indices...>) -> decltype(
} // detail
template <typename... Ret>
struct lua_return_type {
typedef std::tuple<Ret...> type;
};
template <typename Ret>
struct lua_return_type<Ret> {
typedef Ret type;
};
template <>
struct lua_return_type<> {
typedef void type;
};
template <>
struct lua_return_type<void> {
typedef void type;
};
template<typename Function, typename... Args>
inline auto call(Function&& f, const std::tuple<Args...>& t) -> decltype(detail::call(std::forward<Function>(f), t, build_indices<sizeof...(Args)>{})) {
return call(std::forward<Function>(f), t, build_indices<sizeof...(Args)>{});

View File

@ -29,7 +29,7 @@ namespace sol {
namespace detail {
struct lua_func {
virtual int operator () (lua_State* L) {
virtual int operator () (lua_State*) {
throw sol_error("Failure to call specialized wrapped C++ function from lua");
}
@ -48,7 +48,7 @@ struct lambda_lua_func : public lua_func {
}
virtual int operator () (lua_State* L) override {
return (*this)(tuple_types<fx_traits::return_type>(), fx_traits::args_type(), L);
return ( *this )( tuple_types<typename fx_traits::return_type>( ), typename fx_traits::args_type( ), L );
}
template<typename... Args>
@ -77,7 +77,7 @@ struct explicit_lua_func : public lua_func {
}
virtual int operator () (lua_State* L) override {
return (*this)(tuple_types<fx_traits::return_type>(), fx_traits::args_type(), L);
return ( *this )( tuple_types<typename fx_traits::return_type>( ), typename fx_traits::args_type( ), L );
}
template<typename... Args>
@ -99,18 +99,18 @@ struct explicit_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;
struct lambda {
T* member;
TFx invocation;
T* member;
TFx invocation;
template<typename... FxArgs>
lambda(T* m, FxArgs&&... fxargs) : member(m), invocation(std::forward<FxArgs>(fxargs)...) {
template<typename... FxArgs>
lambda(T* m, FxArgs&&... fxargs) : member(m), invocation(std::forward<FxArgs>(fxargs)...) {
}
}
template<typename... Args>
typename fx_traits::return_type operator () (Args&&... args) {
return ((*member).*invocation)(std::forward<Args>(args)...);
}
template<typename... Args>
typename fx_traits::return_type operator () (Args&&... args) {
return ((*member).*invocation)(std::forward<Args>(args)...);
}
} fx;
template<typename... FxArgs>
@ -124,7 +124,7 @@ struct explicit_lua_func<TFx, T, true> : public lua_func {
}
virtual int operator () (lua_State* L) override {
return (*this)(tuple_types<fx_traits::return_type>(), fx_traits::args_type(), L);
return ( *this )( tuple_types<typename fx_traits::return_type>( ), typename fx_traits::args_type( ), L );
}
template<typename... Args>

View File

@ -183,6 +183,12 @@ inline auto pop_call(lua_State* L, TFx&& fx, types<Args...> t)->decltype(detail:
return detail::ltr_pop(L, fx, t);
}
template<typename... Args>
void push_args( lua_State* L, Args&&... args ) {
using swallow = char [];
void( swallow{ '\0', ( stack::push( L, std::forward<Args>( args ) ), '\0' )... } );
}
} // stack
} // sol

View File

@ -149,14 +149,14 @@ public:
template<typename T, typename TFx>
state& set_function(T&& key, TFx&& fx) {
global.set_function(std::forward<T>(key), std::forward<TFx>(fx));
return *this;
global.set_function(std::forward<T>(key), std::forward<TFx>(fx));
return *this;
}
template<typename T, typename TFx, typename TM>
state& set_function(T&& key, TFx&& fx, TM& mem) {
global.set_function(std::forward<T>(key), std::forward<TFx>(fx), mem);
return *this;
global.set_function(std::forward<T>(key), std::forward<TFx>(fx), mem);
return *this;
}
template<typename T>

View File

@ -60,17 +60,17 @@ public:
template<typename T, typename TFx>
table& set_function(T&& key, TFx&& fx) {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
const static bool isfunction = std::is_function<clean_fx>::value;
return set_fx(std::integral_constant<bool, !isfunction>(),
std::forward<T>(key), std::forward<TFx>(fx));
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
const static bool isfunction = std::is_function<clean_fx>::value;
return set_fx(std::integral_constant<bool, !isfunction>(),
std::forward<T>(key), std::forward<TFx>(fx));
}
template<typename T, typename TFx, typename TM>
table& set_function(T&& key, TFx&& fx, TM& mem) {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
std::unique_ptr<detail::lua_func> sptr(new detail::explicit_lua_func<clean_fx, TM>(mem, std::forward<TFx>(fx)));
return set_fx(std::forward<T>(key), std::move(sptr));
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
std::unique_ptr<detail::lua_func> sptr(new detail::explicit_lua_func<clean_fx, TM>(mem, std::forward<TFx>(fx)));
return set_fx(std::forward<T>(key), std::move(sptr));
}
size_t size() const {
@ -80,39 +80,39 @@ public:
private:
template<typename T, typename TFx>
table& set_fx(std::true_type, T&& key, TFx&& fx) {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
std::unique_ptr<detail::lua_func> sptr(new detail::lambda_lua_func<clean_fx>(std::forward<TFx>(fx)));
return set_fx(std::forward<T>(key), std::move(sptr));
}
template<typename T, typename TFx>
table& set_fx(std::true_type, T&& key, TFx&& fx) {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
std::unique_ptr<detail::lua_func> sptr(new detail::lambda_lua_func<clean_fx>(std::forward<TFx>(fx)));
return set_fx(std::forward<T>(key), std::move(sptr));
}
template<typename T, typename TFx>
table& set_fx(std::false_type, T&& key, TFx&& fx) {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
typedef typename std::decay<TFx>::type ptr_fx;
std::unique_ptr<detail::lua_func> sptr(new detail::explicit_lua_func<ptr_fx>(std::forward<TFx>(fx)));
return set_fx(std::forward<T>(key), std::move(sptr));
}
template<typename T, typename TFx>
table& set_fx(std::false_type, T&& key, TFx&& fx) {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
typedef typename std::decay<TFx>::type ptr_fx;
std::unique_ptr<detail::lua_func> sptr(new detail::explicit_lua_func<ptr_fx>(std::forward<TFx>(fx)));
return set_fx(std::forward<T>(key), std::move(sptr));
}
template<typename T>
table& set_fx(T&& key, std::unique_ptr<detail::lua_func> funcptr) {
std::string fkey(key);
auto hint = funcs.find(fkey);
if (hint == funcs.end()) {
std::shared_ptr<detail::lua_func> sptr(funcptr.release());
hint = funcs.emplace_hint(hint, fkey, std::move(sptr));
}
else {
hint->second.reset(funcptr.release());
}
detail::lua_func* target = target = hint->second.get();
lua_CFunction freefunc = &detail::lua_cfun;
lua_pushlightuserdata(state(), static_cast<void*>(target));
lua_pushcclosure(state(), freefunc, 1);
lua_setglobal(state(), fkey.c_str());
return *this;
}
template<typename T>
table& set_fx(T&& key, std::unique_ptr<detail::lua_func> funcptr) {
std::string fkey(key);
auto hint = funcs.find(fkey);
if (hint == funcs.end()) {
std::shared_ptr<detail::lua_func> sptr(funcptr.release());
hint = funcs.emplace_hint(hint, fkey, std::move(sptr));
}
else {
hint->second.reset(funcptr.release());
}
detail::lua_func* target = hint->second.get();
lua_CFunction freefunc = &detail::lua_cfun;
lua_pushlightuserdata(state(), static_cast<void*>(target));
lua_pushcclosure(state(), freefunc, 1);
lua_setglobal(state(), fkey.c_str());
return *this;
}
};
} // sol