Luabind: derived classes!

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Luabind: derived classes!

golgoth

Hello again,

Intro: The way I ve implemented Lua in my project is considering that the API can be compiled with or witout Lua. To achieve this, I ve created a dummy Script class, so I don’t propagate lua in the entire project.

I did condensed my work a big deal, so here it is:

.cpp

 

Script *CreateScript(const Char *in_name, const Char *in_path)

{

        Script *l_script;

 

#if SUPPORT_LUA

        l_script = new LUA_Script();

#else

        l_script = new Script();

#endif

        return l_script;

}

 

class Object

{

        Object() {}

        void AddScript(Script *in_script);

        /// SNIP...

};

 

class Script: public Object

{

        /// SNIP...

};

 

class LUA_Script: public Script

{

        /// SNIP...

};

 

Luabind

 

Int Expose_Objects(lua_State* in_lua)

{

        module(in_lua)

        [

               def("CreateScript", &CreateScript)

               ,

               class_<Object>("Object")

               .def("AddScript", &Object::AddScript)

               ,

               class_<LUA_Script, Object>("Script")

               .def(constructor<>())

        ];

        return 0;

}

 

.lua

 

myScript = CreateScript("Test", "test.ai");

 

myObject = Object();

myObject:AddScript(myScript);

 



The bottleneck is right here:

if I do use new Script(); it will work but it won’t have any Lua functionalities.
when new LUA_Script(); is called I get:

No matching overload found, candidates:
void AddScript(Object&,Script*)

CreateScript creates a LUA_Script* and return a Script* but when passed in
myObject:AddScript(myScript); it is considered to be a LUA_Script wich it is indeed… but there is no C++ match.

In pure C++ this would be legal but Luabind doesn’t seem to like that at all, any elegant way to make this work?


------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Luabind: derived classes!

Tom McCubbin-2
            class_<LUA_Script, Object>("Script")

You tell luabind LUA_Script inherits from Object.  You need to register the Script class as deriving from Object, then register the LUA_Script as deriving from Script.  Luabind has no way to know a LUA_Script object in C++ inherits from Script, hence the inability to find a match...try something like:

        class_<Script,Object>("script"),
        class_<LUA_Script,Script>("Script")
        ...

That should fix you right up...hope that helped,

-tom

On Wed, 2009-02-25 at 18:01 -0500, neosettlers wrote:
        l_script = new LUA_Script();

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Luabind: derived classes!

hymedia

Hi tom thx for responding,

 

That’s what I thought too, but when I do:

 

myObject:AddScript(myScript); I get:

 

No matching overload found, candidates:

void AddScript(Object&,script*)

 

I m gessing that class_<Script,Object>("script") is passed instead and it is not recognize as being a class_<LUA_Script,Script>("Script").

 

It’s confusing…

 

From: Tom McCubbin [mailto:[hidden email]]
Sent: Wednesday, February 25, 2009 7:14 PM
To: [hidden email]
Subject: Re: [luabind] Luabind: derived classes!

 

            class_<LUA_Script, Object>("Script")

You tell luabind LUA_Script inherits from Object.  You need to register the Script class as deriving from Object, then register the LUA_Script as deriving from Script.  Luabind has no way to know a LUA_Script object in C++ inherits from Script, hence the inability to find a match...try something like:

        class_<Script,Object>("script"),
        class_<LUA_Script,Script>("Script")
        ...

That should fix you right up...hope that helped,

-tom

On Wed, 2009-02-25 at 18:01 -0500, neosettlers wrote:

        l_script = new LUA_Script();


------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Luabind: derived classes!

Daniel Wallin
In reply to this post by golgoth
neosettlers wrote:
> Hello again,
>
> Intro: The way I ve implemented Lua in my project is considering that
> the API can be compiled with or witout Lua. To achieve this, I ve
> created a dummy Script class, so I don’t propagate lua in the entire
> project.
[...]
> No matching overload found, candidates:
> void AddScript(Object&,Script*)
>
> CreateScript creates a LUA_Script* and return a Script* but when passed in
> myObject:AddScript(myScript); it is considered to be a LUA_Script wich
> it is indeed… but there is no C++ match.
>
> In pure C++ this would be legal but Luabind doesn’t seem to like that at
> all, any elegant way to make this work?

It should work, provided that you actually register your Script class.
Are you sure this is the actual code you are using? It would help a lot
if you can reduce this to a real failing test case, something I can
compile and run without making changes.

--
Daniel Wallin
BoostPro Computing
http://www.boostpro.com

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Luabind: derived classes!

Tom McCubbin-2
He had failed to register Script, and had LUA_Script registered as an Object...I think he has it fixed now...

On Thu, 2009-02-26 at 23:29 +0100, Daniel Wallin wrote:
neosettlers wrote:
> Hello again,
> 
> Intro: The way I ve implemented Lua in my project is considering that
> the API can be compiled with or witout Lua. To achieve this, I ve
> created a dummy Script class, so I don’t propagate lua in the entire
> project.
[...]
> No matching overload found, candidates:
> void AddScript(Object&,Script*)
> 
> CreateScript creates a LUA_Script* and return a Script* but when passed in
> myObject:AddScript(myScript); it is considered to be a LUA_Script wich
> it is indeed… but there is no C++ match.
> 
> In pure C++ this would be legal but Luabind doesn’t seem to like that at
> all, any elegant way to make this work?

It should work, provided that you actually register your Script class.
Are you sure this is the actual code you are using? It would help a lot
if you can reduce this to a real failing test case, something I can
compile and run without making changes.


------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Luabind: derived classes!

hymedia
In reply to this post by Daniel Wallin
Hello there, for some reason my posts seem to have been delayed so I had no
idea on the follow-up, the good news is, I found what was causing my
problem,

When creating a new LuaScript():

LuaScript::LuaScript(const Char *in_path): Script(in_path),
m_Lua(NULL)
{
        if ((m_Lua = lua_newthread(LUA_STATE)) != NULL)
        {
                lua_pushlightuserdata(LUA_STATE, m_Lua);
                lua_pushlightuserdata(LUA_STATE, this); <<<< wrong thing to
do!
                lua_settable(LUA_STATE, LUA_GLOBALSINDEX);
        }
}

And it works just fine now... except that another thing troubles me, I ll
make another post.




------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Luabind: C++ referenced variables;

hymedia
In reply to this post by Tom McCubbin-2

I might have overlooked this  but I can’t find anything replicating this within the examples so far,

 

I m not crazy about using the properties. I d like to keep a C++ structur as much as possible. So here is what I do:

 

.cpp

 

typedef unsigned int UInt;

 

class Test

{

      public:

 

            Test(): val(0) {}

            ~Test() {}

 

            UInt &Get() {return val;}

            void Set(UInt in_val) {val = in_val;}

 

      protected:

 

            UInt val;

};

 

Luabind

 

      module(in_lua)

      [

            class_<Test>("Test")

            .def(constructor<>())

            .def("Get", &Test::Get)

            .def("Set", &Test::Set)

      ];

 

.lua

 

myTest = Test();

myTest:Set(1);

myVal = myTest:Get();

 

Result:

 

myTest:Get(); assert: you are trying to use an unregistered type!

 

I know for sure the typedef is not an issue here, could have been a int, I have no doubts this have been addressed in the pass, but I could definitely use a hint.

 

Thx in advance.

 


------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Luabind: C++ referenced variables;

Daniel Wallin
hymedia wrote:

> I might have overlooked this  but I can’t find anything replicating this
> within the examples so far,
>
> I m not crazy about using the properties. I d like to keep a C++
> structur as much as possible. So here is what I do:
>
> .cpp
>
> typedef unsigned int UInt;
>
> class Test
> {
>       public:
>             Test(): val(0) {}
>             ~Test() {}
>
>             UInt &Get() {return val;}
>             void Set(UInt in_val) {val = in_val;}
>       protected:
>             UInt val;
> };
>
> Luabind
>
>       module(in_lua)
>       [
>             class_<Test>("Test")
>             .def(constructor<>())
>             .def("Get", &Test::Get)
>             .def("Set", &Test::Set)
>       ];
>  
> .lua
>
> myTest = Test();
> myTest:Set(1);
> myVal = myTest:Get();
>
> Result:
>
> myTest:Get(); assert: you are trying to use an unregistered type!
>
> I know for sure the typedef is not an issue here, could have been a int,
> I have no doubts this have been addressed in the pass, but I could
> definitely use a hint.

The problem is that you are returning a reference to a builtin type.
There's no reasonable way to represent this reference in Lua, so luabind
doesn't accept it. I thought using the copy-policy: "copy(result)" would
work here, but it turns out that only works on class types.

If you can't change your function signature, your best bet is using
"tag_function<>" to bypass luabind signature deduction. Something like:

  def("Get", tag_function<UInt(Test const&)>(&Test::get))

--
Daniel Wallin
BoostPro Computing
http://www.boostpro.com

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user