From 2a1468ccce3b986715dc636eceeec185a99bacf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Neun=C3=BCller?= Date: Thu, 25 Jul 2013 21:25:56 +0200 Subject: [PATCH] Add basic luabind converter for boost::function. --- src/luaexport/LuaFunction.hpp | 39 +++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/luaexport/LuaFunction.hpp b/src/luaexport/LuaFunction.hpp index 9f2c8b1..cf8e484 100644 --- a/src/luaexport/LuaFunction.hpp +++ b/src/luaexport/LuaFunction.hpp @@ -7,6 +7,7 @@ #ifndef LUA_FUNCTION_HPP_INCLUDED #define LUA_FUNCTION_HPP_INCLUDED LUA_FUNCTION_HPP_INCLUDED +#include #include #include #include @@ -67,6 +68,44 @@ struct LuaFunction luabind::object m_func; }; +namespace luabind { + template + struct default_converter> + : native_converter_base> + { + typedef boost::function FType; + typedef typename FType::result_type FResultType; + static int compute_score(lua_State* L, int index) + { + if (lua_type(L, index) == LUA_TFUNCTION) + return 1; + if (luaL_getmetafield(L, index, "__call")) { + lua_pop(L, 1); + return 0; + } + return -1; + } + + FType from(lua_State* L, int index) + { + return LuaFunction( + luabind::object(from_stack(L, index))); + } + + //void to(lua_State* L, FType value); not supported + }; + + template + struct default_converter const> + : default_converter> + {}; + + template + struct default_converter const&> + : default_converter> + {}; +} // namespace luabind + #endif // include guard