Files
ox_lib/imports/class/shared.lua

56 lines
1.4 KiB
Lua
Raw Normal View History

lib.print.warn('ox_lib\'s class module is experimental and may break without warning.')
-- Fields added to the "private" field on a class will not be msgpacked.
-- Use setters/getters when working with these values.
local private_mt = {
__ext = 0,
__pack = function() return '' end,
}
---Ensure the given argument or property has a valid type, otherwise throwing an error.
2023-12-18 11:17:48 +11:00
---@param id number | string
---@param var any
---@param expected type
local function assertType(id, var, expected)
local received = type(var)
if received ~= expected then
error(("expected %s %s to have type '%s' (received %s)"):format(type(id) == 'string' and 'field' or 'argument', id, expected, received), 3)
end
return true
end
local mixins = {}
---Creates a new instance of the given class.
function mixins.new(class, obj)
if obj.private then
2023-12-18 11:17:48 +11:00
assertType('private', obj.private, 'table')
setmetatable(obj.private, private_mt)
end
setmetatable(obj, class)
if class.init then obj:init() end
return obj
end
---Creates a new class.
2023-12-24 16:12:59 +11:00
---@todo add inherited types for new/private/init fields (not yet supported by lls)
2023-12-18 11:17:48 +11:00
---@param name string
---@param super? table
function lib.class(name, super)
2023-12-18 11:17:48 +11:00
assertType(1, name, 'string')
local class = table.clone(mixins)
class.__name = name
class.__index = class
return super and setmetatable(class, super) or class
end
return lib.class