Make objects properly inherit signals from classes

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2014-03-07 14:38:28 +01:00
parent f19912900d
commit 2b1febeabe
4 changed files with 35 additions and 16 deletions

View File

@ -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; \

View File

@ -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);
}

View File

@ -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; \
}

View File

@ -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.