forked from enlightenment/efl
elua: bindings for eina_accessor, accesor support in eina_list bindings, iterator updates
This commit is contained in:
parent
f4d34ce1ab
commit
3ff1633180
|
@ -0,0 +1,135 @@
|
|||
-- EFL LuaJIT bindings: Eina (accessor module)
|
||||
-- For use with Elua
|
||||
|
||||
local ffi = require("ffi")
|
||||
local C = ffi.C
|
||||
|
||||
ffi.cdef [[
|
||||
typedef struct _Eina_Accessor Eina_Accessor;
|
||||
typedef unsigned char Eina_Bool;
|
||||
|
||||
void eina_accessor_free (Eina_Accessor *accessor);
|
||||
Eina_Bool eina_accessor_data_get (Eina_Accessor *accessor,
|
||||
unsigned int position,
|
||||
void **data);
|
||||
Eina_Bool eina_accessor_lock (Eina_Accessor *accessor);
|
||||
Eina_Bool eina_accessor_unlock (Eina_Accessor *accessor);
|
||||
Eina_Accessor *eina_accessor_clone (Eina_Accessor *accessor);
|
||||
void *eina_accessor_container_get(Eina_Accessor *accessor);
|
||||
]]
|
||||
|
||||
local cutil = require("cutil")
|
||||
local util = require("util")
|
||||
|
||||
local M = {}
|
||||
|
||||
local eina
|
||||
|
||||
local init = function()
|
||||
eina = util.lib_load("eina")
|
||||
end
|
||||
|
||||
local shutdown = function()
|
||||
util.lib_unload("eina")
|
||||
end
|
||||
|
||||
ffi.metatype("Eina_Accessor", {
|
||||
__index = {
|
||||
free = function(self) eina.eina_accessor_free(ffi.gc(self, nil)) end,
|
||||
data_get = function(self, position)
|
||||
local data = ffi.new("void*[1]")
|
||||
local r = eina.eina_accessor_data_get(self, position - 1, data)
|
||||
if r == 0 then return nil end
|
||||
return data[0]
|
||||
end,
|
||||
lock = function(self) return eina.eina_accessor_lock (self) ~= 0 end,
|
||||
unlock = function(self) return eina.eina_accessor_unlock(self) ~= 0 end,
|
||||
clone = function(self)
|
||||
local v = eina.eina_accessor_clone(self)
|
||||
if v == nil then return nil end
|
||||
return v
|
||||
end,
|
||||
container_get = function(self)
|
||||
local v = eina.eina_accessor_container_get(self)
|
||||
if v == nil then return nil end
|
||||
return v
|
||||
end
|
||||
}
|
||||
})
|
||||
|
||||
cutil.init_module(init, shutdown)
|
||||
|
||||
M.Accessor = util.Object:clone {
|
||||
__ctor = function(self, acc)
|
||||
-- prevent null stuff
|
||||
if acc == nil then acc = nil end
|
||||
if acc then ffi.gc(acc, acc.free) end
|
||||
self.__eq = function(self, other)
|
||||
return self.__accessor == other.__accessor
|
||||
end
|
||||
self.__call = function(self)
|
||||
return self:next()
|
||||
end
|
||||
local oi = self.__index
|
||||
self.__index = function(self, name)
|
||||
if type(name) == "number" then return self:data_get(name) end
|
||||
return oi[name]
|
||||
end
|
||||
self.__accessor = acc
|
||||
end,
|
||||
|
||||
free = function(self)
|
||||
if not self.__accessor then return end
|
||||
self.__accessor:free()
|
||||
self._accessor = nil
|
||||
end,
|
||||
|
||||
disown = function(self)
|
||||
local acc = self.__accessor
|
||||
self.__accessor = nil
|
||||
return acc
|
||||
end,
|
||||
|
||||
rebase = function(self, acc)
|
||||
self:free()
|
||||
self.__accessor = acc:disown()
|
||||
end,
|
||||
|
||||
lock = function(self)
|
||||
if not self.__accessor then return false end
|
||||
return self.__accessor:lock()
|
||||
end,
|
||||
|
||||
unlock = function(self)
|
||||
if not self.__accessor then return false end
|
||||
return self.__accessor:unlock()
|
||||
end,
|
||||
|
||||
clone = function(self)
|
||||
if not self.__accessor then return nil end
|
||||
return self.__proto(self.__accessor:clone())
|
||||
end,
|
||||
|
||||
data_get = function(self, pos)
|
||||
if not self.__accessor then return nil end
|
||||
return self.__accessor:data_get(pos)
|
||||
end,
|
||||
|
||||
container_get = function(self)
|
||||
if not self.__accessor then return end
|
||||
return self.__accessor:container_get()
|
||||
end,
|
||||
|
||||
each = function(self)
|
||||
local acc = self.__accessor
|
||||
if not acc then return nil end
|
||||
local cnt = 0
|
||||
return function()
|
||||
cnt = cnt + 1
|
||||
local v = self:data_get(cnt)
|
||||
if v then return cnt, v end
|
||||
end
|
||||
end
|
||||
}
|
||||
|
||||
return M
|
|
@ -36,7 +36,7 @@ ffi.metatype("Eina_Iterator", {
|
|||
next = function(self)
|
||||
local data = ffi.new("void*[1]")
|
||||
local r = eina.eina_iterator_next(self, data)
|
||||
if r == nil then return nil end
|
||||
if r == 0 then return nil end
|
||||
return data[0]
|
||||
end,
|
||||
lock = function(self) return eina.eina_iterator_lock (self) ~= 0 end,
|
||||
|
@ -83,22 +83,22 @@ M.Iterator = util.Object:clone {
|
|||
end,
|
||||
|
||||
next = function(self)
|
||||
if not self.__iterator then return end
|
||||
if not self.__iterator then return nil end
|
||||
return self.__iterator:next()
|
||||
end,
|
||||
|
||||
lock = function(self)
|
||||
if not self.__iterator then return end
|
||||
if not self.__iterator then return false end
|
||||
return self.__iterator:lock()
|
||||
end,
|
||||
|
||||
unlock = function(self)
|
||||
if not self.__iterator then return end
|
||||
if not self.__iterator then return false end
|
||||
return self.__iterator:unlock()
|
||||
end,
|
||||
|
||||
container_get = function(self)
|
||||
if not self.__iterator then return end
|
||||
if not self.__iterator then return nil end
|
||||
return self.__iterator:container_get()
|
||||
end
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
local ffi = require("ffi")
|
||||
|
||||
local iterator = require("eina.iterator")
|
||||
local accessor = require("eina.accessor")
|
||||
|
||||
ffi.cdef [[
|
||||
typedef unsigned char Eina_Bool;
|
||||
|
@ -54,7 +55,7 @@ ffi.cdef [[
|
|||
void *eina_list_search_unsorted(const Eina_List *list, Eina_Compare_Cb func, const void *data);
|
||||
Eina_Iterator *eina_list_iterator_new(const Eina_List *list);
|
||||
Eina_Iterator *eina_list_iterator_reversed_new(const Eina_List *list);
|
||||
// Eina_Accessor *eina_list_accessor_new(const Eina_List *list);
|
||||
Eina_Accessor *eina_list_accessor_new(const Eina_List *list);
|
||||
]]
|
||||
|
||||
local cutil = require("cutil")
|
||||
|
@ -103,6 +104,21 @@ M.Reverse_Iterator = Iterator:clone {
|
|||
end
|
||||
}
|
||||
|
||||
local Accessor = accessor.Accessor
|
||||
|
||||
M.Accessor = Accessor:clone {
|
||||
__ctor = function(self, list)
|
||||
self.__list = list
|
||||
return Accessor.__ctor(self, eina.eina_list_accessor_new(list.__list))
|
||||
end,
|
||||
|
||||
data_get = function(self, pos)
|
||||
local v = Accessor.data_get(self, pos)
|
||||
if not v then return nil end
|
||||
return self.__list:data_get(v)
|
||||
end
|
||||
}
|
||||
|
||||
local get_list_t = function(list)
|
||||
return ffi.cast("Eina_List_T*", list)
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue