[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: C/C++ bindings: automated or by hand?
- From: "Leo M. Cabrera" <kixdemp@...>
- Date: Fri, 07 Sep 2007 22:36:54 -0400
When I'm in C I do it by hand, and when in C++ I use toLua (I don't use
exceptions much, so it works for me).
The reason I do this is because in C I can just use lua_register for my
functions, but if I'm on C++ (with classes) it gets more complex to do
by hand. :-P
Daniel Collins wrote:
I was just wondering how most people here bind there native code to lua.
On my first major project with Lua (a mobile phone game with
downloadable content, unfortunately never released) I wrote all the
binding code by hand. In other smaller "personal use" projects since
then I have also written all the binding code by hand.
On a recent project (binding C++ savegame editing code for Darklands, a
1992 RPG set in medieval germany with a cult following today) I decided
to try my hand at automated binding code generation.
I first tried tolua, which worked brilliantly but for one thing:
exceptions. So I ended up with a modified version of tolua++ 1.0.91
(from www.cegui.org.uk) that has support for exception handling.
Unfortunately it also has a bug where fixed size char arrays (eg: char
charArray;) generate incorrect parameter checking code for setting
the value (it tries to make sure the argument is a table rather than a
So my experience has been that hand-binding and automated binding both
have benefits and drawbacks:
++ Absolute control over how the binding occurs. If I need
exception handling, I can create exactly what I need.
+ Forces a solid knowledge of the C API. Some may see this as a
+ Much easier to read and debug.
-- Slow to create.
- Harder and slower to update when either the native code API or
the exported Lua API changes.
+ In theory, should be less error prone.
+ Very fast updating of the binding code when the underlying
native code API changes.
-- Leaves me dependent on the capabilities of the binding tool.
This can manifest as a hunt for a different tool, or in warping the
native code API to accommodate the needs/limitations of the binding
That last point is to me the biggest drawback of automated code binding.
I don't like having to modify the native code API just to suit the
binding tool, especially since the C++ code is also used by other C++
projects. I also don't like being dependent on the tool's functionality
for features I know I could do by hand.
I don't know which method I would use in future projects. I suspect that
the larger the native API the more the speed gains of automated binding
would outweigh the fine control that hand written binding code gives.
In my current situation, I might just hand write the bindings for those
items that cause problems for tolua++.
So what are the thoughts and experiences of others here on this issue?