Exposing classes to Lua

Started by
2 comments, last by XXX_Andrew_XXX 17 years ago
Hi, I am working more with Lua, and exposing more of my C++ internals to Lua scripting. As part of this process I have a number of classes that need to be exposed to Lua - I am doing this using Luabind. My question is: How do you structure this exposure within C++? Each class that needs exposure requires a luabind::module call. The simplest way to do this would be to have a function do all this calling as part of the initialization process. However, this seems to me to be a large compile-time bottleneck - this one file needs to pull in the header files from each of the classes that needs to be exposed. This seems undesirable. I thought about using a static registration of the form:

// .h file
struct MyClass
{
 static bool RegisterWithLua();
};

// .cpp file

namespace 
{
 const bool registered = MyClass::RegisterWithLua();
}

bool MyClass::RegisterWithLua()
{
 // Do the actual registration
}

This works up to the point of needing inheritance, as one can't specify the order of static initializations. So - am I missing something, as I often do? Is there a simple (or even complex) solution to this problem? Or do I just need to suck it up and accept that I will be using one file to expose all my C++ classes? Thanks, Jim.
Advertisement
OK, I have little to no idea what I'm talking about, but here's my input:

Is there a way to possibly pass the entire C++ file through the luabind module? Or is there a way you can make a module that parses information incoming from the source code shown above and does three instances of the luabind module, one converting the .h files, one converting .cpp files, and one receiving input from the other two and registering them in Lua in real-time?
Thanks for the response ShotgunNinja.

Looks like I actually overthought the problem. The compilation dependency is at the .cpp level and not .h level, so the issues I was imagining - well, weren't really issues.
I would strongly recommend that you actually want everything related to luabind to be in a single compilation unit (.cpp). You are exactly right that compile times can be an absolute nightmare. AFAIK there is no nice way of doing this. One suggestion that has helped a little in the past is to use the pimpl (pointer-to-implementation) idiom where possible.

Alternatively you might initially consider exactly how you will expose your API to lua - make sure that you only expose the minimum number of calls necessary. In other words, can you use a message passing system with read / write calls into your data?

If you aren't exactly sure what you need to wrap I would recommend something other than luabind (go with SWIG or ToLua) until you feel comfortable that the interface between languages is fairly static - from past experience luabind is a real pain when it starts to slow down your code/compile/debug cycles, which you're trying to speed up in the first place by scripting.

This topic is closed to new replies.

Advertisement