Anonymous user
Module:IP: Difference between revisions
From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
need to use __index to have a unique key which is really private - if outside code gets uniqueKey with getmetatable then they can set it in another table's __eq metamethod, thus foiling isSubnetObject
(mt variable not needed for Subnet; this is similar to IPAddress) |
(need to use __index to have a unique key which is really private - if outside code gets uniqueKey with getmetatable then they can set it in another table's __eq metamethod, thus foiling isSubnetObject) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 488:
-- uniqueKey is a unique, private key used to test whether a given object
-- is a Subnet object.
local uniqueKey = setmetatable({}, {__metatable = false})
-- Private static methods
Line 502:
-- arg - the argument to be validated
validateSubnet = makeValidationFunction('Subnet', isSubnetObject)
-- Metamethods that don't need upvalues
local function subnetEquals(subnet1, subnet2)
local function concatenateSubnets(subnet1, subnet2)
local function subnetToString(subnet)
-- Constructors
makeSubnetFromRaw = function (rawIP, bitLength)
-- Set up structure
local obj =
__eq = function (self, obj)▼
▲ return self:getCIDR() == obj:getCIDR()
▲ end,
▲ return tostring(self) .. tostring(obj)
▲ end,
__tostring = function (self)▼
▲ return self:getCIDR()
▲ end,
__metatable = uniqueKey,▼
})▼
local data = {
rawIP = rawIP,
Line 599 ⟶ 601:
function obj:overlapsSubnet(subnet)
checkSelf(self, 'overlapsSubnet')
local tp = type(subnet)
if tp == 'string' then
subnet = makeSubnet(subnet)
elseif tp == 'table' then
validateSubnet('overlapsSubnet', 1, subnet)
else
checkTypeMulti('overlapsSubnet', 1, subnet, {'string', 'table'})
end
if self:getVersion() == subnet:getVersion() then
return (
Line 625 ⟶ 635:
end
return setmetatable(obj, {
if key == uniqueKey then
return true
end
end,
__eq = subnetEquals,
__concat = concatenateSubnets,
▲ })
end
|