[英]how to determine whether a dll has been registered with my c++ function?
[英]How to determine if a C++ usertype has been registered with tolua
我们使用tolua ++为C ++类生成Lua绑定。
假设我有一个C ++类:
class Foo
{
//Some methods in Foo, irrelevant to question.
};
和包含以下内容的tolua .pkg文件
class Foo
{
};
考虑以下功能:
void call_some_lua_function(lua_State* luaState)
{
Foo* myFoo = new Foo();
tolua_pushusertype(luaState, (void*)myFoo, "Foo");
//More code to actually call Lua, irrelevant to question.
}
现在,实际问题:
如果第3个参数与通过调用tolua_cclass注册的C ++类的有效完全限定字符串不对应,则tolua_pushusertype会在Lua中导致段错误。 所以,如果参数3中的“Bar”,我们得到一个段错误。
我想做的是以下内容:
void call_some_lua_function(lua_State* luaState)
{
//determine if tolua is aware of my type, how to do this?
//Something like:
//if(!tolua_iscpptype_registered("Foo"))
//{
// abort gracefully
//}
Foo* myFoo = new Foo();
tolua_pushusertype(luaState, (void*)myFoo, "Foo");
//More code to actually call Lua, irrelevant to question.
}
有没有办法用tolua做到这一点?
我正在使用tolua,而不是tolua ++,但我们希望它在某种程度上是相似的。 在tolua中,您可以测试该类是否已注册,如下所示:
tolua_getmetatable(L, "ClassName");
if (lua_isnil(L, -1)) {
// the class wasn't found
}
提示:检查tolua.cast的实现方式并检查其参数。 它将类型名称作为字符串。
编辑:更好奇,我下载了tolua ++来源并查看了内部。 它看起来并不完全相似,并且缺少关键功能。 我必须给你一个可能有用的未经检验的建议:
luaL_getmetatable(L, "ClassName");
if (lua_isnil(L, -1)) {
// the class wasn't found
}
tolua和tolua ++之间的区别似乎是tolua为其创建的metatable(“tolua。”前缀)使用了“命名空间”。
我只是一个lua初学者,因此我的建议是:在你自己的函数中包含你的tolua调用,它跟踪通过它注册的类。 现在你可以询问你的包装器tolua是否知道你的课程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.