| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- local class = require("class")
- local ffi = require("ffi")
- local rawget = rawget
- local c_id = 0
- local function make_C_identifier( )
- local res = "id" .. c_id
- c_id = c_id + 1
- return res
- end
- local function ffi_make_meta( self )
- local c_id = make_C_identifier()
- ffi.cdef("typedef struct {" .. self.__cdef .. "} " .. c_id ..";")
- self.__ctype = ffi.metatype(c_id, self)
- self.__c_id = c_id
- end
- local function ffi_new(self, ...)
- if not rawget(self, "__ctype") then
- ffi_make_meta(self)
- end
- local res = self.__ctype()
- if self.init then
- res:init(...)
- end
- return res
- end
- local function ffi_new_array(self, nb_elem, ...)
- if not rawget(self, "__ctype") then
- ffi_make_meta(self)
- end
- local res = ffi.new(self.__c_id .. "[?]", nb_elem)
- if self.init then
- for i = 0, nb_elem - 1 do
- res[i]:init(...)
- end
- end
- return res
- end
- local function make_ffi_class(structdef, ...)
- local res = class(...)
- res.new = ffi_new
- res.new_array = ffi_new_array
- res.__cdef = structdef
- res.is_ffi_class = true
- return res
- end
- return make_ffi_class
|