Quantcast

How to get the attributes of a lua class without their names, using luabind

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to get the attributes of a lua class without their names, using luabind

Thibault Ehrhart
Hello everyone,

I have a Lua class (defined using luabind's 'class' keyword) and I'd like to get the value of the attributes defined in Lua, with C++.

In the code below, I've defined a class, created an instance "foo" and set a custom attribute "bar" to that instance. Finally, I am calling a C++ function which has to print the attributes of the instance passed as a parameter.

Here's the Lua code :

    class 'MyLuaClass'
    function MyLuaClass:__init() end
    
    foo = MyLuaClass()
    foo.bar = "baz";
    PrintAttributes(foo);

And now the C++ code of the PrintAttributes function, obj is a class instance:

    void PrintAttributes(luabind::object &obj)
    {
        // Direct access works
        std::cout << obj["bar"] << std::endl; // Will print "baz" :-)
    
        // But what if I don't know the name of the variable(s)?
        for (luabind::iterator it(obj); it != luabind::iterator(); ++it)
        {
            // Doesn't work since obj is an userdata
        }
    }

I've also tried to use class_info but it only works with properties defined in a C++ class. Is there a way to access those defined with Lua?

Regards,


------------------------------------------------------------------------------
DreamFactory - Open Source REST & JSON Services for HTML5 & Native Apps
OAuth, Users, Roles, SQL, NoSQL, BLOB Storage and External API Access
Free app hosting. Or install the open source package on any LAMP server.
Sign up and see examples for AngularJS, jQuery, Sencha Touch and Native!
http://pubads.g.doubleclick.net/gampad/clk?id=63469471&iu=/4140/ostg.clktrk
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to get the attributes of a lua class without their names, using luabind

Crashy
Hi !

I hate to 'bump' very old topics, but I'm facing the same problem while writing a lua debugger.
I can access values by name, but using the iterator results in an infinite loop.
Iterators are doing well when the object is a simple table though, it only fails when the object is a luabind class.

Does anybody has a solution to iterate over member variables of a luabind class ?

Thanks !

Thibault Ehrhart wrote
Hello everyone,

I have a Lua class (defined using luabind's 'class' keyword) and I'd like
to get the value of the attributes defined in Lua, with C++.

In the code below, I've defined a class, created an instance "foo" and set
a custom attribute "bar" to that instance. Finally, I am calling a C++
function which has to print the attributes of the instance passed as a
parameter.

Here's the Lua code :

    class 'MyLuaClass'
    function MyLuaClass:__init() end

    foo = MyLuaClass()
    foo.bar = "baz";
    PrintAttributes(foo);

And now the C++ code of the PrintAttributes function, obj is a class
instance:

    void PrintAttributes(luabind::object &obj)
    {
        // Direct access works
        std::cout << obj["bar"] << std::endl; // Will print "baz" :-)

        // But what if I don't know the name of the variable(s)?
        for (luabind::iterator it(obj); it != luabind::iterator(); ++it)
        {
            // Doesn't work since obj is an userdata
        }
    }

I've also tried to use class_info but it only works with properties defined
in a C++ class. Is there a way to access those defined with Lua?

Regards,
Loading...