[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Getting the "ID" of an object.
- From: Eike Decker <zet23t@...>
- Date: Mon, 2 May 2011 10:49:19 +0200
2011/5/2 Lorenzo Donati <lorenzodonatibz@interfree.it>:
> I know that I could either:
> 1) generate an unique ID with a custom algorithm
> 2) upon creation, before attaching the metatable to the object being
> created, call tostring on the object and store the ID in a (weak?) table.
> 3) use a proxy and return the ID of the "private" object instead (preserves
> uniqueness because of the 1-to-1 relationship between proxies and objects)
You can modify 1) a bit to make it less performance critical:
do
local idcnt = 0
local idcache = setmetatable({},{__mode='k',__index = function(t,k)
idcnt = idcnt + 1 t[k] = idcnt return idcnt end})
function getid (o) return idcache[o] end
end
So this would only affect performance for debug code that tags objects
if you take a look at them.
Aside from this, I don't know any other trick. Well, you could also
erase the metatable, get the string and restore the original metatable
again.
And besides all that: You could look into the Lua code and find out,
how the table:#xxx code looks like and provide it as additional
function if you have access on the C API. E.g. table.getpointerid
Cheers,
Eike