简体   繁体   English

通过Swig在C ++和Lua之间传递变量

[英]Pass variables between C++ and Lua via Swig

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. 我正在开发一个包含大量类(150+)的C ++项目,每个类都有10到300个字段左右。 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. 我想在Lua中这样做,因为我对它的C API比我对Python的更熟悉,但如果它能省去头痛,我会很乐意用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. 我已经掌握了如何从我的C ++调用Lua函数,反之亦然,我知道如何来回传递基本数据类型。 The question I have is how to share user-specified data types between the two using SWIG. 我的问题是如何使用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. 例如,在我的C ++中的某个时刻,我可能想要在具有250个字段的对象中评估几个成员数据。 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. 我希望能够将该对象移交给Lua,然后可以(希望?)使用生成的SWIG包装器来操作该对象,显示某些字段,然后将(可能已更改的)对象传递回C ++以继续使用。

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. 我还希望能够使用包装器在Lua中实例化对象的实例,并将其传递给C ++,以用作对象的普通C ++版本。

Is this possible? 这可能吗? Could somebody point me towards a tutorial or an explicit example? 有人能指出我的教程或明确的例子吗?

Thanks for any help you can offer! 谢谢你的尽心帮助!

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. 只要您使用Swig接口包装用户定义的类型(请参阅此处获取有关Swig-Lua API的文档),交互应该是无缝的。 The provided Swig wrappers will allow you to instantiate new objects, pass them along to C++ and vice-versa. 提供的Swig包装器将允许您实例化新对象,将它们传递给C ++,反之亦然。

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. 我不相信Swig-Lua包装支持导向类,这意味着扩展现有类,实例化它们并将它们传递回C ++是不可能的。 Directors are supported for languages such as Python, Java, C# though. 但是,支持Python,Java,C#等语言。

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. 如果swig给你带来麻烦,我已经好好使用最新版本的tolua ++,它的唯一目的是绑定C ++和Lua。 It requires a modified .h file as input, which is a bit tedious, but no more so than Swig's modules. 它需要一个修改过的.h文件作为输入,这有点单调乏味,但不比Swig的模块更多。 I have no reason to prefer one over the other, but it's good to know about both. 我没有理由偏爱另一个,但知道两者都很好。

You should also check out Luabind. 你也应该看看Luabind。 This one implements OOP for Lua and can map classes and data types from Lua back to C++. 这个实现了Lua的OOP,可以将Lua中的类和数据类型映射回C ++。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM