2013-05-11 21:02:42 +02:00
|
|
|
local setmetatable = setmetatable
|
|
|
|
local table = table
|
|
|
|
local rawset = rawset
|
|
|
|
local rawget = rawget
|
|
|
|
local pairs = pairs
|
|
|
|
|
|
|
|
local function setup_object(args)
|
|
|
|
local data,args,private_data,signals = {},args or {},private_data or {},{}
|
2014-03-05 06:12:48 +01:00
|
|
|
local private_data = args.private_data or {}
|
2013-05-11 21:02:42 +02:00
|
|
|
|
|
|
|
function data:connect_signal(name,func)
|
|
|
|
signals[name] = signals[name] or {}
|
|
|
|
table.insert(signals[name],func)
|
2014-04-06 05:54:04 +02:00
|
|
|
data:emit_signal("connection",name,#signals[name])
|
2013-05-11 21:02:42 +02:00
|
|
|
end
|
|
|
|
|
2014-03-05 06:12:48 +01:00
|
|
|
function data:disconnect_signal(name,func)
|
2013-05-11 21:02:42 +02:00
|
|
|
for k,v in pairs(signals[name] or {}) do
|
|
|
|
if v == func then
|
|
|
|
signals[name][k] = nil
|
2014-04-06 05:54:04 +02:00
|
|
|
data:emit_signal("disconnection",name,#signals[name])
|
2013-05-11 21:02:42 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function data:emit_signal(name,...)
|
|
|
|
for k,v in pairs(signals[name] or {}) do
|
|
|
|
v(data,...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function data:add_autosignal_field(name)
|
|
|
|
args.force_private = args.force_private or {}
|
|
|
|
table.insert(args.force_private,name)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function return_data(tab, key)
|
2014-03-05 06:12:48 +01:00
|
|
|
if rawget(tab,"get_"..key) then
|
|
|
|
return rawget(tab,"get_"..key)(tab)
|
2013-05-11 21:02:42 +02:00
|
|
|
elseif args.autogen_getmap == true and private_data[key] ~= nil then
|
|
|
|
return private_data[key]
|
|
|
|
elseif args.other_get_callback then
|
|
|
|
local to_return = args.other_get_callback(key)
|
|
|
|
if to_return then return to_return end
|
|
|
|
end
|
|
|
|
return rawget(tab,key)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function auto_signal(key)
|
|
|
|
if args.autogen_signals == true then
|
|
|
|
data:emit_signal(key.."::changed")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function catch_changes(tab, key,value)
|
2014-03-05 06:12:48 +01:00
|
|
|
local setter = rawget(tab,"set_"..key)
|
|
|
|
if setter == false then
|
2013-05-11 21:02:42 +02:00
|
|
|
--print("This is not a setter",debug.traceback()) --In some case, it may be called "normally", having this print is only good for debug
|
2014-03-05 06:12:48 +01:00
|
|
|
elseif (data[key] ~= value or (args.always_handle ~= nil and args.always_handle[key] == true)) and setter then
|
|
|
|
setter(tab,value)
|
2013-05-11 21:02:42 +02:00
|
|
|
auto_signal(key)
|
|
|
|
elseif (args.force_private or {})[key] == true or (args.autogen_setmap and (private_data[key] ~= nil)) then
|
|
|
|
private_data[key] = value
|
|
|
|
auto_signal(key)
|
2014-03-05 06:12:48 +01:00
|
|
|
elseif setter == nil then
|
2013-05-11 21:02:42 +02:00
|
|
|
rawset(data,key,value)
|
|
|
|
end
|
|
|
|
if args.auto_signal_changed == true then
|
|
|
|
data:emit_signal("changed")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
setmetatable(data, { __index = return_data, __newindex = catch_changes, __len = function() return #data + #private_data end, })
|
2014-03-05 06:12:48 +01:00
|
|
|
return data,private_data
|
2013-05-11 21:02:42 +02:00
|
|
|
end
|
|
|
|
return setmetatable({}, { __call = function(_, ...) return setup_object(...) end })
|