object.lua 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. --[[
  2. Object base class
  3. @author : Siapran Candoris
  4. Features instance of class determination and prototype function (init)
  5. ]]--
  6. local make_class = require("class")
  7. --------------------------------
  8. -- object class
  9. --
  10. local object = make_class()
  11. function object:new( ... )
  12. local res = {}
  13. setmetatable(res, self)
  14. if res.init then
  15. res:init(...)
  16. end
  17. return res
  18. end
  19. function object:instanceof( class )
  20. local cache = self.__instanceof_cache
  21. if cache[class] ~= nil then
  22. return cache[class]
  23. else
  24. for _,v in ipairs(self.__genealogy) do
  25. if class == v then
  26. cache[class] = true
  27. return true
  28. end
  29. end
  30. cache[class] = false
  31. return false
  32. end
  33. end
  34. -- general purpose utils
  35. -- asssign unique ids to tables
  36. do
  37. local cache = setmetatable({}, {__mode = "k"})
  38. local id = 0
  39. function identifier( table )
  40. if cache[table] then
  41. return cache[table]
  42. else
  43. cache[table] = id
  44. id = id + 1
  45. return id
  46. end
  47. end
  48. end
  49. return object