views:

681

answers:

3

I'm working on a C++ project with a large number of classes (150+), each of which has anywhere from 10 to 300 fields or so. I would really like to be able to provide a scripting interface for testing purposes so that I can code callbacks that don't require any re-compilation. I'd like to do this in Lua since I'm more familiar with its C API than I am with Python's, but if it will save headaches I'd be happy to do it in Python.

I've got a solid grasp on how to call Lua functions from my C++ and vice versa, and I know how to pass basic data types back and forth. The question I have is how to share user-specified data types between the two using SWIG.

For example, at some point in my C++, I might want to evaluate a couple of pieces of member data in an object that has 250 fields. I'd like to be able to hand that object off to Lua which could then (hopefully?) use the generated SWIG wrappers to manipulate that object, display certain fields, and then pass the (potentially changed) object back to C++ for continued use.

I would also like to be able to instantiate an instance of the object in Lua using the wrappers and pass it off to C++ to be used as a normal C++ version of the object.

Is this possible? Could somebody point me towards a tutorial or an explicit example?

Thanks for any help you can offer!

+1  A: 

As long as you wrap your user-defined types using Swig interfaces (see here for documentation on Swig-Lua API), the interaction should be seamless. The provided Swig wrappers will allow you to instantiate new objects, pass them along to C++ and vice-versa.

I do not believe that Swig-Lua wrapping supports director classes yet, which means that extending existing classes, instantiating them and passing them back to C++ is not possible. Directors are supported for languages such as Python, Java, C# though.

ASk
That's great, thanks for the link! Even going one-way only is a big help.Even if I can't make a brand new instance of an object in Lua and pass it back to C++, do you know if I can *modify* an existing C++ object that was passed to Lua? That is, can I make a C++ object, pass it to Lua, change a couple of fields in Lua, then resume using it in C++ with the updated fields?
Zack
Yes, of course. Modifying the object in LUA (via the interface provided by SWIG) actually modifies the underlying C++ object. It's all pretty seamless.SWIG also does a pretty good job with language features like arrays, STL, pointers, factory-type functions and operator-based class interfaces (including smart pointers)
ASk
A: 

If swig gives you trouble, I've had good luck with the latest version of tolua++, which is for the sole purpose of binding C++ and Lua. It requires a modified .h file as input, which is a bit tedious, but no more so than Swig's modules. I have no reason to prefer one over the other, but it's good to know about both.

Norman Ramsey
A: 

You should also check out Luabind. This one implements OOP for Lua and can map classes and data types from Lua back to C++.

SW