帮助boost绑定/函数 [英] Help with boost bind/functions
问题描述
我有这个函数签名我必须匹配
typedef int(* lua_CFunction)(lua_State * L) target sig
这里是我到目前为止:
// somehere else ...
...
registerFunction< LuaEngine>(testFunc,& LuaEngine :: testFunc,this);
...
// 0 arg callback
void funcCallback0(boost :: function< void()> func,lua_State * state)
{
func();
}
template< typename SelfType>
void registerFunction(const std :: string& funcName,boost :: function< void(SelfType *)> func,SelfType * self)
{
// funcToCall必须匹配lua_CFunction
boost :: function< void(lua_State *)> funcToCall = boost :: bind(& LuaEngine :: funcCallback0,this,
boost :: bind(func,self),_1);
lua_register(_luaState,funcName.c_str(),funcToCall);但是,在 lua_register(_luaState ... / code>,仍然抱怨转换问题
错误1错误C2664:
'lua_pushcclosure':无法转换
参数2从
'boost :: function'到
'lua_CFunction'
如何解决这个问题?
解决方案这不能直接解决Lua API需要一个简单的函数指针 - 同时, boost :: function
是一个函数对象,没有办法可以转换为一个简单的函数指针,因为 - - 它不仅捕获代码,而且捕获状态在你的例子中,捕获的状态是 self
的值,因此它有一个代码的代码指针,一些数据 - 而目标API只需要代码指针。
I have this function signature I have to match
typedef int (*lua_CFunction) (lua_State *L);//target sig
Here's what I have so far:
//somewhere else...
...
registerFunction<LuaEngine>("testFunc", &LuaEngine::testFunc, this);
...
//0 arg callback
void funcCallback0(boost::function<void ()> func, lua_State *state)
{
func();
}
template<typename SelfType>
void registerFunction(const std::string &funcName, boost::function<void (SelfType*)> func, SelfType *self)
{
//funcToCall has to match lua_CFunction
boost::function<void (lua_State *)> funcToCall = boost::bind(&LuaEngine::funcCallback0, this,
boost::bind(func, self), _1);
lua_register(_luaState, funcName.c_str(), funcToCall);
}
However, at lua_register(_luaState...
, it's still complaining about conversion issues
Error 1 error C2664:
'lua_pushcclosure' : cannot convert
parameter 2 from
'boost::function' to
'lua_CFunction'
Anyone know how this can be solved?
解决方案 This cannot be solved directly. Lua API wants a plain function pointers from you - that's just a code pointer, and nothing else. Meanwhile, boost::function
is a function object, and there's no way it could possibly be convertible to a plain function pointer, because - roughly speaking - it captures not just the code, but also the state. In your example, the captured state is the value of self
. So it has a code pointer for the code, and some data - and the target API expects just the code pointer.
这篇关于帮助boost绑定/函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!