mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Merge pull request #1 from ThePhD/master
Function binding both ways, and return type support
This commit is contained in:
commit
5455efe83c
26
.gitignore
vendored
26
.gitignore
vendored
|
@ -1,7 +1,19 @@
|
|||
obj/*
|
||||
bin/*
|
||||
*.ninja*
|
||||
demacro.txt
|
||||
Shinobi2
|
||||
dev.*
|
||||
main.cpp
|
||||
obj/*
|
||||
bin/*
|
||||
*.ninja*
|
||||
demacro.txt
|
||||
Shinobi2
|
||||
dev.*
|
||||
main.cpp
|
||||
sol.scratch.cpp
|
||||
lua-5.2.2/
|
||||
Debug/
|
||||
Release/
|
||||
x64/
|
||||
*.vcxproj
|
||||
*.vcxproj.filters
|
||||
*.tlog
|
||||
*.lastbuildstate
|
||||
*.idb
|
||||
*.sln
|
||||
*.gitattributes
|
||||
|
|
55
sol.hpp
55
sol.hpp
|
@ -1,28 +1,29 @@
|
|||
// The MIT License (MIT)
|
||||
|
||||
// Copyright (c) 2013 Danny Y., Rapptz
|
||||
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
// this software and associated documentation files (the "Software"), to deal in
|
||||
// the Software without restriction, including without limitation the rights to
|
||||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
// the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
#ifndef SOL_HPP
|
||||
#define SOL_HPP
|
||||
|
||||
#include "sol/state.hpp"
|
||||
#include "sol/object.hpp"
|
||||
|
||||
// The MIT License (MIT)
|
||||
|
||||
// Copyright (c) 2013 Danny Y., Rapptz
|
||||
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
// this software and associated documentation files (the "Software"), to deal in
|
||||
// the Software without restriction, including without limitation the rights to
|
||||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
// the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
#ifndef SOL_HPP
|
||||
#define SOL_HPP
|
||||
|
||||
#include "sol/state.hpp"
|
||||
#include "sol/object.hpp"
|
||||
#include "sol/function.hpp"
|
||||
|
||||
#endif // SOL_HPP
|
68
sol/function.hpp
Normal file
68
sol/function.hpp
Normal file
|
@ -0,0 +1,68 @@
|
|||
// The MIT License (MIT)
|
||||
|
||||
// Copyright (c) 2013 Danny Y., Rapptz
|
||||
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
// this software and associated documentation files (the "Software"), to deal in
|
||||
// the Software without restriction, including without limitation the rights to
|
||||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
// the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
#ifndef SOL_FUNCTION_HPP
|
||||
#define SOL_FUNCTION_HPP
|
||||
|
||||
#include "reference.hpp"
|
||||
#include "tuple.hpp"
|
||||
#include "stack.hpp"
|
||||
|
||||
namespace sol {
|
||||
class function : virtual public reference {
|
||||
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...>( ) );
|
||||
}
|
||||
|
||||
template <typename Ret>
|
||||
Ret call( types<Ret>, std::size_t n ) {
|
||||
lua_pcall( state( ), n, 1, 0 );
|
||||
return stack::pop<Ret>( state( ) );
|
||||
}
|
||||
|
||||
void call( types<void>, std::size_t n ) {
|
||||
lua_pcall( state( ), n, 0, 0 );
|
||||
}
|
||||
|
||||
void call( types<>, std::size_t n ) {
|
||||
lua_pcall( state( ), n, 0, 0 );
|
||||
}
|
||||
|
||||
public:
|
||||
function() : reference() {}
|
||||
function(lua_State* L, int index = -1) : reference(L, index) {
|
||||
type_assert(L, index, type::function);
|
||||
}
|
||||
|
||||
template<typename... Ret, typename... Args>
|
||||
auto invoke(Args&&... args) -> decltype(call(types<Ret...>( ), sizeof...( Args ))) {
|
||||
push( );
|
||||
stack::push_args( state( ), std::forward<Args>( args )... );
|
||||
return call( types<Ret...>( ), sizeof...( Args ) );
|
||||
}
|
||||
};
|
||||
} // sol
|
||||
|
||||
#endif // SOL_FUNCTION_HPP
|
122
sol/functional.hpp
Normal file
122
sol/functional.hpp
Normal file
|
@ -0,0 +1,122 @@
|
|||
// The MIT License (MIT)
|
||||
|
||||
// Copyright (c) 2013 Danny Y., Rapptz
|
||||
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
// this software and associated documentation files (the "Software"), to deal in
|
||||
// the Software without restriction, including without limitation the rights to
|
||||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
// the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
#include "tuple.hpp"
|
||||
|
||||
#ifndef SOL_FUNCTIONAL_HPP
|
||||
#define SOL_FUNCTIONAL_HPP
|
||||
|
||||
namespace sol {
|
||||
namespace detail {
|
||||
|
||||
template<typename TFuncSignature>
|
||||
struct function_traits;
|
||||
|
||||
template<typename T, typename R, typename... Args>
|
||||
struct function_traits<R(T::*)(Args...)> {
|
||||
static const std::size_t arity = sizeof...(Args);
|
||||
static const bool is_member_function = true;
|
||||
typedef std::tuple<Args...> arg_tuple_type;
|
||||
typedef types<Args...> args_type;
|
||||
typedef R(T::* func_pointer_type)(Args...);
|
||||
typedef typename std::remove_pointer<func_pointer_type>::type func_type;
|
||||
typedef R return_type;
|
||||
template<std::size_t i>
|
||||
using arg_n = std::tuple_element<i, arg_tuple_type>;
|
||||
};
|
||||
|
||||
template<typename T, typename R, typename... Args>
|
||||
struct function_traits<R(T::*)(Args...) const> {
|
||||
static const std::size_t arity = sizeof...(Args);
|
||||
static const bool is_member_function = true;
|
||||
typedef std::tuple<Args...> arg_tuple_type;
|
||||
typedef types<Args...> args_type;
|
||||
typedef R(T::* func_type)(Args...);
|
||||
typedef R(T::* func_pointer_type)(Args...);
|
||||
typedef R return_type;
|
||||
template<std::size_t i>
|
||||
using arg_n = std::tuple_element<i, arg_tuple_type>;
|
||||
};
|
||||
|
||||
template<typename R, typename... Args>
|
||||
struct function_traits<R(Args...)> {
|
||||
static const std::size_t arity = sizeof...(Args);
|
||||
static const bool is_member_function = false;
|
||||
typedef std::tuple<Args...> arg_tuple_type;
|
||||
typedef types<Args...> args_type;
|
||||
typedef R(func_type)(Args...);
|
||||
typedef R(*func_pointer_type)(Args...);
|
||||
typedef R return_type;
|
||||
template<std::size_t i>
|
||||
using arg_n = std::tuple_element<i, arg_tuple_type>;
|
||||
};
|
||||
|
||||
template<typename R, typename... Args>
|
||||
struct function_traits<R(*)(Args...)> {
|
||||
static const std::size_t arity = sizeof...(Args);
|
||||
static const bool is_member_function = false;
|
||||
typedef std::tuple<Args...> arg_tuple_type;
|
||||
typedef types<Args...> args_type;
|
||||
typedef R(func_type)(Args...);
|
||||
typedef R(*func_pointer_type)(Args...);
|
||||
typedef R return_type;
|
||||
template<std::size_t i>
|
||||
using arg_n = std::tuple_element<i, arg_tuple_type>;
|
||||
};
|
||||
|
||||
using std::get;
|
||||
|
||||
template<typename Function, typename Tuple, size_t... Indices>
|
||||
inline auto call(Function&& f, const Tuple& t, indices<Indices...>) -> decltype(f(get<Indices>(t)...)) {
|
||||
return f(get<Indices>(t)...);
|
||||
}
|
||||
|
||||
} // 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)>{});
|
||||
}
|
||||
|
||||
} // sol
|
||||
|
||||
|
||||
#endif // SOL_FUNCTIONAL_HPP
|
155
sol/lua_function.hpp
Normal file
155
sol/lua_function.hpp
Normal file
|
@ -0,0 +1,155 @@
|
|||
// The MIT License (MIT)
|
||||
|
||||
// Copyright (c) 2013 Danny Y., Rapptz
|
||||
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
// this software and associated documentation files (the "Software"), to deal in
|
||||
// the Software without restriction, including without limitation the rights to
|
||||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
// the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
#ifndef SOL_LUA_FUNC_HPP
|
||||
#define SOL_LUA_FUNC_HPP
|
||||
|
||||
#include "functional.hpp"
|
||||
#include "stack.hpp"
|
||||
|
||||
namespace sol {
|
||||
namespace detail {
|
||||
|
||||
struct lua_func {
|
||||
virtual int operator () (lua_State*) {
|
||||
throw sol_error("Failure to call specialized wrapped C++ function from lua");
|
||||
}
|
||||
|
||||
virtual ~lua_func() {};
|
||||
};
|
||||
|
||||
template<typename TFx>
|
||||
struct lambda_lua_func : public lua_func {
|
||||
typedef decltype(&TFx::operator()) fx_t;
|
||||
typedef function_traits<fx_t> fx_traits;
|
||||
TFx fx;
|
||||
|
||||
template<typename... FxArgs>
|
||||
lambda_lua_func(FxArgs&&... fxargs) : fx(std::forward<FxArgs>(fxargs)...) {
|
||||
|
||||
}
|
||||
|
||||
virtual int operator () (lua_State* L) override {
|
||||
return ( *this )( tuple_types<typename fx_traits::return_type>( ), typename fx_traits::args_type( ), L );
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
int operator()(types<void>, types<Args...> t, lua_State* L) {
|
||||
stack::pop_call(L, fx, t);
|
||||
return 0;
|
||||
}
|
||||
|
||||
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, r);
|
||||
return sizeof...(TRn);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename TFx, typename T = TFx, bool is_member_pointer = std::is_member_function_pointer<TFx>::value>
|
||||
struct explicit_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;
|
||||
TFx fx;
|
||||
|
||||
template<typename... FxArgs>
|
||||
explicit_lua_func(FxArgs&&... fxargs) : fx(std::forward<FxArgs>(fxargs)...) {
|
||||
|
||||
}
|
||||
|
||||
virtual int operator () (lua_State* L) override {
|
||||
return ( *this )( tuple_types<typename fx_traits::return_type>( ), typename fx_traits::args_type( ), L );
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
int operator () (types<void>, types<Args...> t, lua_State* L) {
|
||||
stack::pop_call(L, fx, t);
|
||||
return 0;
|
||||
}
|
||||
|
||||
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, r);
|
||||
return sizeof...(TRn);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename TFx, typename T>
|
||||
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;
|
||||
|
||||
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)...);
|
||||
}
|
||||
} fx;
|
||||
|
||||
template<typename... FxArgs>
|
||||
explicit_lua_func(T* m, FxArgs&&... fxargs) : fx(m, std::forward<FxArgs>(fxargs)...) {
|
||||
|
||||
}
|
||||
|
||||
template<typename... FxArgs>
|
||||
explicit_lua_func(T& m, FxArgs&&... fxargs) : fx(std::addressof(m), std::forward<FxArgs>(fxargs)...) {
|
||||
|
||||
}
|
||||
|
||||
virtual int operator () (lua_State* L) override {
|
||||
return ( *this )( tuple_types<typename fx_traits::return_type>( ), typename fx_traits::args_type( ), L );
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
int operator () (types<void>, types<Args...> t, lua_State* L) {
|
||||
stack::pop_call(L, fx, t);
|
||||
return 0;
|
||||
}
|
||||
|
||||
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, r);
|
||||
return sizeof...(TRn);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int lua_cfun(lua_State* L) {
|
||||
void* bridgedata = lua_touserdata(L, lua_upvalueindex(1));
|
||||
auto* fx = static_cast<lua_func*>(bridgedata);
|
||||
int r = fx->operator()(L);
|
||||
return r;
|
||||
}
|
||||
|
||||
} // detail
|
||||
} // sol
|
||||
|
||||
#endif // SOL_LUA_FUNC_HPP
|
|
@ -23,6 +23,7 @@
|
|||
#define SOL_STACK_HPP
|
||||
|
||||
#include "reference.hpp"
|
||||
#include "tuple.hpp"
|
||||
#include <utility>
|
||||
#include <type_traits>
|
||||
|
||||
|
@ -150,6 +151,48 @@ inline void push(lua_State* L, const char (&str)[N]) {
|
|||
inline void push(lua_State* L, const std::string& str) {
|
||||
lua_pushlstring(L, str.c_str(), str.size());
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
template<typename T, std::size_t... I>
|
||||
inline void push(lua_State* L, indices<I...>, const T& tuplen) {
|
||||
using swallow = char [];
|
||||
void(swallow{ '\0', (sol::stack::push(L, std::get<I>(tuplen)), '\0')... });
|
||||
}
|
||||
|
||||
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 )... );
|
||||
}
|
||||
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 ) );
|
||||
}
|
||||
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 ) );
|
||||
}
|
||||
|
||||
} // detail
|
||||
|
||||
template<typename... Args>
|
||||
inline void push(lua_State* L, const std::tuple<Args...>& tuplen) {
|
||||
detail::push(L, build_indices<sizeof...(Args)>(), tuplen);
|
||||
}
|
||||
|
||||
template<typename... Args, typename TFx>
|
||||
inline auto pop_call( lua_State* L, TFx&& fx, types<Args...> )->decltype( detail::ltr_pop( L, std::forward<TFx>( fx ), types<Args...>() ) ) {
|
||||
return detail::ltr_pop( L, std::forward<TFx>( fx ), types<Args...>());
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ template<class T, class...>
|
|||
struct are_same : std::true_type {};
|
||||
|
||||
template<class T, class U, class... Args>
|
||||
struct are_same<T, U, Args...> : std::integral_constant<bool, std::is_same<T,U>{} && are_same<T, 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) {
|
||||
throw sol_error(lua_tostring(L, -1));
|
||||
|
@ -58,6 +58,8 @@ private:
|
|||
std::unique_ptr<lua_State, void(*)(lua_State*)> L;
|
||||
table reg;
|
||||
table global;
|
||||
std::unordered_map<std::string, std::unique_ptr<detail::lua_func>> funcs;
|
||||
|
||||
public:
|
||||
state():
|
||||
L(luaL_newstate(), lua_close),
|
||||
|
@ -145,6 +147,18 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
table create_table(T&& key, int narr = 0, int nrec = 0) {
|
||||
if(narr == 0 && nrec == 0) {
|
||||
|
|
|
@ -23,9 +23,14 @@
|
|||
#define SOL_TABLE_HPP
|
||||
|
||||
#include "stack.hpp"
|
||||
#include "lua_function.hpp"
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
|
||||
namespace sol {
|
||||
class table : virtual public reference {
|
||||
private:
|
||||
std::unordered_map<std::string, std::shared_ptr<detail::lua_func>> funcs;
|
||||
public:
|
||||
table() noexcept: reference{} {}
|
||||
table(lua_State* L, int index = -1): reference(L, index) {
|
||||
|
@ -53,10 +58,60 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
push();
|
||||
return lua_rawlen(state(), -1);
|
||||
}
|
||||
|
||||
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::false_type, T&& key, TFx&& 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 = 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
|
||||
|
||||
|
|
|
@ -26,28 +26,25 @@
|
|||
#include <cstddef>
|
||||
|
||||
namespace sol {
|
||||
namespace detail {
|
||||
|
||||
template<size_t... Ns>
|
||||
struct indices {};
|
||||
struct indices { };
|
||||
|
||||
template<size_t N, size_t... Ns>
|
||||
struct build_indices : build_indices<N-1, N-1, Ns...> {};
|
||||
struct build_indices : build_indices<N - 1, N - 1, Ns...> { };
|
||||
|
||||
template<size_t... Ns>
|
||||
struct build_indices<0, Ns...> : indices<Ns...> {};
|
||||
struct build_indices<0, Ns...> : indices<Ns...>{ };
|
||||
|
||||
using std::get;
|
||||
template<typename... Args>
|
||||
struct types : build_indices<sizeof...(Args)> { };
|
||||
|
||||
template<typename Function, typename Tuple, size_t... Indices>
|
||||
inline auto call(Function f, const Tuple& t, indices<Indices...>) -> decltype(f(get<Indices>(t)...)) {
|
||||
return f(get<Indices>(t)...);
|
||||
}
|
||||
} // detail
|
||||
template<typename... Args>
|
||||
struct tuple_types : types<Args...>, std::false_type { };
|
||||
|
||||
template<typename... Args>
|
||||
struct tuple_types<std::tuple<Args...>> : types<Args...>, std::true_type{ };
|
||||
|
||||
template<typename Function, typename... Args>
|
||||
inline auto call(Function f, const std::tuple<Args...>& t) -> decltype(detail::call(f, t, detail::build_indices<sizeof...(Args)>{})) {
|
||||
return call(f, t, detail::build_indices<sizeof...(Args)>{});
|
||||
}
|
||||
} // sol
|
||||
|
||||
#endif // SOL_TUPLE_HPP
|
|
@ -28,7 +28,9 @@
|
|||
|
||||
namespace sol {
|
||||
struct nil_t {};
|
||||
const nil_t nil{};
|
||||
const nil_t nil{ };
|
||||
struct Void_t { };
|
||||
const Void_t Void{};
|
||||
|
||||
enum class type : int {
|
||||
none = LUA_TNONE,
|
||||
|
|
Loading…
Reference in New Issue
Block a user