English 中文(简体)
简明扼要地从问题登记
原标题:lua metatable registration from c question
  • 时间:2011-02-23 13:52:36
  •  标签:
  • lua

Hello I有以下似乎行之有效的代码,但我不敢肯定为什么——我先造了一个试验舱如下:

class testclass {
    int ivalue;
public:
    int getivalue();
    void setivalue(int &v);
};

之后,注册测试舱(实际功能的借项,但基本面)。 它对表一米的登记没有进行。 (eti Value and seti Value are c function that打上同一名称的类别功能)

static const struct luaL_Reg arraylib_f [] = {
    {"new", new_testclass},
    {NULL, NULL}
};

static const struct luaL_Reg arraylib_m [] = {
    {"set", setivalue},
    {"get", getivalue},
    {NULL, NULL}
};

int luaopen_testclass (lua_State *L) {
    luaL_newmetatable(L, "LuaBook.testclass");
    lua_pushvalue(L, -1); /* duplicates the metatable */
    lua_setfield(L, -2, "__index");
    luaL_register(L, NULL, arraylib_m);
    luaL_register(L, "testclass", arraylib_f);
    return 1;
}

The bit I don t understand is I m adding the functions to the __index for the metatable but when I run

a = testclass.new()
a:set(10)
print(a:get())

然后它按预期运作。 我不理解的是,为什么在我把这套装置装在元件中的时候,有人叫它呢? 难道我已经做些什么?

tia

最佳回答
int luaopen_testclass (lua_State *L) {
    luaL_newmetatable(L, "LuaBook.testclass"); //leaves new metatable on the stack
    lua_pushvalue(L, -1); // there are two  copies  of the metatable on the stack
    lua_setfield(L, -2, "__index"); // pop one of those copies and assign it to
                                    // __index field od the 1st metatable
    luaL_register(L, NULL, arraylib_m); // register functions in the metatable
    luaL_register(L, "testclass", arraylib_f);
    return 1;
}

That code is equivalent to the example Lua code:

metatable = {}
metatable.__index = metatable
metatable.set = function() --[[ stuff --]] end
metatable.get = function() --[[ stuff --]] end

我假定,新的C级测试功能为返回的表格设定了“LuaBook.testclass”元。

在您的法典中,你有意在表__指数领域增加职能。 您指派了协调人,为这个称为“......指数”的可计量领域进行表率,并登记并履行职务。

现在,如果你确定从新的肠道功能(我假定你这样做)中恢复的价值,那么,请打电话给这一价值oo,你把 f叫:set(10),而不是Lua:

  1. checks that there is no such field as set in foo
  2. sees that foo has a metatable
  3. looks at that metatable s __index field - sees it s a table
  4. checks if that table assigned to __index field has a field set and it s value is a function
  5. calls set method passing foo as self parameter

我希望这将帮助你了解这里的情况。

问题回答

如果我理解你的问题,你将询问通过<代码>_index metamethod援引(get()。

守则可以简单地表述:

local o = {}

function o.get(self)
    return self.ivalue
end

function o.set(self, val)
    self.ivalue = val
end

a = {}
mt = {
    __index = function(t, n)
        return o[n]
    end
}

setmetatable(a, mt)

print(a:get())
a:set(10)
print(a:get())

结果:

nil
10

举例来说,<代码>mt 表格作为<代码>a表的元。 www.un.org/chinese/sc/presidency.asp 无论在<条码>上还是在<条码>上,均采用代谢法。 ∗∗∗∗∗ 现有表格a

如果将这一例子改为:

local o = {}

function o.get(self)
    return self.ivalue
end

function o.set(self, val)
    self.ivalue = val
end

a = {}

function a.get(self)
    print( here )
    return self.ivalue
end

mt = {
    __index = function(t, n)
        return o[n]
    end
}

setmetatable(a, mt)

print(a:get())
a:set(10)
print(a:get())

结果:

here
nil
here
10

在此情况下,_index metamethod is NOTquot for get(),因为getindex 已列入

一旦你们理解他们的工作方式,就可以使用计量方法创建许多有意义的建筑。 我建议阅读13.4.1-PiL_indexmethod,并通过另外几个例子开展工作。 以上内容也可从c api上查阅。





相关问题
Wrapping a Lua object for use in C++ with SWIG

Currently I know how to have C++ objects instantiated and passed around in Lua using SWIG bindings, what I need is the reverse. I am using Lua & C++ & SWIG. I have interfaces in C++ and ...

Can t lua_resume after async_wait?

I have some lua script that have some long running task like getting a web page so I make it yield then the C code handle get page job async, so the thread free to do other job and after a specify ...

How to remove a lua table entry by its key?

I have a lua table that I use as a hashmap, ie with string keys : local map = { foo = 1, bar = 2 } I would like to "pop" an element of this table identified by its key. There is a table.remove() ...

Lua plain string.gsub

I ve hit s small block with string parsing. I have a string like: footage/down/temp/cars_[100]_upper/cars_[100]_upper.exr and I m having difficulty using gsub to delete a portion of the string. ...

why do i get "attempt to call global require (a nil value)"?

I have 3 lua files, Init.lua, FreeCamera.lua and Camera.lua , init.lua calls require "Core.Camera.FreeCamera" Free Camera: module(...) require "Core.Camera.Camera" local M = {} FreeCamera = M M ...