object.lua 1000 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. -- asssign unique ids to tables
  35. do
  36. local cache = setmetatable({}, {__mode = "k"})
  37. local id = 0
  38. function identifier( table )
  39. if cache[table] then
  40. return cache[table]
  41. else
  42. cache[table] = id
  43. id = id + 1
  44. return id
  45. end
  46. end
  47. end
  48. return object