1
0
Fork 0
mirror of https://github.com/notwa/mm synced 2024-11-05 02:39:02 -08:00

refactor "A" boilerplate

less memory. maybe faster?
This commit is contained in:
Connor Olding 2015-05-26 10:22:18 -07:00
parent e6ad4d05d8
commit 307e3ac751

View file

@ -25,35 +25,35 @@ X3 = m.read_u24_be
X4 = m.read_u32_be X4 = m.read_u32_be
XF = function(addr) return m.readfloat(addr, true) end XF = function(addr) return m.readfloat(addr, true) end
local readers = { local H1 = function(self, value)
[1] = R1, return value and W1(self.addr, value) or R1(self.addr)
[2] = R2, end
[3] = R3, local H2 = function(self, value)
[4] = R4, return value and W2(self.addr, value) or R2(self.addr)
['f'] = RF, end
} local H3 = function(self, value)
return value and W3(self.addr, value) or R3(self.addr)
end
local H4 = function(self, value)
return value and W4(self.addr, value) or R4(self.addr)
end
local HF = function(self, value)
return value and WF(self.addr, value) or RF(self.addr)
end
local writers = { local mts = {
[1] = W1, [1] = {__call = H1},
[2] = W2, [2] = {__call = H2},
[3] = W3, [3] = {__call = H3},
[4] = W4, [4] = {__call = H4},
['f'] = WF, ['f'] = {__call = HF},
}
local mt = {
__call = function(self, value)
return value and self.write(self.addr, value) or self.read(self.addr)
end
} }
function A(addr, atype) function A(addr, atype)
-- TODO: inherit type, read, and write fields from appropriate class local mt = mts[atype]
return setmetatable({ return setmetatable({
addr=addr, addr=addr,
type=atype, type=atype,
read=readers[atype],
write=writers[atype]
}, mt) }, mt)
end end