You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
910 B

  1. local Registry = class("Registry")
  2. Registry.classes_ = {}
  3. function Registry.add(cls, name)
  4. assert(type(cls) == "table" and cls.__cname ~= nil, "Registry.add() - invalid class")
  5. if not name then name = cls.__cname end
  6. assert(Registry.classes_[name] == nil, "Registry.add() - class \"%s\" already exists", tostring(name))
  7. Registry.classes_[name] = cls
  8. end
  9. function Registry.remove(name)
  10. assert(Registry.classes_[name] ~= nil, "Registry.remove() - class \"%s\" not found", name)
  11. Registry.classes_[name] = nil
  12. end
  13. function Registry.newObject(name)
  14. local cls = Registry.classes_[name]
  15. if not cls then
  16. -- auto load
  17. pcall(function()
  18. cls = require(name)
  19. Registry.add(cls, name)
  20. end)
  21. end
  22. assert(cls ~= nil, string.format("Registry.newObject() - invalid class \"%s\"", tostring(name)))
  23. return cls:new()
  24. end
  25. return Registry