如何迭代 luabind 类(在 lua 或 c++ 中)?

发布于 2024-09-05 16:42:22 字数 241 浏览 4 评论 0原文

如何迭代 luabind 类(在 lua 或 c++ 中)?

class 'A'

function A:__init()
    -- Does not work
    -- self is userdata, not a table
    for i, v in pairs(self) do
    end
end

谢谢

How to iterate through luabind class (in lua or in c++)?

class 'A'

function A:__init()
    -- Does not work
    -- self is userdata, not a table
    for i, v in pairs(self) do
    end
end

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一世旳自豪 2024-09-12 16:42:24

如果您尝试查找有关变量的反射信息(方法列表等),则可以使用 class_info()class_names() 函数。

注意: 据我所知,这些函数没有文档记录,但它们至少存在于 Luabind 0.9 中。使用风险自负。

要在 Lua 代码中使用这些 Luabind 函数,您需要先绑定它们。示例:

#include "luabind/class_info.hpp"
/* ... */
luabind::open(L);
luabind::bind_class_info(L);

然后从您的 Lua 代码中,您可以内省一个变量:

-- Variable "game" is an instance of class "Game"
c = class_info(game)

print(c.name)
-- Prints:
--   Game

for k, v in pairs(c.methods) do print(k, v) end
-- Prints:
--   get_config    function: 01765AE0
--   on_init       function: 01765E90
--   ...

for k, v in pairs(c.attributes) do print(k, v) end
-- ...

您还可以获取 Luabind 知道的所有类的列表:

for i, v in ipairs(class_names()) do print(v) end
-- Prints:
--   class_info_data
--   Config
--   Game
--   ...

If you're trying to look up reflection information about a variable (list of methods, etc.) then you can use the class_info() and class_names() functions.

Note: These functions aren't documented as far as I can tell, but they at least exist in Luabind 0.9. Use at your own risk.

To use these Luabind functions in your Lua code, you need to bind them first. Example:

#include "luabind/class_info.hpp"
/* ... */
luabind::open(L);
luabind::bind_class_info(L);

Then from your Lua code, you can introspect a variable:

-- Variable "game" is an instance of class "Game"
c = class_info(game)

print(c.name)
-- Prints:
--   Game

for k, v in pairs(c.methods) do print(k, v) end
-- Prints:
--   get_config    function: 01765AE0
--   on_init       function: 01765E90
--   ...

for k, v in pairs(c.attributes) do print(k, v) end
-- ...

You can also get a list of all the classes Luabind knows about:

for i, v in ipairs(class_names()) do print(v) end
-- Prints:
--   class_info_data
--   Config
--   Game
--   ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文