Re: How to get Lua class name from C++ class?
Glen Fraser <[email protected]> Wed, 28 Aug 2013 17:38:10 +0200
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <[email protected]> |
--===============0994903338847428648== Content-Type: multipart/alternative; boundary="Apple-Mail=_81CBE816-BB2A-4EB4-B481-9AA5E04BE092" --Apple-Mail=_81CBE816-BB2A-4EB4-B481-9AA5E04BE092 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=windows-1252 Thanks, Christian, but I didn't manage to get anything to work in the = way you describe. I can't seem to simply substitute 'object' with = 'argument'=85 I also tried using myObject.push(L), then using the = "from_stack" referenced argument to call get_class_info(), then doing a = lua_pop(), but that just crashes on me (a bit later on)=85and before = that, it hasn't returned the correct class_info structure. I think I'd = need to see a simple example, sorry. On the other hand, the "hacky" code from Eduard (see his earlier reply) = does seem to work fine, as does another approach I came up with, where I = just use luabind::call_function() to call the bound "class_info" = function in Lua, then get the class_info structure by doing object_cast = on the returned object. Of course, this may be (?) a bit less efficient = than doing the direct stack-fiddling you guys suggest, but it works. Thanks, Glen. On Aug 28, 2013, at 4:07 PM, "Christian" <[email protected]> wrote: > Just replace luabind::object with luabind::argument in your code. They = work both the same, only that the first stores the referenced object in = the registry, while the latter just leaves it on the stack. > =20 > If you ever have to convert one into another, just use the ones push() = method and the others from_stack constructor (but avoid relative indices = with luabind::argument). > =20 > From: Glen Fraser [mailto:[email protected]]=20 > Sent: Wednesday, August 28, 2013 1:09 PM > To: [email protected] > Subject: [luabind] How to get Lua class name from C++ class? > =20 > Hello there, I'm using luabind and trying to get the Lua subclass name = from an instance of the base class in C++. > =20 > I want to use get_class_info(), but it expects an "argument" rather = than an "object". Perhaps there is a better way to do this, besides = trying to figure out how to convert a luabind::object into a = luabind::adl::argument=85 Here is some example code that should = illustrate the problem: > =20 > class BaseClass { > public: > virtual ~BaseClass() {} > std::string getClassName(lua_State* L); > // ... more stuff... > }; > =20 > class BaseClass_wrapper : public BaseClass, public luabind::wrap_base = { > // ... some stuff... > }; > =20 > std::string BaseClass::getClassName(lua_State* L) > { > BaseClass_wrapper* p =3D dynamic_cast<BaseClass_wrapper*>(this); > std::string className("UNKNOWN"); > if (p !=3D 0) { > luabind::object myObject(L, p); > luabind::class_info info =3D = luabind::get_class_info(myObject); // no good! > // no viable conversion from 'luabind::object' to 'const = luabind::adl::argument' > className =3D info.name; > } > return className; > } > =20 > =20 > Thanks for any suggestions, > Glen. > =20 > = --------------------------------------------------------------------------= ---- > Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more! > Discover the easy way to master current and previous Microsoft = technologies > and advance your career. Get an incredible 1,500+ hours of = step-by-step > tutorial videos with LearnDevNow. Subscribe today and save! > = http://pubads.g.doubleclick.net/gampad/clk?id=3D58040911&iu=3D/4140/ostg.c= lktrk_______________________________________________ > luabind-user mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/luabind-user --Apple-Mail=_81CBE816-BB2A-4EB4-B481-9AA5E04BE092 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=windows-1252 <html><head><meta http-equiv=3D"Content-Type" content=3D"text/html = charset=3Dwindows-1252"><base href=3D"x-msg://181/"></head><body = style=3D"word-wrap: break-word; -webkit-nbsp-mode: space; = -webkit-line-break: after-white-space; ">Thanks, Christian, but I didn't = manage to get anything to work in the way you describe. I can't = seem to simply substitute 'object' with 'argument'=85 I also tried = using myObject.push(L), then using the "from_stack" referenced argument = to call get_class_info(), then doing a lua_pop(), but that just crashes = on me (a bit later on)=85and before that, it hasn't returned the correct = class_info structure. I think I'd need to see a simple example, = sorry.<div><br></div><div>On the other hand, the "hacky" code from = Eduard (see his earlier reply) does seem to work fine, as does another = approach I came up with, where I just use luabind::call_function() = to call the bound "class_info" function in Lua, then get the = class_info structure by doing object_cast on the returned object. = Of course, this may be (?) a bit less efficient than doing the = direct stack-fiddling you guys suggest, but it = works.</div><div><br></div><div>Thanks,</div><div>Glen.</div><div><br><div= ><div>On Aug 28, 2013, at 4:07 PM, "Christian" <<a = href=3D"mailto:[email protected]">[email protected]</a>> wrote:</div><br = class=3D"Apple-interchange-newline"><blockquote type=3D"cite"><div = lang=3D"DE-AT" link=3D"blue" vlink=3D"purple" style=3D"font-family: = Helvetica; font-size: medium; font-style: normal; font-variant: normal; = font-weight: normal; letter-spacing: normal; line-height: normal; = orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: = none; white-space: normal; widows: 2; word-spacing: 0px; = -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div = class=3D"WordSection1" style=3D"page: WordSection1; "><div = style=3D"margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: 'Times = New Roman', serif; "><span lang=3D"EN-US" style=3D"font-size: 11pt; = ">Just replace</span><span lang=3D"EN-US" style=3D"font-size: 11pt; = font-family: Consolas; "><span = class=3D"Apple-converted-space"> </span>luabind::object</span><span = lang=3D"EN-US" style=3D"font-size: 11pt; "><span = class=3D"Apple-converted-space"> </span>with<span = class=3D"Apple-converted-space"> </span></span><span lang=3D"EN-US" = style=3D"font-size: 11pt; font-family: Consolas; = ">luabind::argument</span><span lang=3D"EN-US" style=3D"font-size: 11pt; = "><span class=3D"Apple-converted-space"> </span>in your code. They = work both the same, only that the first stores the referenced object in = the registry, while the latter just leaves it on the = stack.<o:p></o:p></span></div><div style=3D"margin: 0cm 0cm 0.0001pt; = font-size: 12pt; font-family: 'Times New Roman', serif; "><span = lang=3D"EN-US" style=3D"font-size: 11pt; "> </span></div><div = style=3D"margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: 'Times = New Roman', serif; "><span lang=3D"EN-US" style=3D"font-size: 11pt; ">If = you ever have to convert one into another, just use the ones<span = class=3D"Apple-converted-space"> </span></span><span lang=3D"EN-US" = style=3D"font-size: 11pt; font-family: Consolas; ">push()</span><span = lang=3D"EN-US" style=3D"font-size: 11pt; "><span = class=3D"Apple-converted-space"> </span>method and the others<span = class=3D"Apple-converted-space"> </span></span><span lang=3D"EN-US" = style=3D"font-size: 11pt; font-family: Consolas; = ">from_stack</span><span lang=3D"EN-US" style=3D"font-size: 11pt; = "><span class=3D"Apple-converted-space"> </span>constructor (but = avoid relative indices with</span><span lang=3D"EN-US" style=3D"font-size:= 11pt; font-family: Consolas; "><span = class=3D"Apple-converted-space"> </span>luabind::argument</span><span= lang=3D"EN-US" style=3D"font-size: 11pt; = ">).<o:p></o:p></span></div><div style=3D"margin: 0cm 0cm 0.0001pt; = font-size: 12pt; font-family: 'Times New Roman', serif; "><span = lang=3D"EN-US" style=3D"font-size: 11pt; "> </span></div><div = style=3D"border-style: none none none solid; border-left-width: 1.5pt; = border-left-color: blue; padding: 0cm 0cm 0cm 4pt; "><div><div = style=3D"border-style: solid none none; border-top-width: 1pt; = border-top-color: rgb(181, 196, 223); padding: 3pt 0cm 0cm; "><div = style=3D"margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: 'Times = New Roman', serif; "><b><span lang=3D"DE" style=3D"font-size: 10pt; = font-family: Tahoma, sans-serif; ">From:</span></b><span lang=3D"DE" = style=3D"font-size: 10pt; font-family: Tahoma, sans-serif; "><span = class=3D"Apple-converted-space"> </span>Glen Fraser = [mailto:holaglen@<a href=3D"http://gmail.com" style=3D"color: purple; = text-decoration: underline; ">gmail.com</a>]<span = class=3D"Apple-converted-space"> </span><br><b>Sent:</b><span = class=3D"Apple-converted-space"> </span>Wednesday, August 28, 2013 = 1:09 PM<br><b>To:</b><span class=3D"Apple-converted-space"> </span><a= href=3D"mailto:[email protected]" style=3D"color: = purple; text-decoration: underline; = ">[email protected]</a><br><b>Subject:</b><span = class=3D"Apple-converted-space"> </span>[luabind] How to get Lua = class name from C++ class?<o:p></o:p></span></div></div></div><div = style=3D"margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: 'Times = New Roman', serif; "><o:p> </o:p></div><div style=3D"margin: 0cm = 0cm 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; = ">Hello there, I'm using luabind and trying to get the Lua subclass name = from an instance of the base class in C++.<o:p></o:p></div><div><div = style=3D"margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: 'Times = New Roman', serif; "><o:p> </o:p></div></div><div><div = style=3D"margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: 'Times = New Roman', serif; ">I want to use get_class_info(), but it expects an = "argument" rather than an "object". Perhaps there is a better way = to do this, besides trying to figure out how to convert a = luabind::object into a luabind::adl::argument=85 Here is some = example code that should illustrate the = problem:<o:p></o:p></div></div><div><div style=3D"margin: 0cm 0cm = 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; = "><o:p> </o:p></div></div><div><div><div style=3D"margin: 0cm 0cm = 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; = "><span style=3D"font-size: 10.5pt; font-family: Menlo, serif; color: = rgb(187, 44, 162); ">class</span><span style=3D"font-size: 10.5pt; = font-family: Menlo, serif; "><span = class=3D"Apple-converted-space"> </span>BaseClass = {<o:p></o:p></span></div></div><div><div style=3D"margin: 0cm 0cm = 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; = "><span style=3D"font-size: 10.5pt; font-family: Menlo, serif; color: = rgb(187, 44, 162); ">public</span><span style=3D"font-size: 10.5pt; = font-family: Menlo, serif; ">:</span><span style=3D"font-size: 10.5pt; = font-family: Menlo, serif; color: rgb(187, 44, 162); = "><o:p></o:p></span></div></div><div><div style=3D"margin: 0cm 0cm = 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; = "><span style=3D"font-size: 10.5pt; font-family: Menlo, serif; "> = <span class=3D"Apple-converted-space"> </span><span = style=3D"color: rgb(187, 44, 162); ">virtual</span><span = class=3D"Apple-converted-space"> </span>~BaseClass() = {}<o:p></o:p></span></div></div><div><div style=3D"margin: 0cm 0cm = 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; = "><span style=3D"font-size: 10.5pt; font-family: Menlo, serif; "> = <span class=3D"Apple-converted-space"> </span><span = style=3D"color: rgb(112, 61, 170); ">std</span>::<span style=3D"color: = rgb(112, 61, 170); ">string</span><span = class=3D"Apple-converted-space"> </span>getClassName(<span = style=3D"color: rgb(79, 129, 135); ">lua_State</span>* = L);<o:p></o:p></span></div></div><div><div style=3D"margin: 0cm 0cm = 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; = "><span style=3D"font-size: 10.5pt; font-family: Menlo, serif; "> = <span class=3D"Apple-converted-space"> </span></span><span = style=3D"font-size: 10.5pt; font-family: Menlo, serif; color: rgb(0, = 132, 0); ">// ... more stuff...<o:p></o:p></span></div></div><div><div = style=3D"margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: 'Times = New Roman', serif; "><span style=3D"font-size: 10.5pt; font-family: = Menlo, serif; ">};<o:p></o:p></span></div></div><div><div style=3D"margin:= 0cm 0cm 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', = serif; "><span style=3D"font-size: 10.5pt; font-family: Menlo, serif; = "> </span></div></div><div><div style=3D"margin: 0cm 0cm 0.0001pt; = font-size: 12pt; font-family: 'Times New Roman', serif; "><span = style=3D"font-size: 10.5pt; font-family: Menlo, serif; color: rgb(187, = 44, 162); ">class</span><span style=3D"font-size: 10.5pt; font-family: = Menlo, serif; "><span = class=3D"Apple-converted-space"> </span>BaseClass_wrapper :<span = class=3D"Apple-converted-space"> </span><span style=3D"color: = rgb(187, 44, 162); ">public</span><span = class=3D"Apple-converted-space"> </span><span style=3D"color: = rgb(79, 129, 135); ">BaseClass</span>,<span = class=3D"Apple-converted-space"> </span><span style=3D"color: = rgb(187, 44, 162); ">public</span><span = class=3D"Apple-converted-space"> </span><span style=3D"color: = rgb(79, 129, 135); ">luabind</span>::<span style=3D"color: rgb(79, 129, = 135); ">wrap_base</span><span = class=3D"Apple-converted-space"> </span>{<o:p></o:p></span></div></di= v><div><div style=3D"margin: 0cm 0cm 0.0001pt; font-size: 12pt; = font-family: 'Times New Roman', serif; "><span style=3D"font-size: = 10.5pt; font-family: Menlo, serif; "> <span = class=3D"Apple-converted-space"> </span></span><span = style=3D"font-size: 10.5pt; font-family: Menlo, serif; color: rgb(0, = 132, 0); ">// ... some stuff...<o:p></o:p></span></div></div><div><div = style=3D"margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: 'Times = New Roman', serif; "><span style=3D"font-size: 10.5pt; font-family: = Menlo, serif; ">};<o:p></o:p></span></div></div><div><div style=3D"margin:= 0cm 0cm 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', = serif; "><span style=3D"font-size: 10.5pt; font-family: Menlo, serif; = "> </span></div></div><div><div><div style=3D"margin: 0cm 0cm = 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; = "><span style=3D"font-size: 10.5pt; font-family: Menlo, serif; color: = rgb(112, 61, 170); ">std</span><span style=3D"font-size: 10.5pt; = font-family: Menlo, serif; ">::<span style=3D"color: rgb(112, 61, 170); = ">string</span><span class=3D"Apple-converted-space"> </span><span = style=3D"color: rgb(79, 129, 135); = ">BaseClass</span>::getClassName(<span style=3D"color: rgb(79, 129, = 135); ">lua_State</span>* L)<o:p></o:p></span></div></div><div><div = style=3D"margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: 'Times = New Roman', serif; "><span style=3D"font-size: 10.5pt; font-family: = Menlo, serif; ">{<o:p></o:p></span></div></div><div><div style=3D"margin: = 0cm 0cm 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', = serif; "><span style=3D"font-size: 10.5pt; font-family: Menlo, serif; = "> <span = class=3D"Apple-converted-space"> </span></span><span = style=3D"font-size: 10.5pt; font-family: Menlo, serif; color: rgb(79, = 129, 135); ">BaseClass_wrapper</span><span style=3D"font-size: 10.5pt; = font-family: Menlo, serif; ">* p =3D<span = class=3D"Apple-converted-space"> </span></span><span = style=3D"font-size: 10.5pt; font-family: Menlo, serif; color: rgb(187, = 44, 162); ">dynamic_cast</span><span style=3D"font-size: 10.5pt; = font-family: Menlo, serif; "><</span><span style=3D"font-size: = 10.5pt; font-family: Menlo, serif; color: rgb(79, 129, 135); = ">BaseClass_wrapper</span><span style=3D"font-size: 10.5pt; font-family: = Menlo, serif; ">*>(</span><span style=3D"font-size: 10.5pt; = font-family: Menlo, serif; color: rgb(187, 44, 162); ">this</span><span = style=3D"font-size: 10.5pt; font-family: Menlo, serif; ">);</span><span = style=3D"font-size: 10.5pt; font-family: Menlo, serif; color: rgb(79, = 129, 135); "><o:p></o:p></span></div></div><div><div style=3D"margin: = 0cm 0cm 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', = serif; "><span style=3D"font-size: 10.5pt; font-family: Menlo, serif; = "> <span class=3D"Apple-converted-space"> </span><span = style=3D"color: rgb(112, 61, 170); ">std</span>::<span style=3D"color: = rgb(112, 61, 170); ">string</span><span = class=3D"Apple-converted-space"> </span>className(<span = style=3D"color: rgb(209, 47, 27); = ">"UNKNOWN"</span>);<o:p></o:p></span></div></div><div><div = style=3D"margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: 'Times = New Roman', serif; "><span style=3D"font-size: 10.5pt; font-family: = Menlo, serif; "> <span = class=3D"Apple-converted-space"> </span><span style=3D"color: = rgb(187, 44, 162); ">if</span><span = class=3D"Apple-converted-space"> </span>(p !=3D<span = class=3D"Apple-converted-space"> </span><span style=3D"color: = rgb(39, 42, 216); ">0</span>) {<o:p></o:p></span></div></div><div><div = style=3D"margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: 'Times = New Roman', serif; "><span style=3D"font-size: 10.5pt; font-family: = Menlo, serif; "> luabind::object myObject(L, = p);<o:p></o:p></span></div></div><div><div style=3D"margin: 0cm 0cm = 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; = "><span style=3D"font-size: 10.5pt; font-family: Menlo, serif; color: = rgb(255, 64, 19); "> luabind::class_info info = =3D luabind::get_class_info(myObject); // no good!</span><span = style=3D"font-size: 10.5pt; font-family: Menlo, serif; = "><o:p></o:p></span></div></div><div><div style=3D"margin: 0cm 0cm = 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; = "><span style=3D"font-size: 10.5pt; font-family: Menlo, serif; color: = rgb(255, 64, 19); "> // no viable conversion = from 'luabind::object' to 'const luabind::adl::argument'</span><span = style=3D"font-size: 10.5pt; font-family: Menlo, serif; = "><o:p></o:p></span></div></div><div><div style=3D"margin: 0cm 0cm = 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; = "><span style=3D"font-size: 10.5pt; font-family: Menlo, serif; "> = className =3D = info.name;<o:p></o:p></span></div></div><div><div style=3D"margin: 0cm = 0cm 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; = "><span style=3D"font-size: 10.5pt; font-family: Menlo, serif; "> = }<o:p></o:p></span></div></div><div><div style=3D"margin: 0cm 0cm = 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; = "><span style=3D"font-size: 10.5pt; font-family: Menlo, serif; "> = <span class=3D"Apple-converted-space"> </span><span = style=3D"color: rgb(187, 44, 162); ">return</span><span = class=3D"Apple-converted-space"> </span>className;<o:p></o:p></span><= /div></div><div><div style=3D"margin: 0cm 0cm 0.0001pt; font-size: 12pt; = font-family: 'Times New Roman', serif; "><span style=3D"font-size: = 10.5pt; font-family: Menlo, serif; = ">}<o:p></o:p></span></div></div><div><div style=3D"margin: 0cm 0cm = 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; = "><span style=3D"font-size: 10.5pt; font-family: Menlo, serif; = "> </span></div></div></div></div><div><div style=3D"margin: 0cm = 0cm 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; = "><o:p> </o:p></div></div><div><div style=3D"margin: 0cm 0cm = 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; = ">Thanks for any suggestions,<o:p></o:p></div></div><div><div = style=3D"margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: 'Times = New Roman', serif; ">Glen.<o:p></o:p></div></div><div><div = style=3D"margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: 'Times = New Roman', serif; = "><o:p> </o:p></div></div></div></div>-------------------------------= -----------------------------------------------<br>Learn the = latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!<br>Discover = the easy way to master current and previous Microsoft = technologies<br>and advance your career. Get an incredible 1,500+ hours = of step-by-step<br>tutorial videos with LearnDevNow. Subscribe today and = save!<br><a = href=3D"http://pubads.g.doubleclick.net/gampad/clk?id=3D58040911&iu=3D= /4140/ostg.clktrk_______________________________________________" = style=3D"color: purple; text-decoration: underline; = ">http://pubads.g.doubleclick.net/gampad/clk?id=3D58040911&iu=3D/4140/= ostg.clktrk_______________________________________________</a><br>luabind-= user mailing list<br><a href=3D"mailto:[email protected]"= style=3D"color: purple; text-decoration: underline; = ">[email protected]</a><br><a = href=3D"https://lists.sourceforge.net/lists/listinfo/luabind-user" = style=3D"color: purple; text-decoration: underline; = ">https://lists.sourceforge.net/lists/listinfo/luabind-user</a><br></div><= /blockquote></div><br></div></body></html>= --Apple-Mail=_81CBE816-BB2A-4EB4-B481-9AA5E04BE092-- --===============0994903338847428648== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline ------------------------------------------------------------------------------ Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more! Discover the easy way to master current and previous Microsoft technologies and advance your career. Get an incredible 1,500+ hours of step-by-step tutorial videos with LearnDevNow. Subscribe today and save! http://pubads.g.doubleclick.net/gampad/clk?id=58040911&iu=/4140/ostg.clktrk --===============0994903338847428648== 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 --===============0994903338847428648==--