| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- table.unpack = table.unpack or unpack
- function clone(object, setMetaTable)
- local lookup_table = {}
- local function _copy(object)
- if type(object) ~= "table" then
- return object
- elseif lookup_table[object] then
- return lookup_table[object]
- end
- local new_table = {}
- lookup_table[object] = new_table
- for key, value in pairs(object) do
- new_table[_copy(key)] = _copy(value)
- end
- if not setMetaTable then
- return new_table
- end
- return setmetatable(new_table, getmetatable(object))
- end
- return _copy(object)
- end
- local objectCount = 0
- local function getObjectCount()
- objectCount = objectCount + 1
- return objectCount
- end
- local create
- create = function(c, obj, ...)
- if c.supers then
- for k,v in pairs(c.supers) do
- create(v, obj, ...)
- end
- elseif c.super then
- create(c.super, obj, ...)
- end
- if c.ctor then
- c.ctor(obj, ...)
- end
- end
- function class(classname, ...)
- local supers = {...}
- local super = supers[1]
- local superType = type(super)
- local cls = {}
- cls.__cname = classname
-
- if superType ~= "function" and superType ~= "table" then
- superType = nil
- super = nil
- end
- if superType == "function" or (super and super.__ctype == 1) then
- -- inherited from native C++ Object
- cls.__ctype = 1
- if superType == "table" then
- -- copy fields from super
- -- setmetatable(cls, super)
- for k,v in pairs(super) do cls[k] = v end
- cls.__create = super.__create
- cls.super = super
- else
- cls.__create = super
- end
- function cls:new(o, ...)
- local instance = cls.__create(o, ...)
- tolua.setpeer(instance, {})
- -- instance.instUUId = getObjectCount()
- -- copy fields from class to native object
- for k,v in pairs(cls) do instance[k] = v end
- if tag then
- create(cls, instance, ...)
- else
- create(cls, instance, o, ...)
- end
- return instance
- end
- else
- -- inherited from Lua Object
- cls.super = super
- cls.supers = supers
- cls.__index = cls
- cls.__ctype = 2 -- lua
- if #supers == 1 then
- setmetatable(cls, super)
- else
- LuaExtend(cls, table.unpack(supers))
- end
-
- function cls:new(o, ...)
- local instance = setmetatable({}, cls)
- create(cls, instance, o, ...)
- -- -- instance.instUUId = getObjectCount()
- -- if iskindof(instance, "LuaMono") then
- -- instance:InitGameObject(o)
- -- create(cls, instance, ...)
- -- else
- -- create(cls, instance, o, ...)
- -- end
- return instance
- end
- end
- function cls:getClassName()
- return self.__cname
- end
- return cls
- end
- function instanceOf(cls)
- if cls and type(cls) == 'table' and cls.__cname then
- return cls.__cname
- end
- return NONE_CLASS
- end
- function iskindof(obj, className)
- local t = type(obj)
- if t == "table" then
- -- local mt = getmetatable(obj)
- -- while mt and mt.__index do
- local mt = obj
- if mt.__cname == className then
- return true
- elseif mt.supers then
- for k,v in pairs(mt.supers) do
- if iskindof(v, className) then
- return true
- end
- end
- else
- return iskindof(obj.super, className)
- end
- return false
- elseif t == "userdata" then
- return false
- else
- return false
- end
- end
- function LuaExtend(src, ...)
- local meta = getmetatable(src)
- local parents = {...}
- setmetatable(src, {
- __index = function (tb, key)
- local v = rawget(tb, key)
- if not v and meta then
- local find = meta[key]
- if find then
- return find
- end
- end
- for k,v in pairs(parents) do
- local find = v[key]
- if find then
- return find
- end
- end
- end
- })
- end
- function addmetatable(src, super)
- local meta = getmetatable(src)
- setmetatable(src, {
- __index = function (tb, key)
- local v = rawget(tb, key)
- if not v and meta then
- local find = meta[key]
- if find then
- return find
- end
- end
- find = super[key]
- if find then
- return find
- end
- end
- })
- end
|