Pass variables between C++ and Lua via Swig(通过 Swig 在 C++ 和 Lua 之间传递变量)
问题描述
我正在处理一个包含大量类(150 多个)的 C++ 项目,每个类都有 10 到 300 个左右的字段.我真的希望能够提供一个用于测试目的的脚本接口,以便我可以编写不需要任何重新编译的回调.我想在 Lua 中执行此操作,因为我对它的 C API 比对 Python 更熟悉,但如果这样可以省去麻烦,我很乐意在 Python 中执行此操作.
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.
我对如何从我的 C++ 调用 Lua 函数有一个很好的掌握,反之亦然,并且我知道如何来回传递基本数据类型.我的问题是如何使用 SWIG 在两者之间共享用户指定的数据类型.
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.
例如,在我的 C++ 中的某个时刻,我可能想要评估具有 250 个字段的对象中的几个成员数据.我希望能够将该对象交给 Lua,然后 Lua 可以(希望?)使用生成的 SWIG 包装器来操作该对象,显示某些字段,然后将(可能已更改的)对象传递回 C++ 以继续使用.
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.
我还希望能够使用包装器在 Lua 中实例化对象的实例,并将其传递给 C++ 以用作对象的普通 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.
这可能吗?有人可以指出我的教程或明确的例子吗?
Is this possible? Could somebody point me towards a tutorial or an explicit example?
感谢您提供的任何帮助!
Thanks for any help you can offer!
推荐答案
只要您使用 Swig 接口包装用户定义的类型(请参阅 此处 有关 Swig-Lua API 的文档),交互应该是无缝的.提供的 Swig 包装器将允许您实例化新对象,将它们传递给 C++,反之亦然.
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.
我不认为 Swig-Lua 包装支持导演类,这意味着扩展现有类、实例化它们并将它们传递回 C++ 是不可能的.不过,控制器支持 Python、Java、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. Directors are supported for languages such as Python, Java, C# though.
这篇关于通过 Swig 在 C++ 和 Lua 之间传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:通过 Swig 在 C++ 和 Lua 之间传递变量
基础教程推荐
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01