Re: Trivial pointers result in unregistered class
rod haxton <[email protected]> Tue, 18 Jun 2013 13:01:54 -0400
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <CALz8gR_aHju=52FR4XgvoNhnLhn3wi8rEEgzs=8szyPr8oTrZg@mail.gmail.com> |
--===============3760098140676501489==
Content-Type: multipart/alternative; boundary=e89a8f6435a29748fd04df70abb1
--e89a8f6435a29748fd04df70abb1
Content-Type: text/plain; charset=ISO-8859-1
Take a look at this. I do this and it works with a boost::ref on class, see
"FooManager".
I know...I know...I hate when folks give you a lot of code to look at for
an example. But, perhaps you can glean something from looking at the
"FooManager" class, and see that I assign it as a global.
//The registrate function takes a Class
// name and lua_State, then calls its registrate method
template <typename T>
void registrate(lua_State *pLuaState) {
T::registrate(pLuaState);
}
class FooManager
{
public:
FooManager() {};
~FooManager() {};
void addComponent(IFoo* pFoo) { m_iFooVector.push_back(pFoo); }
IFoo* getComponent(int nIndex) { return m_iFooVector[nIndex]; }
static void registrate(lua_State *pLuaState)
{
luabind::module(pLuaState) [
luabind::class_<FooManager>("FooManager")
.def(luabind::constructor<>())
.def("getComponent", (IFoo*(FooManager::*) (int)) &FooManager::getComponent)
];
}
void doAwake(void)
{
for(std::vector<IFoo*>::iterator it = m_iFooVector.begin(); it !=
m_iFooVector.end(); it++)
{
IFoo *pIFoo = *it;
pIFoo->Awake();
}
}
void doUpdate(void)
{
for(std::vector<IFoo*>::iterator it = m_iFooVector.begin(); it !=
m_iFooVector.end(); it++)
{
IFoo *pIFoo = *it;
pIFoo->update();
}
}
private:
std::vector<IFoo*> m_iFooVector;
};
int main()
{
lua_State *myLuaState = luaL_newstate();
// Connect LuaBind to this lua state
luabind::open(myLuaState);
// open the lua libraries
luaL_openlibs(myLuaState);
// register the luaBind classes
registrate<IFoo>(myLuaState);
registrate<Foo>(myLuaState);
registrate<Foo2>(myLuaState);
registrate<FooManager>(myLuaState);
// create all the objects
Foo *pFoo = new Foo("Rod's Test: 1-2-3!", myLuaState);
Foo2 *pFoo2 = new Foo2("Zimba", myLuaState);
FooManager* pfooManager = new FooManager();
pfooManager->addComponent(pFoo);
pfooManager->addComponent(pFoo2);
// set object manager global
luabind::globals(myLuaState)["fooManager"] = boost::ref(pfooManager);
//reh 16jun13 the Game Manager will also pass the Instigator to the call,
i.e., doAwake(theInstigator). This way the object has the object
interacting with it.
pfooManager->doAwake();
pfooManager->doUpdate();
lua_close(myLuaState);
// clean up
delete pFoo;
delete pFoo2;
return 0;
}
On Tue, Jun 18, 2013 at 12:42 PM, Daniel Walter <
[email protected]> wrote:
> hi rod,
> thank you for your answer.
> what about the struct "A" in my example? In my project I can not change
> struct "A" to use boost::ref or any getters. Of course, I could write a
> wrapper class, but this hurts.
>
> Is there any other way?
>
>
> On 18.06.2013 18:17, rod haxton wrote:
> > give boost:ref a try...
> >
> > luabind::globals(L)["x"] = boost::ref(x);
> >
> >
> > On Tue, Jun 18, 2013 at 10:41 AM, Daniel Walter
> > <[email protected] <mailto:[email protected]>>
> > wrote:
> >
> > Hi folks,
> >
> > I have a struct with some pointers to trivial datatypes like this:
> >
> > struct A {
> > int* x;
> > int* y;
> > };
> >
> > I bound it to lua like this:
> >
> > luabind::module(L)
> > [
> > luabind::class_<A>("A")
> > .def_readonly("x", &A::x)
> > .def_readonly("y", &A::y)
> > ];
> >
> > If I am using this class in my lua script the runtime throws this
> > exception: "Trying to use unregistered class"
> > I managed this using a getter function returning the dereferenced
> > pointer, but there must be a better way.
> >
> > The same exception happens when i am using such an trivial pointer
> > directly:
> >
> > int* x = new int(42);
> > luabind::globals(L)["x"] = x;
> >
> > How to handle this? What i am missing?
> >
> > Thanks in advance,
> > Daniel
> >
> >
> ------------------------------------------------------------------------------
> > This SF.net email is sponsored by Windows:
> >
> > Build for Windows Store.
> >
> > http://p.sf.net/sfu/windows-dev2dev
> > _______________________________________________
> > luabind-user mailing list
> > [email protected]
> > <mailto:[email protected]>
> > https://lists.sourceforge.net/lists/listinfo/luabind-user
> >
> >
> >
> >
> >
> ------------------------------------------------------------------------------
> > This SF.net email is sponsored by Windows:
> >
> > Build for Windows Store.
> >
> > http://p.sf.net/sfu/windows-dev2dev
> >
> >
> >
> > _______________________________________________
> > luabind-user mailing list
> > [email protected]
> > https://lists.sourceforge.net/lists/listinfo/luabind-user
> >
>
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by Windows:
>
> Build for Windows Store.
>
> http://p.sf.net/sfu/windows-dev2dev
> _______________________________________________
> luabind-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>
--e89a8f6435a29748fd04df70abb1
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Take a look at this. I do this and it works with a boost::=
ref on class, see "FooManager".=A0<div><br></div><div>I know...I =
know...I hate when folks give you a lot of code to look at for an example. =
But, perhaps you can glean something from looking at the "FooManager&q=
uot; class, and see that I assign it as a global.<br>
<div><br></div><div><div>//The registrate function takes a Class=A0</div><d=
iv>// name and lua_State, then calls its registrate method=A0</div><div>tem=
plate <typename T>=A0</div><div>void registrate(lua_State *pLuaState)=
{=A0</div>
<div><span class=3D"" style=3D"white-space:pre"> </span>T::registrate(pLuaS=
tate);=A0</div><div>}</div></div><div><br></div><div><br></div><div><div>cl=
ass FooManager</div><div>{</div><div>public:</div><div><span class=3D"" sty=
le=3D"white-space:pre"> </span>FooManager() {};</div>
<div><span class=3D"" style=3D"white-space:pre"> </span>~FooManager() {};</=
div><div><br></div><div><span class=3D"" style=3D"white-space:pre"> </span>=
void<span class=3D"" style=3D"white-space:pre"> </span>addComponent(IFoo* p=
Foo) { m_iFooVector.push_back(pFoo); }</div>
<div><span class=3D"" style=3D"white-space:pre"> </span>IFoo*<span class=3D=
"" style=3D"white-space:pre"> </span>getComponent(int nIndex) { return m_iF=
ooVector[nIndex]; }</div><div><br></div><div><span class=3D"" style=3D"whit=
e-space:pre"> </span>static void registrate(lua_State *pLuaState)</div>
<div><span class=3D"" style=3D"white-space:pre"> </span>{</div><div><span c=
lass=3D"" style=3D"white-space:pre"> </span>luabind::module(pLuaState) [</=
div><div><span class=3D"" style=3D"white-space:pre"> </span>luabind::clas=
s_<FooManager>("FooManager")</div>
<div><span class=3D"" style=3D"white-space:pre"> </span>.def(luabind::co=
nstructor<>())</div><div><span class=3D"" style=3D"white-space:pre"> =
</span>.def("getComponent", (IFoo*(FooManager::*) (int)) &=
FooManager::getComponent)</div>
<div><span class=3D"" style=3D"white-space:pre"> </span>];</div><div><span=
class=3D"" style=3D"white-space:pre"> </span>}</div><div><br></div><div><s=
pan class=3D"" style=3D"white-space:pre"> </span>void doAwake(void)=A0</div=
><div><span class=3D"" style=3D"white-space:pre"> </span>{</div>
<div><span class=3D"" style=3D"white-space:pre"> </span>for(std::vector<=
;IFoo*>::iterator it =3D m_iFooVector.begin(); it !=3D m_iFooVector.end(=
); it++)</div><div><span class=3D"" style=3D"white-space:pre"> </span>{</d=
iv><div>
<span class=3D"" style=3D"white-space:pre"> </span>IFoo *pIFoo =3D *it;</=
div><div><span class=3D"" style=3D"white-space:pre"> </span>pIFoo->Awa=
ke();</div><div><span class=3D"" style=3D"white-space:pre"> </span>}</div>=
<div><span class=3D"" style=3D"white-space:pre"> </span>}</div>
<div><br></div><div><span class=3D"" style=3D"white-space:pre"> </span>void=
doUpdate(void)=A0</div><div><span class=3D"" style=3D"white-space:pre"> </=
span>{</div><div><span class=3D"" style=3D"white-space:pre"> </span>for(st=
d::vector<IFoo*>::iterator it =3D m_iFooVector.begin(); it !=3D m_iFo=
oVector.end(); it++)</div>
<div><span class=3D"" style=3D"white-space:pre"> </span>{</div><div><span =
class=3D"" style=3D"white-space:pre"> </span>IFoo *pIFoo =3D *it;</div><d=
iv><span class=3D"" style=3D"white-space:pre"> </span>pIFoo->update();=
</div><div>
<span class=3D"" style=3D"white-space:pre"> </span>}</div><div><span class=
=3D"" style=3D"white-space:pre"> </span>}</div><div><br></div><div>private:=
</div><div><span class=3D"" style=3D"white-space:pre"> </span>std::vector&l=
t;IFoo*> m_iFooVector;</div>
<div>};</div><div><br></div><div>int main()=A0</div><div>{</div><div><span =
class=3D"" style=3D"white-space:pre"> </span>lua_State *myLuaState =3D luaL=
_newstate();</div><div><br></div><div><span class=3D"" style=3D"white-space=
:pre"> </span>// Connect LuaBind to this lua state</div>
<div><span class=3D"" style=3D"white-space:pre"> </span>luabind::open(myLua=
State);</div><div><br></div><div><span class=3D"" style=3D"white-space:pre"=
> </span>// open the lua libraries</div><div><span class=3D"" style=3D"whit=
e-space:pre"> </span>luaL_openlibs(myLuaState);</div>
<div><br></div><div><span class=3D"" style=3D"white-space:pre"> </span>// r=
egister the luaBind classes</div><div><span class=3D"" style=3D"white-space=
:pre"> </span>registrate<IFoo>(myLuaState);</div><div><span class=3D"=
" style=3D"white-space:pre"> </span>registrate<Foo>(myLuaState);</div=
>
<div><span class=3D"" style=3D"white-space:pre"> </span>registrate<Foo2&=
gt;(myLuaState);</div><div><span class=3D"" style=3D"white-space:pre"> </sp=
an>registrate<FooManager>(myLuaState);</div><div><br></div><div><span=
class=3D"" style=3D"white-space:pre"> </span>// create all the objects</di=
v>
<div><span class=3D"" style=3D"white-space:pre"> </span>Foo *pFoo =3D new F=
oo("Rod's Test: 1-2-3!", myLuaState);</div><div><span class=
=3D"" style=3D"white-space:pre"> </span>Foo2 *pFoo2 =3D new Foo2("Zimb=
a", myLuaState);</div>
<div><span class=3D"" style=3D"white-space:pre"> </span>FooManager* pfooMan=
ager =3D new FooManager();</div><div><br></div><div><span class=3D"" style=
=3D"white-space:pre"> </span>pfooManager->addComponent(pFoo);</div><div>=
<span class=3D"" style=3D"white-space:pre"> </span>pfooManager->addCompo=
nent(pFoo2);</div>
<div><span class=3D"" style=3D"white-space:pre"> </span></div><div><span cl=
ass=3D"" style=3D"white-space:pre"> </span>// set object manager global</di=
v><div><span class=3D"" style=3D"white-space:pre"> </span>luabind::globals(=
myLuaState)["fooManager"] =3D boost::ref(pfooManager);</div>
<div><br></div><div>//reh 16jun13 the Game Manager will also pass the Insti=
gator to the call, i.e., doAwake(theInstigator). This way the object has th=
e object interacting with it.</div><div><span class=3D"" style=3D"white-spa=
ce:pre"> </span>pfooManager->doAwake();</div>
<div><span class=3D"" style=3D"white-space:pre"> </span>pfooManager->doU=
pdate();</div><div><br></div><div><span class=3D"" style=3D"white-space:pre=
"> </span>lua_close(myLuaState);</div><div>=A0</div><div><span class=3D"" s=
tyle=3D"white-space:pre"> </span>// clean up</div>
<div><span class=3D"" style=3D"white-space:pre"> </span>delete pFoo;</div><=
div><span class=3D"" style=3D"white-space:pre"> </span>delete pFoo2;</div><=
div><br></div><div><span class=3D"" style=3D"white-space:pre"> </span>retur=
n 0;</div>
<div>}</div></div></div></div><div class=3D"gmail_extra"><br><br><div class=
=3D"gmail_quote">On Tue, Jun 18, 2013 at 12:42 PM, Daniel Walter <span dir=
=3D"ltr"><<a href=3D"mailto:[email protected]" target=3D"_b=
lank">[email protected]</a>></span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">hi rod,<br>
thank you for your answer.<br>
what about the struct "A" in my example? In my project I can not =
change<br>
struct "A" to use boost::ref or any getters. Of course, I could w=
rite a<br>
wrapper class, but this hurts.<br>
<br>
Is there any other way?<br>
<div class=3D"im"><br>
<br>
On 18.06.2013 18:17, rod haxton wrote:<br>
> give boost:ref a try...<br>
><br>
> luabind::globals(L)["x"] =3D boost::ref(x);<br>
><br>
><br>
> On Tue, Jun 18, 2013 at 10:41 AM, Daniel Walter<br>
</div>> <<a href=3D"mailto:[email protected]">daniel.wal=
[email protected]</a> <mailto:<a href=3D"mailto:daniel.walter@helmund=
walter.de">[email protected]</a>>><br>
<div><div class=3D"h5">> wrote:<br>
><br>
> =A0 =A0 Hi folks,<br>
><br>
> =A0 =A0 I have a struct with some pointers to trivial datatypes like t=
his:<br>
><br>
> =A0 =A0 struct A {<br>
> =A0 =A0 =A0 =A0 =A0 int* x;<br>
> =A0 =A0 =A0 =A0 =A0 int* y;<br>
> =A0 =A0 };<br>
><br>
> =A0 =A0 I bound it to lua like this:<br>
><br>
> =A0 =A0 luabind::module(L)<br>
> =A0 =A0 [<br>
> =A0 =A0 =A0 =A0 =A0 =A0luabind::class_<A>("A")<br>
> =A0 =A0 =A0 =A0 =A0 .def_readonly("x", &A::x)<br>
> =A0 =A0 =A0 =A0 =A0 .def_readonly("y", &A::y)<br>
> =A0 =A0 ];<br>
><br>
> =A0 =A0 If I am using this class in my lua script the runtime throws t=
his<br>
> =A0 =A0 exception: "Trying to use unregistered class"<br>
> =A0 =A0 I managed this using a getter function returning the dereferen=
ced<br>
> =A0 =A0 pointer, but there must be a better way.<br>
><br>
> =A0 =A0 The same exception happens when i am using such an trivial poi=
nter<br>
> =A0 =A0 directly:<br>
><br>
> =A0 =A0 int* x =3D new int(42);<br>
> =A0 =A0 luabind::globals(L)["x"] =3D x;<br>
><br>
> =A0 =A0 How to handle this? What i am missing?<br>
><br>
> =A0 =A0 Thanks in advance,<br>
> =A0 =A0 Daniel<br>
><br>
> =A0 =A0 --------------------------------------------------------------=
----------------<br>
> =A0 =A0 This SF.net email is sponsored by Windows:<br>
><br>
> =A0 =A0 Build for Windows Store.<br>
><br>
> =A0 =A0 <a href=3D"http://p.sf.net/sfu/windows-dev2dev" target=3D"_bla=
nk">http://p.sf.net/sfu/windows-dev2dev</a><br>
> =A0 =A0 _______________________________________________<br>
> =A0 =A0 luabind-user mailing list<br>
> =A0 =A0 <a href=3D"mailto:[email protected]">luabind-=
[email protected]</a><br>
</div></div>> =A0 =A0 <mailto:<a href=3D"mailto:[email protected]=
urceforge.net">[email protected]</a>><br>
> =A0 =A0 <a href=3D"https://lists.sourceforge.net/lists/listinfo/luabin=
d-user" target=3D"_blank">https://lists.sourceforge.net/lists/listinfo/luab=
ind-user</a><br>
<div class=3D"HOEnZb"><div class=3D"h5">><br>
><br>
><br>
><br>
> ----------------------------------------------------------------------=
--------<br>
> This SF.net email is sponsored by Windows:<br>
><br>
> Build for Windows Store.<br>
><br>
> <a href=3D"http://p.sf.net/sfu/windows-dev2dev" target=3D"_blank">http=
://p.sf.net/sfu/windows-dev2dev</a><br>
><br>
><br>
><br>
> _______________________________________________<br>
> luabind-user mailing list<br>
> <a href=3D"mailto:[email protected]">luabind-user@lis=
ts.sourceforge.net</a><br>
> <a href=3D"https://lists.sourceforge.net/lists/listinfo/luabind-user" =
target=3D"_blank">https://lists.sourceforge.net/lists/listinfo/luabind-user=
</a><br>
><br>
<br>
---------------------------------------------------------------------------=
---<br>
This SF.net email is sponsored by Windows:<br>
<br>
Build for Windows Store.<br>
<br>
<a href=3D"http://p.sf.net/sfu/windows-dev2dev" target=3D"_blank">http://p.=
sf.net/sfu/windows-dev2dev</a><br>
_______________________________________________<br>
luabind-user mailing list<br>
<a href=3D"mailto:[email protected]">[email protected]=
urceforge.net</a><br>
<a href=3D"https://lists.sourceforge.net/lists/listinfo/luabind-user" targe=
t=3D"_blank">https://lists.sourceforge.net/lists/listinfo/luabind-user</a><=
br>
</div></div></blockquote></div><br></div>
--e89a8f6435a29748fd04df70abb1--
--===============3760098140676501489==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:
Build for Windows Store.
http://p.sf.net/sfu/windows-dev2dev
--===============3760098140676501489==
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
--===============3760098140676501489==--