2010-09-28 16:23:00 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2010 Uli Schlachter
|
|
|
|
-- @release @AWESOME_VERSION@
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local pairs = pairs
|
|
|
|
local type = type
|
|
|
|
local error = error
|
|
|
|
|
2012-06-12 10:13:46 +02:00
|
|
|
-- gears.object
|
|
|
|
local object = { mt = {} }
|
2010-09-28 16:23:00 +02:00
|
|
|
|
|
|
|
-- Verify that obj is indeed a valid object as returned by new()
|
|
|
|
local function check(obj)
|
|
|
|
if type(obj) ~= "table" or type(obj._signals) ~= "table" then
|
|
|
|
error("add_signal() called on non-object")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-29 21:38:06 +02:00
|
|
|
--- Find a given signal
|
2010-09-28 16:23:00 +02:00
|
|
|
-- @param obj The object to search in
|
|
|
|
-- @param name The signal to find
|
|
|
|
-- @param error_msg Error message for if the signal is not found
|
|
|
|
-- @returns The signal table
|
|
|
|
local function find_signal(obj, name, error_msg)
|
|
|
|
check(obj)
|
|
|
|
if not obj._signals[name] then
|
2012-10-14 16:58:54 +02:00
|
|
|
error("Trying to " .. error_msg .. " non-existent signal '" .. name .. "'")
|
2010-09-28 16:23:00 +02:00
|
|
|
end
|
|
|
|
return obj._signals[name]
|
|
|
|
end
|
|
|
|
|
2010-09-29 21:38:06 +02:00
|
|
|
--- Add a signal to an object. All signals must be added before they can be used.
|
2010-09-28 16:23:00 +02:00
|
|
|
-- @param obj The object
|
|
|
|
-- @param name The name of the new signal.
|
2012-06-12 10:13:46 +02:00
|
|
|
function object.add_signal(obj, name)
|
2010-09-28 16:23:00 +02:00
|
|
|
check(obj)
|
|
|
|
if not obj._signals[name] then
|
|
|
|
obj._signals[name] = {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-29 21:38:06 +02:00
|
|
|
--- Connect to a signal
|
2010-09-28 16:23:00 +02:00
|
|
|
-- @param obj The object
|
|
|
|
-- @param name The name of the signal
|
|
|
|
-- @param func The callback to call when the signal is emitted
|
2012-06-12 10:13:46 +02:00
|
|
|
function object.connect_signal(obj, name, func)
|
2010-09-28 16:23:00 +02:00
|
|
|
local sig = find_signal(obj, name, "connect to")
|
|
|
|
sig[func] = func
|
|
|
|
end
|
|
|
|
|
2010-09-29 21:38:06 +02:00
|
|
|
--- Disonnect to a signal
|
2010-09-28 16:23:00 +02:00
|
|
|
-- @param obj The object
|
|
|
|
-- @param name The name of the signal
|
|
|
|
-- @param func The callback that should be disconnected
|
2012-06-12 10:13:46 +02:00
|
|
|
function object.disconnect_signal(obj, name, func)
|
2010-09-28 16:23:00 +02:00
|
|
|
local sig = find_signal(obj, name, "disconnect from")
|
|
|
|
sig[func] = nil
|
|
|
|
end
|
|
|
|
|
2010-09-29 21:38:06 +02:00
|
|
|
--- Emit a signal
|
2010-09-28 16:23:00 +02:00
|
|
|
-- @param obj The object
|
|
|
|
-- @param name The name of the signal
|
|
|
|
-- @param ... Extra arguments for the callback functions. Each connected
|
|
|
|
-- function receives the object as first argument and then any extra
|
|
|
|
-- arguments that are given to emit_signal()
|
2012-06-12 10:13:46 +02:00
|
|
|
function object.emit_signal(obj, name, ...)
|
2010-09-28 16:23:00 +02:00
|
|
|
local sig = find_signal(obj, name, "emit")
|
|
|
|
for func in pairs(sig) do
|
|
|
|
func(obj, ...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Returns a new object. You can call :emit_signal(), :disconnect_signal,
|
|
|
|
-- :connect_signal() and :add_signal() on the resulting object.
|
|
|
|
local function new()
|
|
|
|
local ret = {}
|
|
|
|
|
|
|
|
-- Copy all our global functions to our new object
|
2012-06-12 10:13:46 +02:00
|
|
|
for k, v in pairs(object) do
|
2010-09-28 16:23:00 +02:00
|
|
|
if type(v) == "function" then
|
|
|
|
ret[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ret._signals = {}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2012-06-12 10:13:46 +02:00
|
|
|
function object.mt:__call(...)
|
|
|
|
return new(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(object, object.mt)
|
2010-09-28 16:23:00 +02:00
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|