Fixing style to fit @Rapptz's usual no-tabs, four-space-indents, template<> no-spaces stuff.

This commit is contained in:
ThePhD 2013-12-08 23:09:07 -05:00
parent 49c73c4725
commit 854cbeef71
6 changed files with 197 additions and 197 deletions

View File

@ -32,7 +32,7 @@ private:
template<typename... Ret>
std::tuple<Ret...> call(types<Ret...>, std::size_t n) {
lua_pcall(state(), n, sizeof...(Ret), 0);
return stack::pop_call(state(), std::make_tuple<Ret...>, types<Ret...>());
return stack::pop_call(state(), std::make_tuple<Ret...>, types<Ret...>());
}
template<typename Ret>

View File

@ -94,22 +94,22 @@ inline auto call(Function&& f, const Tuple& t, indices<Indices...>) -> decltype(
}
} // detail
template <typename... Ret>
template<typename... Ret>
struct lua_return_type {
typedef std::tuple<Ret...> type;
};
template <typename Ret>
template<typename Ret>
struct lua_return_type<Ret> {
typedef Ret type;
};
template <>
template<>
struct lua_return_type<> {
typedef void type;
};
template <>
template<>
struct lua_return_type<void> {
typedef void type;
};

View File

@ -29,98 +29,98 @@ namespace sol {
template<typename TFx>
struct static_lua_func {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t;
typedef detail::function_traits<fx_t> fx_traits;
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t;
typedef detail::function_traits<fx_t> fx_traits;
template<typename... Args>
static int typed_call( types<void>, types<Args...> t, fx_t* fx, lua_State* L ) {
stack::pop_call( L, fx, t );
return 0;
}
template<typename... TRn, typename... Args>
static int typed_call( types<TRn...>, types<Args...> t, fx_t* fx, lua_State* L ) {
auto r = stack::pop_call( L, fx, t );
stack::push( L, std::move( r ) );
return sizeof...( TRn );
}
static int call( lua_State* L ) {
void* functiondata = lua_touserdata( L, lua_upvalueindex( 1 ) );
//if ( functiondata == nullptr )
// throw sol_error( "call from lua to c++ function has null function pointer data" );
fx_t* fx = *static_cast<fx_t*>( functiondata );
int r = typed_call( tuple_types<typename fx_traits::return_type>( ), typename fx_traits::args_type( ), fx, L );
return r;
template<typename... Args>
static int typed_call(types<void>, types<Args...> t, fx_t* fx, lua_State* L) {
stack::pop_call(L, fx, t);
return 0;
}
int operator()( lua_State* L ) {
return call( L );
template<typename... TRn, typename... Args>
static int typed_call(types<TRn...>, types<Args...> t, fx_t* fx, lua_State* L) {
auto r = stack::pop_call(L, fx, t);
stack::push(L, std::move(r));
return sizeof...(TRn);
}
static int call(lua_State* L) {
void* functiondata = lua_touserdata(L, lua_upvalueindex(1));
//if (functiondata == nullptr)
// throw sol_error("call from lua to c++ function has null function pointer data");
fx_t* fx = *static_cast<fx_t*>(functiondata);
int r = typed_call(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), fx, L);
return r;
}
int operator()(lua_State* L) {
return call(L);
}
};
template<typename T, typename TFx>
struct static_object_lua_func {
typedef typename std::decay<TFx>::type fx_t;
typedef detail::function_traits<fx_t> fx_traits;
typedef typename std::decay<TFx>::type fx_t;
typedef detail::function_traits<fx_t> fx_traits;
template<typename... Args>
static int typed_call( types<void>, types<Args...>, T& item, fx_t& ifx, lua_State* L ) {
auto fx = [ &item, &ifx ] ( Args&&... args ) { ( item.*ifx )( std::forward<Args>( args )... ); };
stack::pop_call( L, fx, types<Args...>( ) );
return 0;
}
template<typename... Args>
static int typed_call(types<void>, types<Args...>, T& item, fx_t& ifx, lua_State* L) {
auto fx = [ &item, &ifx ] (Args&&... args) { (item.*ifx)(std::forward<Args>(args)...); };
stack::pop_call(L, fx, types<Args...>());
return 0;
}
template<typename TR, typename... Args>
static int typed_call( types<TR>, types<Args...>, T& item, fx_t& ifx, lua_State* L ) {
auto fx = [ &item, &ifx ] ( Args&&... args ) -> TR { return ( item.*ifx )( std::forward<Args>( args )... ); };
auto r = stack::pop_call( L, fx, types<Args...>( ) );
stack::push( L, std::move( r ) );
return 1;
}
template<typename TR, typename... Args>
static int typed_call(types<TR>, types<Args...>, T& item, fx_t& ifx, lua_State* L) {
auto fx = [ &item, &ifx ] (Args&&... args) -> TR { return (item.*ifx)(std::forward<Args>(args)...); };
auto r = stack::pop_call(L, fx, types<Args...>());
stack::push(L, std::move(r));
return 1;
}
template<typename... TRn, typename... Args>
static int typed_call( types<TRn...>, types<Args...>, T& item, fx_t& ifx, lua_State* L ) {
auto fx = [ &item, &ifx ] ( Args&&... args ) -> std::tuple<TRn...> { return ( item.*ifx )( std::forward<Args>( args )... ); };
auto r = stack::pop_call( L, fx, types<Args...>( ) );
stack::push( L, std::move( r ) );
return sizeof...( TRn );
}
template<typename... TRn, typename... Args>
static int typed_call(types<TRn...>, types<Args...>, T& item, fx_t& ifx, lua_State* L) {
auto fx = [ &item, &ifx ] (Args&&... args) -> std::tuple<TRn...> { return (item.*ifx)(std::forward<Args>(args)...); };
auto r = stack::pop_call(L, fx, types<Args...>());
stack::push(L, std::move(r));
return sizeof...(TRn);
}
static int call( lua_State* L ) {
const static std::size_t data_t_count = ( sizeof(fx_t)+( sizeof(void*)-1 ) ) / sizeof( void* );
typedef std::array<void*, data_t_count> data_t;
data_t fxptrdata;
int upvalue = 1;
for ( std::size_t i = 0; i < fxptrdata.size( ); ++i ) {
fxptrdata[ i ] = lua_touserdata( L, lua_upvalueindex( upvalue++ ) );
}
void* objectdata = lua_touserdata( L, lua_upvalueindex( upvalue++ ) );
//if ( objectdata == nullptr )
// throw sol_error( "call from lua to c++ function has null object data" );
fx_t* fxptr = reinterpret_cast<fx_t*>( static_cast<void*>( fxptrdata.data( ) ) );
fx_t& mem_ptr = *fxptr;
T& obj = *static_cast<T*>( objectdata );
int r = typed_call( tuple_types<typename fx_traits::return_type>( ), typename fx_traits::args_type( ), obj, mem_ptr, L );
return r;
}
static int call(lua_State* L) {
const static std::size_t data_t_count = (sizeof(fx_t)+(sizeof(void*)-1)) / sizeof(void*);
typedef std::array<void*, data_t_count> data_t;
data_t fxptrdata;
int upvalue = 1;
for (std::size_t i = 0; i < fxptrdata.size(); ++i) {
fxptrdata[ i ] = lua_touserdata(L, lua_upvalueindex(upvalue++));
}
void* objectdata = lua_touserdata(L, lua_upvalueindex(upvalue++));
//if (objectdata == nullptr)
// throw sol_error("call from lua to c++ function has null object data");
fx_t* fxptr = reinterpret_cast<fx_t*>(static_cast<void*>(fxptrdata.data()));
fx_t& mem_ptr = *fxptr;
T& obj = *static_cast<T*>(objectdata);
int r = typed_call(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), obj, mem_ptr, L);
return r;
}
int operator()( lua_State* L ) {
return call( L );
}
int operator()(lua_State* L) {
return call(L);
}
};
struct lua_func {
static int call( lua_State* L ) {
void* inheritancedata = lua_touserdata( L, lua_upvalueindex( 1 ) );
if ( inheritancedata == nullptr )
throw sol_error( "call from lua to c++ function has null data" );
lua_func& fx = *static_cast<lua_func*>( inheritancedata );
int r = fx( L );
return r;
}
virtual int operator()(lua_State*) {
static int call(lua_State* L) {
void* inheritancedata = lua_touserdata(L, lua_upvalueindex(1));
if (inheritancedata == nullptr)
throw sol_error("call from lua to c++ function has null data");
lua_func& fx = *static_cast<lua_func*>(inheritancedata);
int r = fx(L);
return r;
}
virtual int operator()(lua_State*) {
throw sol_error("Failure to call specialized wrapped C++ function from lua");
}
@ -176,7 +176,7 @@ struct explicit_lua_func : public lua_func {
template<typename... TRn, typename... Args>
int operator()(types<TRn...>, types<Args...> t, lua_State* L) {
auto r = stack::pop_call(L, fx, t);
stack::push(L, std::move( r ));
stack::push(L, std::move(r));
return sizeof...(TRn);
}
};

View File

@ -140,16 +140,16 @@ inline void push(lua_State* L, const nil_t&) {
lua_pushnil(L);
}
inline void push( lua_State* L, lua_CFunction func ) {
lua_pushcfunction( L, func );
inline void push(lua_State* L, lua_CFunction func) {
lua_pushcfunction(L, func);
}
inline void push( lua_State* L, lua_CFunction func, int n ) {
lua_pushcclosure( L, func, n );
inline void push(lua_State* L, lua_CFunction func, int n) {
lua_pushcclosure(L, func, n);
}
inline void push( lua_State* L, void* userdata ) {
lua_pushlightuserdata( L, userdata );
inline void push(lua_State* L, void* userdata) {
lua_pushlightuserdata(L, userdata);
}
template<size_t N>
@ -158,7 +158,7 @@ inline void push(lua_State* L, const char (&str)[N]) {
}
inline void push(lua_State* L, const char* str) {
lua_pushlstring(L, str, std::char_traits<char>::length( str ));
lua_pushlstring(L, str, std::char_traits<char>::length(str));
}
inline void push(lua_State* L, const std::string& str) {
@ -166,10 +166,10 @@ inline void push(lua_State* L, const std::string& str) {
}
template<typename T, size_t N>
inline void push( lua_State* L, const std::array<T, N>& data ) {
for ( std::size_t i = 0; i < data.size( ); ++i ) {
push( L, data[ i ] );
}
inline void push(lua_State* L, const std::array<T, N>& data) {
for (std::size_t i = 0; i < data.size(); ++i) {
push(L, data[ i ]);
}
}
namespace detail {
@ -181,15 +181,15 @@ inline void push(lua_State* L, indices<I...>, const T& tuplen) {
template<typename F, typename... Vs>
auto ltr_pop(lua_State*, F&& f, types<>, Vs&&... vs) -> decltype(f(std::forward<Vs>(vs)...)) {
return f(std::forward<Vs>(vs)...);
return f(std::forward<Vs>(vs)...);
}
template<typename F, typename Head, typename... Vs>
auto ltr_pop(lua_State* L, F&& f, types<Head>, Vs&&... vs) -> decltype(ltr_pop(L, std::forward<F>(f), types<>(), std::forward<Vs>(vs)..., pop<Head>(L))) {
return ltr_pop(L, std::forward<F>(f), types<>(), std::forward<Vs>(vs)..., pop<Head>(L));
return ltr_pop(L, std::forward<F>(f), types<>(), std::forward<Vs>(vs)..., pop<Head>(L));
}
template<typename F, typename Head, typename... Tail, typename... Vs>
auto ltr_pop(lua_State* L, F&& f, types<Head, Tail...>, Vs&&... vs) -> decltype(ltr_pop(L, std::forward<F>(f), types<Tail...>(), std::forward<Vs>(vs)..., pop<Head>(L))) {
return ltr_pop(L, std::forward<F>(f), types<Tail...>(), std::forward<Vs>(vs)..., pop<Head>(L));
return ltr_pop(L, std::forward<F>(f), types<Tail...>(), std::forward<Vs>(vs)..., pop<Head>(L));
}
} // detail

View File

@ -35,8 +35,8 @@ template<class T, class U, class... Args>
struct are_same<T, U, Args...> : std::integral_constant<bool, std::is_same<T, U>::value && are_same<T, Args...>::value> {};
int atpanic(lua_State* L) {
std::string err = lua_tostring( L, -1 );
throw sol_error( err );
std::string err = lua_tostring(L, -1);
throw sol_error(err);
}
} // detail

View File

@ -31,15 +31,15 @@
namespace sol {
namespace detail {
template <typename T>
T* get_ptr( T& val ) {
return std::addressof( val );
}
template<typename T>
T* get_ptr(T& val) {
return std::addressof(val);
}
template <typename T>
T* get_ptr( T* val ) {
return val;
}
template<typename T>
T* get_ptr(T* val) {
return val;
}
}
class table : virtual public reference {
private:
@ -74,13 +74,13 @@ 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;
return set_isfunction_fx( std::is_function<clean_fx>( ), std::forward<T>( key ), std::forward<TFx>( fx ) );
return set_isfunction_fx(std::is_function<clean_fx>(), std::forward<T>(key), std::forward<TFx>(fx));
}
template<typename T, typename TFx, typename TObj>
table& set_function(T&& key, TFx&& fx, TObj&& obj) {
return set_lvalue_fx( std::integral_constant<bool, std::is_lvalue_reference<TObj>::value || std::is_pointer<TObj>::value>( ),
std::forward<T>( key ), std::forward<TFx>( fx ), std::forward<TObj>( obj ) );
return set_lvalue_fx(std::integral_constant<bool, std::is_lvalue_reference<TObj>::value || std::is_pointer<TObj>::value>(),
std::forward<T>(key), std::forward<TFx>(fx), std::forward<TObj>(obj));
}
size_t size() const {
@ -89,131 +89,131 @@ public:
}
private:
template<typename T, typename TFx>
table& set_isfunction_fx( std::true_type, T&& key, TFx&& fx ) {
return set_fx( std::false_type( ), std::forward<T>( key ), std::forward<TFx>( fx ) );
}
template<typename T, typename TFx>
table& set_isfunction_fx(std::true_type, T&& key, TFx&& fx) {
return set_fx(std::false_type(), std::forward<T>(key), std::forward<TFx>(fx));
}
template<typename T, typename TFx>
table& set_isfunction_fx(std::false_type, T&& key, TFx&& fx) {
typedef typename std::decay<TFx>::type clean_lambda;
typedef typename detail::function_traits<decltype( &clean_lambda::operator() )>::free_function_pointer_type raw_func_t;
typedef typename std::decay<TFx>::type clean_lambda;
typedef typename detail::function_traits<decltype(&clean_lambda::operator())>::free_function_pointer_type raw_func_t;
typedef std::is_convertible<clean_lambda, raw_func_t> isconvertible;
return set_isconvertible_fx( isconvertible( ), std::forward<T>( key ), std::forward<TFx>( fx ) );
return set_isconvertible_fx(isconvertible(), std::forward<T>(key), std::forward<TFx>(fx));
}
template<typename T, typename TFx>
table& set_isconvertible_fx( std::true_type, T&& key, TFx&& fx ) {
typedef typename std::decay<TFx>::type clean_lambda;
typedef typename detail::function_traits<decltype( &clean_lambda::operator() )>::free_function_pointer_type raw_func_t;
return set_isfunction_fx( std::true_type( ), std::forward<T>( key ), raw_func_t( std::forward<TFx>( fx ) ) );
table& set_isconvertible_fx(std::true_type, T&& key, TFx&& fx) {
typedef typename std::decay<TFx>::type clean_lambda;
typedef typename detail::function_traits<decltype(&clean_lambda::operator())>::free_function_pointer_type raw_func_t;
return set_isfunction_fx(std::true_type(), std::forward<T>(key), raw_func_t(std::forward<TFx>(fx)));
}
template<typename T, typename TFx>
table& set_isconvertible_fx( std::false_type, T&& key, TFx&& fx ) {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
std::unique_ptr<lua_func> sptr( new lambda_lua_func<clean_fx>( std::forward<TFx>( fx ) ) );
return set_fx( std::forward<T>( key ), std::move( sptr ) );
table& set_isconvertible_fx(std::false_type, T&& key, TFx&& fx) {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
std::unique_ptr<lua_func> sptr(new lambda_lua_func<clean_fx>(std::forward<TFx>(fx)));
return set_fx(std::forward<T>(key), std::move(sptr));
}
template<typename T, typename TFx, typename TObj>
table& set_lvalue_fx( std::true_type, T&& key, TFx&& fx, TObj&& obj ) {
return set_fx( std::true_type(), std::forward<T>( key ), std::forward<TFx>( fx ), std::forward<TObj>( obj ) );
table& set_lvalue_fx(std::true_type, T&& key, TFx&& fx, TObj&& obj) {
return set_fx(std::true_type(), std::forward<T>(key), std::forward<TFx>(fx), std::forward<TObj>(obj));
}
template<typename T, typename TFx, typename TM>
table& set_lvalue_fx( std::false_type, T&& key, TFx&& fx, TM& mem ) {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
std::unique_ptr<lua_func> sptr( new explicit_lua_func<clean_fx, TM>( mem, std::forward<TFx>( fx ) ) );
return set_fx( std::forward<T>( key ), std::move( sptr ) );
table& set_lvalue_fx(std::false_type, T&& key, TFx&& fx, TM& mem) {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
std::unique_ptr<lua_func> sptr(new explicit_lua_func<clean_fx, TM>(mem, std::forward<TFx>(fx)));
return set_fx(std::forward<T>(key), std::move(sptr));
}
template<typename T, typename TFx, typename TObj>
table& set_fx( std::true_type, T&& key, TFx&& fx, TObj&& obj ) {
typedef typename std::decay<TObj>::type decay_of_to;
typedef typename std::decay<TFx>::type decay_of_tfx;
const static std::size_t data_t_count = ( sizeof(decay_of_tfx)+( sizeof(void*)-1 ) ) / sizeof( void* );
typedef std::array<void*, data_t_count> data_t;
table& set_fx(std::true_type, T&& key, TFx&& fx, TObj&& obj) {
typedef typename std::decay<TObj>::type decay_of_to;
typedef typename std::decay<TFx>::type decay_of_tfx;
const static std::size_t data_t_count = (sizeof(decay_of_tfx)+(sizeof(void*)-1)) / sizeof(void*);
typedef std::array<void*, data_t_count> data_t;
std::string fkey( key );
// Layout:
// idx 1...n: verbatim data of member function pointer
// idx n + 1: is the object's void pointer
// We don't need to store the size, because the other side is templated
// with the same member function pointer type
decay_of_tfx fxptr( std::forward<TFx>( fx ) );
data_t fxptrdata;
std::size_t fxptrsize = sizeof( fxptr );
std::memcpy( std::addressof( fxptrdata[ 0 ] ), std::addressof( fxptr ), fxptrsize );
void* userobjdata = static_cast<void*>( detail::get_ptr( obj ) );
lua_CFunction freefunc = &static_object_lua_func<decay_of_to, TFx>::call;
const char* freefuncname = fkey.c_str( );
const luaL_Reg funcreg[ 2 ] = {
{ freefuncname, freefunc },
{ }
};
std::string fkey(key);
// Layout:
// idx 1...n: verbatim data of member function pointer
// idx n + 1: is the object's void pointer
// We don't need to store the size, because the other side is templated
// with the same member function pointer type
decay_of_tfx fxptr(std::forward<TFx>(fx));
data_t fxptrdata;
std::size_t fxptrsize = sizeof(fxptr);
std::memcpy(std::addressof(fxptrdata[ 0 ]), std::addressof(fxptr), fxptrsize);
void* userobjdata = static_cast<void*>(detail::get_ptr(obj));
lua_CFunction freefunc = &static_object_lua_func<decay_of_to, TFx>::call;
const char* freefuncname = fkey.c_str();
const luaL_Reg funcreg[ 2 ] = {
{ freefuncname, freefunc },
{ }
};
push( );
push();
stack::push( state( ), fxptrdata );
stack::push( state( ), userobjdata );
luaL_setfuncs( state( ), funcreg, fxptrdata.size() + 1 );
stack::push(state(), fxptrdata);
stack::push(state(), userobjdata);
luaL_setfuncs(state(), funcreg, fxptrdata.size() + 1);
lua_pop( state( ), 1 );
return *this;
lua_pop(state(), 1);
return *this;
}
template<typename T, typename TFx>
table& set_fx(std::false_type, T&& key, TFx&& fx) {
typedef typename std::decay<TFx>::type ptr_fx;
std::string fkey( key );
ptr_fx target( std::forward<TFx>( fx ) );
void* userdata = static_cast<void*>( target );
lua_CFunction freefunc = &static_lua_func<TFx>::call;
const char* freefuncname = fkey.c_str( );
const luaL_Reg funcreg[ 2 ] = {
{ freefuncname, freefunc },
{ }
};
std::string fkey(key);
ptr_fx target(std::forward<TFx>(fx));
void* userdata = static_cast<void*>(target);
lua_CFunction freefunc = &static_lua_func<TFx>::call;
const char* freefuncname = fkey.c_str();
const luaL_Reg funcreg[ 2 ] = {
{ freefuncname, freefunc },
{ }
};
push( );
push();
stack::push( state( ), target );
luaL_setfuncs( state( ), funcreg, 1 );
stack::push(state(), target);
luaL_setfuncs(state(), funcreg, 1);
lua_pop( state( ), 1 );
return *this;
lua_pop(state(), 1);
return *this;
}
template<typename T>
table& set_fx( T&& key, std::unique_ptr<lua_func> luafunc ) {
std::string fkey( key );
auto hint = funcs.find( fkey );
if ( hint == funcs.end( ) ) {
std::shared_ptr<lua_func> sptr( luafunc.release( ) );
hint = funcs.emplace_hint( hint, fkey, std::move( sptr ) );
}
else {
hint->second.reset( luafunc.release( ) );
}
lua_func* target = hint->second.get( );
void* userdata = static_cast<void*>( target );
lua_CFunction freefunc = &lua_func::call;
const char* freefuncname = hint->first.c_str( );
const luaL_Reg funcreg[ 2 ] = {
{ freefuncname, freefunc },
{ }
};
table& set_fx(T&& key, std::unique_ptr<lua_func> luafunc) {
std::string fkey(key);
auto hint = funcs.find(fkey);
if (hint == funcs.end()) {
std::shared_ptr<lua_func> sptr(luafunc.release());
hint = funcs.emplace_hint(hint, fkey, std::move(sptr));
}
else {
hint->second.reset(luafunc.release());
}
lua_func* target = hint->second.get();
void* userdata = static_cast<void*>(target);
lua_CFunction freefunc = &lua_func::call;
const char* freefuncname = hint->first.c_str();
const luaL_Reg funcreg[ 2 ] = {
{ freefuncname, freefunc },
{ }
};
push( );
push();
stack::push( state( ), userdata );
luaL_setfuncs( state( ), funcreg, 1 );
stack::push(state(), userdata);
luaL_setfuncs(state(), funcreg, 1);
lua_pop( state( ), 1 );
return *this;
lua_pop(state(), 1);
return *this;
}
};
} // sol