开发者

C++ Factory using lua

开发者 https://www.devze.com 2023-02-16 04:26 出处:网络
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

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 objec开发者_Go百科t 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.


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消