File: LuaEventCallback.h

package info (click to toggle)
freespace2 24.0.2%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: trixie
  • size: 43,188 kB
  • sloc: cpp: 583,107; ansic: 21,729; python: 1,174; sh: 464; makefile: 248; xml: 181
file content (48 lines) | stat: -rw-r--r-- 1,091 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#pragma once

#include "object/object.h"
#include "scripting/lua/LuaFunction.h"

namespace scripting {
namespace api {
namespace util {

void convert_arg(lua_State* L, luacpp::LuaValueList& out, object* objp);
}

template <typename Ret, typename... Args>
class LuaEventCallback {
	luacpp::LuaFunction _func;

	void convert_args(lua_State* /*L*/, luacpp::LuaValueList& /*out*/) {}

	template <typename T, typename... A>
	void convert_args(lua_State* L, luacpp::LuaValueList& out, T t, A... args)
	{
		util::convert_arg(L, out, t);

		convert_args(L, out, args...);
	}

  public:
	explicit LuaEventCallback(const luacpp::LuaFunction& func) : _func(func) {}

	void operator()(Args... args)
	{
		using namespace luacpp;

		LuaValueList lua_args;
		convert_args(_func.getLuaState(), lua_args, args...);

		_func(_func.getLuaState(), lua_args);
	}
};

template <typename Ret, typename... Args>
std::function<Ret(Args...)> make_lua_callback(const luacpp::LuaFunction& func)
{
	return std::function<Ret(Args...)>(LuaEventCallback<Ret, Args...>(func));
}

} // namespace api
} // namespace scripting