From 2b1febeabe90dc829db082517884dc2c6691d696 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Fri, 7 Mar 2014 14:38:28 +0100 Subject: [PATCH] Make objects properly inherit signals from classes Signed-off-by: Uli Schlachter --- common/array.h | 7 +++++++ common/luaclass.c | 8 +------- common/luaobject.h | 8 +------- common/signal.h | 28 ++++++++++++++++++++++++++-- 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/common/array.h b/common/array.h index 8d3a4e11..72ab0da4 100644 --- a/common/array.h +++ b/common/array.h @@ -34,6 +34,13 @@ int len, size; \ } pfx##_array_t; +#define ARRAY_TYPE_EXTRA(type_t, pfx, extra) \ + typedef struct pfx##_array_t { \ + type_t *tab; \ + int len, size; \ + extra; \ + } pfx##_array_t; + #define foreach(var, array) \ for(int __foreach_index_##var = 0; \ __foreach_index_##var < (array).len; \ diff --git a/common/luaclass.c b/common/luaclass.c index 6d7a006e..f78cd691 100644 --- a/common/luaclass.c +++ b/common/luaclass.c @@ -249,14 +249,8 @@ luaA_class_setup(lua_State *L, lua_class_t *class, signal_add(&class->signals, "new"); - /** @todo This is ugly :/ */ - /* Copy all signals from the parent */ if (parent) - foreach(sig, parent->signals) - { - signal_t s = { .id = sig->id }; - signal_array_insert(&class->signals, s); - } + class->signals.inherits_from = &parent->signals; lua_class_array_append(&luaA_classes, class); } diff --git a/common/luaobject.h b/common/luaobject.h index f3154438..c26af6fc 100644 --- a/common/luaobject.h +++ b/common/luaobject.h @@ -175,13 +175,7 @@ int luaA_object_emit_signal_simple(lua_State *); lua_setmetatable(L, -2); \ luaA_setuservalue(L, -2); \ lua_pushvalue(L, -1); \ - /** @todo This is wrong we shouldn't copy the existing signals from */ \ - /* the class, but I'm too lazy for doing this correctly right now. */ \ - foreach(sig, (lua_class).signals) \ - { \ - signal_t s = { .id = sig->id }; \ - signal_array_insert(&p->signals, s); \ - } \ + p->signals.inherits_from = &(lua_class).signals; \ luaA_class_emit_signal(L, &(lua_class), "new", 1); \ return p; \ } diff --git a/common/signal.h b/common/signal.h index 25fe4382..a30a9ab8 100644 --- a/common/signal.h +++ b/common/signal.h @@ -46,13 +46,37 @@ signal_wipe(signal_t *sig) cptr_array_wipe(&sig->sigfuncs); } -DO_BARRAY(signal_t, signal, signal_wipe, signal_cmp) +ARRAY_TYPE_EXTRA(signal_t, signal, struct signal_array_t *inherits_from) +BARRAY_FUNCS(signal_t, signal, signal_wipe, signal_cmp) static inline signal_t * signal_array_getbyid(signal_array_t *arr, unsigned long id) { signal_t sig = { .id = id }; - return signal_array_lookup(arr, &sig); + signal_t *result; + + result = signal_array_lookup(arr, &sig); + if(result) + return result; + + /* The signal doesn't exist yet. Check if some of our inherits_from have the + * signal and if yes, add it. + */ + signal_array_t *inherit = arr->inherits_from; + while(inherit != NULL) + { + if(signal_array_lookup(inherit, &sig)) + break; + inherit = inherit->inherits_from; + } + if(inherit) + { + /* Add the signal to this array to pretend it always existed */ + signal_array_insert(arr, sig); + result = signal_array_lookup(arr, &sig); + assert(result != NULL); + } + return result; } /** Add a signal to a signal array. -- 2.11.4.GIT