Re: [luabind] How to get the attributes of a lua class, without their names, using luabind
Brought to you by:
arvidn,
daniel_wallin
From: jean-baptiste g. <jea...@la...> - 2015-12-12 16:40:03
|
This works for me too in such a case, but it fails if the object is a luabind 'class' object, as in the OP message: class 'MyLuaClass' function MyLuaClass:__init() end foo = MyLuaClass() foo.bar = "baz"; PrintAttributes(foo); On 12/12/2015 14:48, Pavel Kovalenko wrote: > This example works for me (but I use another fork: > https://github.com/decimad/luabind-deboostified) > > lua code: > > local obj = {} > obj.foo = function (self) > puts("obj:foo") > end > obj.data = 42 > > dump_object(obj) > > cpp code: > > void dump_object(const luabind::object &obj) > { > for (luabind::iterator it(obj), end; it!=end; it++) > { > auto s = object_cast<std::string>(it.key()); > puts(s.c_str()); > } > } > > What kind of lua object are you trying to iterate over? > On 12/12/2015 0:23 AM, jean-baptiste griffo wrote: >> Hi, >> >> Well it's weird but I don't remember the exact fork I'm using, I think I >> use this one >> https://github.com/mrsharpoblunto/LHP/tree/master/luabind-0.7/include/luabind >> I do not use the official release because I had some problems related to >> size_t & x64 builds, and this fork solved it. >> >> Here is the c++ code, please note that at this point I'm 100% sure the >> object on the stack is the "self" of my class instance >> >> //get the luabind object from the stack >> luabind::object o(luabind::from_stack(L, lastIndex)); >> >> for (luabind::iterator i(o), end; i != end; ++i) >> { >> try >> { >> std::string strKey = luabind::object_cast<std::string>(i.key()); >> printf("%s\n",strKey.c_str()); >> } >> catch(luabind::cast_failed & e) >> { >> printf("error\n"); >> } >> } >> >> ---------- >> >> As a result, it simply prints "error" indefinitely. >> >> Thanks. >> >> >> On 11/12/2015 17:57, Pavel Kovalenko wrote: >>> Hi, >>> >>> I don't have such problem (using luabind-deboostified fork). Could you >>> show the code which causes an infinite loop? Which fork do you use? >>> >>> On 12/11/2015 3:00 PM, luabind-user-request@... wrote: >>>> Send luabind-user mailing list submissions to >>>> luabind-user@... >>>> >>>> To subscribe or unsubscribe via the World Wide Web, visit >>>> https://lists.sourceforge.net/lists/listinfo/luabind-user >>>> or, via email, send a message with subject or body 'help' to >>>> luabind-user-request@... >>>> >>>> You can reach the person managing the list at >>>> luabind-user-owner@... >>>> >>>> When replying, please edit your Subject line so it is more specific >>>> than "Re: Contents of luabind-user digest..." >>>> >>>> >>>> Today's Topics: >>>> >>>> 1. Re: How to get the attributes of a lua class without their >>>> names, using luabind (Crashy) >>>> >>>> >>>> ---------------------------------------------------------------------- >>>> >>>> Message: 1 >>>> Date: Thu, 10 Dec 2015 05:07:38 -0700 (MST) >>>> From: Crashy <jean-baptiste.griffo@...> >>>> Subject: Re: [luabind] How to get the attributes of a lua class >>>> without their names, using luabind >>>> To: luabind-user@... >>>> Message-ID: <1449749258848-7670726.post@...> >>>> Content-Type: text/plain; charset=us-ascii >>>> >>>> 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, >>>> >>>> >>>> >>>> -- >>>> View this message in context: http://lua.2524044.n2.nabble.com/How-to-get-the-attributes-of-a-lua-class-without-their-names-using-luabind-tp7654070p7670726.html >>>> Sent from the Lua C++ Bind mailing list archive at Nabble.com. >>>> >>>> >>>> >>>> ------------------------------ >>>> >>>> ------------------------------------------------------------------------------ >>>> >>>> >>>> ------------------------------ >>>> >>>> _______________________________________________ >>>> luabind-user mailing list >>>> luabind-user@... >>>> https://lists.sourceforge.net/lists/listinfo/luabind-user >>>> >>>> >>>> End of luabind-user Digest, Vol 94, Issue 1 >>>> ******************************************* >>>> >>>> |