Re: How to get Lua class name from C++ class?

eduard mueller <[email protected]> Wed, 28 Aug 2013 14:06:24 +0200
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <CAMCDzW3HUk65dam8bTjFa9WNpAqs3Seq-vf88+crMRz50xe+Vw@mail.gmail.com>
--===============4570925712512607369==
Content-Type: multipart/alternative; boundary=089e0141a79cb993c404e500d2ea

--089e0141a79cb993c404e500d2ea
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Hey,

you can
=E2=80=8B =E2=80=8B
use the "private" luabind::detail::object_rep*
luabind::detail::get_instance(lua_State* L, int index)
=E2=80=8Bhere =E2=80=8B
instead.
This is quite a hack, but we at found no other "official" way on doing this
within a wrap_base:

=E2=80=8B=E2=80=8B
 std::string
=E2=80=8B=E2=80=8B
BaseClass::getClassName(lua_State* L)
 {
   luabind::detail::wrap_access::ref(*this).get(L);
   luabind::detail::object_rep* pObjectRep =3D
luabind::detail::get_instance(L, -1);
   ::lua_pop(L, 1); //ref.get

   if (pObjectRep)
   {
     return
=E2=80=8B =E2=80=8B
std::string (pObjectRep->crep()->name());
   }
   else
   {
     throw std::runtime_error(
       "Internal Error: failed to resolve a Lua wrapper class name");
   }
 }

Hope this helps.

Best regards,
Eduard


On Wed, Aug 28, 2013 at 1:09 PM, Glen Fraser <[email protected]> wrote:
>
> Hello there, I'm using luabind and trying to get the Lua subclass name
from an instance of the base class in C++.
>
> 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=
=E2=80=A6
 Here is some example code that should illustrate the problem:
>
> class BaseClass {
> public:
>     virtual ~BaseClass() {}
>     std::string getClassName(lua_State* L);
>     // ... more stuff...
> };
>
> class BaseClass_wrapper : public BaseClass, public luabind::wrap_base {
>     // ... some stuff...
> };
>
> 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;
> }
>
>
> Thanks for any suggestions,
> Glen.
>
>
>
---------------------------------------------------------------------------=
---
> 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.cl=
ktrk
> _______________________________________________
> luabind-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>

--089e0141a79cb993c404e500d2ea
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><font face=3D"courier new, monospace">Hey,<br><br>you can<=
div class=3D"gmail_default" style=3D"display:inline">=E2=80=8B =E2=80=8B</d=
iv>use the &quot;private&quot; luabind::detail::object_rep* luabind::detail=
::get_instance(lua_State* L, int index) <div class=3D"gmail_default" style=
=3D"font-family:&#39;courier new&#39;,monospace;display:inline">

=E2=80=8Bhere =E2=80=8B</div>instead.<br>This is quite a hack, but we at fo=
und no other &quot;official&quot; way on doing this within a wrap_base:<br>=
<br><div class=3D"gmail_default" style=3D"font-family:&#39;courier new&#39;=
,monospace;display:inline">

=E2=80=8B=E2=80=8B</div>=C2=A0std::string<div class=3D"gmail_default" style=
=3D"font-family:&#39;courier new&#39;,monospace;display:inline">=E2=80=8B=
=E2=80=8B=C2=A0</div>BaseClass::getClassName(lua_State* L)<br>=C2=A0{<br>=
=C2=A0 =C2=A0luabind::detail::wrap_access::ref(*this).get(L);<br>

=C2=A0 =C2=A0luabind::detail::object_rep* pObjectRep =3D luabind::detail::g=
et_instance(L, -1);<br>=C2=A0 =C2=A0::lua_pop(L, 1); //ref.get<br><br>=C2=
=A0 =C2=A0if (pObjectRep)<br>=C2=A0 =C2=A0{<br>=C2=A0 =C2=A0 =C2=A0return<d=
iv class=3D"gmail_default" style=3D"display:inline">=E2=80=8B =E2=80=8B</di=
v>

std::string (pObjectRep-&gt;crep()-&gt;name());<br>=C2=A0 =C2=A0}<br>=C2=A0=
 =C2=A0else<br>=C2=A0 =C2=A0{<br>=C2=A0 =C2=A0 =C2=A0throw std::runtime_err=
or(<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0&quot;Internal Error: failed to resolve a=
 Lua wrapper class name&quot;);<br>=C2=A0 =C2=A0}<br>=C2=A0}<br><br>Hope th=
is helps.<br>

<br>Best regards,<br>Eduard<br><br></font><br>On Wed, Aug 28, 2013 at 1:09 =
PM, Glen Fraser &lt;<a href=3D"mailto:[email protected]">[email protected]=
m</a>&gt; wrote:<br>&gt;<br>&gt; Hello there, I&#39;m using luabind and try=
ing to get the Lua subclass name from an instance of the base class in C++.=
<br>

&gt;<br>&gt; I want to use get_class_info(), but it expects an &quot;argume=
nt&quot; rather than an &quot;object&quot;. =C2=A0Perhaps there is a better=
 way to do this, besides trying to figure out how to convert a luabind::obj=
ect into a luabind::adl::argument=E2=80=A6 =C2=A0Here is some example code =
that should illustrate the problem:<br>

&gt;<br>&gt; class BaseClass {<br>&gt; public:<br>&gt; =C2=A0 =C2=A0 virtua=
l ~BaseClass() {}<br>&gt; =C2=A0 =C2=A0 std::string getClassName(lua_State*=
 L);<br>&gt; =C2=A0 =C2=A0 // ... more stuff...<br>&gt; };<br>&gt;<br>&gt; =
class BaseClass_wrapper : public BaseClass, public luabind::wrap_base {<br>

&gt; =C2=A0 =C2=A0 // ... some stuff...<br>&gt; };<br>&gt;<br>&gt; std::str=
ing<br>&gt; BaseClass::getClassName(lua_State* L)<br>&gt; {<br>&gt; =C2=A0 =
=C2=A0 BaseClass_wrapper* p =3D dynamic_cast&lt;BaseClass_wrapper*&gt;(this=
);<br>&gt; =C2=A0 =C2=A0 std::string className(&quot;UNKNOWN&quot;);<br>

&gt; =C2=A0 =C2=A0 if (p !=3D 0) {<br>&gt; =C2=A0 =C2=A0 =C2=A0 =C2=A0 luab=
ind::object myObject(L, p);<br>&gt; =C2=A0 =C2=A0 =C2=A0 =C2=A0 luabind::cl=
ass_info info =3D luabind::get_class_info(myObject); // no good!<br>&gt; =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 // no viable conversion from &#39;luabind::obje=
ct&#39; to &#39;const luabind::adl::argument&#39;<br>

&gt; =C2=A0 =C2=A0 =C2=A0 =C2=A0 className =3D <a href=3D"http://info.name"=
>info.name</a>;<br>&gt; =C2=A0 =C2=A0 }<br>&gt; =C2=A0 =C2=A0 return classN=
ame;<br>&gt; }<br>&gt;<br>&gt;<br>&gt; Thanks for any suggestions,<br>&gt; =
Glen.<br>&gt;<br>&gt;<br>&gt; ---------------------------------------------=
---------------------------------<br>

&gt; Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!=
<br>&gt; Discover the easy way to master current and previous Microsoft tec=
hnologies<br>&gt; and advance your career. Get an incredible 1,500+ hours o=
f step-by-step<br>

&gt; tutorial videos with LearnDevNow. Subscribe today and save!<br>&gt; <a=
 href=3D"http://pubads.g.doubleclick.net/gampad/clk?id=3D58040911&amp;iu=3D=
/4140/ostg.clktrk">http://pubads.g.doubleclick.net/gampad/clk?id=3D58040911=
&amp;iu=3D/4140/ostg.clktrk</a><br>

&gt; _______________________________________________<br>&gt; luabind-user m=
ailing list<br>&gt; <a href=3D"mailto:[email protected]">l=
[email protected]</a><br>&gt; <a href=3D"https://lists.sour=
ceforge.net/lists/listinfo/luabind-user">https://lists.sourceforge.net/list=
s/listinfo/luabind-user</a><br>

&gt;<br></div>

--089e0141a79cb993c404e500d2ea--


--===============4570925712512607369==
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
--===============4570925712512607369==
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

--===============4570925712512607369==--