[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: userdata as table keys
- From: Matt Campbell <mattc@...>
- Date: Mon, 08 Jan 2007 14:40:02 -0600
To address this problem, I use a cache table stored in the Lua registry.
In this table, the keys are light userdata (the raw pointers, to COM
objects in my case), and the values are the full userdata. The cache
table has a metatable whose __mode field is "v", so the values in the
cache table are weak reference. Then when a C function is about to wrap
an object, it first checks the cache table for an existing wrapper. If
there is one, that is returned; otherwise, a new full userdatum is
created and returned, and the appropriate entry is added to the cache
table. This way, userdata can serve as table keys, and I don't even
have to implement an __eq metamethod, unless the object being wrapped
has its own semantics for equality beyond comparing pointers.
I hope this helps.
--
Matt Campbell
Lead Programmer
Serotek Corporation
www.freedombox.info
"The Accessibility Anywhere People"