原创文章,欢迎转载,转载请注明:文章来自[寒江孤叶丶的Cocos2d-x之旅系列]
博客地址:http://blog.csdn.net/qq446569365
写个深拷贝放这儿存着……以后好找……
-- table 的深拷贝,会判断userdata,nil,以及其他值类型并将拷贝结果返回
--(userdata 会判断是否有clone方法如果有返回clone调用结果,如果没有返回nil)
function _G.APUtils.deepCopy(t)
if not t then
error("deepCopy t is nil ")
return nil
end
if type(t) ~= "table" then
if type(t) == "userdata" then
if t.clone then
return t:clone()
else
return nil
end
else
return t
end
end
local function _deepCopy(from ,to)
for k, v in pairs(from) do
if type(v) ~= "table" then
to[k] = v;
else
to[k] = {};
_deepCopy(v,to[k]);
end
end
return setmetatable(to, getmetatable(from))
end
local u = {}
_deepCopy(t,u)
return u
end