我有一个C++项目,其中1个类的1个方法经常更改.所以我想把代码从C++转移到Lua.注意,我是Lua的新手.
整个任务:
我已经找到了如何使用农历第一步,并且无法应对第二和第三.
我不能使用SWIG和boost.
//This has a large number of steps, but I'm gonna post them all. This is all using native Lua 5 and the lua CAPI.
int CreateInstanceOfT(lua_State* L) {
new (lua_newuserdata(L, sizeof(T))) T(constructor args);
return 1;
}
int CallSomeFuncOnT(lua_State* L) {
if (lua_istable(L, 1)) { // If we're passed a table, get CData
lua_getfield(L, 1, "CData");
lua_replace(L, 1);
}
if (!lua_touserdata(L, 1))
lua_error(L); // longjmp out.
T& ref = *(T*)lua_touserdata(L, 1);
ref.SomeFunc(); // If you want args, I'll assume that you can pass them yourself
return 0;
}
int main() {
lua_State* L = luaL_newstate();
lua_pushcfunction(L, CreateInstanceOfT);
lua_setglobal(L, "CreateInstanceOfT");
lua_pushcfunction(L, CallSomeFuncOnT);
lua_setglobal(L, "CallSomeFuncOnT");
luaL_dofile(L, "something.lua");
lua_close(L);
}
-- Accompanying Lua code: semicolons are optional but I do out of habit. In something.lua
function CreateCInstance()
local Instance = {
CData = CreateInstanceOfT();
SomeFunc = CallSomeFuncOnT;
}
return Instance;
end
local object = CreateCInstance();
object:SomeFunc(); // Calls somefunc.
Run Code Online (Sandbox Code Playgroud)
我可以发布大量有关如何使曝光更容易的细节,以及如何进行继承等等 - 如果你想暴露多个T,我需要改变(我认为最常见的解决方案是一个简单的struct { std::auto_ptr<void>, int type }交易).但是,如果您对此过程一无所知,那么它应该是一个起点.
基本上,首先,我们要求Lua分配一些空间(userdata),然后将T放入其中.当CallSomeFuncOnT出现时,首先我们问它是否有一个表(许多Lua类基于表,因为它们支持面向对象,元表等),并获取用户数据,然后我们将其转换为指向我们的对象,然后转换为引用.请记住,lua_touserdata给你一个空白*,所以你最好还是确定另一端是什么.然后我们调用somefunc并返回.在Main中,我们只是将函数注册为全局变量.
现在,在Lua中,当您调用CreateInstanceOfT时,它实际上只是对Lua用户透明地调用T构造函数.然后我们在一个表中抛弃它,这对于Lua新手来说更简单,并通过传递它来调用SomeFunc.
| 归档时间: |
|
| 查看次数: |
8397 次 |
| 最近记录: |