106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
self.ct = self.ct - n
return self.ptr
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}
|
|
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
self.ct = self.ct - n
return self.ptr
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.__add = terra(self: &t, sz: intptr): t
var n = @self n:advance(sz) return n
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}
|