Tomasu / LuaGlue

C++11 Lua 5.2 Binding Library
zlib License
79 stars 22 forks source link

LuaGlueClassRef #41

Closed JMLX42 closed 10 years ago

JMLX42 commented 10 years ago

Hi,

the idea is the same as described in #25 but for Lua "classes" tables. In C++, there is no way to manipulate a "class object". Yet, as Lua stores classes as tables, we can easily emulate some aspects of C++ meta-programming if we were able to get a reference to a comparable Lua class table object.

MyComponentClass = {}
function MyComponentClass:someMethod()
-- some code...
end

MyOtherComponentClass = {}
function MyOtherComponentClass:someMethod()
-- some other code...
end

cppObject:removeComponentByType(MyOtherComponentClass)
class MyCppClass
{
public:
  template <typename T>
  removeComponentByType()
  {
    // use typeid...
  }

  void
  removeComponentByTypeWrapper(LuaGlueClassRef ref)
  {
    // compare "ref"...
  }
};

So I was thinking about having a LuaGlueClassRef and the required stack<> specializations. This class should only store the things required to identify and compare Lua class bound from C++ using LuaGlue. For example, we could compare their METATABLE_CLASSIDX_FIELD field.

What do you think?

Tomasu commented 10 years ago

Hm, that could work.

One question though, if I added a wrapper for Lua tables, and allowed passing them to C++, would also work? I think not being able to pass tables around is probably a rather big missing feature. At any rate, adding a table wrapper is going to happen.

JMLX42 commented 10 years ago

Well I don't pass tables from C++ to Lua. And when I'm dealing with Lua tables in C++, I actually only need somekind of comparable ref: not the actual table and its content. I just want to be able to "compare types".

Anyway we could have a LuaGlueTable object with get/set methods: those won't weight anything since they'll just mess with the Lua stack when called. Right?

I guess the two features can be implemented in a single class then...

Tomasu commented 10 years ago

Yeah, I meant passing tables from lua to C++.

But yeah, I see what you want.. I'm just not sure it needs to be implemented in the core like that. What about adding a global function that grabs the type from any LuaGlue object/class? then you can pass that in as a string (not sure exporting the class idx is a good idea, its an internal implementation detail, and the actual integer value will change based on the order things are added to the symtab).

Tomasu commented 10 years ago

Ok, I've pushed up a "typeid" function that can be called on a Class or an instance. Let me know if this works for you.

Tomasu commented 10 years ago

closing for now. if theres something I've missed, please reopen.