2013-12-02 07:15:26 +08:00
|
|
|
#ifndef SOL_FUNCTION_HPP
|
|
|
|
#define SOL_FUNCTION_HPP
|
|
|
|
|
2013-12-02 07:57:28 +08:00
|
|
|
#include "reference.hpp"
|
|
|
|
#include "tuple.hpp"
|
|
|
|
#include "stack.hpp"
|
2013-12-02 07:15:26 +08:00
|
|
|
|
|
|
|
namespace sol {
|
|
|
|
class function : virtual public reference {
|
|
|
|
private:
|
|
|
|
template<typename... Args>
|
2013-12-02 07:57:28 +08:00
|
|
|
void push_args(Args&&... args) {
|
|
|
|
auto L = state();
|
2013-12-02 07:15:26 +08:00
|
|
|
using swallow = char [];
|
2013-12-02 07:57:28 +08:00
|
|
|
void( swallow{ (stack::push(L, std::forward<Args>(args)), '\0')... } );
|
2013-12-02 07:15:26 +08:00
|
|
|
}
|
|
|
|
public:
|
2013-12-02 07:57:28 +08:00
|
|
|
function() : reference() {}
|
|
|
|
function(lua_State* L, int index = -1) : reference(L, index) {
|
|
|
|
type_assert(L, index, type::function);
|
2013-12-02 07:15:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename... Args>
|
2013-12-02 07:57:28 +08:00
|
|
|
T invoke(Args&&... args) {
|
|
|
|
push();
|
|
|
|
push_args(std::forward<Args>(args)...);
|
|
|
|
lua_pcall(state(), sizeof...(Args), 1, 0);
|
|
|
|
return stack::pop<T>(state());
|
2013-12-02 07:15:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
2013-12-02 07:57:28 +08:00
|
|
|
void invoke(Args&&... args) {
|
|
|
|
push();
|
|
|
|
push_args(std::forward<Args>(args)...);
|
|
|
|
lua_pcall(state(), sizeof...(Args), 0, 0);
|
2013-12-02 07:15:26 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // sol
|
|
|
|
|
|
|
|
|
|
|
|
#endif // SOL_FUNCTION_HPP
|