├── LICENSE ├── README.md ├── luaClass ├── algorithm │ ├── Func.lua │ ├── init.lua │ └── iterator.lua ├── class │ ├── class.lua │ ├── config │ │ ├── classConfig.lua │ │ └── init.lua │ ├── helper │ │ ├── debug.lua │ │ ├── funcHelper.lua │ │ ├── init.lua │ │ ├── member.lua │ │ ├── property.lua │ │ ├── protected.lua │ │ ├── public.lua │ │ ├── serilize.lua │ │ ├── signalSlot.lua │ │ └── super.lua │ ├── init.lua │ ├── namespace.lua │ └── object │ │ ├── LMetaObject.lua │ │ ├── LObject.lua │ │ └── init.lua ├── container │ ├── array.lua │ ├── graph.lua │ ├── init.lua │ ├── map.lua │ ├── mat.lua │ ├── queue.lua │ ├── set.lua │ └── stack.lua ├── include.lua ├── init.lua └── uiBase │ ├── LUIObject.lua │ ├── READ.md │ ├── UIScript.lua │ └── init.lua └── test ├── AccessedTest ├── EmmyLuaTest.lua ├── LMetaObjectTest.lua ├── LObjectTest.lua ├── LString.lua ├── inheritTest.lua ├── propertyTest.lua ├── protectedTest.lua ├── publicTest.lua └── signalSlotTest.lua ├── __serilizeResult.lua ├── containerTest ├── arrayTest.lua ├── graphTest.lua ├── mapTest.lua ├── queueTest.lua ├── setTest.lua ├── stackTest.lua └── timeTest.lua ├── normalTest ├── Equipment.lua ├── Role.lua ├── item.lua └── test.lua ├── serilizeTest.lua ├── test.lua └── unSerilizeTest.lua /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 CppCXY 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 弃坑 2 | -------------------------------------------------------------------------------- /luaClass/algorithm/Func.lua: -------------------------------------------------------------------------------- 1 | --可序列化函数类 2 | _ENV=namespace "luaClass" 3 | 4 | class ("Func"){ 5 | public{ 6 | FUNCTION.Func(function(self,captureList,luafunc) 7 | self._luaf=luafunc 8 | self._captureList=captureList 9 | end); 10 | FUNCTION.call(function(self,...) 11 | return self._luaf(self._captureList,...) 12 | end); 13 | 14 | META.__call(function(self,...) 15 | return self:call(...) 16 | end); 17 | 18 | }; 19 | 20 | protected{ 21 | MEMBER._luaf(); 22 | MEMBER._captureList(); 23 | 24 | 25 | }; 26 | 27 | } 28 | -------------------------------------------------------------------------------- /luaClass/algorithm/init.lua: -------------------------------------------------------------------------------- 1 | require "luaClass.include" 2 | include "luaClass.class.init" 3 | include "luaClass.algorithm.iterator" 4 | include "luaClass.algorithm.Func" -------------------------------------------------------------------------------- /luaClass/algorithm/iterator.lua: -------------------------------------------------------------------------------- 1 | include "luaClass.class.init" 2 | _ENV=namespace "algorithm.iterator" 3 | 4 | local table_unpack=unapck or table.unpack 5 | 6 | local _zip2=function ( arrs,index ) 7 | index=index+1 8 | if arrs[1][index]==nil then return end 9 | return index,arrs[1][index],arrs[2][index] 10 | end 11 | 12 | local _zip3=function ( arrs,index ) 13 | index=index+1 14 | if arrs[1][index]==nil then return end 15 | return index,arrs[1][index],arrs[2][index],arrs[3][index] 16 | end 17 | 18 | local _zip=function ( arrs,index ) 19 | index=index+1 20 | if arrs[1][index]==nil then return end 21 | local ar={} 22 | for i,arr in ipairs(arrs) do 23 | ar[i]=arr[index] 24 | end 25 | return index,table_unpack(ar) 26 | end 27 | --[[ 28 | --可以同时遍历多个数组table,针对主要使用情况进行优化 29 | --for i,a,b,c in zip(arr,brr,crr) do 30 | -- print(i,a,b,c) 31 | --end 32 | --]] 33 | function zip(arr,... ) 34 | local index=0 35 | local arrs={arr,...} 36 | local size=#arrs 37 | if size==1 then 38 | return ipairs(arr) 39 | elseif size==2 then 40 | return _zip2,arrs,index 41 | elseif size==3 then 42 | return _zip3,arrs,index 43 | else 44 | return _zip,arrs,index 45 | end 46 | end 47 | 48 | --反向迭代器 49 | 50 | local __reverse=function (arr,index ) 51 | index=index-1 52 | if index==0 then return end 53 | return index,arr[index] 54 | end 55 | 56 | function reverse(array) 57 | local index=#array 58 | return __reverse,array,index+1 59 | end -------------------------------------------------------------------------------- /luaClass/class/class.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright(c) 2019 CppCXY 3 | Author: CppCXY 4 | Github: https://github.com/CppCXY 5 | 如果觉得我的作品不错,可以去github给我的项目打星星. 6 | ]] 7 | include "luaClass.class.config.init" 8 | include "luaClass.class.helper.init" 9 | 10 | _ENV=namespace "luaClass" 11 | 12 | __debugCtor=function(cls,...) 13 | local instance={__class=cls} 14 | instance._inFunction=false 15 | instance._protected_={} 16 | setmetatable(instance,cls) 17 | local ctor=instance[cls.__cname] 18 | if ctor then 19 | 20 | for key,value in pairs(cls.__public_member) do 21 | classRawSet(instance,key,value) 22 | end 23 | local protected=instance._protected_ 24 | for key,value in pairs(cls.__protected_member) do 25 | protected[key]=value 26 | end 27 | for key,_ in pairs(cls.__signals) do 28 | protected[key]=LSIGNAL_IMPLEMENT:new() 29 | end 30 | 31 | ctor(instance,...) 32 | instance=classRawGet(instance,"__instance") or instance 33 | else 34 | error("wrong: constructor"..cls.__cname..":"..cls.__cname.." is protected") 35 | end 36 | return instance 37 | end 38 | __classDebugIndex=function (instance,key) 39 | local cls=instance.__class 40 | if cls.__decl[key] then 41 | local declInfo=cls.__decl[key] 42 | if declInfo=="publicFunction" 43 | or declInfo=="publicStaticFunction" then 44 | return cls.__function[key] 45 | elseif declInfo=="default" 46 | or declInfo=="publicStatic" then 47 | return cls[key] 48 | elseif declInfo=="publicConst" then 49 | return instance._protected_[key] 50 | elseif declInfo=="property" then 51 | local property=cls.__property[key] 52 | local read=property.readName 53 | if read then 54 | instance=cls.__super and instance.__instance or instance 55 | local getter=instance[read] 56 | return getter(instance) 57 | else 58 | error("error: attempt to access "..cls.__cname.."::"..key.."'s getter ,it is inaccessible ") 59 | end 60 | elseif instance._inFunction or cls.__inStatic then 61 | if declInfo=="protectedFunction" 62 | or declInfo=="protectedStaticFunction" then 63 | return cls.__function[key] 64 | elseif declInfo=="protectedStatic" then 65 | return cls[key] 66 | else 67 | return instance._protected_[key] 68 | end 69 | else 70 | error("error :attempt access protected member "..cls.__cname.."::"..key) 71 | end 72 | elseif cls.__super then 73 | return nil 74 | else 75 | 76 | error("error: attempt to access undeclare member "..cls.__cname.."::"..key) 77 | end 78 | end 79 | 80 | __classDebugNewIndex=function(instance,key,value) 81 | local cls=instance.__class 82 | if cls.__decl[key] then 83 | local declInfo=cls.__decl[key] 84 | if declInfo=="publicMember" then 85 | classRawSet(instance,key,value) 86 | elseif declInfo=="publicStatic" then 87 | rawset(cls,key,value) 88 | elseif declInfo=="publicConst" then 89 | if instance._protected_[key]~=nil then 90 | error("error:attempt to assign to member "..cls.__cname.."::"..key.." ,it is Const") 91 | else 92 | instance._protected_[key]=value 93 | end 94 | elseif declInfo=="property" then 95 | local property=cls.__property[key] 96 | if property.setName then 97 | instance=cls.__super and instance.__instance or instance 98 | local setter=instance[property.setName] 99 | setter(instance,value) 100 | else 101 | error("error :attempt to access "..cls.__cname.."::"..key.."'s setter ,it is inaccessible") 102 | end 103 | elseif instance._inFunction or cls.__inStatic then 104 | if declInfo=="protectedMember" then 105 | instance._protected_[key]=value 106 | elseif declInfo=="protectedStatic" then 107 | rawset(cls,key,value) 108 | elseif declInfo=="protectedConst" then 109 | if instance._protected_[key]~=nil then 110 | error("error:attempt to assign to member "..cls.__cname.."::"..key.." ,it is Const") 111 | else 112 | instance._protected_[key]=value 113 | end 114 | else 115 | error("error:attempt to assign to immutable member "..cls.__cname.."::"..key) 116 | end 117 | else 118 | error("error:attempt to assign to member "..cls.__cname.."::"..key.." ,it is protected") 119 | end 120 | else 121 | error("error: attempt to assign undeclare member "..cls.__cname.."::"..key) 122 | end 123 | 124 | end 125 | 126 | __metaNewIndex=function(cls,key,value) 127 | local funcImp=cls.__function[key] 128 | if funcImp then 129 | cls.__function[key]=LFUNCTION_IMPLEMENT:new(funcImp.key,value,funcImp.accessKey) 130 | else 131 | error("error: attempt define undeclare function") 132 | end 133 | end 134 | 135 | __ctor=function(cls,...) 136 | local instance={__class=cls} 137 | setmetatable(instance,cls) 138 | local ctor=instance[cls.__cname] 139 | for key,value in pairs(cls.__public_member) do 140 | classRawSet(instance,key,value) 141 | end 142 | local protected=instance._protected_ 143 | for key,value in pairs(cls.__protected_member) do 144 | classRawSet(instance,key,value) 145 | end 146 | for key,_ in pairs(cls.__signals) do 147 | classRawSet(instance,key,LSIGNAL_IMPLEMENT:new()) 148 | end 149 | ctor(instance,...) 150 | instance=classRawGet(instance,"__instance") or instance 151 | return instance 152 | end 153 | __classIndex=function (instance,key) 154 | local cls=instance.__class 155 | local declInfo=cls.__decl[key] 156 | if declInfo=="property" then 157 | local property=cls.__property[key] 158 | local read=property.readName 159 | instance=cls.__super and instance.__instance or instance 160 | local getter=instance[read] 161 | return getter(instance) 162 | else 163 | return cls[key] 164 | end 165 | end 166 | 167 | __classNewIndex=function(instance,key,value) 168 | local cls=instance.__class 169 | local declInfo=cls.__decl[key] 170 | if declInfo=="property" then 171 | local property=cls.__property[key] 172 | instance=cls.__super and instance.__instance or instance 173 | local setter=instance[property.setName] 174 | setter(instance,value) 175 | elseif declInfo=="publicMember" 176 | or declInfo=="publicConst" 177 | or declInfo=="protectedConst" 178 | or declInfo=="protectedMember" 179 | or declInfo==nil then 180 | classRawSet(instance,key,value) 181 | else 182 | cls[key]=value 183 | end 184 | end 185 | 186 | 187 | function __class(className,classTable,namespaceTable,namespaceName) 188 | local cls={__cname=className,__nsName=namespaceName} 189 | cls.__public_member={} 190 | 191 | cls.__protected_member={} 192 | cls.__function={} 193 | cls.__inStatic=false 194 | cls.__signals={} 195 | cls.__metaMethod={} 196 | cls.__debug=LUA_DEBUG 197 | cls.__autoInherit=true 198 | cls.__property={} 199 | cls.__supers={} 200 | cls.__inherits={} 201 | cls.__decl={ 202 | __cname="default", 203 | __nsName="default", 204 | __supers="default", 205 | __super="default", 206 | __inherits="default", 207 | __signals="default", 208 | __debug="default", 209 | __instance="publicMember" 210 | } 211 | 212 | for _,declType in pairs(classTable) do 213 | declType:implement(cls) 214 | end 215 | if cls.__autoInherit and cls.__inherits["LObject"]==nil then 216 | super(LObject):implement(cls,false) 217 | end 218 | cls.__decl.new=cls.__decl[className] 219 | cls.__decl.create=cls.__decl[className] 220 | 221 | local ctor= cls.__debug and __debugCtor or __ctor 222 | 223 | cls.new=ctor 224 | cls.create=ctor 225 | local meta={__call=ctor} 226 | if cls.__debug then 227 | cls.__index=__classDebugIndex 228 | cls.__newindex=__classDebugNewIndex 229 | meta.__newindex=__metaNewIndex 230 | else 231 | cls.__index=__classIndex 232 | cls.__newindex=__classNewIndex 233 | end 234 | 235 | if cls.__debug==false then 236 | local hasProperty=false 237 | for key,value in pairs(cls.__decl) do 238 | if value=="property" then 239 | hasProperty=true 240 | break; 241 | end 242 | end 243 | if hasProperty==false then 244 | cls.__index=cls 245 | end 246 | end 247 | setmetatable(cls,meta) 248 | namespaceTable[className]=cls 249 | return cls 250 | end 251 | 252 | 253 | 254 | function class (className,namespaceTable,namespaceName) 255 | 256 | ---@param classTable table 257 | return function(classTable) 258 | return __class(className,classTable,namespaceTable,namespaceName) 259 | end 260 | end 261 | 262 | namespace_register("class",class) 263 | 264 | 265 | 266 | -------------------------------------------------------------------------------- /luaClass/class/config/classConfig.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright(c) 2019 CppCXY 3 | Author: CppCXY 4 | Github: https://github.com/CppCXY/luaClass 5 | ]] 6 | include "luaClass.class.namespace" 7 | _ENV=namespace "luaClass" 8 | 9 | --this variable will open the all check.if all test finish,please set LUA_DEBUG=false 10 | --it will be 5 times better performance 11 | LUA_DEBUG=(DEBUG~=1) 12 | --this variable will open hostInherit 13 | CXX_TO_LUA_HOST_ENV=true 14 | 15 | --If you have a host language and attempt to inherit the host type 16 | --modify the following functions, but be compatible with Lua native classes 17 | if CXX_TO_LUA_HOST_ENV then 18 | --this for tolua++ 19 | --If it's another way of luabind, modify it yourself 20 | function isHostClass (classObject) 21 | return type(classObject)=="function" or classObject[".isclass"] 22 | end 23 | 24 | function __hostCreate(self,...) 25 | local instance 26 | local super=self.__supers["__hostClass"] 27 | if type(super)=="function" then 28 | instance=super(...) 29 | else 30 | instance=super:create(...) 31 | end 32 | tolua.setpeer(instance,self) 33 | self.__instance=instance 34 | return instance 35 | end 36 | 37 | function hostInherit(classObject,superClass) 38 | classObject.__supers["__hostClass"]=superClass 39 | classObject.__super=__hostCreate 40 | end 41 | 42 | --This uses a new function because it cannot use rawset for UserData 43 | function classRawSet (classObject,key,value) 44 | return (type(classObject)=="userdata") 45 | and 46 | rawset(tolua.getpeer(classObject),key,value) 47 | or 48 | rawset(classObject,key,value) 49 | end 50 | --This uses a new function because it cannot use rawget for UserData 51 | function classRawGet(classObject,key) 52 | return ( type(classObject)=="userdata") 53 | and 54 | rawget(tolua.getpeer(classObject),key) 55 | or 56 | rawget(classObject,key) 57 | end 58 | 59 | 60 | else 61 | function isHostClass(classObject) return false end 62 | function hostInherit() return end 63 | classRawGet=rawget 64 | classRawSet=rawset 65 | end 66 | 67 | 68 | -------------------------------------------------------------------------------- /luaClass/class/config/init.lua: -------------------------------------------------------------------------------- 1 | include "luaClass.class.config.classConfig" -------------------------------------------------------------------------------- /luaClass/class/helper/debug.lua: -------------------------------------------------------------------------------- 1 | include "luaClass.class.namespace" 2 | include "luaClass.class.config.init" 3 | _ENV=namespace "luaClass" 4 | 5 | LDEBUG={} 6 | LDEBUG.__index=LDEBUG 7 | 8 | DEBUG_INSTANCE={__ctype==true} 9 | setmetatable(DEBUG_INSTANCE,LDEBUG) 10 | 11 | NO_DEBUG_INSTANCE={__ctype==false} 12 | setmetatable(NO_DEBUG_INSTANCE,LDEBUG) 13 | 14 | function LDEBUG:implement(classObject) 15 | classObject.__debug=self.__ctype 16 | end 17 | 18 | function CLASS_DEBUG(bool) 19 | return bool and DEBUG_INSTANCE or NO_DEBUG_INSTANCE 20 | end 21 | 22 | 23 | -------------------------------------------------------------------------------- /luaClass/class/helper/funcHelper.lua: -------------------------------------------------------------------------------- 1 | include "luaClass.class.namespace" 2 | 3 | _ENV=namespace "luaClass" 4 | 5 | function is(self,classObject) 6 | local ctype=type(classObject) 7 | local stype=type(self) 8 | if ctype~="table" then return stype==ctype end 9 | local tname=classObject.__cname 10 | if (stype=="table" or stype=="userdata") and self.__class then 11 | return self.__class==classObject or self:inherit(classObject) 12 | end 13 | return false 14 | end 15 | 16 | function connect(source,signalName,target,slotName) 17 | assert(signalName~=nil,"error : param signalName is nil") 18 | assert(slotName~=nil,"error : param slotName is nil") 19 | if type(slotName)=="string" then 20 | assert(target[slotName]~=nil,"error : slot function "..slotName.." not exit") 21 | end 22 | if type(signalName)=="string" then 23 | assert(source[signalName]~=nil,"error : signal "..signalName.." not exit") 24 | end 25 | source[signalName].slots[slotName]=target 26 | end 27 | 28 | function inheritInstance(self,classInstance) 29 | local metatable=getmetatable(self) 30 | if metatable then 31 | local __index=metatable.__index 32 | if __index then 33 | if type(__index)=="table" then 34 | metatable.__index=function (self,key) 35 | local result=__index[key] 36 | if result~=nil then return result end 37 | return classInstance[key] 38 | end 39 | elseif type(__index)=="function" then 40 | metatable.__index=function(self,key) 41 | local result=__index(self,key) 42 | if result~=nil then return result end 43 | return classInstance[key] 44 | end 45 | else 46 | error("error: wrong code __index must be table or function") 47 | end 48 | else 49 | metatable.__index=classInstance 50 | end 51 | else 52 | setmetatable(self,{__index=classInstance}) 53 | end 54 | end -------------------------------------------------------------------------------- /luaClass/class/helper/init.lua: -------------------------------------------------------------------------------- 1 | 2 | include "luaClass.class.config.init" 3 | include "luaClass.class.helper.member" 4 | include "luaClass.class.helper.public" 5 | include "luaClass.class.helper.protected" 6 | include "luaClass.class.helper.property" 7 | include "luaClass.class.helper.super" 8 | include "luaClass.class.helper.funcHelper" 9 | include "luaClass.class.helper.serilize" 10 | include "luaClass.class.helper.debug" 11 | include "luaClass.class.helper.signalSlot" -------------------------------------------------------------------------------- /luaClass/class/helper/member.lua: -------------------------------------------------------------------------------- 1 | 2 | include "luaClass.class.namespace" 3 | include "luaClass.class.config.init" 4 | _ENV=namespace "luaClass" 5 | local unpack=unpack or table.unpack 6 | LMEMBER={} 7 | LMEMBER.__index=function (self,key) 8 | return LMEMBER_INSTANCE:new(key,self.__ctype) 9 | end 10 | 11 | MEMBER={__ctype="member"} 12 | setmetatable(MEMBER,LMEMBER) 13 | 14 | STATIC={__ctype="static"} 15 | setmetatable(STATIC,LMEMBER) 16 | 17 | STATICFUNC={__ctype="staticFunction"} 18 | setmetatable(STATICFUNC,LMEMBER) 19 | 20 | FUNCTION={__ctype="function"} 21 | setmetatable(FUNCTION,LMEMBER) 22 | 23 | CONST={__ctype="const"} 24 | setmetatable(CONST,LMEMBER) 25 | 26 | META={__ctype="meta"} 27 | setmetatable(META,LMEMBER) 28 | 29 | 30 | LMEMBER_INSTANCE={} 31 | LMEMBER_INSTANCE.__index=LMEMBER_INSTANCE 32 | function LMEMBER_INSTANCE:new(key,ctype) 33 | local object={ 34 | key=key; 35 | ctype=ctype; 36 | } 37 | setmetatable(object,self) 38 | return object 39 | end 40 | 41 | function LMEMBER_INSTANCE:__call(value) 42 | self.object=value 43 | return self 44 | end 45 | 46 | function LMEMBER_INSTANCE:check(debug) 47 | if debug==false then return end 48 | if self.ctype=="function" then 49 | self.object=LFUNCTION_IMPLEMENT:new(self.key,self.object,"_inFunction") 50 | elseif self.ctype=="staticFunction" then 51 | self.object=LFUNCTION_IMPLEMENT:new(self.key,self.object,"_inStatic") 52 | elseif self.ctype=="meta" then 53 | if self.key=="__index" or self.key=="__newindex" then 54 | error("error :meta method __index or __newindex can't be declared") 55 | end 56 | end 57 | end 58 | 59 | LFUNCTION_IMPLEMENT={} 60 | function LFUNCTION_IMPLEMENT:new(key,functionObject,accessKey) 61 | local object={ 62 | object=functionObject; 63 | key=key; 64 | accessKey=accessKey; 65 | } 66 | setmetatable(object,self) 67 | return object 68 | end 69 | 70 | function LFUNCTION_IMPLEMENT:__call(instance,...) 71 | assert(instance~=nil,"instance is nil maybe you should use ':' instead of '.' to call method") 72 | assert(self.object~=nil,"method ",instance.__cname,"::",self.key,"don't implement") 73 | local accessKey=self.accessKey 74 | local old=instance[accessKey] 75 | instance[accessKey]=true 76 | local result={self.object(instance,...)} 77 | instance[accessKey]=old 78 | return unpack(result) 79 | 80 | end -------------------------------------------------------------------------------- /luaClass/class/helper/property.lua: -------------------------------------------------------------------------------- 1 | include "luaClass.class.namespace" 2 | _ENV=namespace "luaClass" 3 | 4 | 5 | LProperty={} 6 | LProperty.__index=LProperty 7 | function LProperty:new(propertyName,readName,setName) 8 | local object={ 9 | propertyName=propertyName; 10 | readName=readName; 11 | setName=setName; 12 | } 13 | setmetatable(object,LProperty) 14 | return object 15 | end 16 | function LProperty:implement(classObject) 17 | local __property=classObject.__property 18 | classObject.__decl[self.propertyName]="property" 19 | __property[self.propertyName]={readName=self.readName,setName=self.setName} 20 | end 21 | function __property(propertyName,propertyTable) 22 | local read,write; 23 | for _,declType in pairs(propertyTable) do 24 | if declType.read then 25 | read=declType.read 26 | elseif declType.write then 27 | write=declType.write 28 | end 29 | end 30 | return LProperty:new(propertyName,read,write) 31 | end 32 | 33 | function property(propertyName) 34 | return function (propertyTable) 35 | return __property(propertyName,propertyTable) 36 | end 37 | end 38 | 39 | LREAD={} 40 | LREAD.__index=function(self,key) 41 | return {read=key} 42 | end 43 | READ={} 44 | setmetatable(READ,LREAD) 45 | 46 | LWRITE={} 47 | LWRITE.__index=function(self,key) 48 | return {write=key} 49 | end 50 | WRITE={} 51 | setmetatable(WRITE,LWRITE) -------------------------------------------------------------------------------- /luaClass/class/helper/protected.lua: -------------------------------------------------------------------------------- 1 | include "luaClass.class.namespace" 2 | include "luaClass.class.config.init" 3 | _ENV=namespace "luaClass" 4 | 5 | LProtected={} 6 | LProtected.__index=LProtected 7 | function LProtected:new(memberTable) 8 | local object={ 9 | members=memberTable; 10 | } 11 | setmetatable(object,self) 12 | return object 13 | 14 | end 15 | function LProtected:implement(classObject) 16 | local members=classObject.__protected_member 17 | local decl=classObject.__decl 18 | local funcTable=classObject.__function 19 | if classObject.__debug then 20 | for _,member in pairs(self.members) do 21 | member:check(true) 22 | if member.ctype=="function" then 23 | decl[member.key]="protectedFunction" 24 | funcTable[member.key]=member.object 25 | elseif member.ctype=="member" then 26 | decl[member.key]="protectedMember" 27 | members[member.key]=member.object 28 | elseif member.ctype=="static" then 29 | decl[member.key]="protectedStatic" 30 | classObject[member.key]=member.object 31 | elseif member.ctype=="staticFunction" then 32 | decl[member.key]="protectedStaticFunction" 33 | funcTable[member.key]=member.object 34 | elseif member.ctype=="const" then 35 | decl[member.key]="protectedConst" 36 | members[member.key]=member.object 37 | end 38 | end 39 | else 40 | for _,member in pairs(self.members) do 41 | member:check(false) 42 | if member.ctype=="function" then 43 | decl[member.key]="protectedFunction" 44 | classObject[member.key]=member.object 45 | elseif member.ctype=="member" then 46 | decl[member.key]="protectedMember" 47 | members[member.key]=member.object 48 | elseif member.ctype=="static" then 49 | decl[member.key]="protectedStatic" 50 | classObject[member.key]=member.object 51 | elseif member.ctype=="staticFunction" then 52 | decl[member.key]="protectedStaticFunction" 53 | classObject[member.key]=member.object 54 | elseif member.ctype=="const" then 55 | decl[member.key]="protectedConst" 56 | members[member.key]=member.object 57 | end 58 | end 59 | end 60 | end 61 | 62 | function protected(memberTable) 63 | return LProtected:new(memberTable) 64 | end 65 | 66 | -------------------------------------------------------------------------------- /luaClass/class/helper/public.lua: -------------------------------------------------------------------------------- 1 | include "luaClass.class.namespace" 2 | include "luaClass.class.config.init" 3 | _ENV=namespace "luaClass" 4 | 5 | LPublic={} 6 | LPublic.__index=LPublic 7 | function LPublic:new(memberTable) 8 | local object={ 9 | members=memberTable; 10 | } 11 | setmetatable(object,self) 12 | return object 13 | 14 | end 15 | 16 | 17 | 18 | function LPublic:implement(classObject) 19 | local members=classObject.__public_member 20 | local protectedMembers=classObject.__protected_member 21 | local decl=classObject.__decl 22 | local funcTable=classObject.__function 23 | local meta=classObject.__metaMethod 24 | if classObject.__debug then 25 | for _,member in pairs(self.members) do 26 | member:check(true) 27 | if member.ctype=="function" then 28 | decl[member.key]="publicFunction" 29 | funcTable[member.key]=member.object 30 | elseif member.ctype=="member" then 31 | decl[member.key]="publicMember" 32 | members[member.key]=member.object 33 | elseif member.ctype=="static" then 34 | decl[member.key]="publicStatic" 35 | classObject[member.key]=member.object 36 | elseif member.ctype=="const" then 37 | decl[member.key]="publicConst" 38 | protectedMembers[member.key]=member.object 39 | elseif member.ctype=="staticFunction" then 40 | decl[member.key]="publicStaticFunction" 41 | funcTable[member.key]=member.object 42 | elseif member.ctype=="meta" then 43 | meta[member.key]=member.object 44 | classObject[member.key]=member.object 45 | end 46 | end 47 | else 48 | for _,member in pairs(self.members) do 49 | member:check(false) 50 | if member.ctype=="function" then 51 | decl[member.key]="publicFunction" 52 | classObject[member.key]=member.object 53 | elseif member.ctype=="member" then 54 | decl[member.key]="publicMember" 55 | members[member.key]=member.object 56 | elseif member.ctype=="static" then 57 | decl[member.key]="publicStatic" 58 | classObject[member.key]=member.object 59 | elseif member.ctype=="const" then 60 | decl[member.key]="publicConst" 61 | members[member.key]=member.object 62 | elseif member.ctype=="staticFunction" then 63 | decl[member.key]="publicStaticFunction" 64 | classObject[member.key]=member.object 65 | elseif member.ctype=="meta" then 66 | meta[member.key]=member.object 67 | classObject[member.key]=member.object 68 | end 69 | end 70 | 71 | end 72 | end 73 | 74 | function public(memberTable) 75 | return LPublic:new(memberTable) 76 | end 77 | 78 | -------------------------------------------------------------------------------- /luaClass/class/helper/serilize.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright(c) 2019 CppCXY 3 | Author: CppCXY 4 | Github: https://github.com/CppCXY 5 | 如果觉得我的作品不错,可以去github给我的项目打星星. 6 | 序列化方案 7 | 针对我自己的编码规则进行特定的序列化和反序列化方式 8 | 默认屏蔽一切双下划线开头的字段 9 | 这部分内容与luaClass没有太多耦合, 10 | 如果想分离出来,需要将include 部分删掉,_ENV删掉,initSerilize不使用 11 | ]] 12 | include "luaClass.class.namespace" 13 | _ENV=namespace "luaClass" 14 | 15 | --这些key并不参与序列化 16 | local disableKey={ 17 | ["class"]=true,--实际上我并没有这个字段 18 | ["super"]=true, 19 | } 20 | 21 | local serilizeTable={} 22 | local serilizeClassTable={} 23 | local luaIDQuery={} 24 | local waitTable={} 25 | local id=0 26 | local getId=function () 27 | id=id+1 28 | return id 29 | end 30 | local reset=function () 31 | id=0 32 | luaIDQuery={} 33 | waitTable={} 34 | end 35 | 36 | 37 | local type=type 38 | local getmetatable=getmetatable 39 | local string_format=string.format 40 | local table_concat=table.concat 41 | local table_insert=table.insert 42 | local getType=function ( value ) 43 | local t=type(value) 44 | if t=="table" then 45 | 46 | if not getmetatable(value) then return "unMetaTable" end 47 | if getmetatable(value) then return "class" end 48 | return "nil" 49 | elseif t=="string" then 50 | if value:find("\n") then return "blockString" end 51 | if disableKey[value] then return "nil" end 52 | if value:match("^__") then return "nil" end 53 | return t 54 | elseif t=="number" or t=="boolean" or t=="function" then 55 | return t 56 | end 57 | return "nil" 58 | end 59 | 60 | local arraySerilize=function(array) 61 | local t={} 62 | for i=1,#array do 63 | local value=array[i] 64 | t[i]=serilizeTable[getType(value)](value) 65 | end 66 | local str="{"..table_concat(t,",").."}" 67 | return str 68 | end 69 | serilizeTable["array"]=arraySerilize 70 | 71 | local numberSerilize=tostring 72 | 73 | serilizeTable["number"]=numberSerilize 74 | 75 | local stringSerilize=function ( str ) 76 | return "\""..str.."\"" 77 | end 78 | serilizeTable["string"]=stringSerilize 79 | local blockStringSerilize=function ( str ) 80 | return "[["..str.."]]" 81 | end 82 | serilizeTable["blockString"]=blockStringSerilize 83 | local booleanSerilize=function ( bool ) 84 | return bool and "true" or "false" 85 | end 86 | serilizeTable["boolean"]=booleanSerilize 87 | local nilSerilize=function ( value ) 88 | return "\"\"" 89 | end 90 | serilizeTable["nil"]=nilSerilize 91 | local functionSerilize=function (value) 92 | local text={} 93 | local size=1 94 | for char in string.gmatch(string.dump(value),".") do 95 | text[size]= "\\"..char:byte(1) 96 | size=size+1 97 | end 98 | return "\"".."function"..table.concat(text).."\"" 99 | end 100 | serilizeTable["function"]=functionSerilize 101 | local unMetaTableSerilize=function ( tb) 102 | local t={} 103 | for k,v in pairs(tb) do 104 | local kType=getType(k) 105 | if kType~="nil" then 106 | table_insert(t, 107 | string_format("[%s]=%s", 108 | serilizeTable[kType](k),serilizeTable[getType(v)](v) 109 | )) 110 | end 111 | end 112 | return "{"..table_concat(t,",").."}" 113 | end 114 | serilizeTable["unMetaTable"]=unMetaTableSerilize 115 | 116 | --以下专门针对用class 声明得到的类型 117 | --并不处理元表 118 | local classSerilize=function (classValue) 119 | local t={} 120 | if luaIDQuery[classValue]==nil then 121 | local id=getId() 122 | luaIDQuery[classValue]=id 123 | else 124 | return ("\"luaID"..luaIDQuery[classValue].."\"") 125 | end 126 | table_insert(t,"__luaID="..id) 127 | for key,value in pairs(classValue) do 128 | local kType=getType(key) 129 | local vType=getType(value) 130 | if kType~="nil" and vType~="nil" then 131 | table_insert(t, 132 | "[".. 133 | ( 134 | luaIDQuery[key] and 135 | ("\"luaID"..luaIDQuery[key].."\"") 136 | or 137 | serilizeTable[kType](key) 138 | ) 139 | .."]=".. 140 | ( 141 | luaIDQuery[value] and 142 | ("\"luaID"..luaIDQuery[value].."\"") 143 | or 144 | serilizeTable[vType](value) 145 | ) 146 | ) 147 | end 148 | 149 | end 150 | 151 | table_insert(t, 152 | "__class="..classValue.__nsName.."."..classValue.__cname 153 | ) 154 | return "{"..table_concat(t,",").."}" 155 | end 156 | serilizeTable["class"]=classSerilize 157 | 158 | local __serilizeAux 159 | __serilizeAux=function (object) 160 | local objectType=type(object) 161 | if objectType=="table" then 162 | if object.__luaID then 163 | luaIDQuery[object.__luaID]=object 164 | local wt=waitTable[object.__luaID] 165 | if wt then 166 | for _,wts in pairs(wt) do 167 | local obj=wts.obj 168 | if wts.keys.oldKey then 169 | local newKey=object 170 | obj[newKey]=obj[wts.keys.oldKey] 171 | obj[wts.keys.oldKey]=nil 172 | elseif wts.keys.key then 173 | obj[wts.keys.key]=object 174 | end 175 | end 176 | waitTable[object.__luaID]=nil 177 | end 178 | end 179 | local keyAlterTable={} 180 | local valueAlterTable={} 181 | for key,value in pairs(object) do 182 | local keyType=type(key) 183 | if keyType=="table" then 184 | __serilizeAux(key) 185 | elseif keyType=="string" then 186 | local mc=key:match("^luaID(%d+)") 187 | if mc then 188 | table_insert( 189 | keyAlterTable, 190 | {oldKey=key,mc=tonumber(mc)} 191 | ) 192 | end 193 | end 194 | 195 | local valueType=type(value) 196 | if valueType=="string" then 197 | local mc=value:match("^luaID(%d+)") 198 | if mc then 199 | table_insert(valueAlterTable, 200 | {key=key,mc=tonumber(mc)} 201 | ) 202 | end 203 | mc=value:match("^function(.+)") 204 | if mc then 205 | object[key]=load(mc) 206 | end 207 | elseif key~="__class" and valueType=="table" then 208 | __serilizeAux(value) 209 | end 210 | end 211 | for _,keys in pairs(keyAlterTable) do 212 | local newKey=luaIDQuery[keys.mc] 213 | if newKey then 214 | object[newKey]=object[keys.oldKey] 215 | object[keys.oldKey]=nil 216 | else 217 | local wt=waitTable[keys.mc] or {} 218 | table_insert(wt, 219 | {obj=object,keys=keys} 220 | ) 221 | waitTable[keys.mc]=wt 222 | end 223 | end 224 | for _,keys in pairs(valueAlterTable) do 225 | local newValue=luaIDQuery[keys.mc] 226 | if newValue then 227 | object[keys.key]=newValue 228 | else 229 | local wt=waitTable[keys.mc] or {} 230 | table_insert(wt, 231 | {obj=object,keys=keys} 232 | ) 233 | waitTable[keys.mc]=wt 234 | end 235 | end 236 | if object.__class then 237 | --object.__class=serilizeClassTable[object.__createClass] 238 | setmetatable(object,object.__class) 239 | end 240 | end 241 | end 242 | 243 | function serilize(object) 244 | local str=serilizeTable[getType(object)](object) 245 | reset() 246 | return str 247 | end 248 | 249 | function unSerilize(str,isAdd) 250 | if isAdd==nil then 251 | isAdd=true 252 | end 253 | local g 254 | if isAdd then 255 | local result=str:find("return") 256 | g=load(result and str or ("return "..str)) 257 | else 258 | g=load(str) 259 | end 260 | local t=g() 261 | __serilizeAux(t) 262 | reset() 263 | 264 | return t 265 | end 266 | 267 | 268 | -------------------------------------------------------------------------------- /luaClass/class/helper/signalSlot.lua: -------------------------------------------------------------------------------- 1 | include "luaClass.class.namespace" 2 | _ENV=namespace "luaClass" 3 | 4 | 5 | LSINGAL={} 6 | LSINGAL.__index=function(self,key) 7 | --donot check the param ,It's not necessary 8 | return function(...) 9 | return LSINGAL_INSTANCE:new(key) 10 | end 11 | end 12 | SINGAL={} 13 | setmetatable(SINGAL,LSINGAL) 14 | 15 | LSINGAL_INSTANCE={} 16 | LSINGAL_INSTANCE.__index=LSINGAL_INSTANCE 17 | 18 | function LSINGAL_INSTANCE:new(key) 19 | local obj={ 20 | key=key 21 | } 22 | setmetatable(obj,self) 23 | return obj 24 | end 25 | 26 | function LSINGAL_INSTANCE:implement(classObject) 27 | classObject.__decl[self.key]="publicConst" 28 | classObject.__signals[self.key]=true 29 | end 30 | 31 | LSIGNAL_IMPLEMENT={__cname="LSIGNAL_IMPLEMENT"} 32 | LSIGNAL_IMPLEMENT.__index=LSIGNAL_IMPLEMENT 33 | LSIGNAL_IMPLEMENT.__nsName="luaClass" 34 | LSIGNAL_IMPLEMENT.__call=function(self,source,...) 35 | for slotName,target in pairs(self.slots) do 36 | 37 | if slotName~="__class" then 38 | local ctype=type(slotName) 39 | if ctype=="string" then 40 | target[slotName](target,...) 41 | elseif ctype=="function" then 42 | slotName(...) 43 | 44 | end 45 | end 46 | end 47 | end 48 | function LSIGNAL_IMPLEMENT:new() 49 | local obj={ 50 | slots=LSLOTS:new(), 51 | __class=self 52 | } 53 | setmetatable(obj,self) 54 | return obj 55 | end 56 | --clear all connect 57 | function LSIGNAL_IMPLEMENT:clear() 58 | 59 | self.slots=LSLOTS:new() 60 | 61 | end 62 | 63 | LSLOTS={__cname="LSLOT"} 64 | LSLOTS.__nsName="luaClass" 65 | LSLOTS.__index=LSLOTS 66 | LSLOTS.__mode="v" 67 | 68 | function LSLOTS:new() 69 | local obj={ 70 | __class=self 71 | } 72 | setmetatable(obj,self) 73 | return obj 74 | end 75 | -------------------------------------------------------------------------------- /luaClass/class/helper/super.lua: -------------------------------------------------------------------------------- 1 | 2 | include "luaClass.class.namespace" 3 | _ENV=namespace "luaClass" 4 | 5 | 6 | LSuper={} 7 | LSuper.__index=LSuper 8 | local unpack=unpack or table.unpack 9 | function LSuper:new(...) 10 | local object={ 11 | supers={...} 12 | } 13 | setmetatable(object,self) 14 | return object 15 | end 16 | 17 | function LSuper:implement(classObject,isCover) 18 | if isCover==nil then 19 | isCover=true 20 | end 21 | 22 | for _,cobject in pairs(self.supers) do 23 | self:inherit(classObject,cobject,isCover) 24 | end 25 | end 26 | __mergeTable=function (target,source,isCover) 27 | if isCover then 28 | for key,value in pairs(source) do 29 | target[key]=value 30 | end 31 | else 32 | for key,value in pairs(source) do 33 | if target[key]==nil then 34 | 35 | target[key]=value 36 | end 37 | end 38 | 39 | end 40 | end 41 | 42 | 43 | function LSuper:inherit(classObject,superClass,isCover) 44 | local cls=classObject 45 | 46 | if isHostClass(superClass) then return hostInherit(classObject,superClass) end 47 | cls.__supers[superClass.__cname]=superClass 48 | cls.__inherits[superClass.__cname]=true 49 | __mergeTable(cls.__public_member,superClass.__public_member,isCover) 50 | __mergeTable(cls.__protected_member,superClass.__protected_member,isCover) 51 | __mergeTable(cls.__signals,superClass.__signals,isCover) 52 | __mergeTable(cls.__property,superClass.__property,isCover) 53 | __mergeTable(cls.__decl,superClass.__decl,isCover) 54 | __mergeTable(cls.__metaMethod,superClass.__metaMethod,isCover) 55 | __mergeTable(cls,superClass.__metaMethod,isCover) 56 | __mergeTable(cls.__inherits,superClass.__inherits,isCover) 57 | __mergeTable(cls.__supers,superClass.__supers,isCover) 58 | __mergeTable(cls.__function,superClass.__function,isCover) 59 | if superClass.__super then 60 | cls.__super=superClass.__super 61 | end 62 | if isCover then 63 | for key ,value in pairs(superClass) do 64 | local mc=key:match("^__") 65 | if mc==nil then 66 | cls[key]=value 67 | end 68 | end 69 | else 70 | for key ,value in pairs(superClass) do 71 | local mc=key:match("^__") 72 | if mc==nil and cls[key]==nil then 73 | cls[key]=value 74 | end 75 | end 76 | end 77 | end 78 | 79 | function super(...) 80 | return LSuper:new(...) 81 | end 82 | 83 | LNOOBJECT={} 84 | LNOOBJECT.__index=LNOOBJECT 85 | function LNOOBJECT:implement(classObject) 86 | classObject.__autoInherit=false 87 | end 88 | NOOBJECT={} 89 | setmetatable(NOOBJECT,LNOOBJECT) 90 | function NO_AUTO_INHERIT () 91 | return NOOBJECT 92 | end -------------------------------------------------------------------------------- /luaClass/class/init.lua: -------------------------------------------------------------------------------- 1 | require "luaClass.include" 2 | include "luaClass.class.config.init" 3 | include "luaClass.class.namespace" 4 | include "luaClass.class.class" 5 | include "luaClass.class.object.init" -------------------------------------------------------------------------------- /luaClass/class/namespace.lua: -------------------------------------------------------------------------------- 1 | 2 | local string_gsub=string.gsub 3 | local function split(str,sep) 4 | local rt= {} 5 | local size=1 6 | string_gsub(str, '[^'..sep..']+', function(w) rt[size]=w size=size+1 end ) 7 | return rt 8 | end 9 | local namespace_function_table={} 10 | local using_namespace 11 | local function namespace(nsName) 12 | nsName=nsName or "_G" 13 | local names=split(nsName,".") 14 | local index=1 15 | local lastNs=_G 16 | while(names[index]~=nil) do 17 | local name=names[index] 18 | if rawget(lastNs,name)==nil then 19 | local ns={} 20 | rawset(lastNs,name,ns) 21 | lastNs=ns 22 | else 23 | lastNs=rawget(lastNs,name) 24 | end 25 | index=index+1 26 | end 27 | local newNs={} 28 | local old=_G 29 | local meta={__ns=lastNs,_G=old,__usingtable={}} 30 | meta.__index=function (self,key ) 31 | --优先保证本地命名空间变量先获取 32 | local res=rawget(meta.__ns,key) 33 | if res~=nil then return res end 34 | --然后保证using过的命名空间获取 35 | for _,using_table in old.pairs(meta.__usingtable) do 36 | --防止无限递归访问 37 | local res=rawget(using_table,key) 38 | if res~=nil then return res end 39 | end 40 | --然后再查找全局变量 41 | return rawget(meta._G,key) 42 | end 43 | meta.__newindex=function(self,k,v) 44 | rawset(meta.__ns,k,v) 45 | end 46 | old.setmetatable(newNs,meta) 47 | newNs.using_namespace=function (nsName) 48 | using_namespace(newNs,nsName) 49 | end 50 | for key,value in pairs(namespace_function_table) do 51 | rawset(newNs,key,function (...) 52 | return value(...,newNs,nsName) 53 | end) 54 | end 55 | if lastNs.__nsName==nil then 56 | --主要用于序列化 57 | lastNs.__nsName=nsName 58 | end 59 | if _VERSION =="Lua 5.1" then 60 | setfenv(2,newNs) 61 | end 62 | return newNs 63 | end 64 | 65 | rawset(_G,"namespace",namespace) 66 | rawset(_G,"__nsName","_G") 67 | 68 | local function namespace_register(name,luaf) 69 | namespace_function_table[name]=luaf 70 | end 71 | rawset(_G,"namespace_register",namespace_register) 72 | using_namespace=function(nsTable,nsName) 73 | local names=split(nsName,".") 74 | local index=1 75 | local __lastNs=_G 76 | while(names[index]~=nil) do 77 | local name=names[index] 78 | if rawget(__lastNs,name)==nil then 79 | local ns={} 80 | rawset(__lastNs,name,ns) 81 | __lastNs=ns 82 | else 83 | __lastNs=rawget(__lastNs,name) 84 | end 85 | index=index+1 86 | end 87 | local meta=getmetatable(nsTable) 88 | local using_table=meta.__usingtable 89 | using_table[nsName]=__lastNs 90 | end 91 | 92 | 93 | -------------------------------------------------------------------------------- /luaClass/class/object/LMetaObject.lua: -------------------------------------------------------------------------------- 1 | include "luaClass.class.namespace" 2 | include "luaClass.class.class" 3 | _ENV=namespace "luaClass" 4 | ---@class LMetaObject 5 | local LMetaObject=class ("LMetaObject"){ 6 | NO_AUTO_INHERIT(); 7 | public{ 8 | FUNCTION.LMetaObject(function(self,classObject) 9 | self.classObject=classObject 10 | end); 11 | --get the public function name list 12 | FUNCTION.getPublicFunctionList(function(self) 13 | local list={} 14 | for key,value in pairs(self.classObject.__decl) do 15 | if value=="publicFunction" then 16 | table.insert(list,key) 17 | end 18 | end 19 | return list 20 | end); 21 | --get the protected function name list 22 | FUNCTION.getProtectedFunctionList(function(self) 23 | local list={} 24 | for key,value in pairs(self.classObject.__decl) do 25 | if value=="protectedFunction" then 26 | table.insert(list,key) 27 | end 28 | end 29 | return list 30 | end); 31 | --get the public member name list 32 | FUNCTION.getPublicMemberList(function(self) 33 | local list={} 34 | for key,value in pairs(self.classObject.__decl) do 35 | if value=="publicMember" then 36 | table.insert(list,key) 37 | end 38 | end 39 | return list 40 | end); 41 | --get the protected member name list 42 | FUNCTION.getProtectedMemberList(function (self) 43 | local list={} 44 | for key,value in pairs(self.classObject.__decl) do 45 | if value=="protectedMember" then 46 | table.insert(list,key) 47 | end 48 | end 49 | return list 50 | end); 51 | --get the public static list 52 | FUNCTION.getPublicStaticList(function (self) 53 | local list={} 54 | for key,value in pairs(self.classObject.__decl) do 55 | if value=="publicStatic" then 56 | table.insert(list,key) 57 | end 58 | end 59 | return list 60 | end); 61 | --get the protected member name list 62 | FUNCTION.getProtectedStaticList(function (self) 63 | local list={} 64 | for key,value in pairs(self.classObject.__decl) do 65 | if value=="protectedStatic" then 66 | table.insert(list,key) 67 | end 68 | end 69 | return list 70 | end); 71 | --get the public const name list 72 | FUNCTION.getPublicConstList(function (self) 73 | local list={} 74 | for key,value in pairs(self.classObject.__decl) do 75 | if value=="publicConst" then 76 | table.insert(list,key) 77 | end 78 | end 79 | return list 80 | end); 81 | --get the protected const name list 82 | FUNCTION.getProtectedConstList(function (self) 83 | local list={} 84 | for key,value in pairs(self.classObject.__decl) do 85 | if value=="protectedConst" then 86 | table.insert(list,key) 87 | end 88 | end 89 | return list 90 | end); 91 | --get the property name list 92 | FUNCTION.getPropertyList(function (self) 93 | local list={} 94 | for key,value in pairs(self.classObject.__decl) do 95 | if value=="property" then 96 | table.insert(list,key) 97 | end 98 | end 99 | return list 100 | end); 101 | --get the metaMethod name list 102 | FUNCTION.getMetaMethodList(function (self) 103 | local list={} 104 | for key,value in pairs(self.classObject.__metaMethod) do 105 | table.insert(list,key) 106 | end 107 | return list 108 | end); 109 | FUNCTION.getStaticFunctionList(function(self) 110 | local list={} 111 | for key,value in pairs(self.classObject.__decl) do 112 | if value=="publicStaticFunction" then 113 | table.insert(list,key) 114 | end 115 | end 116 | return list 117 | 118 | end) 119 | 120 | 121 | }; 122 | protected{ 123 | MEMBER.classObject(); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /luaClass/class/object/LObject.lua: -------------------------------------------------------------------------------- 1 | include "luaClass.class.namespace" 2 | include "luaClass.class.class" 3 | include "luaClass.class.object.LMetaObject" 4 | _ENV=namespace "luaClass" 5 | 6 | ---@class LObject 7 | ---@field public getType fun(self:LObject):classObject 8 | ---@field public getClassName fun(self:LObject):string 9 | ---@field public getNamespace fun(self:LObject):string 10 | ---@field public getMetaObject fun(self:LObject):LMetaObject 11 | ---@field public getSupers fun(self:LObject,name:string):LObject 12 | ---@field public inherit fun(self:LObject,classObject:ClassObject):boolean 13 | ---@field public is fun(self:LObject,classObject:ClassObject):boolean 14 | ---@field public serilize fun(self:LObject):string 15 | ---@field public toString fun(self:LObject):string 16 | local LObject=class ("LObject"){ 17 | NO_AUTO_INHERIT(); 18 | public{ 19 | FUNCTION.LObject(function(self) 20 | end); 21 | --get the classObject 22 | FUNCTION.getType(function(self) 23 | return self.__class 24 | end); 25 | --get object class name 26 | FUNCTION.getClassName(function(self) 27 | return self.__class.__cname 28 | end); 29 | --get namespace name 30 | FUNCTION.getNamespace(function(self) 31 | return self.__class.__nsName 32 | end); 33 | --get metaObject 34 | FUNCTION.getMetaObject(function (self) 35 | return LMetaObject(self.__class) 36 | end); 37 | --get super class by name 38 | FUNCTION.getSupers(function (self,name) 39 | return self.__supers[name] 40 | end); 41 | 42 | FUNCTION.getSuperMethod(function(self,superName,methodName) 43 | if self.__debug then 44 | return self.__supers[superName].__function[methodName] 45 | else 46 | return self.__supers[superName][methodName] 47 | end 48 | end); 49 | 50 | FUNCTION.inherit(function(self,classObject) 51 | local cname=classObject.__cname 52 | if cname then 53 | return self.__inherits[cname] 54 | else 55 | return self.__supers["__hostClass"]==classObject 56 | end 57 | end); 58 | FUNCTION.isHost(function(self) 59 | return self.__instance~=nil 60 | end); 61 | 62 | FUNCTION.isExistField(function(self,key) 63 | local get=classRawGet(self,key) 64 | if get~=nil then return true end 65 | get=classRawGet(self._protected_,key) 66 | if get~=nil then return true end 67 | return false 68 | end); 69 | 70 | FUNCTION.isExistFunction(function(self,key) 71 | local get=classRawGet(self.__class,key) 72 | if get~=nil then return true end 73 | return false 74 | end); 75 | 76 | --return whether self is target class object or not 77 | FUNCTION.is(is); 78 | --return string of serilize result 79 | FUNCTION.serilize(serilize); 80 | 81 | --get object string desciption 82 | FUNCTION.toString(function(self) 83 | return " [LOBJECT: "..self.__cname.."]" 84 | end); 85 | 86 | META.__tostring(function(self) 87 | return self:toString() 88 | end); 89 | META.__concat(function(concat1,concat2) 90 | return (is(concat1,LObject) and concat1:toString() or concat1) 91 | ..(is(concat2,LObject) and concat2:toString() or concat2) 92 | end); 93 | 94 | }; 95 | } 96 | 97 | 98 | -------------------------------------------------------------------------------- /luaClass/class/object/init.lua: -------------------------------------------------------------------------------- 1 | include "luaClass.class.object.LObject" 2 | include "luaClass.class.object.LMetaObject" -------------------------------------------------------------------------------- /luaClass/container/array.lua: -------------------------------------------------------------------------------- 1 | _ENV=namespace "container" 2 | using_namespace "luaClass" 3 | using_namespace "algorithm.iterator" 4 | 5 | 6 | ---@class array 7 | local array=class ("array"){ 8 | public{ 9 | FUNCTION.array; 10 | 11 | FUNCTION.getData; 12 | 13 | FUNCTION.at; 14 | 15 | FUNCTION.set; 16 | 17 | FUNCTION.merge; 18 | 19 | FUNCTION.size; 20 | 21 | FUNCTION.push_back; 22 | 23 | FUNCTION.unpack; 24 | 25 | FUNCTION.back; 26 | 27 | FUNCTION.front; 28 | 29 | FUNCTION.join; 30 | 31 | FUNCTION.pop_back; 32 | 33 | FUNCTION.empty; 34 | 35 | FUNCTION.iter; 36 | 37 | FUNCTION.clear; 38 | 39 | FUNCTION.reverse; 40 | 41 | FUNCTION.sort; 42 | 43 | FUNCTION.zip; 44 | 45 | FUNCTION.for_each; 46 | 47 | FUNCTION.zip_each; 48 | 49 | }; 50 | protected{ 51 | MEMBER._data; 52 | MEMBER._size; 53 | } 54 | 55 | } 56 | function array:array(data) 57 | data=data or {} 58 | self._data=data 59 | self._size=#data 60 | end 61 | 62 | function array:getData() 63 | return self._data 64 | end 65 | 66 | function array:at(index) 67 | return self._data[index] 68 | end 69 | 70 | function array:set(index,value) 71 | self._data[index]=value 72 | end 73 | 74 | function array:merge(arr) 75 | if is(arr,array) then 76 | local arrsize=arr:size() 77 | local size=self._size 78 | local selfData=self._data 79 | local arrData=arr:getData() 80 | for i=1,arrsize do 81 | selfData[size+i]=arrData[i] 82 | end 83 | self._size=size+arrsize 84 | else 85 | local arrsize=#arr 86 | local size=self._size 87 | local selfData=self._data 88 | local arrData=arr 89 | for i=1,arrsize do 90 | selfData[size+i]=arrData[i] 91 | end 92 | self._size=size+arrsize 93 | end 94 | end 95 | 96 | function array:size() 97 | return self._size 98 | end 99 | 100 | function array:push_back(value) 101 | if value==nil then return end 102 | local num=self._size+1 103 | self._size=num 104 | self._data[num]=value 105 | end 106 | 107 | function array:back() 108 | return self._data[self._size] 109 | end 110 | 111 | function array:front() 112 | return self._data[1] 113 | end 114 | 115 | function array:join(sep) 116 | return table.concat(self._data,sep) 117 | end 118 | local unpack=unpack or table.unpack 119 | 120 | function array:unpack() 121 | return unpack(self._data) 122 | end 123 | function array:pop_back() 124 | local num=self._size 125 | local elem=self._data[num] 126 | self._data[num]=nil 127 | num=num-1 128 | self._size=num<0 and 0 or num 129 | return elem 130 | end 131 | 132 | function array:empty() 133 | return self._size==0 134 | end 135 | 136 | function array:iter() 137 | local data=self._data 138 | return ipairs(data) 139 | end 140 | 141 | function array:clear() 142 | self._data={} 143 | self._size=0 144 | end 145 | 146 | function array:reverse() 147 | local data={} 148 | local sourceData=self._data 149 | local size=self._size 150 | if size~=0 then 151 | for i=1,size do 152 | data[i]=sourceData[size+1-i] 153 | end 154 | end 155 | return array(data) 156 | end 157 | 158 | function array:sort(cmpFunction) 159 | table.sort(self._data,cmpFunction ) 160 | end 161 | 162 | 163 | function array:zip(arr2) 164 | return zip(self._data,arr2:getData()) 165 | end 166 | 167 | function array:for_each(callBack) 168 | for index,value in self:iter() do 169 | callBack(index,value) 170 | end 171 | end 172 | 173 | function array:zip_each(arr2,callBack) 174 | for index,v1,v2 in self:zip(arr2) do 175 | callBack(index,v1,v2) 176 | end 177 | end 178 | 179 | -------------------------------------------------------------------------------- /luaClass/container/graph.lua: -------------------------------------------------------------------------------- 1 | include "luaClass.container.array" 2 | include "luaClass.container.set" 3 | include "luaClass.container.queue" 4 | 5 | _ENV=namespace "container" 6 | using_namespace "luaClass" 7 | 8 | ---@class graph 9 | local graph=class ("graph"){ 10 | public{ 11 | --此数据结构要求给出顶点数组或者表 12 | FUNCTION.graph; 13 | --设置一个顶点到另一个顶点的边, 14 | --ver1是一个表或者数组 15 | --ver2是一个表或者数组 16 | --dir代表方向true代表有向,false代表无向 17 | FUNCTION.setEdge; 18 | 19 | --level 如果为0就执行完整的搜索,否则就执行指定层数的搜索 20 | FUNCTION.BFS; 21 | 22 | FUNCTION.DFS; 23 | 24 | }; 25 | protected{ 26 | FUNCTION.dfs; 27 | 28 | MEMBER._data(); 29 | MEMBER._edges(); 30 | } 31 | } 32 | 33 | function graph:graph(data) 34 | local _data=is(data,array) and data or array(data) 35 | self._data=_data 36 | local edges=array() 37 | 38 | for i=1,_data:size() do 39 | edges:set(i,set()) 40 | end 41 | self._edges=edges 42 | end 43 | 44 | 45 | function graph:setEdge(ver1,ver2,dir) 46 | ver1=is(ver1,array) and ver1 or array(ver1) 47 | ver2=is(ver2,array) and ver2 or array(ver2) 48 | dir=not not dir 49 | local edges=self._edges 50 | for _,v1,v2 in ver1:zip(ver2) do 51 | edges:at(v1):insert(v2) 52 | if not dir then edges:at(v2):insert(v1) end 53 | end 54 | end 55 | 56 | function graph:BFS(startVertexNum,level) 57 | --结果数组 58 | local result=array() 59 | 60 | local size=self._data:size() 61 | --顶点队列 62 | local verQueue=queue(size,{startVertexNum}) 63 | --访问过的顶点集 64 | local accessedSet=set({startVertexNum}) 65 | 66 | local count1=1 67 | local count2=0 68 | local _level=0 69 | while not verQueue:empty() do 70 | --弹出队列前部 71 | local front=verQueue:pop_front() 72 | result:push_back(self._data:at(front)) 73 | 74 | for k,_ in self._edges:at(front):iter() do 75 | if not accessedSet:has(k) then 76 | verQueue:push_back(k) 77 | accessedSet:insert(k) 78 | count2=count2+1 79 | end 80 | end 81 | --level 非0 才采用 82 | if level~=0 then 83 | --记录层级 84 | count1=count1-1 85 | if count1==0 then 86 | --层级增加 87 | count1=count2 88 | count2=0 89 | _level=_level+1 90 | if _level==level then 91 | while not verQueue:empty() do 92 | local front=verQueue:pop_front() 93 | result:push_back(self._data:at(front)) 94 | end 95 | break 96 | end 97 | end 98 | end 99 | end 100 | return result 101 | end 102 | 103 | function graph:DFS(startVertexNum) 104 | --结果数组 105 | local result=array({self._data:at(startVertexNum)}) 106 | --访问过的顶点集 107 | local accessedSet=set({startVertexNum}) 108 | self:dfs(startVertexNum,self._data,self._edges,accessedSet,result) 109 | return result 110 | end 111 | 112 | 113 | function graph:dfs(startVertexNum,verArr,edges,accessedSet,result) 114 | for k,_ in edges:at(startVertexNum):iter() do 115 | if not accessedSet:has(k) then 116 | --这样写是降低递归深度 117 | accessedSet:insert(k) 118 | result:push_back(verArr:at(k)) 119 | self:dfs(k,verArr,edges,accessedSet,result) 120 | end 121 | end 122 | end 123 | 124 | 125 | 126 | -------------------------------------------------------------------------------- /luaClass/container/init.lua: -------------------------------------------------------------------------------- 1 | require "luaClass.include" 2 | include "luaClass.class.init" 3 | include "luaClass.algorithm.init" 4 | include "luaClass.container.array" 5 | include "luaClass.container.stack" 6 | include "luaClass.container.map" 7 | include "luaClass.container.queue" 8 | include "luaClass.container.graph" 9 | include "luaClass.container.set" 10 | include "luaClass.container.mat" 11 | -------------------------------------------------------------------------------- /luaClass/container/map.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | 和STL map 不同的是,这并不能指定类型 3 | ]] 4 | _ENV=namespace "container" 5 | using_namespace "luaClass" 6 | 7 | ---@class map 8 | local map=class ("map"){ 9 | public{ 10 | FUNCTION.map; 11 | 12 | FUNCTION.del; 13 | 14 | FUNCTION.has; 15 | 16 | FUNCTION.get; 17 | 18 | FUNCTION.insert; 19 | 20 | FUNCTION.onFun; 21 | 22 | FUNCTION.size; 23 | 24 | FUNCTION.iter; 25 | 26 | FUNCTION.merge; 27 | 28 | FUNCTION.for_each; 29 | }; 30 | 31 | protected{ 32 | MEMBER._data; 33 | }; 34 | } 35 | 36 | function map:map(dict) 37 | dict=dict or {} 38 | self._data=dict 39 | end 40 | 41 | function map:del(key) 42 | self._data[key]=nil 43 | end 44 | 45 | function map:has(key) 46 | return self._data[key]~=nil 47 | end 48 | 49 | function map:get(key,default) 50 | return self._data[key] or default 51 | end 52 | 53 | function map:insert(key,value) 54 | self._data[key]=value 55 | end 56 | 57 | ---@param alterFunc fun(value:any,value2:any) 58 | function map:onFun(key,alterFunc) 59 | self._data[key]=alterFunc() 60 | end 61 | 62 | function map:size() 63 | local count=0 64 | for _,_ in pairs(self._data) do 65 | count=count+1 66 | end 67 | return count 68 | end 69 | 70 | 71 | function map:iter() 72 | local data=self._data 73 | return pairs(data) 74 | end 75 | 76 | function map:merge(map2) 77 | for k,v in map2:iter() do 78 | self:insert(k,v) 79 | end 80 | end 81 | 82 | function map:for_each(callBack) 83 | for k,v in self:iter() do 84 | callBack(k,v) 85 | end 86 | end -------------------------------------------------------------------------------- /luaClass/container/mat.lua: -------------------------------------------------------------------------------- 1 | _ENV=namespace "container" 2 | using_namespace "luaClass" 3 | 4 | ---@class mat 5 | local mat=class "mat" { 6 | 7 | property "colNum" {READ.getColNum;}; 8 | property "rowNum" {READ.getRowNum;}; 9 | public{ 10 | FUNCTION.mat; 11 | 12 | FUNCTION.at; 13 | 14 | FUNCTION.set; 15 | 16 | FUNCTION.del; 17 | 18 | FUNCTION.getColNum; 19 | 20 | FUNCTION.getRowNum; 21 | 22 | FUNCTION.size; 23 | 24 | FUNCTION.iter; 25 | 26 | FUNCTION.clear; 27 | 28 | FUNCTION.onFun; 29 | }; 30 | protected{ 31 | MEMBER._data; 32 | MEMBER._rowNum; 33 | MEMBER._colNum; 34 | }; 35 | 36 | 37 | } 38 | 39 | 40 | function mat:mat(rowNum,colNum) 41 | local data={} 42 | self._data=data 43 | self._rowNum=rowNum 44 | self._colNum=colNum 45 | for i=1,rowNum*colNum do 46 | data[i]=false 47 | end 48 | end 49 | 50 | function mat:at(row,col) 51 | return self._data[col+(row-1)*self._colNum] or nil 52 | end 53 | 54 | function mat:set(row,col,value) 55 | self._data[col+(row-1)*self._colNum]=value 56 | end 57 | 58 | function mat:del(row,col) 59 | self:set(row,col,false) 60 | end 61 | 62 | function mat:getColNum() 63 | return self._colNum 64 | end 65 | 66 | function mat:getRowNum() 67 | return self._rowNum 68 | end 69 | 70 | function mat:size() 71 | return self._colNum*self._rowNum 72 | end 73 | 74 | function mat:iter() 75 | return ipairs(self._data) 76 | end 77 | 78 | function mat:clear() 79 | for i=1,self:size() do 80 | self._data[i]=false 81 | end 82 | end 83 | 84 | function mat:onFun(callBack) 85 | for _,v in ipairs(self._data) do 86 | callBack(v) 87 | end 88 | end -------------------------------------------------------------------------------- /luaClass/container/queue.lua: -------------------------------------------------------------------------------- 1 | _ENV=namespace "container" 2 | using_namespace "luaClass" 3 | 4 | ---@class queue 5 | local queue=class ("queue"){ 6 | public{ 7 | FUNCTION.queue; 8 | 9 | FUNCTION.push_back; 10 | 11 | FUNCTION.pop_front; 12 | 13 | FUNCTION.front; 14 | 15 | FUNCTION.empty; 16 | 17 | FUNCTION.full; 18 | }; 19 | protected{ 20 | MEMBER._query; 21 | MEMBER._data; 22 | MEMBER._tail; 23 | MEMBER._front; 24 | }; 25 | } 26 | 27 | 28 | function queue:queue(queueMax,data) 29 | queueMax=queueMax or 4 30 | local data=data or {} 31 | local size=#data 32 | local query={} 33 | for i=1,queueMax do 34 | data[i]=data[i] or false 35 | query[i]=i+1 36 | end 37 | query[queueMax]=1 38 | self._query=query 39 | self._data=data 40 | self._tail=1+size 41 | self._front=1 42 | end 43 | 44 | function queue:push_back(value) 45 | local tail=self._tail 46 | self._data[tail]=value 47 | self._tail=self._query[tail] 48 | end 49 | 50 | function queue:pop_front() 51 | local front=self._front 52 | local elem=self._data[front] 53 | self._data[front]=false 54 | self._front=self._query[front] 55 | return elem 56 | end 57 | 58 | function queue:front() 59 | return self._data[self._front] 60 | end 61 | 62 | function queue:empty() 63 | return self._front==self._tail and (self._data[self._tail]==false) 64 | end 65 | 66 | function queue:full() 67 | return (self._front==self._tail) and (self._data[self._tail]~=false) 68 | end -------------------------------------------------------------------------------- /luaClass/container/set.lua: -------------------------------------------------------------------------------- 1 | _ENV=namespace "container" 2 | using_namespace "luaClass" 3 | 4 | class ("set"){ 5 | public{ 6 | FUNCTION.set; 7 | 8 | FUNCTION.del; 9 | 10 | FUNCTION.size; 11 | 12 | FUNCTION.merge; 13 | 14 | FUNCTION.has; 15 | 16 | FUNCTION.insert; 17 | 18 | FUNCTION.iter; 19 | 20 | FUNCTION.for_each; 21 | 22 | }; 23 | protected{ 24 | MEMBER._data; 25 | } 26 | 27 | }; 28 | 29 | 30 | function set:set(data_t) 31 | local data={} 32 | if data_t then 33 | for _,value in pairs(data_t) do 34 | data[value]=true 35 | end 36 | end 37 | self._data=data 38 | end 39 | 40 | function set:del(key) 41 | self._data[key]=nil 42 | end 43 | 44 | function set:size() 45 | local count=0 46 | for _ in pairs(self._data) do 47 | count=count+1 48 | end 49 | return count 50 | end 51 | 52 | function set:merge(set2) 53 | for key,_ in set2:iter() do 54 | self:insert(key) 55 | end 56 | end 57 | 58 | function set:has(key) 59 | return self._data[key]~=nil 60 | end 61 | 62 | function set:insert(key) 63 | self._data[key]=true 64 | end 65 | 66 | function set:iter() 67 | local data=self._data 68 | return pairs(data) 69 | end 70 | 71 | function set:for_each(callBack) 72 | for key,_ in self:iter() do 73 | callBack(key) 74 | end 75 | end -------------------------------------------------------------------------------- /luaClass/container/stack.lua: -------------------------------------------------------------------------------- 1 | _ENV=namespace "container" 2 | using_namespace "luaClass" 3 | 4 | ---@class stack 5 | local stack=class ("stack"){ 6 | public{ 7 | FUNCTION.stack; 8 | 9 | FUNCTION.size; 10 | 11 | FUNCTION.push; 12 | 13 | FUNCTION.top; 14 | 15 | FUNCTION.pop; 16 | 17 | FUNCTION.empty; 18 | 19 | }; 20 | protected{ 21 | MEMBER._data; 22 | MEMBER._size; 23 | }; 24 | } 25 | 26 | function stack:stack(data) 27 | data=data or {} 28 | self._data=data 29 | self._size=#data 30 | end 31 | 32 | function stack:size() 33 | return self._size 34 | end 35 | 36 | 37 | function stack:push(value) 38 | local num=self._size+1 39 | self._size=num 40 | self._data[num]=value 41 | end 42 | 43 | function stack:top() 44 | return self._data[self._size] 45 | end 46 | 47 | function stack:pop() 48 | local num=self._size 49 | local elem=self._data[num] 50 | self._data[num]=nil 51 | num=num-1 52 | self._size=num<0 and 0 or num 53 | return elem 54 | end 55 | 56 | function stack:empty() 57 | return self._size==0 58 | end 59 | 60 | -------------------------------------------------------------------------------- /luaClass/include.lua: -------------------------------------------------------------------------------- 1 | --This part is experimental. default not open 2 | LUA_PACKAGE_MANAGE=false 3 | 4 | if LUA_PACKAGE_MANAGE then 5 | 6 | __loadStack={} 7 | include=function(modName) 8 | if package.loaded[modName]==nil then 9 | table.insert(__loadStack,modName) 10 | require (modName) 11 | end 12 | end 13 | reloadLua=function() 14 | for _,modName in pairs(__loadStack) do 15 | package.loaded[modName]=nil 16 | end 17 | local oldStack=__loadStack 18 | __loadStack={} 19 | for _,modName in pairs(oldStack) do 20 | include (modName) 21 | end 22 | end 23 | 24 | else 25 | include=require 26 | end -------------------------------------------------------------------------------- /luaClass/init.lua: -------------------------------------------------------------------------------- 1 | require "luaClass.include" 2 | include "luaClass.class.init" 3 | include "luaClass.container.init" 4 | include "luaClass.algorithm.init" 5 | include "luaClass.uiBase.init" 6 | -------------------------------------------------------------------------------- /luaClass/uiBase/LUIObject.lua: -------------------------------------------------------------------------------- 1 | include "luaClass.class.init" 2 | include "luaClass.uiBase.UIScript" 3 | _ENV=namespace "ui" 4 | using_namespace "luaClass" 5 | using_namespace "container" 6 | local unpack=unpack or table.unpack 7 | 8 | local LUIObject=class ("LUIObject"){ 9 | super(LObject); 10 | SINGAL.touched("touch","event"); 11 | SINGAL.destroyed(); 12 | property "tag" {WRITE.setTag; READ.getTag}; 13 | property "anchor" {WRITE.setAnchorPoint; READ.getAnchorPoint;}; 14 | property "pos" {WRITE.setPosition; READ.getPosition}; 15 | property "scale" {WRITE.setScale;READ.getScale}; 16 | property "opacity" {WRITE.setOpacity;READ.getOpacity}; 17 | property "color" {WRITE.setColor;READ.getColor}; 18 | property "rotation" {WRITE.setRotation;READ.getRotation}; 19 | property "onExit" {WRITE.setOnExitCallBack}; 20 | property "id" {WRITE.setID;READ.getID}; 21 | property "size" {WRITE.setContentSize;READ.getContentSize}; 22 | property "onTouch" {WRITE.onNodeTouch}; 23 | property "backGround" {WRITE.setBackGround}; 24 | public{ 25 | FUNCTION.LUIObject(function(self) 26 | self._idParams={} 27 | self._idComponents={} 28 | 29 | end); 30 | 31 | FUNCTION.getID(function(self) 32 | return self._id 33 | end); 34 | 35 | FUNCTION.setID(function(self,value) 36 | self._id=value 37 | end); 38 | 39 | FUNCTION.onNodeTouch(function(self,swall) 40 | 41 | local onTouchBegin = function(touch, event) 42 | if self:isVisible()==false then return true end 43 | local target = event:getCurrentTarget() 44 | local size = target:getContentSize() 45 | local rect = cc.rect(0, 0, size.width, size.height) 46 | --local p = touch:getLocation() 47 | local p = target:convertTouchToNodeSpace(touch) 48 | if cc.rectContainsPoint(rect, p) then 49 | self:touched(touch,event) 50 | return swall 51 | end 52 | return false 53 | end 54 | local listener = cc.EventListenerTouchOneByOne:create() 55 | listener:setSwallowTouches(swall) 56 | listener:registerScriptHandler(onTouchBegin, cc.Handler.EVENT_TOUCH_BEGAN) 57 | local eventDispatcher = cc.Director:getInstance():getEventDispatcher() 58 | eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self) 59 | end); 60 | 61 | FUNCTION.load(function(self,uiScript) 62 | self._uiScript=uiScript:getScript() 63 | end); 64 | 65 | FUNCTION.setParams(function(self,id,paramsTable) 66 | self._idParams[id]=paramsTable 67 | end); 68 | 69 | FUNCTION.implement(function(self) 70 | self:_parse(self._uiScript, nil,self,nil) 71 | self._uiScript=nil 72 | self._idParams=nil 73 | end); 74 | 75 | FUNCTION.find(function(self,id) 76 | return self._idComponents[id] 77 | end); 78 | 79 | FUNCTION.setBackGround( function(self,backPath,contentSize,color) 80 | self._backGroundPath=backPath 81 | contentSize=contentSize or self:getContentSize() 82 | if self._backGround then 83 | self._backGround:removeFromParent() 84 | self._backGround=nil 85 | end 86 | local backGround; 87 | 88 | if backPath then 89 | backGround=cc.Sprite:create(backPath) 90 | :setPosition(0,0) 91 | :setAnchorPoint(0,0) 92 | :setContentSize(contentSize) 93 | :addTo(self,-10) 94 | else 95 | local pointArr={ 96 | cc.p(0,0), 97 | cc.p(contentSize.width,0), 98 | cc.p(contentSize.width,contentSize.height), 99 | cc.p(0,contentSize.height) 100 | } 101 | local fill=color or cc.c4f(0,0,0,0.6) 102 | backGround=cc.DrawNode:create() 103 | backGround:drawPolygon(pointArr,4,fill,2,fill) 104 | backGround:setAnchorPoint(0,0) 105 | :setContentSize(contentSize) 106 | :addTo(self,-10) 107 | end 108 | self._backGround=backGround 109 | return self 110 | end); 111 | 112 | FUNCTION.clear(function(self,key) 113 | if self[key] then 114 | self[key]:removeFromParent() 115 | self[key]=nil 116 | end 117 | end); 118 | 119 | FUNCTION.delayClear(function(self,key,delayTime) 120 | local node=self[key] 121 | 122 | local action=cc.Sequence:create( 123 | cc.DelayTime:create(delayTime), 124 | cc.CallFunc:create(function() 125 | node:release() 126 | end) 127 | ) 128 | if node then 129 | node:retain() 130 | node:removeFromParent() 131 | self:runAction(action) 132 | end 133 | self[key]=nil 134 | end); 135 | 136 | FUNCTION.remove(function(self) 137 | local destroyed=self.destroyed 138 | self:removeFromParent() 139 | destroyed(nil) 140 | end); 141 | 142 | 143 | FUNCTION.addUpdateFunction(function(self,name,callBack,bindArgs) 144 | if not self.InUpdate then 145 | self.InUpdate=true 146 | self.CallFunctions=map() 147 | self:onUpdate(function() 148 | for _,call in self.CallFunctions:iter() do 149 | call(bindArgs) 150 | end 151 | end) 152 | end 153 | self.CallFunctions:insert(name,callBack) 154 | end); 155 | 156 | 157 | STATICFUNC.script(function(cls,uiScript) 158 | uiScript.__class=cls 159 | return uiScript 160 | end); 161 | 162 | --get object string desciption 163 | FUNCTION.toString(function(self) 164 | return " [LUIObject: "..self.__cname.."]" 165 | end); 166 | }; 167 | protected{ 168 | FUNCTION._parse(function (self,uiScript,classObject,classInstance,parentInstance) 169 | local instance=classInstance 170 | if instance==nil then 171 | local create=self._idParams[uiScript.id] or uiScript.params 172 | instance=create 173 | and classObject(unpack(create)) 174 | or classObject() 175 | end 176 | self._idComponents[uiScript.id]=instance 177 | if parentInstance then 178 | parentInstance:addChild(instance) 179 | end 180 | 181 | for prop,value in pairs(uiScript) do 182 | if type(prop)=="number" then 183 | self:_parse(value,value.__class,nil,instance) 184 | elseif prop~="__class" and prop~="params" then 185 | instance[prop]=value 186 | end 187 | end 188 | end); 189 | }; 190 | public{ 191 | MEMBER.CallFunctions(); 192 | MEMBER.InUpdate(); 193 | }; 194 | protected{ 195 | MEMBER._id(); 196 | MEMBER._uiScript(); 197 | MEMBER._idParams(); 198 | MEMBER._idComponents(); 199 | MEMBER._backGround(); 200 | MEMBER._backGroundPath(); 201 | }; 202 | } 203 | 204 | 205 | -------------------------------------------------------------------------------- /luaClass/uiBase/READ.md: -------------------------------------------------------------------------------- 1 | # 这是一个UI开发框架 2 | --- 3 | 这个框架是实验性的,基于luaClass.算一个赠品. 4 | -------------------------------------------------------------------------------- /luaClass/uiBase/UIScript.lua: -------------------------------------------------------------------------------- 1 | include "luaClass.class.init" 2 | 3 | _ENV=namespace "uis" 4 | using_namespace "luaClass" 5 | 6 | 7 | 8 | class "UIScript" { 9 | public{ 10 | FUNCTION.UIScript(function(self,scriptName) 11 | uis[scriptName]=self 12 | end); 13 | 14 | FUNCTION.setScript(function(self,uiScript) 15 | self._uiScript=uiScript; 16 | end); 17 | 18 | META.__call(function(self,uiScript) 19 | self:setScript(uiScript) 20 | end); 21 | 22 | FUNCTION.getScript(function(self) 23 | return self._uiScript 24 | end) 25 | }; 26 | 27 | protected{ 28 | MEMBER._uiScript(); 29 | 30 | }; 31 | } -------------------------------------------------------------------------------- /luaClass/uiBase/init.lua: -------------------------------------------------------------------------------- 1 | include "luaClass.uiBase.LUIObject" 2 | include "luaClass.uiBase.UIScript" -------------------------------------------------------------------------------- /test/AccessedTest/EmmyLuaTest.lua: -------------------------------------------------------------------------------- 1 | require "luaClass.init" 2 | _ENV=namespace "test" 3 | using_namespace "luaClass" 4 | 5 | ---@class EmmyLuaTest 6 | local EmmyLuaTest=class "EmmyLuaTest" { 7 | public{ 8 | FUNCTION.EmmyLuaTest; 9 | FUNCTION.get; 10 | FUNCTION.set; 11 | }; 12 | protected{ 13 | MEMBER.lua23333; 14 | MEMBER.lua996; 15 | } 16 | } 17 | 18 | function EmmyLuaTest:EmmyLuaTest() 19 | self.lua23333=996; 20 | self.lua996=9999; 21 | end 22 | ---@return number 23 | function EmmyLuaTest:get() 24 | return self.lua23333 25 | end 26 | ---@param value number 27 | ---@return number 28 | function EmmyLuaTest:set(value) 29 | return self.lua996 30 | end 31 | 32 | local emmy=EmmyLuaTest() 33 | 34 | print(emmy:get()) -------------------------------------------------------------------------------- /test/AccessedTest/LMetaObjectTest.lua: -------------------------------------------------------------------------------- 1 | require "luaClass.init" 2 | _ENV=namespace "test" 3 | using_namespace "luaClass" 4 | 5 | class ("metaTest") { 6 | property("Time"){READ.getTime;WRITE.setTime}; 7 | public{ 8 | FUNCTION.getTime(function(self) end); 9 | FUNCTION.setTime(function(self) end); 10 | FUNCTION.Do(function(self) end); 11 | FUNCTION.you(function(self) end); 12 | FUNCTION.want(function(self) end); 13 | FUNCTION.nine(function(self) end); 14 | FUNCTION.nine(function(self) end); 15 | FUNCTION.six(function(self) end); 16 | -- ? 17 | MEMBER.no(); 18 | MEMBER.I(); 19 | MEMBER.need(); 20 | MEMBER.rest(); 21 | 22 | CONST.NINE(); 23 | CONST.SIX(); 24 | CONST.FIVE(); 25 | -- ? 26 | STATIC.good(); 27 | 28 | STATICFUNC.getInstance(function(cls) 29 | if cls.s_instance==nil then 30 | cls.s_instance=cls() 31 | end 32 | return cls.s_instance 33 | end); 34 | }; 35 | protected{ 36 | FUNCTION.metaTest(function(self) 37 | self.name="metaTest" 38 | end); 39 | STATIC.s_instance(); 40 | CONST.name(); 41 | }; 42 | } 43 | local instance=metaTest:getInstance() 44 | ---@type LMetaObject 45 | local meta=instance:getMetaObject() 46 | 47 | local printList=function(preStr,t) print("----------",preStr) for _,str in pairs(t) do print(str) end end 48 | 49 | printList("publicFunction",meta:getPublicFunctionList()) 50 | 51 | printList("property",meta:getPropertyList()) 52 | 53 | printList("metaMethod",meta:getMetaMethodList()) 54 | 55 | printList("protectedConst",meta:getProtectedConstList()) 56 | 57 | printList("protectedFunction",meta:getProtectedFunctionList()) 58 | 59 | printList("protectedStatic",meta:getProtectedStaticList()) 60 | 61 | printList("publicConst",meta:getPublicConstList()) 62 | 63 | printList("publicMember",meta:getPublicMemberList()) 64 | 65 | printList("staticFunction",meta:getStaticFunctionList()) 66 | 67 | 68 | -------------------------------------------------------------------------------- /test/AccessedTest/LObjectTest.lua: -------------------------------------------------------------------------------- 1 | require "luaClass.init" 2 | _ENV=namespace "test" 3 | using_namespace "luaClass" 4 | 5 | 6 | --By default, all classes inherit directly or indirectly from LObject 7 | class ("ObjectTest"){ 8 | public{ 9 | FUNCTION.ObjectTest(function(self) 10 | 11 | end) 12 | 13 | }; 14 | 15 | } 16 | local obj=ObjectTest() 17 | --LObject overload the __concat and __tostring ,all call the tostring 18 | print(obj,obj.."996",obj:toString()) 19 | 20 | print(obj:inherit(LObject)) 21 | 22 | print(is(1,LObject)) 23 | 24 | print(obj:getClassName()) 25 | 26 | print(obj:getNamespace()) 27 | 28 | print(obj:getMetaObject()) -------------------------------------------------------------------------------- /test/AccessedTest/LString.lua: -------------------------------------------------------------------------------- 1 | 2 | require "luaClass.init" 3 | 4 | _ENV=namespace "test" 5 | using_namespace "luaClass" 6 | using_namespace "container" 7 | class "LString" { 8 | 9 | public{ 10 | MEMBER._table(); 11 | MEMBER._args(); 12 | }; 13 | public{ 14 | FUNCTION.LString(function(self) 15 | self._table={} 16 | self._args=map() 17 | end); 18 | 19 | FUNCTION.toString(function(self) 20 | local result=table.concat(self._table) 21 | local args=self._args 22 | return result:gsub("{.-}",function (s) 23 | local index=string.match(s,"%s-(%w+)%s-") 24 | local newindex=tonumber(index) 25 | if newindex then 26 | return args:get(newindex) 27 | else 28 | return args:get(index) 29 | end 30 | end) 31 | 32 | end); 33 | 34 | FUNCTION.format(function(self,...) 35 | self._args:merge({...}) 36 | return self 37 | end); 38 | FUNCTION.data(function(self,mapData) 39 | self._args=mapData 40 | return self 41 | end); 42 | 43 | META.__call(function(self,str_OR_table) 44 | local ctype=type(str_OR_table) 45 | if ctype=="string" then 46 | table.insert(self._table,str_OR_table) 47 | elseif ctype=="table" then 48 | for _,v in pairs(str_OR_table) do 49 | table.insert(self._table,tostring(v)) 50 | end 51 | 52 | end 53 | return self 54 | 55 | end) 56 | } 57 | 58 | 59 | } 60 | 61 | local fm=LString() 62 | local data=map() 63 | 64 | local str=fm "1234" {996} "xixixi" {5555} 65 | print(str) -------------------------------------------------------------------------------- /test/AccessedTest/inheritTest.lua: -------------------------------------------------------------------------------- 1 | require "luaClass.init" 2 | _ENV=namespace "test" 3 | using_namespace "luaClass" 4 | 5 | class ("baseClass"){ 6 | public { 7 | FUNCTION.baseClass(function(self) 8 | end); 9 | FUNCTION.speak(function(self) 10 | print("base speak"); 11 | end); 12 | FUNCTION.speak2(function(self) 13 | print("base speak2"); 14 | end) 15 | }; 16 | } 17 | class ("baseClass2"){ 18 | public{ 19 | FUNCTION.baseClass2(function(self) 20 | self._fff=996 21 | end); 22 | FUNCTION.say(function(self) 23 | print(self._fff.." is your reward") 24 | end); 25 | }; 26 | protected{ 27 | MEMBER._fff(); 28 | 29 | } 30 | } 31 | 32 | class ("subclass"){ 33 | super(baseClass,baseClass2); 34 | public{ 35 | FUNCTION.subclass(function(self) 36 | --call base1 class ctor 37 | self:baseClass() 38 | --call base2 class ctor 39 | self:baseClass2() 40 | end); 41 | FUNCTION.speak(function(self) 42 | 43 | print("subclass speak"); 44 | end); 45 | FUNCTION.say(function(self) 46 | print("hello world") 47 | self:getSuperMethod("baseClass2","say")(self) 48 | end); 49 | } 50 | 51 | } 52 | 53 | local base =baseClass() 54 | base:speak() 55 | base:speak2() 56 | 57 | local sub=subclass() 58 | sub:speak() 59 | sub:speak2() 60 | sub:say() 61 | 62 | -------------------------------------------------------------------------------- /test/AccessedTest/propertyTest.lua: -------------------------------------------------------------------------------- 1 | require "luaClass.init" 2 | _ENV=namespace "test" 3 | using_namespace "luaClass" 4 | 5 | class ("PropertyTest"){ 6 | property("Parent"){READ.getParent,WRITE.setParent}; 7 | property("Order"){READ.getOrder}; 8 | public{ 9 | FUNCTION.PropertyTest(function(self) 10 | self._parent="parent" 11 | self._order=2 12 | end); 13 | FUNCTION.getParent(function(self) 14 | print("getter test") 15 | return self._parent 16 | end); 17 | 18 | FUNCTION.setParent(function(self,value); 19 | print("seeter test") 20 | self._parent=value 21 | end); 22 | FUNCTION.getOrder(function(self) 23 | print("getter order test") 24 | return self._order 25 | end); 26 | 27 | }; 28 | protected{ 29 | MEMBER._parent(); 30 | MEMBER._order(); 31 | MEMBER._DATA(); 32 | 33 | }; 34 | } 35 | 36 | local pro=PropertyTest() 37 | print(pro.Parent) 38 | pro.Parent="parent parent" 39 | print(pro.Parent) 40 | 41 | --throw error test 42 | --print(pro._parent) 43 | 44 | local a=pro.Order 45 | print(a) 46 | 47 | --thorw error test 48 | --pro.Order=998 49 | 50 | 51 | local test=class ("test") { 52 | NO_AUTO_INHERIT(); 53 | CLASS_DEBUG(false); 54 | super(PropertyTest); 55 | } 56 | function test:test(name) 57 | self:PropertyTest() 58 | self.name=name 59 | end 60 | 61 | local t=test() 62 | print(t:getType()) 63 | print(t.Parent) 64 | -------------------------------------------------------------------------------- /test/AccessedTest/protectedTest.lua: -------------------------------------------------------------------------------- 1 | require "luaClass.init" 2 | _ENV=namespace "test" 3 | using_namespace "luaClass" 4 | 5 | class ("ProtectedTest"){ 6 | property ("Value"){READ.getValue }; 7 | property ("Change") {WRITE.setProtected}; 8 | public{ 9 | STATICFUNC.getInstance(function(cls) 10 | if cls.s_instance==nil then 11 | cls.s_instance=cls() 12 | end 13 | return cls.s_instance 14 | end) 15 | }; 16 | protected{ 17 | FUNCTION.ProtectedTest(function(self) 18 | self._protected=998 19 | print(self.Value) 20 | self.Change=3344 21 | print(self._protected) 22 | end); 23 | FUNCTION.getValue(function(self) 24 | print("get _VALUE"); 25 | return self._VALUE 26 | end); 27 | FUNCTION.setProtected(function(self,value) 28 | print("set _protected") 29 | self._protected=value 30 | end); 31 | MEMBER._protected(); 32 | CONST._VALUE(7744); 33 | STATIC.s_instance(); 34 | }; 35 | } 36 | 37 | local pr=ProtectedTest:getInstance() 38 | print(pr) 39 | print(pr:serilize()) 40 | --throw error 41 | --local pr2=ProtectedTest() 42 | --thorw error 43 | --print(pr.Value) 44 | --thorw error 45 | --pr.Change=666; 46 | -------------------------------------------------------------------------------- /test/AccessedTest/publicTest.lua: -------------------------------------------------------------------------------- 1 | require "luaClass.init" 2 | _ENV=namespace "test" 3 | using_namespace "luaClass" 4 | 5 | class ("PublicTest"){ 6 | public{ 7 | FUNCTION.PublicTest(function(self) 8 | --thorw error 9 | --self.FIRST=996; 10 | 11 | self.SECOND=997; 12 | end); 13 | 14 | CONST.FIRST(3333); 15 | CONST.SECOND(); 16 | STATIC.myStatic(996); 17 | }; 18 | 19 | } 20 | local p=PublicTest() 21 | print(p.FIRST) 22 | print(p.SECOND) 23 | print(p) 24 | print(p.myStatic) 25 | p.myStatic=966 26 | local p2=PublicTest() 27 | print(p.myStatic,p2.myStatic) 28 | -------------------------------------------------------------------------------- /test/AccessedTest/signalSlotTest.lua: -------------------------------------------------------------------------------- 1 | require "luaClass.init" 2 | _ENV=namespace "test" 3 | using_namespace "luaClass" 4 | 5 | class ("SignalTest"){ 6 | SINGAL.classCreate(); 7 | public{ 8 | FUNCTION.SignalTest(function(self,content) 9 | self._content=content 10 | connect(self,"classCreate",self,"hello") 11 | connect(self,"classCreate",self,"world") 12 | 13 | self:classCreate() 14 | 15 | end); 16 | FUNCTION.hello(function(self) 17 | print("hello",self._content) 18 | end); 19 | 20 | FUNCTION.world(function(self) 21 | print("world",self._content) 22 | end) 23 | 24 | }; 25 | protected{ 26 | MEMBER._content(); 27 | 28 | }; 29 | } 30 | local instance1=SignalTest(996) 31 | local instance2=SignalTest(966) 32 | local instance3=SignalTest(965) 33 | print(instance3:serilize()) 34 | 35 | 36 | -------------------------------------------------------------------------------- /test/__serilizeResult.lua: -------------------------------------------------------------------------------- 1 | return 2 | {__luaID=1,["_protected_"]={},["equipments"]={__luaID=2,["_protected_"]={["_size"]=3,["_data"]={[1]={__luaID=3,["_protected_"]={["defence"]=2,["attack"]=1},["_inFunction"]=false,["name"]="xixixi",["number"]=1,__class=battle.Equipment},[2]={__luaID=4,["_protected_"]={["defence"]=2,["attack"]=1},["_inFunction"]=false,["name"]="hahah",["number"]=1,__class=battle.Equipment},[3]={__luaID=5,["_protected_"]={["defence"]=2,["attack"]=1},["_inFunction"]=false,["name"]="yinyinyin",["number"]=1,__class=battle.Equipment}}},["_inFunction"]=false,__class=container.array},["_inFunction"]=true,["defence"]=888,["attack"]=998,["name"]="xixixi",["hp"]=666,__class=battle.Role} -------------------------------------------------------------------------------- /test/containerTest/arrayTest.lua: -------------------------------------------------------------------------------- 1 | require "luaClass.init" 2 | require "test.containerTest.timeTest" 3 | _ENV=namespace "test" 4 | using_namespace "luaClass" 5 | using_namespace "container" 6 | 7 | 8 | local arr=array({1,2,3,4,5,6,8}) 9 | arr:for_each(function(k,v) print(k,v) end) 10 | arr:push_back(998) 11 | arr:push_back(11511) 12 | arr:push_back(666) 13 | print("--------") 14 | arr:reverse():for_each(function(k,v) print(k,v) end) 15 | print(arr:size()) 16 | print(arr:at(1),arr:at(2),arr:at(7)) 17 | arr:set(1,998) 18 | arr:set(7,888) 19 | print("--------") 20 | for k,v in arr:iter() do 21 | print(k,v) 22 | end 23 | print("-----------") 24 | local resv=arr:reverse() 25 | for i,v1,v2 in arr:zip(resv) do 26 | print(i,v1,v2) 27 | end 28 | arr:merge(resv) 29 | print(arr:size()) 30 | arr:merge({999,777,666,555,444,33,22}) 31 | print(arr:size()) 32 | print(arr:empty()) 33 | arr:clear() 34 | print(arr:empty()) 35 | print("------push time test--------") 36 | 37 | local count=1000000 38 | local tarr=array() 39 | local t={} 40 | local t2={} 41 | testTime(function() 42 | for i=1,count do 43 | t[i]=i 44 | end 45 | end) 46 | testTime(function() 47 | for i=1,count do 48 | tarr:push_back(i) 49 | end 50 | end) 51 | testTime(function() 52 | for i=1,count do 53 | t2[#t2+1]=i 54 | end 55 | end) 56 | 57 | --array 并没有卓绝的性能,但是提供了许多有意义的方法,使代码更具有维护性 58 | 59 | 60 | -------------------------------------------------------------------------------- /test/containerTest/graphTest.lua: -------------------------------------------------------------------------------- 1 | require "luaClass.init" 2 | require "test.containerTest.timeTest" 3 | _ENV=namespace "test" 4 | 5 | using_namespace "container" 6 | 7 | local arrData=array({1,2,3,4,5,6,7,8,9,10}) 8 | local gf=graph(arrData) 9 | 10 | local v1={1,1,1,} 11 | local v2={2,3,4,} 12 | gf:setEdge(v1,v2) 13 | 14 | local v1={2,3,4,4} 15 | local v2={10,9,8,5} 16 | gf:setEdge(v1,v2) 17 | 18 | local v1={5,6} 19 | local v2={6,7} 20 | gf:setEdge(v1,v2) 21 | 22 | 23 | local re=gf:DFS(1) 24 | for _,v in re:iter() do 25 | print(v) 26 | end 27 | re=gf:BFS(1) 28 | print("----------------------") 29 | for _,v in re:iter() do 30 | print(v) 31 | end -------------------------------------------------------------------------------- /test/containerTest/mapTest.lua: -------------------------------------------------------------------------------- 1 | require "luaClass.init" 2 | require "test.containerTest.timeTest" 3 | _ENV=namespace "test" 4 | 5 | using_namespace "container" 6 | 7 | local m=map() 8 | m:insert("998","yinyinyin") 9 | print(m:has(998)) 10 | print(m:has("998")) 11 | m:del("998") 12 | print(m:has("998")) 13 | m:insert("xixixix","11511") 14 | m:insert("hahaha","yinyinyin") 15 | print(m:get("xixixix")) 16 | print(m:get("11511","wuwuwuwuwuw")) 17 | 18 | 19 | 20 | m:for_each(function(k,v) 21 | print(k,v) 22 | end) 23 | print("------------") 24 | m:del("yinyinyin") 25 | for k,v in m:iter() do 26 | print(k,v) 27 | end 28 | 29 | -------------------------------------------------------------------------------- /test/containerTest/queueTest.lua: -------------------------------------------------------------------------------- 1 | require "luaClass.init" 2 | require "test.containerTest.timeTest" 3 | _ENV=namespace "test" 4 | 5 | using_namespace "container" 6 | 7 | local q=queue(50) 8 | 9 | q:push_back(998) 10 | print(q:front()) 11 | print(q:pop_front()) 12 | print(q:empty()) 13 | q:push_back(7766) 14 | q:push_back(8877) 15 | 16 | print(q:pop_front()) 17 | print(q:pop_front()) 18 | print(q:empty()) 19 | 20 | for i=1,50 do 21 | q:push_back(i) 22 | end 23 | print(q:full()) 24 | print(q:empty()) 25 | print("-------性能测试----------") 26 | local count=1000000 27 | testTime(function() 28 | for i=1,count do 29 | if i%1==1 then 30 | q:push_back(i) 31 | else 32 | q:pop_front() 33 | end 34 | end 35 | end) 36 | print(q:empty()) 37 | 38 | ----------其实这个性能非常高,虽然跟前面的测试时间上似乎差不多,循环队列的设计先天保证了没有rehash开销---------------- 39 | -------------------------------------------------------------------------------- /test/containerTest/setTest.lua: -------------------------------------------------------------------------------- 1 | require "luaClass.init" 2 | require "test.containerTest.timeTest" 3 | _ENV=namespace "test" 4 | 5 | using_namespace "container" 6 | 7 | local s=set() 8 | s:insert("998") 9 | print(s:has(998)) 10 | print(s:has("998")) 11 | s:del("998") 12 | print(s:has("998")) 13 | s:insert("xixixix") 14 | s:insert("hahaha") 15 | s:insert("yinyinyin") 16 | s:for_each(function(k) 17 | print(k) 18 | end) 19 | print("------------") 20 | s:del("yinyinyin") 21 | for k in s:iter() do 22 | print(k) 23 | end 24 | 25 | -------------------------------------------------------------------------------- /test/containerTest/stackTest.lua: -------------------------------------------------------------------------------- 1 | require "luaClass.init" 2 | require "test.containerTest.timeTest" 3 | _ENV=namespace "test" 4 | 5 | using_namespace "container" 6 | 7 | local st=stack() 8 | st:push(1) 9 | print(st:empty()) 10 | print(st:pop()) 11 | print(st:empty()) 12 | print("---------------------") 13 | st:push(998) 14 | st:push(8877) 15 | print(st:top()) 16 | print(st:pop()) 17 | print(st:top()) 18 | print("----性能测试---------") 19 | 20 | local count=1000000 21 | st=stack() 22 | testTime(function() 23 | for i=1,count do 24 | if i%2==1 then 25 | st:push(i) 26 | else 27 | st:pop() 28 | end 29 | end 30 | end) 31 | print(st:size()) 32 | print(st:top()) 33 | 34 | --较高的抽象程度,其实绝大部分开销都来自于函数调用,性能还算可以 -------------------------------------------------------------------------------- /test/containerTest/timeTest.lua: -------------------------------------------------------------------------------- 1 | require "luaClass.init" 2 | 3 | _ENV=namespace "test" 4 | 5 | function testTime(luaf) 6 | local startTime=os.clock() 7 | luaf() 8 | local endTime=os.clock() 9 | print("it cost time "..(endTime-startTime).." s ") 10 | end 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /test/normalTest/Equipment.lua: -------------------------------------------------------------------------------- 1 | require "luaClass.init" 2 | require "test.normalTest.Item" 3 | 4 | _ENV=namespace "battle" 5 | using_namespace "luaClass" 6 | 7 | class ("Equipment"){ 8 | super(Item); 9 | public{ 10 | FUNCTION.Equipment(function(self,name,attack,defence) 11 | -- base constructor 12 | self:Item(name,1) 13 | self.attack=attack 14 | self.defence=defence 15 | end); 16 | --override super's method showInfo 17 | FUNCTION.showInfo(function(self) 18 | --call super's method 19 | local super=self.__super 20 | super.showInfo(self) 21 | print(self.attack,self.defence) 22 | 23 | end); 24 | 25 | }; 26 | protected{ 27 | MEMBER.attack(); 28 | MEMBER.defence(); 29 | }; 30 | 31 | } 32 | 33 | -------------------------------------------------------------------------------- /test/normalTest/Role.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | 你会在这里看到最常见的使用方式。 3 | 1.和类型同名即为构造函数 4 | 2.其他名字作为方法 5 | 3.class 存在于luaClass 命名空间中,要么使用完全限定名luaClass.class 6 | 要么给当前环境通过using_namespace 引入这样就可以直接使用class了 7 | 4.命名空间的声明必须是_ENV=namespace (nsName)的形式 8 | 实际上 如果仅仅是lua5.1 可以直接写成 namespace "battle",考虑到版本移植,应该写成第一种形式 9 | 10 | ]] 11 | 12 | require "luaClass.init" 13 | require "test.normalTest.Equipment" 14 | _ENV=namespace "battle" 15 | 16 | using_namespace "luaClass" 17 | using_namespace "container" 18 | 19 | 20 | class ("Role"){ 21 | public{ 22 | FUNCTION.Role(function(self,name) 23 | self.name=name 24 | self.hp=0 25 | self.attack=0 26 | self.defence=0 27 | self.equipments=array() 28 | end); 29 | 30 | FUNCTION.attackRole(function(self,role) 31 | print(self.name.." attack "..role.name) 32 | local hurt=self.attack-role.defence 33 | role.hp=role.hp-hurt 34 | print("oh ! "..role.name.." hurt: "..hurt) 35 | end); 36 | 37 | FUNCTION.speak(function(self,str) 38 | print(self.name.." say "..str) 39 | end); 40 | 41 | FUNCTION.showInfo(function(self) 42 | print(self.name,self.hp,self.attack,self.defence) 43 | end); 44 | }; 45 | public{ 46 | MEMBER.name(); 47 | MEMBER.hp(); 48 | MEMBER.attack(); 49 | MEMBER.defence(); 50 | MEMBER.equipments(); 51 | }; 52 | 53 | } 54 | -------------------------------------------------------------------------------- /test/normalTest/item.lua: -------------------------------------------------------------------------------- 1 | require "luaClass.init" 2 | 3 | 4 | _ENV=namespace "battle" 5 | 6 | using_namespace "luaClass" 7 | 8 | class ("Item"){ 9 | public{ 10 | FUNCTION.Item(function(self,name,number) 11 | self.name=name 12 | self.number=number 13 | end); 14 | 15 | FUNCTION.showInfo(function(self) 16 | print(self.name,self.number) 17 | end); 18 | 19 | }; 20 | public{ 21 | MEMBER.name(); 22 | MEMBER.number(); 23 | }; 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /test/normalTest/test.lua: -------------------------------------------------------------------------------- 1 | require "test.normalTest.Role" 2 | 3 | _ENV=namespace "test" 4 | 5 | using_namespace "battle" 6 | 7 | local role=Role("oneRole") 8 | role.attack=998 9 | role.defence=334 10 | role.hp=9988 11 | role:speak("ok") 12 | role:showInfo() 13 | 14 | local equipment=Equipment("xixixi",3,4) 15 | role.equipments:push_back(equipment) 16 | local role2=Role("tworole") 17 | role:attackRole(role2) 18 | equipment:showInfo() 19 | print(role:serilize()) 20 | 21 | -------------------------------------------------------------------------------- /test/serilizeTest.lua: -------------------------------------------------------------------------------- 1 | require "test.normalTest.Role" 2 | require "profile" 3 | _ENV=namespace "test" 4 | 5 | profiler:start() 6 | 7 | using_namespace "battle" 8 | 9 | local role=Role("xixixi") 10 | role.attack=998 11 | role.defence=888 12 | role.hp=666 13 | role.equipments:push_back(Equipment("xixixi",1,2)) 14 | role.equipments:push_back(Equipment("hahah",1,2)) 15 | role.equipments:push_back(Equipment("yinyinyin",1,2)) 16 | 17 | local file=io.open('test./__serilizeResult.lua','w') 18 | file:write("return \n") 19 | file:write(role:serilize()) 20 | 21 | file:close() 22 | 23 | profiler:stop() -------------------------------------------------------------------------------- /test/test.lua: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CppCXY/luaClass/bf214e7d671d62b4168d2062f7ce823ab8d3cafe/test/test.lua -------------------------------------------------------------------------------- /test/unSerilizeTest.lua: -------------------------------------------------------------------------------- 1 | require "test.normalTest.Role" 2 | 3 | _ENV=namespace "test" 4 | 5 | using_namespace "luaClass" 6 | 7 | local file=io.open('test/__serilizeResult.lua','rb') 8 | if not file then return end 9 | 10 | 11 | local str=file:read("a") 12 | local obj=unSerilize(str) 13 | print(obj:serilize()) 14 | 15 | 16 | --------------------------------------------------------------------------------