2010-09-28 16:23:00 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2010 Uli Schlachter
|
|
|
|
-- @release @AWESOME_VERSION@
|
2014-05-19 15:15:39 +02:00
|
|
|
-- @classmod gears.object
|
2010-09-28 16:23:00 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local pairs = pairs
|
|
|
|
local type = type
|
|
|
|
local error = error
|
|
|
|
|
2012-06-12 10:13:46 +02:00
|
|
|
local object = { mt = {} }
|
2010-09-28 16:23:00 +02:00
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Verify that obj is indeed a valid object as returned by new()
|
2010-09-28 16:23:00 +02:00
|
|
|
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
|
2012-11-19 14:09:10 +01:00
|
|
|
-- @return The signal table
|
2010-09-28 16:23:00 +02:00
|
|
|
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 name The name of the new signal.
|
2012-11-19 13:40:37 +01:00
|
|
|
function object:add_signal(name)
|
|
|
|
check(self)
|
2015-01-11 11:04:33 +01:00
|
|
|
assert(type(name) == "string", "name must be a string, got: " .. type(name))
|
2012-11-19 13:40:37 +01:00
|
|
|
if not self._signals[name] then
|
2015-06-14 14:21:23 +02:00
|
|
|
self._signals[name] = {
|
|
|
|
strong = {},
|
2015-11-19 18:11:51 +01:00
|
|
|
weak = setmetatable({}, { __mode = "kv" })
|
2015-06-14 14:21:23 +02:00
|
|
|
}
|
2010-09-28 16:23:00 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-29 21:38:06 +02:00
|
|
|
--- Connect to a signal
|
2010-09-28 16:23:00 +02:00
|
|
|
-- @param name The name of the signal
|
|
|
|
-- @param func The callback to call when the signal is emitted
|
2012-11-19 13:40:37 +01:00
|
|
|
function object:connect_signal(name, func)
|
2015-01-11 11:04:33 +01:00
|
|
|
assert(type(func) == "function", "callback must be a function, got: " .. type(func))
|
2012-11-19 13:40:37 +01:00
|
|
|
local sig = find_signal(self, name, "connect to")
|
2015-06-14 14:21:23 +02:00
|
|
|
assert(sig.weak[func] == nil, "Trying to connect a strong callback which is already connected weakly")
|
|
|
|
sig.strong[func] = true
|
|
|
|
end
|
|
|
|
|
2015-11-19 18:11:51 +01:00
|
|
|
local function make_the_gc_obey(func)
|
|
|
|
if _VERSION <= "Lua 5.1" then
|
|
|
|
-- Lua 5.1 only has the behaviour we want if a userdata is used as the
|
|
|
|
-- value in a weak table. Thus, do some magic so that we get a userdata.
|
|
|
|
|
|
|
|
local userdata = newproxy(true)
|
|
|
|
getmetatable(userdata).__gc = function() end
|
|
|
|
-- Now bind the lifetime of userdata to the lifetime of func. For this,
|
|
|
|
-- we mess with the function's environment and add a table for all the
|
|
|
|
-- various userdata that it should keep alive.
|
|
|
|
local key = "_secret_key_used_by_gears_object_in_Lua51"
|
|
|
|
local old_env = getfenv(func)
|
|
|
|
if old_env[key] then
|
|
|
|
-- Assume the code in the else branch added this and the function
|
|
|
|
-- already has its own, private environment
|
|
|
|
table.insert(old_env[key], userdata)
|
|
|
|
else
|
|
|
|
-- No table yet, add it
|
|
|
|
local new_env = { [key] = { userdata } }
|
|
|
|
setmetatable(new_env, { __index = old_env, __newindex = old_env })
|
|
|
|
setfenv(func, new_env)
|
|
|
|
end
|
|
|
|
assert(_G[key] == nil, "Something broke, things escaped to _G")
|
|
|
|
return userdata
|
|
|
|
end
|
|
|
|
-- Lua 5.2+ already behaves the way we want with functions directly, no magic
|
|
|
|
return func
|
|
|
|
end
|
|
|
|
|
2015-06-14 14:21:23 +02:00
|
|
|
--- Connect to a signal weakly. This allows the callback function to be garbage
|
|
|
|
-- collected and automatically disconnects the signal when that happens.
|
|
|
|
-- @param name The name of the signal
|
|
|
|
-- @param func The callback to call when the signal is emitted
|
|
|
|
function object:weak_connect_signal(name, func)
|
|
|
|
assert(type(func) == "function", "callback must be a function, got: " .. type(func))
|
|
|
|
local sig = find_signal(self, name, "connect to")
|
|
|
|
assert(sig.strong[func] == nil, "Trying to connect a weak callback which is already connected strongly")
|
2015-11-19 18:11:51 +01:00
|
|
|
sig.weak[func] = make_the_gc_obey(func)
|
2010-09-28 16:23:00 +02:00
|
|
|
end
|
|
|
|
|
2010-09-29 21:38:06 +02:00
|
|
|
--- Disonnect to a signal
|
2010-09-28 16:23:00 +02:00
|
|
|
-- @param name The name of the signal
|
|
|
|
-- @param func The callback that should be disconnected
|
2012-11-19 13:40:37 +01:00
|
|
|
function object:disconnect_signal(name, func)
|
|
|
|
local sig = find_signal(self, name, "disconnect from")
|
2015-06-14 14:21:23 +02:00
|
|
|
sig.weak[func] = nil
|
|
|
|
sig.strong[func] = nil
|
2010-09-28 16:23:00 +02:00
|
|
|
end
|
|
|
|
|
2010-09-29 21:38:06 +02:00
|
|
|
--- Emit a signal
|
2014-05-19 15:15:39 +02:00
|
|
|
--
|
2010-09-28 16:23:00 +02:00
|
|
|
-- @param name The name of the signal
|
|
|
|
-- @param ... Extra arguments for the callback functions. Each connected
|
2014-05-19 15:15:39 +02:00
|
|
|
-- function receives the object as first argument and then any extra arguments
|
|
|
|
-- that are given to emit_signal()
|
2012-11-19 13:40:37 +01:00
|
|
|
function object:emit_signal(name, ...)
|
|
|
|
local sig = find_signal(self, name, "emit")
|
2015-06-14 14:21:23 +02:00
|
|
|
for func in pairs(sig.strong) do
|
|
|
|
func(self, ...)
|
|
|
|
end
|
|
|
|
for func in pairs(sig.weak) do
|
2012-11-19 13:40:37 +01:00
|
|
|
func(self, ...)
|
2010-09-28 16:23:00 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Returns a new object. You can call :emit_signal(), :disconnect_signal,
|
2010-09-28 16:23:00 +02:00
|
|
|
-- :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
|
|
|
|
|
2015-07-23 21:42:54 +02:00
|
|
|
--- Helper function to get the module name out of `debug.getinfo`.
|
|
|
|
-- @usage
|
|
|
|
-- local mt = {}
|
|
|
|
-- mt.__tostring = function(o)
|
|
|
|
-- return require("gears.object").modulename(2)
|
|
|
|
-- end
|
|
|
|
-- return setmetatable(ret, mt)
|
|
|
|
--
|
|
|
|
-- @tparam[opt=2] integer level Level for `debug.getinfo(level, "S")`.
|
|
|
|
-- Typically 2 or 3.
|
|
|
|
-- @treturn string The module name, e.g. "wibox.widget.background".
|
|
|
|
function object.modulename(level)
|
|
|
|
return debug.getinfo(level, "S").source:gsub(".*/lib/", ""):gsub("/", "."):gsub("%.lua", "")
|
|
|
|
end
|
|
|
|
|
2012-06-12 10:13:46 +02:00
|
|
|
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
|