2013-12-03 03:22:51 +08:00
|
|
|
// 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 {
|
2013-12-14 13:05:14 +08:00
|
|
|
class function : public reference {
|
2013-12-03 03:22:51 +08:00
|
|
|
private:
|
2013-12-12 01:42:00 +08:00
|
|
|
void luacall (std::size_t argcount, std::size_t resultcount) {
|
|
|
|
lua_call(state(), static_cast<uint32_t>(argcount), static_cast<uint32_t>(resultcount));
|
|
|
|
}
|
|
|
|
|
2013-12-03 12:33:23 +08:00
|
|
|
template<typename... Ret>
|
2013-12-12 00:18:13 +08:00
|
|
|
std::tuple<Ret...> invoke(types<Ret...>, std::size_t n) {
|
2013-12-16 11:09:06 +08:00
|
|
|
luacall(n, sizeof...(Ret));
|
2013-12-16 05:27:20 +08:00
|
|
|
return stack::pop_reverse_call(state(), std::make_tuple<Ret...>, types<Ret...>());
|
2013-12-03 08:15:23 +08:00
|
|
|
}
|
|
|
|
|
2013-12-03 12:33:23 +08:00
|
|
|
template<typename Ret>
|
2013-12-12 00:18:13 +08:00
|
|
|
Ret invoke(types<Ret>, std::size_t n) {
|
2013-12-12 01:42:00 +08:00
|
|
|
luacall(n, 1);
|
2013-12-03 12:33:23 +08:00
|
|
|
return stack::pop<Ret>(state());
|
2013-12-03 08:15:23 +08:00
|
|
|
}
|
|
|
|
|
2013-12-12 00:18:13 +08:00
|
|
|
void invoke(types<void>, std::size_t n) {
|
2013-12-12 01:42:00 +08:00
|
|
|
luacall(n, 0);
|
2013-12-03 08:15:23 +08:00
|
|
|
}
|
|
|
|
|
2013-12-12 00:18:13 +08:00
|
|
|
void invoke(types<>, std::size_t n) {
|
2013-12-12 01:42:00 +08:00
|
|
|
luacall(n, 0);
|
2013-12-03 08:15:23 +08:00
|
|
|
}
|
|
|
|
|
2013-12-03 03:22:51 +08:00
|
|
|
public:
|
|
|
|
function() : reference() {}
|
2013-12-03 12:33:23 +08:00
|
|
|
function(lua_State* L, int index = -1): reference(L, index) {
|
2013-12-03 03:22:51 +08:00
|
|
|
type_assert(L, index, type::function);
|
|
|
|
}
|
2013-12-14 03:16:59 +08:00
|
|
|
function(const function&) = default;
|
|
|
|
function& operator=(const function&) = default;
|
2013-12-03 03:22:51 +08:00
|
|
|
|
2013-12-14 06:50:24 +08:00
|
|
|
template<typename... Args>
|
2013-12-14 09:09:51 +08:00
|
|
|
void operator()(Args&&... args) {
|
2013-12-14 17:42:53 +08:00
|
|
|
call<>(std::forward<Args>(args)...);
|
2013-12-14 06:50:24 +08:00
|
|
|
}
|
2013-12-14 13:15:14 +08:00
|
|
|
|
|
|
|
template<typename... Ret, typename... Args>
|
|
|
|
typename multi_return<Ret...>::type operator()(types<Ret...>, Args&&... args) {
|
2013-12-15 12:25:44 +08:00
|
|
|
return call<Ret...>(std::forward<Args>(args)...);
|
2013-12-14 13:15:14 +08:00
|
|
|
}
|
2013-12-14 06:50:24 +08:00
|
|
|
|
2013-12-03 03:22:51 +08:00
|
|
|
template<typename... Ret, typename... Args>
|
2013-12-14 13:15:14 +08:00
|
|
|
typename multi_return<Ret...>::type call(Args&&... args) {
|
2013-12-03 12:33:23 +08:00
|
|
|
push();
|
|
|
|
stack::push_args(state(), std::forward<Args>(args)...);
|
2013-12-12 00:18:13 +08:00
|
|
|
return invoke(types<Ret...>(), sizeof...(Args));
|
2013-12-03 03:22:51 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // sol
|
|
|
|
|
|
|
|
#endif // SOL_FUNCTION_HPP
|