summaryrefslogtreecommitdiff
path: root/src/bindings/luajit/eina/counter.lua
blob: a188d48ecdc11b71e968cbd862cee066be6d5253 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
-- EFL LuaJIT bindings: Eina (counter module)
-- For use with Elua

local ffi = require("ffi")
local C = ffi.C

ffi.cdef [[
    typedef struct _Eina_Counter Eina_Counter;
    Eina_Counter *eina_counter_new  (const char   *name);
    void          eina_counter_free (Eina_Counter *counter);
    void          eina_counter_start(Eina_Counter *counter);
    void          eina_counter_stop (Eina_Counter *counter, int specimen);
    char         *eina_counter_dump (Eina_Counter *counter);

    void free(void*);
]]

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

cutil.init_module(init, shutdown)

M.Counter = ffi.metatype("Eina_Counter", {
    __new = function(self, name)
        return ffi.gc(eina.eina_counter_new(name), self.free)
    end,
    __index = {
        free = function(self)
            return eina.eina_counter_free(ffi.gc(self, nil))
        end,

        start = function(self)
            return eina.eina_counter_start(self)
        end,

        stop = function(self, specimen)
            return eina.eina_counter_stop(self, specimen)
        end,

        dump = function(self)
            local v = eina.eina_counter_dump(self)
            if v == nil then return nil end
            local r = ffi.string(v)
            C.free(v)
            return r
        end
    }
})

return M