From 42dceb1db8a8dc73acd5a8abef5d4ffe9818dad8 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Sat, 19 Oct 2019 20:17:31 -0400 Subject: [PATCH] object: Add a standard implementation for append and remove. This is for legacy accessors only, but it will be expanded later. --- lib/gears/object/properties.lua | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/gears/object/properties.lua b/lib/gears/object/properties.lua index ed072fc2..6118ef4c 100644 --- a/lib/gears/object/properties.lua +++ b/lib/gears/object/properties.lua @@ -193,7 +193,8 @@ local function copy_object(obj, to_set, name, capi_name, is_object, join_if, set }) end -function object._legacy_accessors(obj, name, capi_name, is_object, join_if, set_empty, delay) +function object._legacy_accessors(obj, name, capi_name, is_object, join_if, set_empty, delay, add_append_name) + delay = delay or add_append_name -- Some objects have a special "object" property to add more properties, but -- not all. @@ -284,6 +285,31 @@ function object._legacy_accessors(obj, name, capi_name, is_object, join_if, set_ obj, objs, name, capi_name, is_object, join_if, set_empty ) end + + if add_append_name then + magic_obj["append_"..add_append_name] = function(self, obj2) + self._private[name] = self._private[name] + or magic_obj["get_"..name](self, nil) + + table.insert(self._private[name], obj2) + + magic_obj["set_"..name](self, self._private[name]) + end + + magic_obj["remove_"..add_append_name] = function(self, obj2) + self._private[name] = self._private[name] + or magic_obj["get_"..name](self, nil) + + for k, v in ipairs(self._private[name]) do + if v == obj2 then + table.remove(self._private[name], k) + break + end + end + + magic_obj["set_"..name](self, self._private[name]) + end + end end return setmetatable( object, {__call = function(_,...) object.capi_index_fallback(...) end})