C++使用lua的工厂

发布于 2024-10-21 13:56:56 字数 628 浏览 1 评论 0原文

我有一个脚本:

  • 仅用于数据的自定义语言
  • 使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

习惯成性 2024-10-28 13:56:56

您可以允许脚本环境创建或不允许创建 C++ 对象,具体取决于您的需要。
tolua++ 使用所有元表功能来允许在 lua 中非常简单地操作 C++ 类型。

例如,此声明:


// tolua_begin
class SomeCppClass
{
public:
   SomeCppClass();
   ~SomeCppClass();

   int some_field;
   void some_method();
};
// tolua_end

将自动生成 lua 绑定以允许此 lua scipt:


#!lua
-- obj1 must be deleted manually
local obj1 = SomeCppClass:new() 
-- obj1 will be automatically garbage collected 
local obj2 = SomeCppClass:new_local()

obj1.some_field = 3 -- direct access to "some_field"
obj2:some_method() -- direct call to "some_method"

obj1:delete()

这种技术的优点是您的 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 :


// tolua_begin
class SomeCppClass
{
public:
   SomeCppClass();
   ~SomeCppClass();

   int some_field;
   void some_method();
};
// tolua_end

Will automatically generate the lua bindings to allow this lua scipt :


#!lua
-- obj1 must be deleted manually
local obj1 = SomeCppClass:new() 
-- obj1 will be automatically garbage collected 
local obj2 = SomeCppClass:new_local()

obj1.some_field = 3 -- direct access to "some_field"
obj2:some_method() -- direct call to "some_method"

obj1:delete()

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

む无字情书 2024-10-28 13:56:56

在这种情况下,我更喜欢设置一个绑定 C 函数,该函数将参数表作为参数。因此,Lua 脚本如下所示。

CreateObject{
    Type = "someType"'
    someProperty = someValue,
    -- ...
}

该表将位于回调函数中的堆栈顶部,并且可以使用 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.

CreateObject{
    Type = "someType"'
    someProperty = someValue,
    -- ...
}

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.

‖放下 2024-10-28 13:56:56

第一种方法很可能会更快,但第二种方法可能会导致更少的对象初始化代码(假设您正在初始化很多对象)。如果您选择第一种方法,您可以手动完成。如果您选择第二种方法,您可能需要使用像 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文