
local class = require('pl.class')

local Magic = class()

function Magic:_init(addr, len, elem_type, elem)
    self.addr = tonumber(addr)
    self.len = tonumber(len)
    self.elem_type = elem_type

    if elem_type == 'uint' then
        self.elem = tonumber(elem)
    elseif elem_type == 'string' then
        self.elem = elem
    else
        self.elem = elem
    end

    -- Add Equality comparison, a Magic is equal if all properties are
    -- equivalent.
    local mt = getmetatable(self)
    mt.__eq = function(a, b)
        for k, v in pairs(a) do
            if b[k] ~= v then return false end
        end
        for k, v in pairs(b) do
            if a[k] ~= v then return false end
        end

        return true
    end
    setmetatable(self, mt)
end

return Magic
