user2328978
user2328978

Reputation: 55

lua userdata with array access and object oriented access?

I'm writing a "C" userdata array structure. As setter and getter i want normal array access (u[0] = 1 u[0]) like it's discussed here: [c array share][1]Share Array between lua and C. For that i need to set __index and __newindex to the set and get functions in c.

Additional i want object-oriented access, too, "like u:mymethod()". My trouble is, that i need to set now __index to the metatable itself.

Is there a way, to achieve both?

Upvotes: 1

Views: 1712

Answers (1)

user2008074
user2008074

Reputation:

Just one of many possible ways to achieve this:

local userdata = { _array = { "A", "B", "C" } }
local mt = { }
local methods = { }

function mt.__index(userdata, k)
    if methods[k] then
        return methods[k]
    else
        return rawget(userdata, "_array")[k]
    end
end

function mt.__newindex(userdata, k, v)
    if methods[k] then
        error "can't assign to method!"
    else
        rawget(userdata, "_array")[k] = v
    end
end

function methods.count(userdata)
    return #rawget(userdata, "_array")
end

setmetatable(userdata, mt)

userdata[3] = "Z"

print(userdata[1])
print(userdata[2])
print(userdata[3])
print(userdata:count())

userdata.count = 0

edit: As lhf pointed in his comment, it is not dangerous to use metatable as it's __index table at all, because c-methods should always check on what self they operate.

Upvotes: 4

Related Questions