C++使用lua的工厂
我有一个脚本:
- 仅用于数据的自定义语言
- 使用 C++ 的脚本类加载
- 我有 Type 等标签
- 获取标签值的接口 - Script::GetValue(Tag, T& value)
脚本像这样使用:
Script* script("someFile");
script->GetValue("Type", type);
Object* obj = CreateObject(type);
obj->Load(script);
其中 Load 函数来自 object 用于加载其余 obj 参数。 现在我将脚本语言更改为lua。我的问题是:
我应该保留这种创建对象的方式(仅使用lua用于数据)还是应该在lua中公开工厂并从lua中使用它,像这样(在lua中):
CreateObject("someType")
SetProperty(someObj, someProperty, someValue)
首先我想知道哪个是更快,第一种或第二种方法。您还有其他建议吗?因为我正在重构这部分,所以我愿意接受其他建议。我想保留 lua,因为它速度快、易于集成且体积小。
I had a script with:
- Custom language used only for data
- Was loaded using a Script class from C++
- I had tags like Type, etc
- An interface to get a value for a tag - Script::GetValue(Tag, T& value)
The script was used like this:
Script* script("someFile");
script->GetValue("Type", type);
Object* obj = CreateObject(type);
obj->Load(script);
Where Load functions from object was used to load the rest of obj parameters.
Now I changed the script language to lua. My questions is:
Should I keep this way of creating objects(use lua only for data) or should I expose the factory in lua and use it from lua, something like this(in lua):
CreateObject("someType")
SetProperty(someObj, someProperty, someValue)
First of all I want to know which is faster, first or second approach. Do you have other suggestions? Because I'm refactoring this part I'm open to other suggestions. I want to keep lua because is fast, easy to integrate, and small.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以允许脚本环境创建或不允许创建 C++ 对象,具体取决于您的需要。
tolua++ 使用所有元表功能来允许在 lua 中非常简单地操作 C++ 类型。
例如,此声明:
将自动生成 lua 绑定以允许此 lua scipt:
这种技术的优点是您的 lua 代码将与依赖的 C++ 代码非常一致。请参阅http://www.codenix.com/~tolua/tolua++.html
You may allow your script environment to create C++ objects or not, depending on your needs.
tolua++ uses all the metatable features to allow a very straightforward manipulation of your c++ types in lua.
For example, this declaration :
Will automatically generate the lua bindings to allow this lua scipt :
The advantage of this technique is that your lua code will ve very consistent with the relying C++ code. See http://www.codenix.com/~tolua/tolua++.html
在这种情况下,我更喜欢设置一个绑定 C 函数,该函数将参数表作为参数。因此,Lua 脚本如下所示。
该表将位于回调函数中的堆栈顶部,并且可以使用 lua_getfield。
您可能还想研究沙箱化您的 Lua 环境。
In situations like that, I prefer to setup a bound C function that takes a table of parameters as an argument. So, the Lua script would look like the following.
This table would be on top of the stack in the callback function, and all parameters can be accessed by name using lua_getfield.
You may also want to investigate sandboxing your Lua environment.
第一种方法很可能会更快,但第二种方法可能会导致更少的对象初始化代码(假设您正在初始化很多对象)。如果您选择第一种方法,您可以手动完成。如果您选择第二种方法,您可能需要使用像 Luabind 这样的绑定库来避免错误并加快实施时间,假设您正在针对多种对象类型和数据类型执行此操作。
最简单的方法可能是只使用 Lua 来处理数据;如果你想公开工厂并通过 Lua 使用它,请确保首先值得付出努力。
The first approach would most likely be faster, but the second approach would probably result in less object initialization code (assuming you're initializing a lot of objects). If you choose the first approach, you can do it manually. If you choose the second approach you might want to use a binding library like Luabind to avoid errors and speed up implementation time, assuming you're doing this for multiple object types and data types.
The simplest approach will probably be to just use Lua for data; if you want to expose the factory and use it via Lua, make sure it's worth the effort first.