[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua - C++ Bindings
- From: "Ariel Manzur" <puntob@...>
- Date: Tue, 20 Jan 2009 17:46:11 -0200
On Tue, Jan 20, 2009 at 4:32 PM, Anders Backman <andersb@cs.umu.se> wrote:
>
>
> On Tue, Jan 20, 2009 at 7:07 PM, Ariel Manzur <puntob@gmail.com> wrote:
>>
>> Hi.
>>
>> On Tue, Jan 20, 2009 at 11:13 AM, Anders Backman <andersb@cs.umu.se>
>> wrote:
>> > - LuaBind
>> [...]
>> > * Full support of virtual methods
>>
>> do you know if this is documented? I didn't see anything on their
>> manual, but I'd love to see how this kind of thing is implemented.
>
> Hm, I guess it was only one way.
> http://www.rasterbar.com/products/luabind/docs.html
right, this is what I saw, which explains how to do it manually..
>
> I implemented this together with tolua a while back though. For me it was
> important that I could create a class in lua, implement the virtual method,
> register the class to a listener, and then this (virtual) method was
> executed from C++.
> To do this, I had to implement a class:
> namespace {
> LuaClass: public Class {
> bool virtual keyboard( int key, int modkey, float x, float y, bool keyDown
> )
> {
> vrutils::LuaCall lc(m_lua, "namespace::LuaClass", this, "lua_keyboard",
> false);
> if (!lc.isValid())
> return false;
>
> // Rest is arguments
> lc.pushNumber(key);
> lc.pushNumber(modKey);
> lc.pushNumber(x);
> lc.pushNumber(y);
> lc.pushBoolean(keydown);
> if (lc.call(1)) // Make the call return 1 value
> error("keyboard", "Error during execution of luafunction lua_keyboard");
> // Is there a value on the stack?
> if (!lua_gettop (m_lua))
> return false; //error("keyboard", "Lua function call does not return a
> value, expecting bool");
> // Is there a value on the stack?
> if (!lua_isboolean (m_lua,-1))
> return false; //error("keyboard", "Lua function call does not return a
> value, expecting bool");
>
> // Get it
> bool f = tolua_toboolean (m_lua, -1, 0) ? true : false;
> lua_pop(m_lua, 1);
> return f;
> }
> };
> }
> This could of course be automated through xml2cpp and tolua...
> SO whenever tolua encounters a virtual method, it creates the above code
> automatically and implements a class which can be used from lua:
This is exactly what the code on the wiki page does :) It also tries
to handle some cases you'll find when parsing "real life" c++ objects,
like protected virtual methods, classes with pure virtual methods and
constructors, etc. I think lua_qt also had a 'class.lua' module that
provides objects with inheritance that uses this (to avoid all the
metatable manipulation on the wiki example).
I see using something like xml2cpp as an alternative to expose a big
api with minimal effort, but the tolua support is already there.
>
> test.lua:
> listener = namespace.LuaClass:new();
> function listener:lua_keyboard(key, modKey, x,y,down)
> print("key is: "..key)
> end
> listenerManager:add( listener )
>
>
> Now the listenerManager can trigger the listener, and it doesnt matter
> whether the virtual method (keyboard/lua_keyboard) is implemented in c++ or
> Lua, it will be executed no matter what.
> The drawback is that I dont think it will work to have the same name (or
> perhaps it will?) of the method keyboard in lua...I dont recall why I had to
> add the lua_ prefix.
You probably had infinite recursion when the lua method didn't exist,
and you'd call the tolua generated wrapper function instead.
>>
>> And it would solve the main problem with the solution to implement
>> virtual methods on lua, which is acquiring all the methods at all
>> levels of inheritance. This would probably make the qt bindings _huge_
>> tho.
>
> You mean each and every virtual method in QT would be bound to this code...
> But isnt that the problem already in lqt?
And also every class (that might not have any virtual methods) that
derives from a class with virtual methods. I wouldn't call this a
problem really for desktop applications.. we have enough memory to
load big shared objects.
>
>>
>> >
>> > Looking for quite some feedback because I know this is a hot issue!
>> >
>> > Cheers, Anders
>> >
>> >
>>
>> Ariel.
>
>
>
>
>