• 80.30% Rate
  • 53 Hits
  • 13 Missed
  • 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
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 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
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 8x
  • 2x
  • 6x
  • 3x
  • 3x
  • 3x
  • 1x
  • 1x
  • 14x
  • 7x
  • 2x
  • 2x
  • 6x
  • 1x
  • 6x
  • 6x
  • 6x
  • 12x
  • 6x
  • 7x
  • 7x
  • 2x
  • 2x
  • 6x
  • 7x
  • 2x
  • 2x
  • 6x
  • 2x
  • 2x
  • 5x
  • 5x
  • 5x
  • 10x
  • 5x
  • 5x
  • 5x
  • 5x
  • 1x
  • 1x
  • 1x
  • 1x
  • -------------------------------------------------------------------------------
  • -- Wraper for PUMAS recorders
  • -- Author: Valentin Niess
  • -- License: GNU LGPL-3.0
  • -------------------------------------------------------------------------------
  • local ffi = require('ffi')
  • local call = require('pumas.call')
  • local clib = require('pumas.clib')
  • local enum = require('pumas.enum')
  • local error = require('pumas.error')
  • local medium = require('pumas.medium')
  • local metatype = require('pumas.metatype')
  • local state = require('pumas.state')
  • local recorder = {}
  • -------------------------------------------------------------------------------
  • -- The recorder metatype
  • -------------------------------------------------------------------------------
  • local Recorder = {}
  • function Recorder:__index (k)
  • if k == '__metatype' then
  • return 'Recorder'
  • elseif k == 'period' then
  • return self._c.period
  • elseif k == 'record' then
  • return self._record
  • else
  • error.raise{
  • ['type'] = 'Recorder',
  • bad_member = k
  • }
  • end
  • end
  • do
  • local state_size = ffi.sizeof('struct pumas_state')
  • function Recorder:__newindex (k, v)
  • if k == 'record' then
  • if (v ~= 'nil') and (type(v) ~= 'function') then
  • error.raise{['type'] = 'Recorder', argname = 'record',
  • expected = 'a function', got = metatype.a(v)}
  • end
  • if self._c.record ~= nil then
  • self._c.record:free()
  • end
  • if v then
  • local wrapped_state = state.State()
  • local wrapped_event = enum.Event()
  • self._c.record = ffi.cast('pumas_recorder_cb *',
  • function (_, c_state, c_medium, c_event)
  • local wrapped_medium = medium.get(c_medium)
  • ffi.copy(wrapped_state._c, c_state, state_size)
  • wrapped_event._value = c_event
  • v(wrapped_state, wrapped_medium, wrapped_event)
  • end)
  • else
  • self._c.record = nil
  • end
  • rawset(self, '_record', v)
  • elseif k == 'period' then
  • if (v ~= 'nil') and (type(v) ~= 'number') then
  • error.raise{['type'] = 'Recorder', argname = 'period',
  • expected = 'a number', got = metatype.a(v)}
  • end
  • self._c.period = v
  • else
  • error.raise{
  • ['type'] = 'Recorder',
  • bad_member = k
  • }
  • end
  • end
  • end
  • -------------------------------------------------------------------------------
  • -- The recorder constructor
  • -------------------------------------------------------------------------------
  • local function default_callback (state_, medium_, event)
  • print(event, medium_, state_) -- XXX pretty print
  • end
  • do
  • local function new (cls, callback, period)
  • if (callback ~= nil) and (type(callback) ~= 'function') then
  • error.raise{fname = 'Recorder', argnum = 1, expected = 'a function',
  • got = metatype.a(callback)}
  • end
  • if (period ~= nil) and (type(period) ~= 'number') then
  • error.raise{fname = 'Recorder', argnum = 2, expected = 'a number',
  • got = metatype.a(period)}
  • end
  • local ptr = ffi.new('struct pumas_recorder *[1]')
  • call(clib.pumas_recorder_create, ptr, 0)
  • local c = ptr[0]
  • ffi.gc(c, function ()
  • if c.record ~= nil then
  • c.record:free()
  • end
  • clib.pumas_recorder_destroy(ptr)
  • end)
  • local self = setmetatable({_c = c}, cls)
  • self.record = callback or default_callback
  • self.period = period or 1
  • return self
  • end
  • recorder.Recorder = setmetatable(Recorder, {__call = new})
  • end
  • -------------------------------------------------------------------------------
  • -- Register the subpackage
  • -------------------------------------------------------------------------------
  • function recorder.register_to (t)
  • t.Recorder = recorder.Recorder
  • end
  • -------------------------------------------------------------------------------
  • -- Return the package
  • -------------------------------------------------------------------------------
  • return recorder