Skip to content
This repository has been archived by the owner on Dec 30, 2023. It is now read-only.

Commit

Permalink
use cocos lua class
Browse files Browse the repository at this point in the history
  • Loading branch information
fztcjjl committed Aug 20, 2015
1 parent 01ff094 commit 860b0db
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 75 deletions.
4 changes: 2 additions & 2 deletions common/datacenter/accountdc.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
local skynet = require "skynet"
local snax = require "snax"
local entity = require "Entity"
local EntityFactory = require "EntityFactory"

local EntAccount

function init(...)
EntAccount = entity.Get("d_account")
EntAccount = EntityFactory.Get("d_account")
EntAccount:Init()
EntAccount:Load()
end
Expand Down
6 changes: 3 additions & 3 deletions common/datacenter/userdc.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
local skynet = require "skynet"
local snax = require "snax"
local entity = require "Entity"
local EntityFactory = require "EntityFactory"

local EntUser
local EntUserCustom

function init(...)
EntUser = entity.Get("d_user")
EntUser = EntityFactory.Get("d_user")
EntUser:Init()

EntUserCustom = entity.Get("d_user_custom")
EntUserCustom = EntityFactory.Get("d_user_custom")
EntUserCustom:Init()
EntUserCustom:Load()
end
Expand Down
5 changes: 3 additions & 2 deletions common/entity/d_account.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require "CommonEntity"
local CommonEntity = require "CommonEntity"

local EntityType = class(CommonEntity)
local EntityType = class("d_account", CommonEntity)

function EntityType:ctor()
EntityType.super.ctor(self)
self.tbname = "d_account"
end

Expand Down
5 changes: 3 additions & 2 deletions common/entity/d_user.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require "UserSingleEntity"
local UserSingleEntity = require "UserSingleEntity"

local EntityType = class(UserSingleEntity)
local EntityType = class("d_user", UserSingleEntity)

function EntityType:ctor()
EntityType.super.ctor(self)
self.tbname = "d_user"
end

Expand Down
5 changes: 3 additions & 2 deletions common/entity/d_user_custom.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require "CommonEntity"
local CommonEntity = require "CommonEntity"

local EntityType = class(CommonEntity)
local EntityType = class("d_user_custom", CommonEntity)

function EntityType:ctor()
EntityType.super.ctor(self)
self.tbname = "d_user"
end

Expand Down
7 changes: 5 additions & 2 deletions common/entitybase/CommonEntity.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
local skynet = require "skynet"
local snax = require "snax"
require "Entity"
local Entity = require "Entity"

-- CommonEntity
CommonEntity = class(Entity)
local CommonEntity = class("CommonEntity", Entity)

function CommonEntity:ctor()
CommonEntity.super.ctor(self)
self.type = 3
end

Expand Down Expand Up @@ -162,3 +163,5 @@ end
function CommonEntity:GetAll( )
return self.recordset
end

return CommonEntity
7 changes: 5 additions & 2 deletions common/entitybase/ConfigEntity.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
local skynet = require "skynet"
require "Entity"
local Entity = require "Entity"

-- 定义ConfigEntity类型
ConfigEntity = class(Entity)
local ConfigEntity = class("ConfigEntity", Entity)

function ConfigEntity:ctor()
ConfigEntity.super.ctor(self)
self.type = 1
end

Expand Down Expand Up @@ -48,3 +49,5 @@ end
function ConfigEntity:GetAll( )
return self.recordset
end

return ConfigEntity
18 changes: 2 additions & 16 deletions common/entitybase/Entity.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local skynet = require "skynet"

-- 定义Entity类型
Entity = class()
local Entity = class("Entity")

function Entity:ctor()
self.recordset = {} -- 存放记录集
Expand All @@ -28,18 +28,4 @@ function Entity:Init()
--self.tbschema = skynet.call("dbmgr", "lua", "get_schema", self.tbname)
end

local M = {}
local entities = {} -- 保存实体对象

-- 工厂方法,获取具体对象,name为表名
function M.Get(name)
if entities[name] then
return entities[name]
end

local ent = require(name)
entities[name] = ent
return ent
end

return M
return Entity
15 changes: 15 additions & 0 deletions common/entitybase/EntityFactory.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
local M = {}
local entities = {} -- 保存实体对象

-- 工厂方法,获取具体对象,name为表名
function M.Get(name)
if entities[name] then
return entities[name]
end

local ent = require(name)
entities[name] = ent
return ent
end

return M
7 changes: 5 additions & 2 deletions common/entitybase/UserEntity.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
local skynet = require "skynet"
require "Entity"
local Entity = require "Entity"

-- 定义UserEntity类型
UserEntity = class(Entity)
local UserEntity = class("UserEntity", Entity)

function UserEntity:ctor()
UserEntity.super.ctor(self)
self.ismulti = false -- 是否多行记录
self.type = 2
end

function UserEntity:dtor()
end

return UserEntity
7 changes: 5 additions & 2 deletions common/entitybase/UserMultiEntity.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local skynet = require "skynet"
require "UserEntity"
local UserEntity = require "UserEntity"

-- UserMultiEntity
UserMultiEntity = class(UserEntity)
local UserMultiEntity = class("UserMultiEntity", UserEntity)

-- self.recordset格式如下:
--[[
Expand All @@ -22,6 +22,7 @@ UserMultiEntity = class(UserEntity)


function UserMultiEntity:ctor()
UserMultiEntity.super.ctor(self)
self.ismulti = true -- 是否多行记录
end

Expand Down Expand Up @@ -217,3 +218,5 @@ function UserMultiEntity:GetMulti(uid)
end
return rs
end

return UserMultiEntity
7 changes: 5 additions & 2 deletions common/entitybase/UserSingleEntity.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local skynet = require "skynet"
require "UserEntity"
local UserEntity = require "UserEntity"

-- UserSingleEntity
UserSingleEntity = class(UserEntity)
local UserSingleEntity = class("UserSingleEntity", UserEntity)

-- self.recordset格式如下:
--[[
Expand All @@ -13,6 +13,7 @@ UserSingleEntity = class(UserEntity)
--]]

function UserSingleEntity:ctor()
UserSingleEntity.super.ctor(self)
self.ismulti = false -- 是否多行记录
end

Expand Down Expand Up @@ -173,3 +174,5 @@ function UserSingleEntity:SetValue(uid, field, value)

return self:Update(record)
end

return UserSingleEntity
113 changes: 75 additions & 38 deletions global/luaext.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ table.copy = function(t, nometa)
return result
end

table.merge = function(dest, src)
for k, v in pairs(src) do
dest[k] = v
end
end

-- string扩展

Expand Down Expand Up @@ -176,49 +181,81 @@ end


-- lua面向对象扩展
local _class={}

function class(super)
local class_type={}
class_type.ctor=false
class_type.super=super
class_type.new=function(...)
local obj={}
do
local create
create = function(c,...)
if c.super then
create(c.super,...)
end
if c.ctor then
c.ctor(obj,...)
end
end

create(class_type,...)
end
setmetatable(obj,{ __index=_class[class_type] })
return obj
function class(classname, super)
local superType = type(super)
local cls

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 = {}

if superType == "table" then
-- copy fields from super
for k,v in pairs(super) do cls[k] = v end
cls.__create = super.__create
cls.super = super
else
cls.__create = super
cls.ctor = function() end
end
local vtbl={}
_class[class_type]=vtbl

setmetatable(class_type,{__newindex=
function(t,k,v)
vtbl[k]=v
cls.__cname = classname
cls.__ctype = 1

function cls.new(...)
local instance = cls.__create(...)
-- copy fields from class to native object
for k,v in pairs(cls) do instance[k] = v end
instance.class = cls
instance:ctor(...)
return instance
end
})

if super then
setmetatable(vtbl,{__index=
function(t,k)
local ret=_class[super][k]
vtbl[k]=ret
return ret
end
})
else
-- inherited from Lua Object
if super then
cls = {}
setmetatable(cls, {__index = super})
cls.super = super
else
cls = {ctor = function() end}
end

cls.__cname = classname
cls.__ctype = 2 -- lua
cls.__index = cls

function cls.new(...)
local instance = setmetatable({}, cls)
instance.class = cls
instance:ctor(...)
return instance
end
end

return class_type
return cls
end

function iskindof(obj, classname)
local t = type(obj)
local mt
if t == "table" then
mt = getmetatable(obj)
elseif t == "userdata" then
mt = tolua.getpeer(obj)
end

while mt do
if mt.__cname == classname then
return true
end
mt = mt.super
end

return false
end

0 comments on commit 860b0db

Please sign in to comment.