108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
end
terra t.methods.null(): t return t { ptr = nil, ct = 0 } end -- maybe should be a macro?
terra t:ref() return self.ptr ~= nil end
t.metamethods.__not = macro(function(self) return `not self:ref() end)
t.metamethods.__apply = macro(function(self,idx) return `self.ptr[ [idx or 0] ] end)
t.metamethods.__update = macro(function(self,idx,rhs)
return quote self.ptr[idx] = rhs end end)
if not ty:isstruct() then
terra t:cmp_raw(other: &ty)
for i=0, self.ct do
if self.ptr[i] ~= other[i] then return false end
end
return true
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
end
terra t.methods.null(): t return t { ptr = nil, ct = 0 } end -- maybe should be a macro?
terra t:ref() return self.ptr ~= nil end
t.metamethods.__not = macro(function(self) return `not self:ref() end)
t.metamethods.__apply = macro(function(self,idx) return `self.ptr[ [idx or 0] ] end)
t.metamethods.__update = macro(function(self,idx,rhs)
return quote self.ptr[idx] = rhs end end)
t.metamethods.__cast = function(from,to,exp)
if to == t then
if from == niltype then return `t.null()
elseif from == &ty then return `t {ptr = exp, ct = 1}
elseif from == ty then return `t {ptr = &exp, ct = 1}
elseif from.N and from.type == ty then
return `t {ptr = &exp[0], ct = from.N }
end
error('invalid cast to ' .. t.name .. ' from ' .. tostring(from))
elseif from == t then
if to == &ty then return `exp.ptr
elseif to == ty then return `@exp.ptr
elseif to == bool then return `exp:ref() end
error('invalid cast from ' .. t.name .. ' to ' .. tostring(to))
end
error('invalid pointer cast')
end
if not ty:isstruct() then
terra t:cmp_raw(other: &ty)
for i=0, self.ct do
if self.ptr[i] ~= other[i] then return false end
end
return true
|