mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Fixing style to fit @Rapptz's usual no-tabs, four-space-indents, template<> no-spaces stuff.
This commit is contained in:
parent
49c73c4725
commit
854cbeef71
|
@ -94,22 +94,22 @@ inline auto call(Function&& f, const Tuple& t, indices<Indices...>) -> decltype(
|
||||||
}
|
}
|
||||||
} // detail
|
} // detail
|
||||||
|
|
||||||
template <typename... Ret>
|
template<typename... Ret>
|
||||||
struct lua_return_type {
|
struct lua_return_type {
|
||||||
typedef std::tuple<Ret...> type;
|
typedef std::tuple<Ret...> type;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename Ret>
|
template<typename Ret>
|
||||||
struct lua_return_type<Ret> {
|
struct lua_return_type<Ret> {
|
||||||
typedef Ret type;
|
typedef Ret type;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template<>
|
||||||
struct lua_return_type<> {
|
struct lua_return_type<> {
|
||||||
typedef void type;
|
typedef void type;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template<>
|
||||||
struct lua_return_type<void> {
|
struct lua_return_type<void> {
|
||||||
typedef void type;
|
typedef void type;
|
||||||
};
|
};
|
||||||
|
|
|
@ -33,29 +33,29 @@ struct static_lua_func {
|
||||||
typedef detail::function_traits<fx_t> fx_traits;
|
typedef detail::function_traits<fx_t> fx_traits;
|
||||||
|
|
||||||
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, fx_t* fx, lua_State* L) {
|
||||||
stack::pop_call( L, fx, t );
|
stack::pop_call(L, fx, t);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... TRn, typename... Args>
|
template<typename... TRn, typename... Args>
|
||||||
static int typed_call( types<TRn...>, types<Args...> t, fx_t* fx, lua_State* L ) {
|
static int typed_call(types<TRn...>, types<Args...> t, fx_t* fx, lua_State* L) {
|
||||||
auto r = stack::pop_call( L, fx, t );
|
auto r = stack::pop_call(L, fx, t);
|
||||||
stack::push( L, std::move( r ) );
|
stack::push(L, std::move(r));
|
||||||
return sizeof...( TRn );
|
return sizeof...(TRn);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int call( lua_State* L ) {
|
static int call(lua_State* L) {
|
||||||
void* functiondata = lua_touserdata( L, lua_upvalueindex( 1 ) );
|
void* functiondata = lua_touserdata(L, lua_upvalueindex(1));
|
||||||
//if ( functiondata == nullptr )
|
//if (functiondata == nullptr)
|
||||||
// throw sol_error( "call from lua to c++ function has null function pointer data" );
|
// throw sol_error("call from lua to c++ function has null function pointer data");
|
||||||
fx_t* fx = *static_cast<fx_t*>( functiondata );
|
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 );
|
int r = typed_call(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), fx, L);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
int operator()( lua_State* L ) {
|
int operator()(lua_State* L) {
|
||||||
return call( L );
|
return call(L);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -65,58 +65,58 @@ struct static_object_lua_func {
|
||||||
typedef detail::function_traits<fx_t> fx_traits;
|
typedef detail::function_traits<fx_t> fx_traits;
|
||||||
|
|
||||||
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, fx_t& ifx, lua_State* L) {
|
||||||
auto fx = [ &item, &ifx ] ( Args&&... args ) { ( item.*ifx )( std::forward<Args>( args )... ); };
|
auto fx = [ &item, &ifx ] (Args&&... args) { (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 TR, typename... Args>
|
template<typename TR, typename... Args>
|
||||||
static int typed_call( types<TR>, types<Args...>, T& item, fx_t& ifx, lua_State* L ) {
|
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 fx = [ &item, &ifx ] (Args&&... args) -> TR { return (item.*ifx)(std::forward<Args>(args)...); };
|
||||||
auto r = stack::pop_call( L, fx, types<Args...>( ) );
|
auto r = stack::pop_call(L, fx, types<Args...>());
|
||||||
stack::push( L, std::move( r ) );
|
stack::push(L, std::move(r));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... TRn, typename... Args>
|
template<typename... TRn, typename... Args>
|
||||||
static int typed_call( types<TRn...>, types<Args...>, T& item, fx_t& ifx, lua_State* L ) {
|
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 fx = [ &item, &ifx ] (Args&&... args) -> std::tuple<TRn...> { return (item.*ifx)(std::forward<Args>(args)...); };
|
||||||
auto r = stack::pop_call( L, fx, types<Args...>( ) );
|
auto r = stack::pop_call(L, fx, types<Args...>());
|
||||||
stack::push( L, std::move( r ) );
|
stack::push(L, std::move(r));
|
||||||
return sizeof...( TRn );
|
return sizeof...(TRn);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int call( lua_State* L ) {
|
static int call(lua_State* L) {
|
||||||
const static std::size_t data_t_count = ( sizeof(fx_t)+( sizeof(void*)-1 ) ) / sizeof( void* );
|
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;
|
typedef std::array<void*, data_t_count> data_t;
|
||||||
data_t fxptrdata;
|
data_t fxptrdata;
|
||||||
int upvalue = 1;
|
int upvalue = 1;
|
||||||
for ( std::size_t i = 0; i < fxptrdata.size( ); ++i ) {
|
for (std::size_t i = 0; i < fxptrdata.size(); ++i) {
|
||||||
fxptrdata[ i ] = lua_touserdata( L, lua_upvalueindex( upvalue++ ) );
|
fxptrdata[ i ] = lua_touserdata(L, lua_upvalueindex(upvalue++));
|
||||||
}
|
}
|
||||||
void* objectdata = lua_touserdata( L, lua_upvalueindex( upvalue++ ) );
|
void* objectdata = lua_touserdata(L, lua_upvalueindex(upvalue++));
|
||||||
//if ( objectdata == nullptr )
|
//if (objectdata == nullptr)
|
||||||
// throw sol_error( "call from lua to c++ function has null object data" );
|
// 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* fxptr = reinterpret_cast<fx_t*>(static_cast<void*>(fxptrdata.data()));
|
||||||
fx_t& mem_ptr = *fxptr;
|
fx_t& mem_ptr = *fxptr;
|
||||||
T& obj = *static_cast<T*>( objectdata );
|
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 );
|
int r = typed_call(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), obj, mem_ptr, L);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
int operator()( lua_State* L ) {
|
int operator()(lua_State* L) {
|
||||||
return call( L );
|
return call(L);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct lua_func {
|
struct lua_func {
|
||||||
static int call( lua_State* L ) {
|
static int call(lua_State* L) {
|
||||||
void* inheritancedata = lua_touserdata( L, lua_upvalueindex( 1 ) );
|
void* inheritancedata = lua_touserdata(L, lua_upvalueindex(1));
|
||||||
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& fx = *static_cast<lua_func*>( inheritancedata );
|
lua_func& fx = *static_cast<lua_func*>(inheritancedata);
|
||||||
int r = fx( L );
|
int r = fx(L);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,7 +176,7 @@ struct explicit_lua_func : public lua_func {
|
||||||
template<typename... TRn, typename... Args>
|
template<typename... TRn, typename... Args>
|
||||||
int operator()(types<TRn...>, types<Args...> t, lua_State* L) {
|
int operator()(types<TRn...>, types<Args...> t, lua_State* L) {
|
||||||
auto r = stack::pop_call(L, fx, t);
|
auto r = stack::pop_call(L, fx, t);
|
||||||
stack::push(L, std::move( r ));
|
stack::push(L, std::move(r));
|
||||||
return sizeof...(TRn);
|
return sizeof...(TRn);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -140,16 +140,16 @@ inline void push(lua_State* L, const nil_t&) {
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void push( lua_State* L, lua_CFunction func ) {
|
inline void push(lua_State* L, lua_CFunction func) {
|
||||||
lua_pushcfunction( L, func );
|
lua_pushcfunction(L, func);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void push( lua_State* L, lua_CFunction func, int n ) {
|
inline void push(lua_State* L, lua_CFunction func, int n) {
|
||||||
lua_pushcclosure( L, func, n );
|
lua_pushcclosure(L, func, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void push( lua_State* L, void* userdata ) {
|
inline void push(lua_State* L, void* userdata) {
|
||||||
lua_pushlightuserdata( L, userdata );
|
lua_pushlightuserdata(L, userdata);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<size_t N>
|
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) {
|
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) {
|
inline void push(lua_State* L, const std::string& str) {
|
||||||
|
@ -166,9 +166,9 @@ inline void push(lua_State* L, const std::string& str) {
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, size_t N>
|
template<typename T, size_t N>
|
||||||
inline void push( lua_State* L, const std::array<T, N>& data ) {
|
inline void push(lua_State* L, const std::array<T, N>& data) {
|
||||||
for ( std::size_t i = 0; i < data.size( ); ++i ) {
|
for (std::size_t i = 0; i < data.size(); ++i) {
|
||||||
push( L, data[ i ] );
|
push(L, data[ i ]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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> {};
|
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) {
|
int atpanic(lua_State* L) {
|
||||||
std::string err = lua_tostring( L, -1 );
|
std::string err = lua_tostring(L, -1);
|
||||||
throw sol_error( err );
|
throw sol_error(err);
|
||||||
}
|
}
|
||||||
} // detail
|
} // detail
|
||||||
|
|
||||||
|
|
116
sol/table.hpp
116
sol/table.hpp
|
@ -31,13 +31,13 @@
|
||||||
|
|
||||||
namespace sol {
|
namespace sol {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
template <typename T>
|
template<typename T>
|
||||||
T* get_ptr( T& val ) {
|
T* get_ptr(T& val) {
|
||||||
return std::addressof( val );
|
return std::addressof(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template<typename T>
|
||||||
T* get_ptr( T* val ) {
|
T* get_ptr(T* val) {
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -74,13 +74,13 @@ public:
|
||||||
template<typename T, typename TFx>
|
template<typename T, typename TFx>
|
||||||
table& set_function(T&& key, TFx&& fx) {
|
table& set_function(T&& key, TFx&& fx) {
|
||||||
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_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>
|
template<typename T, typename TFx, typename TObj>
|
||||||
table& set_function(T&& key, TFx&& fx, TObj&& obj) {
|
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>( ),
|
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 ) );
|
std::forward<T>(key), std::forward<TFx>(fx), std::forward<TObj>(obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
|
@ -90,129 +90,129 @@ public:
|
||||||
private:
|
private:
|
||||||
|
|
||||||
template<typename T, typename TFx>
|
template<typename T, typename TFx>
|
||||||
table& set_isfunction_fx( std::true_type, T&& key, TFx&& fx ) {
|
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 ) );
|
return set_fx(std::false_type(), std::forward<T>(key), std::forward<TFx>(fx));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, typename TFx>
|
template<typename T, typename TFx>
|
||||||
table& set_isfunction_fx(std::false_type, T&& key, TFx&& fx) {
|
table& set_isfunction_fx(std::false_type, T&& key, TFx&& fx) {
|
||||||
typedef typename std::decay<TFx>::type clean_lambda;
|
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 detail::function_traits<decltype(&clean_lambda::operator())>::free_function_pointer_type raw_func_t;
|
||||||
typedef std::is_convertible<clean_lambda, raw_func_t> isconvertible;
|
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>
|
template<typename T, typename TFx>
|
||||||
table& set_isconvertible_fx( std::true_type, T&& key, TFx&& fx ) {
|
table& set_isconvertible_fx(std::true_type, T&& key, TFx&& fx) {
|
||||||
typedef typename std::decay<TFx>::type clean_lambda;
|
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 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 ) ) );
|
return set_isfunction_fx(std::true_type(), std::forward<T>(key), raw_func_t(std::forward<TFx>(fx)));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, typename TFx>
|
template<typename T, typename TFx>
|
||||||
table& set_isconvertible_fx( std::false_type, T&& key, TFx&& fx ) {
|
table& set_isconvertible_fx(std::false_type, T&& key, TFx&& fx) {
|
||||||
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_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 ) ) );
|
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 ) );
|
return set_fx(std::forward<T>(key), std::move(sptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, typename TFx, typename TObj>
|
template<typename T, typename TFx, typename TObj>
|
||||||
table& set_lvalue_fx( std::true_type, T&& key, TFx&& fx, 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 ) );
|
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>
|
template<typename T, typename TFx, typename TM>
|
||||||
table& set_lvalue_fx( std::false_type, T&& key, TFx&& fx, TM& mem ) {
|
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;
|
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 ) ) );
|
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 ) );
|
return set_fx(std::forward<T>(key), std::move(sptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, typename TFx, typename TObj>
|
template<typename T, typename TFx, typename TObj>
|
||||||
table& set_fx( std::true_type, T&& key, TFx&& fx, TObj&& obj ) {
|
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<TObj>::type decay_of_to;
|
||||||
typedef typename std::decay<TFx>::type decay_of_tfx;
|
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* );
|
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;
|
typedef std::array<void*, data_t_count> data_t;
|
||||||
|
|
||||||
std::string fkey( key );
|
std::string fkey(key);
|
||||||
|
|
||||||
// Layout:
|
// Layout:
|
||||||
// idx 1...n: verbatim data of member function pointer
|
// idx 1...n: verbatim data of member function pointer
|
||||||
// idx n + 1: is the object's void pointer
|
// idx n + 1: is the object's void pointer
|
||||||
// We don't need to store the size, because the other side is templated
|
// We don't need to store the size, because the other side is templated
|
||||||
// with the same member function pointer type
|
// with the same member function pointer type
|
||||||
decay_of_tfx fxptr( std::forward<TFx>( fx ) );
|
decay_of_tfx fxptr(std::forward<TFx>(fx));
|
||||||
data_t fxptrdata;
|
data_t fxptrdata;
|
||||||
std::size_t fxptrsize = sizeof( fxptr );
|
std::size_t fxptrsize = sizeof(fxptr);
|
||||||
std::memcpy( std::addressof( fxptrdata[ 0 ] ), std::addressof( fxptr ), fxptrsize );
|
std::memcpy(std::addressof(fxptrdata[ 0 ]), std::addressof(fxptr), fxptrsize);
|
||||||
void* userobjdata = static_cast<void*>( detail::get_ptr( obj ) );
|
void* userobjdata = static_cast<void*>(detail::get_ptr(obj));
|
||||||
lua_CFunction freefunc = &static_object_lua_func<decay_of_to, TFx>::call;
|
lua_CFunction freefunc = &static_object_lua_func<decay_of_to, TFx>::call;
|
||||||
const char* freefuncname = fkey.c_str( );
|
const char* freefuncname = fkey.c_str();
|
||||||
const luaL_Reg funcreg[ 2 ] = {
|
const luaL_Reg funcreg[ 2 ] = {
|
||||||
{ freefuncname, freefunc },
|
{ freefuncname, freefunc },
|
||||||
{ }
|
{ }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
push( );
|
push();
|
||||||
|
|
||||||
stack::push( state( ), fxptrdata );
|
stack::push(state(), fxptrdata);
|
||||||
stack::push( state( ), userobjdata );
|
stack::push(state(), userobjdata);
|
||||||
luaL_setfuncs( state( ), funcreg, fxptrdata.size() + 1 );
|
luaL_setfuncs(state(), funcreg, fxptrdata.size() + 1);
|
||||||
|
|
||||||
lua_pop( state( ), 1 );
|
lua_pop(state(), 1);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, typename TFx>
|
template<typename T, typename TFx>
|
||||||
table& set_fx(std::false_type, T&& key, TFx&& fx) {
|
table& set_fx(std::false_type, T&& key, TFx&& fx) {
|
||||||
typedef typename std::decay<TFx>::type ptr_fx;
|
typedef typename std::decay<TFx>::type ptr_fx;
|
||||||
std::string fkey( key );
|
std::string fkey(key);
|
||||||
ptr_fx target( std::forward<TFx>( fx ) );
|
ptr_fx target(std::forward<TFx>(fx));
|
||||||
void* userdata = static_cast<void*>( target );
|
void* userdata = static_cast<void*>(target);
|
||||||
lua_CFunction freefunc = &static_lua_func<TFx>::call;
|
lua_CFunction freefunc = &static_lua_func<TFx>::call;
|
||||||
const char* freefuncname = fkey.c_str( );
|
const char* freefuncname = fkey.c_str();
|
||||||
const luaL_Reg funcreg[ 2 ] = {
|
const luaL_Reg funcreg[ 2 ] = {
|
||||||
{ freefuncname, freefunc },
|
{ freefuncname, freefunc },
|
||||||
{ }
|
{ }
|
||||||
};
|
};
|
||||||
|
|
||||||
push( );
|
push();
|
||||||
|
|
||||||
stack::push( state( ), target );
|
stack::push(state(), target);
|
||||||
luaL_setfuncs( state( ), funcreg, 1 );
|
luaL_setfuncs(state(), funcreg, 1);
|
||||||
|
|
||||||
lua_pop( state( ), 1 );
|
lua_pop(state(), 1);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
table& set_fx( T&& key, std::unique_ptr<lua_func> luafunc ) {
|
table& set_fx(T&& key, std::unique_ptr<lua_func> luafunc) {
|
||||||
std::string fkey( key );
|
std::string fkey(key);
|
||||||
auto hint = funcs.find( fkey );
|
auto hint = funcs.find(fkey);
|
||||||
if ( hint == funcs.end( ) ) {
|
if (hint == funcs.end()) {
|
||||||
std::shared_ptr<lua_func> sptr( luafunc.release( ) );
|
std::shared_ptr<lua_func> sptr(luafunc.release());
|
||||||
hint = funcs.emplace_hint( hint, fkey, std::move( sptr ) );
|
hint = funcs.emplace_hint(hint, fkey, std::move(sptr));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
hint->second.reset( luafunc.release( ) );
|
hint->second.reset(luafunc.release());
|
||||||
}
|
}
|
||||||
lua_func* target = hint->second.get( );
|
lua_func* target = hint->second.get();
|
||||||
void* userdata = static_cast<void*>( target );
|
void* userdata = static_cast<void*>(target);
|
||||||
lua_CFunction freefunc = &lua_func::call;
|
lua_CFunction freefunc = &lua_func::call;
|
||||||
const char* freefuncname = hint->first.c_str( );
|
const char* freefuncname = hint->first.c_str();
|
||||||
const luaL_Reg funcreg[ 2 ] = {
|
const luaL_Reg funcreg[ 2 ] = {
|
||||||
{ freefuncname, freefunc },
|
{ freefuncname, freefunc },
|
||||||
{ }
|
{ }
|
||||||
};
|
};
|
||||||
|
|
||||||
push( );
|
push();
|
||||||
|
|
||||||
stack::push( state( ), userdata );
|
stack::push(state(), userdata);
|
||||||
luaL_setfuncs( state( ), funcreg, 1 );
|
luaL_setfuncs(state(), funcreg, 1);
|
||||||
|
|
||||||
lua_pop( state( ), 1 );
|
lua_pop(state(), 1);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user