How to create a Lua-defined subclass from C++
Glen Fraser <[email protected]> Mon, 22 Jul 2013 17:43:13 +0200
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <[email protected]> |
--===============5290918966389231282==
Content-Type: multipart/alternative;
boundary="Apple-Mail=_0C99ADA1-A105-4C0F-ABA9-88BF70733BA2"
--Apple-Mail=_0C99ADA1-A105-4C0F-ABA9-88BF70733BA2
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
charset=us-ascii
Hi there, I've got subclassing working pretty well between Lua and C++. =
So I have a C++ base class, and can create subclasses in Lua and =
instantiate them there, add them to my "world", have them traversed and =
their methods called from C++, etc.
What I don't understand how to do is how to create instances of Lua =
classes from C++. Is this possible? I have the name of the classes, =
because I call my own "register" method from Lua, so my app knows the =
names of available subclasses. But how can I create a new instance of =
one of them from C++?
Lua:
class 'Derived' (app.Base)
function Derived:__init(str)
app.Base.__init(self, str .. " (Derived)")
end
app.registerSubClass('Derived')
Later on, in C++, I'd like to do the Lua equivalent of:
x =3D Derived("hello")
Then I can do:
world =3D app.getWorld()
world:addObject(x)
All I can think of is something like:
shared_ptr<Base> x =3D luabind::call_function<shared_ptr<Base> =
>(luaState, "createObject", "Derived", "hello");
world->addObject(x); // and so on...
Any ideas? I can't just call new on my luabind::wrap_base - derived =
object, then register that with Lua/luabind somehow (and tell it what =
Lua subclass it should be using)? (that would be nice ;-)
Thanks,
Glen.
--Apple-Mail=_0C99ADA1-A105-4C0F-ABA9-88BF70733BA2
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
charset=us-ascii
<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html =
charset=3Dus-ascii"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi =
there, I've got subclassing working pretty well between Lua and C++. =
So I have a C++ base class, and can create subclasses in Lua and =
instantiate them there, add them to my "world", have them traversed and =
their methods called from C++, etc.<div><br></div><div>What I don't =
understand how to do is how to create instances of Lua classes from C++. =
Is this possible? I have the name of the classes, because I =
call my own "register" method from Lua, so my app knows the names of =
available subclasses. But how can I create a new instance of one =
of them from C++?</div><div><br></div><div>Lua:</div><div><div =
style=3D"margin: 0px; font-size: 14px; font-family: Menlo; "><span =
class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>class =
'Derived' (app.Base)</div><div style=3D"margin: 0px; font-size: 14px; =
font-family: Menlo; min-height: 16px; "><br></div><div style=3D"margin: =
0px; font-size: 14px; font-family: Menlo; "><span class=3D"Apple-tab-span"=
style=3D"white-space:pre"> </span>function =
Derived:__init(str)</div><div style=3D"margin: 0px; font-size: 14px; =
font-family: Menlo; "><span class=3D"Apple-tab-span" =
style=3D"white-space:pre"> </span> =
app.Base.__init(self, str .. " (Derived)")</div><div style=3D"margin: =
0px; font-size: 14px; font-family: Menlo; "><span class=3D"Apple-tab-span"=
style=3D"white-space:pre"> =
</span>end</div></div><div><br></div><div><div style=3D"margin: 0px; =
font-size: 14px; font-family: Menlo; "><span class=3D"Apple-tab-span" =
style=3D"white-space:pre"> =
</span>app.registerSubClass('Derived')</div></div><div><br></div><div>Late=
r on, in C++, I'd like to do the Lua equivalent of:</div><div><span =
class=3D"Apple-tab-span" style=3D"font-family: Menlo; font-size: 14px; =
white-space: pre; "> </span><span style=3D"font-family: Menlo; =
font-size: 14px; ">x =3D =
Derived("hello")</span></div><div><br></div><div>Then I can =
do:</div><div><span style=3D"font-family: Menlo; font-size: 14px; =
"><span class=3D"Apple-tab-span" style=3D"white-space:pre"> =
</span>world =3D app.getWorld()</span></div><div style=3D"margin: 0px; =
font-size: 14px; font-family: Menlo; "><span class=3D"Apple-tab-span" =
style=3D"white-space:pre"> =
</span>world:addObject(x)</div><div><br></div><div>All I can think of is =
something like:</div><div><span class=3D"Apple-tab-span" =
style=3D"font-family: Verdana, Geneva, Helvetica, Arial, sans-serif; =
font-size: 13px; white-space: pre; "> </span><span =
style=3D"background-color: rgb(255, 255, 255); font-family: Verdana, =
Geneva, Helvetica, Arial, sans-serif; font-size: 13px; =
">shared_ptr<Base> x =3D </span><span =
style=3D"background-color: rgb(255, 255, 255); font-family: Verdana, =
Geneva, Helvetica, Arial, sans-serif; font-size: 13px; =
">luabind::call_function<shared_ptr<Base> >(luaState, =
"createObject", "Derived", "hello");</span></div><div><span =
style=3D"background-color: rgb(255, 255, 255); font-family: Verdana, =
Geneva, Helvetica, Arial, sans-serif; font-size: 13px; "><span =
class=3D"Apple-tab-span" style=3D"white-space:pre"> =
</span>world->addObject(x); // and so =
on...</span></div><div><br></div><div>Any ideas? I can't just call =
new on my luabind::wrap_base - derived object, then register that with =
Lua/luabind somehow (and tell it what Lua subclass it should be using)? =
(that would be nice =
;-)</div><div><br></div><div>Thanks,</div><div>Glen.</div><div><br></div><=
/body></html>=
--Apple-Mail=_0C99ADA1-A105-4C0F-ABA9-88BF70733BA2--
--===============5290918966389231282==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
------------------------------------------------------------------------------
See everything from the browser to the database with AppDynamics
Get end-to-end visibility with application monitoring from AppDynamics
Isolate bottlenecks and diagnose root cause in seconds.
Start your free trial of AppDynamics Pro today!
http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk
--===============5290918966389231282==
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
--===============5290918966389231282==--