1. <small id='237sr'></small><noframes id='237sr'>

      <i id='237sr'><tr id='237sr'><dt id='237sr'><q id='237sr'><span id='237sr'><b id='237sr'><form id='237sr'><ins id='237sr'></ins><ul id='237sr'></ul><sub id='237sr'></sub></form><legend id='237sr'></legend><bdo id='237sr'><pre id='237sr'><center id='237sr'></center></pre></bdo></b><th id='237sr'></th></span></q></dt></tr></i><div id='237sr'><tfoot id='237sr'></tfoot><dl id='237sr'><fieldset id='237sr'></fieldset></dl></div>
    2. <tfoot id='237sr'></tfoot>
      • <bdo id='237sr'></bdo><ul id='237sr'></ul>
      <legend id='237sr'><style id='237sr'><dir id='237sr'><q id='237sr'></q></dir></style></legend>

    3. 使用 Lua + SWIG 将成员动态添加到类

      时间:2023-09-26
        <tfoot id='alL1A'></tfoot>

            • <bdo id='alL1A'></bdo><ul id='alL1A'></ul>

                <i id='alL1A'><tr id='alL1A'><dt id='alL1A'><q id='alL1A'><span id='alL1A'><b id='alL1A'><form id='alL1A'><ins id='alL1A'></ins><ul id='alL1A'></ul><sub id='alL1A'></sub></form><legend id='alL1A'></legend><bdo id='alL1A'><pre id='alL1A'><center id='alL1A'></center></pre></bdo></b><th id='alL1A'></th></span></q></dt></tr></i><div id='alL1A'><tfoot id='alL1A'></tfoot><dl id='alL1A'><fieldset id='alL1A'></fieldset></dl></div>
                  <tbody id='alL1A'></tbody>
              1. <small id='alL1A'></small><noframes id='alL1A'>

              2. <legend id='alL1A'><style id='alL1A'><dir id='alL1A'><q id='alL1A'></q></dir></style></legend>
                本文介绍了使用 Lua + SWIG 将成员动态添加到类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                这段 Lua 代码,创建了一个表并动态添加了一个新成员.运行这个我可以按预期在屏幕上看到 "hello" :

                This Lua code, creates a table and dynamically adds a new member. Running this I can get "hello" on the screen as expected:

                foo = {}
                foo.x = "hello"
                print(foo.x)
                

                但现在我使用 SWIG 将一些 C++ 类绑定到 Lua.为此,在 test.i(SWIG 模块文件)中,我创建了一个像这样的简单类:

                But now I'm using SWIG to bind some C++ classes to Lua. For that purpose, in a test.i (SWIG module file) I created a simple class like this:

                %module test
                
                %inline
                %{
                
                class Foo
                {
                public:
                  Foo() { X = 0; }
                  void SetX(int x) { X = x; }
                  int GetX() { return X; }
                private:
                  int X;
                };
                
                %}
                

                然后我写了一个这样的测试Lua代码:

                Then I wrote a test Lua code like that:

                obj = test.Foo()
                obj:SetX(5)
                print("Number: " .. obj:GetX())
                

                按预期运行并获取 "Number 5".问题是,当我动态地向我的 SWIG 绑定对象添加一个新成员时,我尝试访问它,如下所示:

                Running and getting "Number 5" as expected. The problem is that when I dynamically add a new member to my SWIG-binded object, and I try to access it, like so:

                obj.Y = 7
                print("Number: " .. obj.Y)
                

                我收到此错误消息:

                "attempt to concatenate field 'Y' (a nil value)"
                

                是否可以在使用 SWIG 绑定的对象上动态添加新成员?是否有一些选项而不必移动到另一个 Lua 绑定库?

                Is it possible to dynamically add new members on objects binded using SWIG? Is there some option without having to move to another Lua binding library?

                推荐答案

                SWIG 的对象不使用表;它使用用户数据.毕竟那些对象都是C++对象,需要存放Lua代码不能接触到的C++数据.

                SWIG doesn't use tables for its objects; it uses userdata. After all, those objects are C++ objects, and need to store C++ data that Lua code shouldn't be able to touch.

                而且我不会费心寻找另一个 Lua 绑定库";几乎所有都使用用户数据,Lua 代码明确无法修改这些数据(为了提供完全执行此操作的能力).

                And I wouldn't bother looking for "another Lua binding library"; pretty much all of them use userdata, which Lua code explicitly cannot modify (in order to provide the ability to do exactly this).

                然而,这并不意味着你不能作弊.

                However, that doesn't mean you can't cheat.

                你总是可以把你从 C++ 代码中得到的对象包装到你自己的 Lua 表中,它会有一个元表,将未知的调用转发到 C++ 对象.执行此操作的代码如下所示:

                You can always wrap the object you get from C++ code into your own Lua table, which would have a metatable that forwards unknown calls to the C++ object. The code to do so would look something like this:

                local function WrapObject(cppObject)
                
                    local proxy = {}
                
                    local wrapper_metatable = {}
                
                function wrapper_metatable.__index(self, key)
                    local ret = rawget(self, key)
                    if(not ret) then
                        ret = cppObject[key]
                        if(type(ret) == "function") then
                            return function(self, ...)
                                return ret(cppObject, ...)
                            end
                        else
                            return ret
                        end
                    else
                        return ret
                    end
                
                end
                
                
                    setmetatable(proxy, wrapper_metatable)
                    return proxy
                end
                

                返回的代理对象是一个 Lua 表,可以在其上设置键和值.当您获得一个值时,例如调用一个函数,它会查看该值是否已在表中设置.如果没有,它会尝试从您包装的 C++ 对象中获取它,该对象将通过它的元表.

                The returned proxy object is a Lua table that can have keys and values set on it. When you get a value, such as to call a function, it will see if that value was set in the table. If not, it attempts to fetch it from the C++ object that you wrapped, which will go through its metatable.

                如果您的 C++ 类使用其他元函数,如 __add__sub__tostring 等,则您需要扩展此元表.

                You'll need to expand this metatable if your C++ class uses other metafunctions like __add, __sub, __tostring and so forth.

                这篇关于使用 Lua + SWIG 将成员动态添加到类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:通过 require 在不同 Lua 状态之间共享全局变量 下一篇:luaL_dostring 在堆栈上什么都不放?

                相关文章

                <small id='r6zpM'></small><noframes id='r6zpM'>

                <legend id='r6zpM'><style id='r6zpM'><dir id='r6zpM'><q id='r6zpM'></q></dir></style></legend>
                <tfoot id='r6zpM'></tfoot>

              3. <i id='r6zpM'><tr id='r6zpM'><dt id='r6zpM'><q id='r6zpM'><span id='r6zpM'><b id='r6zpM'><form id='r6zpM'><ins id='r6zpM'></ins><ul id='r6zpM'></ul><sub id='r6zpM'></sub></form><legend id='r6zpM'></legend><bdo id='r6zpM'><pre id='r6zpM'><center id='r6zpM'></center></pre></bdo></b><th id='r6zpM'></th></span></q></dt></tr></i><div id='r6zpM'><tfoot id='r6zpM'></tfoot><dl id='r6zpM'><fieldset id='r6zpM'></fieldset></dl></div>

                    <bdo id='r6zpM'></bdo><ul id='r6zpM'></ul>