[英]How to determine if a C++ usertype has been registered with tolua
We use tolua++ to generate Lua bindings for C++ classes. 我们使用tolua ++为C ++类生成Lua绑定。
Assume I have a C++ class: 假设我有一个C ++类:
class Foo
{
//Some methods in Foo, irrelevant to question.
};
and a tolua .pkg file with the following contents 和包含以下内容的tolua .pkg文件
class Foo
{
};
Consider the following function: 考虑以下功能:
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.
}
Now, the actual question: 现在,实际问题:
tolua_pushusertype causes a segfault in Lua if the 3rd parameter does not correspond to a valid fully qualified string of a C++ class that was registered with a call to tolua_cclass. 如果第3个参数与通过调用tolua_cclass注册的C ++类的有效完全限定字符串不对应,则tolua_pushusertype会在Lua中导致段错误。 So, if parameter 3 where "Bar", we get a segfault.
所以,如果参数3中的“Bar”,我们得到一个段错误。
What I would like to do is the following: 我想做的是以下内容:
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.
}
Is there a way to do this using tolua? 有没有办法用tolua做到这一点?
I am using tolua, not tolua++, but let's hope it is somehow similar. 我正在使用tolua,而不是tolua ++,但我们希望它在某种程度上是相似的。 In tolua, you can test if the class is registered with it like this:
在tolua中,您可以测试该类是否已注册,如下所示:
tolua_getmetatable(L, "ClassName");
if (lua_isnil(L, -1)) {
// the class wasn't found
}
Hint: check how tolua.cast is implemented and checks its arguments. 提示:检查tolua.cast的实现方式并检查其参数。 It takes a type name as string.
它将类型名称作为字符串。
Edited: More curious, I downloaded the tolua++ sources and looked inside. 编辑:更好奇,我下载了tolua ++来源并查看了内部。 It doesn't look completely similar, and the critical function is missing.
它看起来并不完全相似,并且缺少关键功能。 I have to give you an untested suggestion that might work:
我必须给你一个可能有用的未经检验的建议:
luaL_getmetatable(L, "ClassName");
if (lua_isnil(L, -1)) {
// the class wasn't found
}
The difference between tolua and tolua++ seems to b that tolua uses a "namespace" for its created metatables ("tolua." prefix). tolua和tolua ++之间的区别似乎是tolua为其创建的metatable(“tolua。”前缀)使用了“命名空间”。
I am just a lua beginner, hence my suggestion: Wrap your tolua-calls in your own function which keeps track of the classes registered through it. 我只是一个lua初学者,因此我的建议是:在你自己的函数中包含你的tolua调用,它跟踪通过它注册的类。 Now you can ask your wrapper if tolua is aware of your class.
现在你可以询问你的包装器tolua是否知道你的课程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.