lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 21 December 2010 11:34, Dirk Laurie <dpl@sun.ac.za> wrote:
> On Tue, Dec 21, 2010 at 05:59:09PM +0200, Marc Balmer wrote:
>> In Lua, I create a hierarchy of "objects" (Lua tables) that finally
>> represent a GUI widget hierarchy.
>>
>> Now I want to somehow store C data in these objects, or link them with
>> such information.
>>
>> E.g. I have to store the Widget pointer (a C lightuserdata value) for
>> each widget represented in the hierarchy.
>>
>> What is a good way to do this?  My adventourous approach at the moment
>> is to create an additional table when an object is created, store my
>> values in this additional table and add it as metatable to the created
>> object...
>>
> You could also create just one table and index it with the objects:
>
> C_data={}
> C_data[this_widget]={...}
>
> That way, a routine to which you pass this_widget will not automatically
> know its C data.

The only issue with this is that the widgets will never be garbage
collected until they are manually removed from the table. Of course,
maybe you actually want this as a feature, but otherwise you can use a
weak-keyed table to do this. (see my other reply)

    henk