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 "private" luabind::detail::object_rep* luabind::detail=
::get_instance(lua_State* L, int index) <div class=3D"gmail_default" style=
=3D"font-family:'courier new',monospace;display:inline">
=E2=80=8Bhere =E2=80=8B</div>instead.<br>This is quite a hack, but we at fo=
und no other "official" way on doing this within a wrap_base:<br>=
<br><div class=3D"gmail_default" style=3D"font-family:'courier new'=
,monospace;display:inline">
=E2=80=8B=E2=80=8B</div>=C2=A0std::string<div class=3D"gmail_default" style=
=3D"font-family:'courier new',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->crep()->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"Internal Error: failed to resolve a=
Lua wrapper class name");<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 <<a href=3D"mailto:[email protected]">[email protected]=
m</a>> wrote:<br>><br>> Hello there, I'm using luabind and try=
ing to get the Lua subclass name from an instance of the base class in C++.=
<br>
><br>> I want to use get_class_info(), but it expects an "argume=
nt" rather than an "object". =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>
><br>> class BaseClass {<br>> public:<br>> =C2=A0 =C2=A0 virtua=
l ~BaseClass() {}<br>> =C2=A0 =C2=A0 std::string getClassName(lua_State*=
L);<br>> =C2=A0 =C2=A0 // ... more stuff...<br>> };<br>><br>> =
class BaseClass_wrapper : public BaseClass, public luabind::wrap_base {<br>
> =C2=A0 =C2=A0 // ... some stuff...<br>> };<br>><br>> std::str=
ing<br>> BaseClass::getClassName(lua_State* L)<br>> {<br>> =C2=A0 =
=C2=A0 BaseClass_wrapper* p =3D dynamic_cast<BaseClass_wrapper*>(this=
);<br>> =C2=A0 =C2=A0 std::string className("UNKNOWN");<br>
> =C2=A0 =C2=A0 if (p !=3D 0) {<br>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 luab=
ind::object myObject(L, p);<br>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 luabind::cl=
ass_info info =3D luabind::get_class_info(myObject); // no good!<br>> =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 // no viable conversion from 'luabind::obje=
ct' to 'const luabind::adl::argument'<br>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 className =3D <a href=3D"http://info.name"=
>info.name</a>;<br>> =C2=A0 =C2=A0 }<br>> =C2=A0 =C2=A0 return classN=
ame;<br>> }<br>><br>><br>> Thanks for any suggestions,<br>> =
Glen.<br>><br>><br>> ---------------------------------------------=
---------------------------------<br>
> Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!=
<br>> Discover the easy way to master current and previous Microsoft tec=
hnologies<br>> and advance your career. Get an incredible 1,500+ hours o=
f 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">http://pubads.g.doubleclick.net/gampad/clk?id=3D58040911=
&iu=3D/4140/ostg.clktrk</a><br>
> _______________________________________________<br>> luabind-user m=
ailing list<br>> <a href=3D"mailto:[email protected]">l=
[email protected]</a><br>> <a href=3D"https://lists.sour=
ceforge.net/lists/listinfo/luabind-user">https://lists.sourceforge.net/list=
s/listinfo/luabind-user</a><br>
><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==--