Luna, a small generic C++ template class

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

Luna, a small generic C++ template class

Lenny Palozzi-4
Luna is a small generic C++ template class that binds your C++ 
classes to Lua. Allowing you to write in Lua constructs like

  a = Account{100}
  a:withdraw(50)
  b = v:balance()

where Account is a C++ class registered as 'Account', withdraw and
balance are member functions of that class registered as such.

* Just as C functions registered in Lua must have a signature of 
"int (*)(lua_State*)", member functions of Account registered in 
Lua must have a signature of "int(Account::*)(lua_State*)"

* Currently, only binding of member functions is supported. Access 
to public primitive or user-defined types are not.

* The code is typesafe, errors are caught at compile time.

* Objects created are gc'd by Lua.

* The code consists of a small header file(3.6K) just over 100 lines.

* Very easy to use. Simply create an array of member functions to 
bind and create an instance of the template class.

http://sophia.eyep.net/code/luna.tar.gz

 or

http://sophia.eyep.net/code/luna.h
http://sophia.eyep.net/code/lunatest.cc
http://sophia.eyep.net/code/lunatest.lua

Any feedback is appreciated.

Thanks,
-Lenny

Reply | Threaded
Open this post in threaded view
|

RE: Luna, a small generic C++ template class

Pete Gardner
In what ways is this better/different than tolua?

Thanks,

Pete Gardner
President, CEO
Helikon Technologies Inc.
http://www.helikon.com




> -----Original Message-----
> From: Lenny Palozzi [[hidden email]]
> Sent: Monday, February 12, 2001 10:19 AM
> To: Multiple recipients of list
> Subject: Luna, a small generic C++ template class
> 
> 
> Luna is a small generic C++ template class that binds your C++ 
> classes to Lua. Allowing you to write in Lua constructs like
> 
>   a = Account{100}
>   a:withdraw(50)
>   b = v:balance()
> 
> where Account is a C++ class registered as 'Account', withdraw and
> balance are member functions of that class registered as such.
> 
> * Just as C functions registered in Lua must have a signature of 
> "int (*)(lua_State*)", member functions of Account registered in 
> Lua must have a signature of "int(Account::*)(lua_State*)"
> 
> * Currently, only binding of member functions is supported. Access 
> to public primitive or user-defined types are not.
> 
> * The code is typesafe, errors are caught at compile time.
> 
> * Objects created are gc'd by Lua.
> 
> * The code consists of a small header file(3.6K) just over 100 lines.
> 
> * Very easy to use. Simply create an array of member functions to 
> bind and create an instance of the template class.
> 
> http://sophia.eyep.net/code/luna.tar.gz
> 
>  or
> 
> http://sophia.eyep.net/code/luna.h
> http://sophia.eyep.net/code/lunatest.cc
> http://sophia.eyep.net/code/lunatest.lua
> 
> Any feedback is appreciated.
> 
> Thanks,
> -Lenny
> 

Reply | Threaded
Open this post in threaded view
|

Re: Luna, a small generic C++ template class

Lenny Palozzi-4
On Mon, Feb 12, 2001 at 02:24:20PM -0800, Pete Gardner wrote:
> 
> In what ways is this better/different than tolua?
> 

I'll try and answer how it's different...

- tolua will bind _everything_ in a package file to Lua, my template class 
doesn't, so its less feature rich than tolua. It only binds those
member functions that you specify, tolua can bind any primitive or
user-defined types.

- my template class is probably easier to use than tolua, but that's only a
result that it's simple. It's also alot smaller than tolua and doesn't
require you to run an external build tool(tolua) to build the binding code.

My intentions were to build a system that would allow you to register class 
member functions as easily as possible to Lua. Member functions that return
an int and take a lua_State*, similar to the rules laid out for C functions
registered to Lua. Thus, it turned out that it came to be similar to the
functionality that tolua provides.

If you have existing classes that you would like to enable in Lua, then tolua 
is the right tool. If you're just starting to code your class and would also
like to enable it in Lua, then this template class may be of help.

Thanks,
-Lenny.

Reply | Threaded
Open this post in threaded view
|

Re: Luna, a small generic C++ template class

Lenny Palozzi-4
In reply to this post by Lenny Palozzi-4
On Mon, Feb 12, 2001 at 01:18:33PM -0500, Lenny Palozzi wrote:
> 
> * The code consists of a small header file(3.6K) just over 100 lines.

I had a clever thought and now its only ~70 lines of code and 1.9K. Get it
here.

> http://sophia.eyep.net/code/luna.tar.gz

-Lenny.

Reply | Threaded
Open this post in threaded view
|

Re: Luna, a small generic C++ template class

Kelvin McDowell
Just curious, was this at all inspired by the boost python library?  I ask
because after seeing it I had been thinking of doing something like this
myself.
http://www.boost.org/libs/python/doc/index.html

Kelvin

----- Original Message -----
From: "Lenny Palozzi" <[hidden email]>
To: "Multiple recipients of list" <[hidden email]>
Sent: Monday, February 12, 2001 18:25
Subject: Re: Luna, a small generic C++ template class


> On Mon, Feb 12, 2001 at 01:18:33PM -0500, Lenny Palozzi wrote:
> >
> > * The code consists of a small header file(3.6K) just over 100 lines.
>
> I had a clever thought and now its only ~70 lines of code and 1.9K. Get it
> here.
>
> > http://sophia.eyep.net/code/luna.tar.gz
>
> -Lenny.


Reply | Threaded
Open this post in threaded view
|

Re: Luna, a small generic C++ template class

Lenny Palozzi-4
On Mon, Feb 12, 2001 at 11:03:42PM -0800, Kelvin McDowell wrote:
> Just curious, was this at all inspired by the boost python library?  I ask
> because after seeing it I had been thinking of doing something like this
> myself.
> http://www.boost.org/libs/python/doc/index.html

No, never heard of it. I had a look at it just now, Luna is similar to it.

-Lenny

Reply | Threaded
Open this post in threaded view
|

Re: Luna, a small generic C++ template class

Jean-Claude Wippler
In reply to this post by Lenny Palozzi-4
On Mon, Feb 12, 2001, Lenny Palozzi <[hidden email]> wrote:

>Luna is a small generic C++ template class that binds your C++ 
>classes to Lua. [...]

Thank you very much for sharing this delightful bit of C++ code!

I have taken the liberty to alter it and add some more stuff, see:
    http://mini.net/cgi-bin/lua/42.html
It's not perfect, and I'm not that much of a template wizard, but so far
it seems to work nicely for my purposes.  The big change is that
instances are now user data objects instead of tables, and these no
longer each carry a copy of the member functions around - instead, a tag
method is used to delegate all accesses to a single table.

Feel free to use/improve/trim as you like.

Regards,
Jean-Claude

-jcw