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

Thibault Ehrhart <[email protected]> Thu, 14 Nov 2013 14:46:35 +0100
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <CAKAJq7Cg-3TL9Oyzo7Zu0w5B=QpVkJhQujd77Nt16jdQ6=R+mg@mail.gmail.com>
--===============2333798257709661047==
Content-Type: multipart/alternative; boundary=001a11c1a2f037a97b04eb235173

--001a11c1a2f037a97b04eb235173
Content-Type: text/plain; charset=ISO-8859-1

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,

--001a11c1a2f037a97b04eb235173
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hello everyone,<br><br>I have a Lua class (defined using l=
uabind&#39;s &#39;class&#39; keyword) and I&#39;d like to get the value of =
the attributes defined in Lua, with C++.<div><br></div><div>In the code bel=
ow, I&#39;ve defined a class, created an instance &quot;foo&quot; and set a=
 custom attribute &quot;bar&quot; to that instance. Finally, I am calling a=
 C++ function which has to print the attributes of the instance passed as a=
 parameter.</div>

<div><br></div><div>Here&#39;s the Lua code :</div><div><br></div><div><div=
>=A0 =A0 class &#39;MyLuaClass&#39;</div><div>=A0 =A0 function MyLuaClass:_=
_init() end</div><div>=A0 =A0=A0</div><div>=A0 =A0 foo =3D MyLuaClass()</di=
v><div>=A0 =A0 foo.bar =3D &quot;baz&quot;;</div>

<div>=A0 =A0 PrintAttributes(foo);</div></div><div><br></div><div><div>And =
now the C++ code of the PrintAttributes function, obj is a class instance:<=
/div><div><br></div><div>=A0 =A0 void PrintAttributes(luabind::object &amp;=
obj)</div>

<div>=A0 =A0 {</div><div>=A0 =A0 =A0 =A0 // Direct access works</div><div>=
=A0 =A0 =A0 =A0 std::cout &lt;&lt; obj[&quot;bar&quot;] &lt;&lt; std::endl;=
 // Will print &quot;baz&quot; :-)</div><div>=A0 =A0=A0</div><div>=A0 =A0 =
=A0 =A0 // But what if I don&#39;t know the name of the variable(s)?</div>

<div>=A0 =A0 =A0 =A0 for (luabind::iterator it(obj); it !=3D luabind::itera=
tor(); ++it)</div><div>=A0 =A0 =A0 =A0 {</div><div>=A0 =A0 =A0 =A0 =A0 =A0 =
// Doesn&#39;t work since obj is an userdata</div><div>=A0 =A0 =A0 =A0 }</d=
iv><div>=A0 =A0 }</div></div><div><br>

</div><div>I&#39;ve also tried to use class_info but it only works with pro=
perties defined in a C++ class. Is there a way to access those defined with=
 Lua?</div><div><br></div><div>Regards,</div><div><br></div></div>

--001a11c1a2f037a97b04eb235173--


--===============2333798257709661047==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

------------------------------------------------------------------------------
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
--===============2333798257709661047==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user

--===============2333798257709661047==--