[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Checking if a table has changed
- From: Florian Berger <fberger@...>
- Date: Thu, 11 Dec 2003 16:25:28 +0200
Thanks for the tip! I found code from lua-l archive (see below, I
modified it a little bit). The problem is that as you can see that it
does not work for nested tables. I could traverse recursively the
subtables through and call proxy for each table but I cannot do this
because of performance reasons. This cannot be automated somehow?
Floru
Source: http://lua-users.org/lists/lua-l/2003-07/msg00173.html
do
local signature = {}
-- set a proxy on table
function proxy(table)
local meta =
{
__index = table,
__newindex = function(_, index, value)
print('Modified!')
table[index] = value
end,
__signature = signature,
}
return setmetatable({}, meta)
end
local n = next
-- next replacement to support pairs and "old style" for loops
function next(table, index)
local m = getmetatable(table)
return n(m and m.__signature == signature and m.__index or table,
index)
end
end
local x = proxy( {{a = 1}, a = 1, b = 2} )
for k, v in pairs(x) do
print(k, v)
end
x[1].a = 5
x.a = 2
Result:
1 table: 00379EB8
a 1
b 2
Modified!
Jamie Webb wrote:
On Thursday 11 December 2003 08:22, Florian Berger wrote:
Hi.
Is there a simple and efficient way to check if a table has changed? By
using metatables I'm able to catch some modifications but not all of them.
You could use a proxy table with a __newindex which makes modifications to the
actual table and sets a 'modified' flag. Since the proxy table stays empty,
__newindex is always called.
-- Jamie Webb
There are only 10 types of people in this world:
those who understand binary, and those who don't.