fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

BkacjiosMore class changes27686d7

settings-overhaul
3.0 KiB127 linesraw
1local class = require("class")
2
3local OBJECT = {}
4
5function OBJECT:__call(...)
6	if not self[self.__constructor] then
7		return error(string.format("can not create object for class '%s' (no constructor)", self))
8	end
9	self[self.__constructor](self, ...)
10	return self
11end
12
13function OBJECT:__index(key)
14	-- Check for any specific, class-based, methods like "super"
15	local meta_index = rawget(getmetatable(self), key) -- OBJECT
16	if meta_index ~= nil then
17		-- Only return if not nil
18		return meta_index
19	end
20
21	-- Try the base class next
22	local base = rawget(self, "__baseclass")
23	if base then
24		-- Only do the lookup once to prevent infinite __index calls
25		local value = base[key]
26		if value ~= nil then
27			-- Only return if not nil
28			return value
29		end
30	end
31
32	-- Welp, nothing to find
33	return nil
34end
35
36function OBJECT:super(method, ...)
37	local base = self
38
39	-- Attempt to determine the base via the current scope of the call
40	for i=1, self.__superscope do
41		base = rawget(base, "__baseclass")
42	end
43
44	-- Error if the scope of the call has no baseclass (AKA the main class)
45	if not base then return error("attempted to call method 'super' in class with no baseclass") end
46
47	local call = method or base.__constructor
48
49	if base[call] == nil then
50		return error(string.format("attempted to call method '%s' (a nil value)", call))
51	end
52
53	-- +1 to the scope
54	self.__superscope = self.__superscope + 1
55
56	-- Call the method
57	-- This can trigger another super call, so thats why we need __superscope
58	local ret = base[call](self, ...)
59
60	-- -1 from the scope
61	self.__superscope = self.__superscope - 1
62
63	-- return super return
64	return ret
65end
66
67function OBJECT:getBaseClass()
68	return self.__baseclass
69end
70
71function OBJECT:getClass()
72	local super = getmetatable(self)
73	return (super ~= OBJECT and super or nil)
74end
75
76function OBJECT:__tostring()
77	return string.format("%s::%s", self.__baseclass or "", self.__classname)
78end
79
80local object = {}
81
82do
83	local function copy(object)
84		local lookup_table = {}
85		local function _copy(object)
86			if type(object) ~= "table" then
87				return object
88			elseif lookup_table[object] then
89				return lookup_table[object]
90			end
91			local new_table = {}
92			lookup_table[object] = new_table
93			for index, value in pairs(object) do
94				new_table[_copy(index)] = _copy(value)
95			end
96			return setmetatable(new_table, getmetatable(object))
97		end
98		return _copy(object)
99	end
100
101	local function createObject(name)
102		local main = class.getStruct(name)
103		local base = class.getBaseClassname(name)
104		local obj = setmetatable(copy(main), OBJECT)
105		if base then
106			obj.__baseclass = createObject(base)
107		end
108		return obj
109	end
110
111	function object.new(name, ...)
112		if not class.getStruct(name) then
113			return error(("failed to create object for unknown class '%s'"):format(name)) 
114		end
115
116		-- Create a copy of the struct, with metatable and all
117		local obj = createObject(name)
118
119		-- Set values that are unique to this instance
120		obj.__superscope = 1
121
122		-- Return our new object and call constructor
123		return obj(...)
124	end
125end
126
127return object