Eo: Added debug-mode check for eo_data_get.
[profile/ivi/eobj.git] / lib / eo.c
1 #include <Eina.h>
2
3 #include "Eo.h"
4 #include "eo_private.h"
5
6 #include "config.h"
7
8 typedef int Eo_Class_Id;
9
10 /* Used inside the class_get functions of classes, see #EO_DEFINE_CLASS */
11 EAPI Eina_Lock _eo_class_creation_lock;
12 int _eo_log_dom = -1;
13
14 static Eo_Class **_eo_classes;
15 static Eo_Class_Id _eo_classes_last_id;
16 static Eina_Bool _eo_init_count = 0;
17
18 static void _eo_callback_remove_all(Eo *obj);
19 static void _eo_constructor(Eo *obj, const Eo_Class *klass);
20 static void _eo_destructor(Eo *obj, const Eo_Class *klass);
21 static void eo_constructor_error_unset(Eo *obj);
22
23 typedef struct _Eo_Callback_Description Eo_Callback_Description;
24
25 #define EO_EINA_MAGIC 0xa186bc32 /* Nothing magical about this number. */
26 #define EO_EINA_MAGIC_STR "Eo"
27 #define EO_CLASS_EINA_MAGIC 0xa186bb32 /* Nothing magical about this number. */
28 #define EO_CLASS_EINA_MAGIC_STR "Eo Class"
29
30 #define EO_MAGIC_RETURN_VAL(d, magic, ret) \
31    do { \
32         if (!EINA_MAGIC_CHECK(d, magic)) \
33           { \
34              EINA_MAGIC_FAIL(d, magic); \
35              return ret; \
36           } \
37    } while (0)
38
39 #define EO_MAGIC_RETURN(d, magic) \
40    do { \
41         if (!EINA_MAGIC_CHECK(d, magic)) \
42           { \
43              EINA_MAGIC_FAIL(d, magic); \
44              return; \
45           } \
46    } while (0)
47
48 struct _Eo {
49      EINA_MAGIC
50      Eo *parent;
51      const Eo_Class *klass;
52      int refcount;
53 #ifndef NDEBUG
54      Eina_Inlist *xrefs;
55 #endif
56
57      Eina_List *composite_objects;
58
59      Eina_Inlist *callbacks;
60      int walking_list;
61
62      Eina_Inlist *kls_itr;
63
64      Eina_Bool delete:1;
65      Eina_Bool construct_error:1;
66 };
67
68 /* Start of Dich */
69 /* Dich search, split to 0xff 0xff 0xffff */
70
71 #define DICH_CHAIN1_MASK (0xff)
72 #define DICH_CHAIN2_MASK (0xff)
73 #define DICH_CHAIN_LAST_MASK (0xffff)
74 #define DICH_CHAIN1_SIZE (DICH_CHAIN1_MASK + 1)
75 #define DICH_CHAIN2_SIZE (DICH_CHAIN2_MASK + 1)
76 #define DICH_CHAIN_LAST_SIZE (DICH_CHAIN_LAST_MASK + 1)
77 #define DICH_CHAIN1(x) (((x) >> 24) & DICH_CHAIN1_MASK)
78 #define DICH_CHAIN2(x) (((x) >> 16) & DICH_CHAIN2_MASK)
79 #define DICH_CHAIN_LAST(x) ((x) & DICH_CHAIN_LAST_MASK)
80
81 #define OP_CLASS_OFFSET 16
82 #define OP_CLASS_OFFSET_GET(x) (((x) >> OP_CLASS_OFFSET) & 0xffff)
83 #define OP_CLASS_GET(op) ({ \
84       Eo_Class_Id tmp = OP_CLASS_OFFSET_GET(op); \
85       (Eo_Class *) ((tmp <= _eo_classes_last_id) && (tmp > 0)) ? \
86       (_eo_classes[tmp - 1]) : NULL; \
87       })
88 #define OP_SUB_ID_GET(op) ((op) & 0xffff)
89
90 #define EO_ALIGN_SIZE(size) \
91         ((size) + (sizeof(void *) - ((size) % sizeof(void *))))
92
93 /* Structure of Eo_Op is:
94  * 16bit: class
95  * 16bit: op.
96  */
97
98 typedef struct _Dich_Chain1 Dich_Chain1;
99
100 typedef struct
101 {
102    eo_op_func_type func;
103 } op_type_funcs;
104
105 typedef struct
106 {
107    op_type_funcs *funcs;
108 } Dich_Chain2;
109
110 struct _Dich_Chain1
111 {
112    Dich_Chain2 *chain;
113 };
114
115 typedef struct
116 {
117      EINA_INLIST;
118      const Eo_Class *klass;
119 } Eo_Extension_Node;
120
121 typedef struct
122 {
123      const Eo_Class *klass;
124      size_t offset;
125 } Eo_Extension_Data_Offset;
126
127 struct _Eo_Class
128 {
129    EINA_MAGIC
130    Eo_Class_Id class_id;
131    const Eo_Class *parent;
132    const Eo_Class_Description *desc;
133    Dich_Chain1 chain[DICH_CHAIN1_SIZE];
134    Eina_Inlist *extensions;
135
136    Eo_Extension_Data_Offset *extn_data_off;
137    size_t extn_data_size;
138
139    const Eo_Class **mro;
140
141    size_t data_offset; /* < Offset of the data within object data. */
142
143    Eina_Bool constructed : 1;
144 };
145
146 static inline eo_op_func_type
147 dich_func_get(const Eo_Class *klass, Eo_Op op)
148 {
149    const Dich_Chain1 *chain1 = &klass->chain[DICH_CHAIN1(op)];
150    if (!chain1) return NULL;
151    if (!chain1->chain) return NULL;
152    Dich_Chain2 *chain2 = &chain1->chain[DICH_CHAIN2(op)];
153    if (!chain2) return NULL;
154    if (!chain2->funcs) return NULL;
155
156    /* num_ops is calculated from the class. */
157    const Eo_Class *op_klass = OP_CLASS_GET(op);
158    if (!op_klass || (DICH_CHAIN_LAST(op) >= op_klass->desc->ops.count))
159       return NULL;
160
161    return chain2->funcs[DICH_CHAIN_LAST(op)].func;
162 }
163
164 static inline void
165 dich_func_set(Eo_Class *klass, Eo_Op op, eo_op_func_type func)
166 {
167    const Eo_Class *op_klass = OP_CLASS_GET(op);
168    size_t num_ops;
169
170    /* Verify op is valid. */
171    if (op_klass)
172      {
173         /* num_ops is calculated from the class. */
174         num_ops = op_klass->desc->ops.count;
175         if (DICH_CHAIN_LAST(op) >= num_ops)
176           {
177              ERR("OP %x is too big for the domain '%s', expected value < %x.",
178                    op, op_klass->desc->name, op_klass->desc->ops.count);
179              return;
180           }
181      }
182    else
183      {
184         ERR("OP %x is from an illegal class.", op);
185         return;
186      }
187
188    Dich_Chain1 *chain1 = &klass->chain[DICH_CHAIN1(op)];
189    if (!chain1->chain)
190      {
191         chain1->chain = calloc(DICH_CHAIN2_SIZE, sizeof(*(chain1->chain)));
192      }
193
194    Dich_Chain2 *chain2 = &chain1->chain[DICH_CHAIN2(op)];
195    if (!chain2->funcs)
196      {
197         chain2->funcs = calloc(num_ops, sizeof(*(chain2->funcs)));
198      }
199
200    chain2->funcs[DICH_CHAIN_LAST(op)].func = func;
201 }
202
203 static inline void
204 dich_func_clean_all(Eo_Class *klass)
205 {
206    int i;
207    Dich_Chain1 *chain1 = klass->chain;
208
209    for (i = 0 ; i < DICH_CHAIN1_SIZE ; i++, chain1++)
210      {
211         int j;
212         Dich_Chain2 *chain2 = chain1->chain;
213
214         if (!chain2)
215            continue;
216
217         for (j = 0 ; j < DICH_CHAIN2_SIZE ; j++, chain2++)
218           {
219              free(chain2->funcs);
220           }
221         free(chain1->chain);
222         chain1->chain = NULL;
223      }
224 }
225
226 /* END OF DICH */
227
228 static const Eo_Op_Description noop_desc =
229         EO_OP_DESCRIPTION(EO_NOOP, "", "No operation.");
230
231 static const Eo_Op_Description *
232 _eo_op_id_desc_get(Eo_Op op)
233 {
234    const Eo_Class *klass = OP_CLASS_GET(op);
235    Eo_Op sub_id = OP_SUB_ID_GET(op);
236
237    if (op == EO_NOOP)
238       return &noop_desc;
239
240    if (klass && (sub_id < klass->desc->ops.count))
241       return klass->desc->ops.descs + sub_id;
242
243    return NULL;
244 }
245
246 static const char *
247 _eo_op_id_name_get(Eo_Op op)
248 {
249    const Eo_Op_Description *desc = _eo_op_id_desc_get(op);
250    return (desc) ? desc->name : NULL;
251 }
252
253 typedef struct
254 {
255    EINA_INLIST;
256    Eo_Op op;
257    const Eo_Class **kls_itr;
258 } Eo_Kls_Itr_Node;
259
260 static inline Eina_Bool
261 _eo_kls_itr_init(Eo *obj, Eo_Op op)
262 {
263    if (obj->kls_itr)
264      {
265         Eo_Kls_Itr_Node *node =
266            EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eo_Kls_Itr_Node);
267         if (node->op == op)
268           {
269              return EINA_FALSE;
270           }
271      }
272
273
274      {
275         Eo_Kls_Itr_Node *node = calloc(1, sizeof(*node));
276         node->op = op;
277         node->kls_itr = obj->klass->mro;
278         obj->kls_itr = eina_inlist_prepend(obj->kls_itr,
279               EINA_INLIST_GET(node));
280
281         return EINA_TRUE;
282      }
283 }
284
285 static inline void
286 _eo_kls_itr_end(Eo *obj, Eo_Op op)
287 {
288    Eo_Kls_Itr_Node *node =
289       EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eo_Kls_Itr_Node);
290
291    if (node->op != op)
292       return;
293
294    obj->kls_itr = eina_inlist_remove(obj->kls_itr, obj->kls_itr);
295    free(node);
296 }
297
298 static inline const Eo_Class *
299 _eo_kls_itr_get(Eo *obj)
300 {
301    Eo_Kls_Itr_Node *node =
302       EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eo_Kls_Itr_Node);
303
304    return (node) ? *(node->kls_itr) : NULL;
305 }
306
307 static inline const Eo_Class *
308 _eo_kls_itr_next(Eo *obj, Eo_Op op)
309 {
310    Eo_Kls_Itr_Node *node =
311       EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eo_Kls_Itr_Node);
312
313    if (!node || (node->op != op))
314      {
315         Eo_Op node_op = (node) ? node->op : EO_NOOP;
316         ERR("Called with op %d ('%s') while expecting: %d ('%s'). This probaly means you called eo_*_super functions from a wrong place.",
317               op, _eo_op_id_name_get(op),
318               node_op, _eo_op_id_name_get(node_op));
319         return NULL;
320      }
321
322    const Eo_Class **kls_itr = node->kls_itr;
323    if (*kls_itr)
324      {
325         kls_itr++;
326         node->kls_itr = kls_itr;
327         return *kls_itr;
328      }
329    else
330      {
331         return NULL;
332      }
333 }
334
335 static inline Eina_Bool
336 _eo_kls_itr_reached_end(const Eo *obj)
337 {
338    Eo_Kls_Itr_Node *node =
339       EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eo_Kls_Itr_Node);
340    const Eo_Class **kls_itr = node->kls_itr;
341    return !(*kls_itr && *(kls_itr + 1));
342 }
343
344 static Eina_Bool
345 _eo_op_internal(Eo *obj, Eina_Bool constant, Eo_Op op, va_list *p_list)
346 {
347    const Eo_Class *klass;
348    Eina_Bool ret = EINA_FALSE;
349    Eina_Bool _itr_init;
350
351    const Eo_Op_Description *op_desc = _eo_op_id_desc_get(op);
352
353    if (op_desc &&
354          ((constant == EINA_TRUE) && (op_desc->constant == EINA_FALSE)))
355      {
356         ERR("Tried calling non-const or non-existant op '%s' (%d) from a const (query) function.", (op_desc) ? op_desc->name : NULL, op);
357         return EINA_FALSE;
358      }
359
360    _itr_init = _eo_kls_itr_init(obj, op);
361    klass = _eo_kls_itr_get(obj);
362    while (klass)
363      {
364         eo_op_func_type func = dich_func_get(klass, op);
365
366         if (func)
367           {
368              func(obj, eo_data_get(obj, klass), p_list);
369              ret = EINA_TRUE;
370              goto end;
371           }
372
373         klass = _eo_kls_itr_next(obj, op);
374      }
375
376    /* Try composite objects */
377      {
378         Eina_List *itr;
379         Eo *emb_obj;
380         EINA_LIST_FOREACH(obj->composite_objects, itr, emb_obj)
381           {
382              if (_eo_op_internal(emb_obj, constant, op, p_list))
383                {
384                   ret = EINA_TRUE;
385                   goto end;
386                }
387           }
388      }
389
390 end:
391
392    if (_itr_init) _eo_kls_itr_end(obj, op);
393    return ret;
394 }
395
396 EAPI Eina_Bool
397 eo_do_internal(Eo *obj, Eina_Bool constant, ...)
398 {
399    Eina_Bool ret = EINA_TRUE;
400    Eo_Op op = EO_NOOP;
401    va_list p_list;
402
403    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
404
405    eo_ref(obj);
406
407    va_start(p_list, constant);
408
409    op = va_arg(p_list, Eo_Op);
410    while (op)
411      {
412         if (!_eo_op_internal(obj, constant, op, &p_list))
413           {
414              const Eo_Class *op_klass = OP_CLASS_GET(op);
415              const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL;
416              ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.",
417                    op, _eo_op_id_name_get(op), _dom_name,
418                    obj->klass->desc->name);
419              ret = EINA_FALSE;
420              break;
421           }
422         op = va_arg(p_list, Eo_Op);
423      }
424
425    va_end(p_list);
426
427    eo_unref(obj);
428    return ret;
429 }
430
431 EAPI Eina_Bool
432 eo_do_super_internal(Eo *obj, Eina_Bool constant, Eo_Op op, ...)
433 {
434    const Eo_Class *obj_klass;
435    Eina_Bool ret = EINA_TRUE;
436    va_list p_list;
437    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
438
439    /* Advance the kls itr. */
440    obj_klass = _eo_kls_itr_next(obj, op);
441
442    if (!obj_klass)
443      {
444         return EINA_FALSE;
445      }
446
447    va_start(p_list, op);
448    if (!_eo_op_internal(obj, constant, op, &p_list))
449      {
450         const Eo_Class *op_klass = OP_CLASS_GET(op);
451         const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL;
452         ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.",
453               op, _eo_op_id_name_get(op), _dom_name,
454               (obj_klass) ? obj_klass->desc->name : NULL);
455         ret = EINA_FALSE;
456      }
457    va_end(p_list);
458
459    return ret;
460 }
461
462 EAPI const Eo_Class *
463 eo_class_get(const Eo *obj)
464 {
465    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
466
467    return obj->klass;
468 }
469
470 EAPI const char *
471 eo_class_name_get(const Eo_Class *klass)
472 {
473    EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, NULL);
474
475    return klass->desc->name;
476 }
477
478 static void
479 _eo_class_base_op_init(Eo_Class *klass)
480 {
481    const Eo_Class_Description *desc = klass->desc;
482    if (!desc || !desc->ops.base_op_id)
483       return;
484
485    /* FIXME: Depends on values defined above! */
486    *(desc->ops.base_op_id) = klass->class_id << OP_CLASS_OFFSET;
487 }
488
489 static Eina_Bool
490 _eo_class_mro_has(const Eo_Class *klass, const Eo_Class *find)
491 {
492    const Eo_Class **itr;
493    for (itr = klass->mro ; *itr ; itr++)
494      {
495         if (*itr == find)
496           {
497              return EINA_TRUE;
498           }
499      }
500
501    return EINA_FALSE;
502 }
503
504 static Eina_List *
505 _eo_class_mro_add(Eina_List *mro, const Eo_Class *klass)
506 {
507    Eina_List *extn_pos = NULL;
508    Eina_Bool check_consistency = !mro;
509    if (!klass)
510       return mro;
511
512    mro = eina_list_append(mro, klass);
513
514    /* Recursively add extenions. */
515      {
516         Eo_Extension_Node *extn;
517         EINA_INLIST_FOREACH(klass->extensions, extn)
518           {
519              mro = _eo_class_mro_add(mro, extn->klass);
520              /* Not possible: if (!mro) return NULL; */
521
522              if (check_consistency)
523                {
524                   extn_pos = eina_list_append(extn_pos, eina_list_last(mro));
525                }
526           }
527      }
528
529    /* Check if we can create a consistent mro. We only do it for the class
530     * we are working on (i.e no parents). */
531    if (check_consistency)
532      {
533         Eo_Extension_Node *extn;
534
535         Eina_List *itr = extn_pos;
536         EINA_INLIST_FOREACH(klass->extensions, extn)
537           {
538              /* Get the first one after the extension. */
539              Eina_List *extn_list = eina_list_next(eina_list_data_get(itr));
540
541              /* If we found the extension again. */
542              if (eina_list_data_find_list(extn_list, extn->klass))
543                {
544                   eina_list_free(mro);
545                   ERR("Cannot create a consistent method resolution order for class '%s' because of '%s'.", klass->desc->name, extn->klass->desc->name);
546                   return NULL;
547                }
548
549              itr = eina_list_next(itr);
550           }
551      }
552
553
554    mro = _eo_class_mro_add(mro, klass->parent);
555
556    return mro;
557 }
558
559 static Eina_Bool
560 _eo_class_mro_init(Eo_Class *klass)
561 {
562    Eina_List *mro = NULL;
563
564    DBG("Started creating MRO for class '%s'", klass->desc->name);
565    mro = _eo_class_mro_add(mro, klass);
566
567    if (!mro)
568       return EINA_FALSE;
569
570    /* Remove duplicates and make them the right order. */
571      {
572         Eina_List *itr1, *itr2, *itr2n;
573
574         itr1 = eina_list_last(mro);
575         while (itr1)
576           {
577              itr2 = eina_list_prev(itr1);
578
579              while (itr2)
580                {
581                   itr2n = eina_list_prev(itr2);
582
583                   if (eina_list_data_get(itr1) == eina_list_data_get(itr2))
584                     {
585                        mro = eina_list_remove_list(mro, itr2);
586                     }
587
588                   itr2 = itr2n;
589                }
590
591              itr1 = eina_list_prev(itr1);
592           }
593      }
594
595    /* Copy the mro and free the list. */
596      {
597         const Eo_Class *kls_itr;
598         const Eo_Class **mro_itr;
599         klass->mro = calloc(sizeof(*klass->mro), eina_list_count(mro) + 1);
600
601         mro_itr = klass->mro;
602
603         EINA_LIST_FREE(mro, kls_itr)
604           {
605              *(mro_itr++) = kls_itr;
606
607              DBG("Added '%s' to MRO", kls_itr->desc->name);
608           }
609         *(mro_itr) = NULL;
610      }
611
612    DBG("Finished creating MRO for class '%s'", klass->desc->name);
613
614    return EINA_TRUE;
615 }
616
617 static void
618 _eo_class_constructor(Eo_Class *klass)
619 {
620    if (klass->constructed)
621       return;
622
623    klass->constructed = EINA_TRUE;
624
625    if (klass->desc->class_constructor)
626       klass->desc->class_constructor(klass);
627 }
628
629 EAPI void
630 eo_class_funcs_set(Eo_Class *klass, const Eo_Op_Func_Description *func_descs)
631 {
632    EO_MAGIC_RETURN(klass, EO_CLASS_EINA_MAGIC);
633
634    const Eo_Op_Func_Description *itr;
635    itr = func_descs;
636    if (itr)
637      {
638         for ( ; itr->op != 0 ; itr++)
639           {
640              const Eo_Op_Description *op_desc = _eo_op_id_desc_get(itr->op);
641
642              if (EINA_LIKELY(!op_desc || (itr->constant == op_desc->constant)))
643                {
644                   dich_func_set(klass, itr->op, itr->func);
645                }
646              else
647                {
648                   ERR("Set function's constant property (%d) is different than the one in the op description (%d) for op '%s' in class '%s'.", itr->constant, op_desc->constant, op_desc->name, klass->desc->name);
649                }
650           }
651      }
652 }
653
654 static void
655 eo_class_free(Eo_Class *klass)
656 {
657    if (klass->constructed)
658      {
659         if (klass->desc->class_destructor)
660            klass->desc->class_destructor(klass);
661
662         dich_func_clean_all(klass);
663      }
664
665      {
666         Eina_Inlist *itrn;
667         Eo_Extension_Node *extn = NULL;
668         EINA_INLIST_FOREACH_SAFE(klass->extensions, itrn, extn)
669           {
670              free(extn);
671           }
672      }
673
674    if (klass->mro)
675       free(klass->mro);
676
677    if (klass->extn_data_off)
678       free(klass->extn_data_off);
679
680    free(klass);
681 }
682
683 /* DEVCHECK */
684 static Eina_Bool
685 _eo_class_check_op_descs(const Eo_Class *klass)
686 {
687    const Eo_Class_Description *desc = klass->desc;
688    const Eo_Op_Description *itr;
689    size_t i;
690
691    if (desc->ops.count > 0)
692      {
693         if (!desc->ops.base_op_id)
694           {
695              ERR("Class '%s' has a non-zero ops count, but base_id is NULL.",
696                    desc->name);
697              return EINA_FALSE;
698           }
699
700         if (!desc->ops.descs)
701           {
702              ERR("Class '%s' has a non-zero ops count, but there are no descs.",
703                    desc->name);
704              return EINA_FALSE;
705           }
706      }
707
708    itr = desc->ops.descs;
709    for (i = 0 ; i < desc->ops.count ; i++, itr++)
710      {
711         if (itr->sub_op != i)
712           {
713              if (itr->name)
714                {
715                   ERR("Wrong order in Ops description for class '%s'. Expected %d and got %d", desc->name, i, itr->sub_op);
716                }
717              else
718                {
719                   ERR("Found too few Ops description for class '%s'. Expected %d descriptions, but found %d.", desc->name, desc->ops.count, i);
720                }
721              return EINA_FALSE;
722           }
723      }
724
725    if (itr && itr->name)
726      {
727         ERR("Found extra Ops description for class '%s'. Expected %d descriptions, but found more.", desc->name, desc->ops.count);
728         return EINA_FALSE;
729      }
730
731    return EINA_TRUE;
732 }
733
734 EAPI const Eo_Class *
735 eo_class_new(const Eo_Class_Description *desc, const Eo_Class *parent, ...)
736 {
737    Eo_Class *klass;
738    va_list p_list;
739
740    if (parent && !EINA_MAGIC_CHECK(parent, EO_CLASS_EINA_MAGIC))
741      {
742         EINA_MAGIC_FAIL(parent, EO_CLASS_EINA_MAGIC);
743         return NULL;
744      }
745
746    va_start(p_list, parent);
747
748    EINA_SAFETY_ON_NULL_RETURN_VAL(desc, NULL);
749    EINA_SAFETY_ON_NULL_RETURN_VAL(desc->name, NULL);
750
751    /* Check restrictions on Interface types. */
752    if (desc->type == EO_CLASS_TYPE_INTERFACE)
753      {
754         EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->constructor, NULL);
755         EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->destructor, NULL);
756         EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->class_constructor, NULL);
757         EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->class_destructor, NULL);
758         EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->data_size, NULL);
759      }
760
761    klass = calloc(1, sizeof(Eo_Class));
762    klass->parent = parent;
763
764    /* Handle class extensions */
765      {
766         Eo_Class *extn = NULL;
767
768         extn = va_arg(p_list, Eo_Class *);
769         while (extn)
770           {
771              switch (extn->desc->type)
772                {
773                 case EO_CLASS_TYPE_REGULAR:
774                 case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
775                    /* Use it like an interface. */
776                 case EO_CLASS_TYPE_INTERFACE:
777                    break;
778                 case EO_CLASS_TYPE_MIXIN:
779                      {
780                         Eo_Extension_Node *node = calloc(1, sizeof(*node));
781                         node->klass = extn;
782                         klass->extensions =
783                            eina_inlist_append(klass->extensions,
784                                  EINA_INLIST_GET(node));
785                      }
786                    break;
787                }
788
789              extn = va_arg(p_list, Eo_Class *);
790           }
791      }
792
793    klass->desc = desc;
794
795    /* Handle the inheritance */
796    if (klass->parent)
797      {
798         /* Verify the inheritance is allowed. */
799         switch (klass->desc->type)
800           {
801            case EO_CLASS_TYPE_REGULAR:
802            case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
803               if ((klass->parent->desc->type != EO_CLASS_TYPE_REGULAR) &&
804                     (klass->parent->desc->type != EO_CLASS_TYPE_REGULAR_NO_INSTANT))
805                 {
806                    ERR("Regular classes ('%s') aren't allowed to inherit from non-regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
807                    goto cleanup;
808                 }
809               break;
810            case EO_CLASS_TYPE_INTERFACE:
811            case EO_CLASS_TYPE_MIXIN:
812               if ((klass->parent->desc->type != EO_CLASS_TYPE_INTERFACE) &&
813                     (klass->parent->desc->type != EO_CLASS_TYPE_MIXIN))
814                 {
815                    ERR("Non-regular classes ('%s') aren't allowed to inherit from regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
816                    goto cleanup;
817                 }
818               break;
819           }
820
821
822         /* Update the current offset. */
823         /* FIXME: Make sure this alignment is enough. */
824         klass->data_offset = klass->parent->data_offset +
825            EO_ALIGN_SIZE(klass->parent->desc->data_size);
826      }
827
828    if (!_eo_class_check_op_descs(klass))
829      {
830         goto cleanup;
831      }
832
833    if (!_eo_class_mro_init(klass))
834      {
835         goto cleanup;
836      }
837
838    /* create MIXIN offset table. */
839      {
840         const Eo_Class **mro_itr = klass->mro;
841         Eo_Extension_Data_Offset *extn_data_itr;
842         size_t extn_num = 0;
843         size_t extn_data_off = klass->data_offset +
844            EO_ALIGN_SIZE(klass->desc->data_size);
845
846         /* FIXME: Make faster... */
847         while (*mro_itr)
848           {
849              if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
850                    ((*mro_itr)->desc->data_size > 0))
851                {
852                   extn_num++;
853                }
854              mro_itr++;
855           }
856
857         klass->extn_data_off = calloc(extn_num + 1,
858               sizeof(*klass->extn_data_off));
859
860         extn_data_itr = klass->extn_data_off;
861         mro_itr = klass->mro;
862         while (*mro_itr)
863           {
864              if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
865                    ((*mro_itr)->desc->data_size > 0))
866                {
867                   extn_data_itr->klass = *mro_itr;
868                   extn_data_itr->offset = extn_data_off;
869
870                   extn_data_off += EO_ALIGN_SIZE(extn_data_itr->klass->desc->data_size);
871                   extn_data_itr++;
872                }
873              mro_itr++;
874           }
875
876         klass->extn_data_size = extn_data_off;
877      }
878
879    eina_lock_take(&_eo_class_creation_lock);
880    klass->class_id = ++_eo_classes_last_id;
881      {
882         /* FIXME: Handle errors. */
883         Eo_Class **tmp;
884         tmp = realloc(_eo_classes, _eo_classes_last_id * sizeof(*_eo_classes));
885         _eo_classes = tmp;
886         _eo_classes[klass->class_id - 1] = klass;
887      }
888    eina_lock_release(&_eo_class_creation_lock);
889
890    EINA_MAGIC_SET(klass, EO_CLASS_EINA_MAGIC);
891
892    _eo_class_base_op_init(klass);
893
894    _eo_class_constructor(klass);
895
896    va_end(p_list);
897
898    return klass;
899
900 cleanup:
901    eo_class_free(klass);
902    return NULL;
903 }
904
905 EAPI Eo *
906 eo_add(const Eo_Class *klass, Eo *parent)
907 {
908    EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, NULL);
909
910    if (parent) EO_MAGIC_RETURN_VAL(parent, EO_EINA_MAGIC, NULL);
911
912    if (EINA_UNLIKELY(klass->desc->type != EO_CLASS_TYPE_REGULAR))
913      {
914         ERR("Class '%s' is not instantiate-able. Aborting.", klass->desc->name);
915         return NULL;
916      }
917
918    Eo *obj = calloc(1, EO_ALIGN_SIZE(sizeof(*obj)) +
919          (klass->data_offset + EO_ALIGN_SIZE(klass->desc->data_size)) +
920          klass->extn_data_size);
921    obj->klass = klass;
922    obj->parent = parent;
923
924    obj->refcount++;
925
926    _eo_kls_itr_init(obj, EO_NOOP);
927    eo_constructor_error_unset(obj);
928
929    EINA_MAGIC_SET(obj, EO_EINA_MAGIC);
930    eo_ref(obj);
931    _eo_constructor(obj, klass);
932
933    if (EINA_UNLIKELY(eo_constructor_error_get(obj)))
934      {
935         ERR("Type '%s' - One of the object constructors have failed.", klass->desc->name);
936         goto fail;
937      }
938
939    if (EINA_UNLIKELY(!_eo_kls_itr_reached_end(obj)))
940      {
941         ERR("Type '%s' - Not all of the object constructors have been executed.", klass->desc->name);
942         goto fail;
943      }
944    _eo_kls_itr_end(obj, EO_NOOP);
945    eo_unref(obj);
946
947    return obj;
948
949 fail:
950    /* Unref twice, once for the ref above, and once for the basic object ref. */
951    eo_unref(obj);
952    eo_unref(obj);
953    return NULL;
954 }
955
956 typedef struct
957 {
958    EINA_INLIST;
959    const Eo *ref_obj;
960    const char *file;
961    int line;
962 } Eo_Xref_Node;
963
964 EAPI Eo *
965 eo_xref_internal(Eo *obj, const Eo *ref_obj, const char *file, int line)
966 {
967    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, obj);
968
969    eo_ref(obj);
970
971 #ifndef NDEBUG
972    Eo_Xref_Node *xref = calloc(1, sizeof(*xref));
973    xref->ref_obj = ref_obj;
974    xref->file = file;
975    xref->line = line;
976
977    /* FIXME: Make it sorted. */
978    obj->xrefs = eina_inlist_prepend(obj->xrefs, EINA_INLIST_GET(xref));
979 #else
980    (void) ref_obj;
981    (void) file;
982    (void) line;
983 #endif
984
985    return obj;
986 }
987
988 EAPI void
989 eo_xunref(Eo *obj, const Eo *ref_obj)
990 {
991    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
992 #ifndef NDEBUG
993    Eo_Xref_Node *xref = NULL;
994    EINA_INLIST_FOREACH(obj->xrefs, xref)
995      {
996         if (xref->ref_obj == ref_obj)
997            break;
998      }
999
1000    if (xref)
1001      {
1002         obj->xrefs = eina_inlist_remove(obj->xrefs, EINA_INLIST_GET(xref));
1003         free(xref);
1004      }
1005    else
1006      {
1007         ERR("ref_obj (%p) does not reference obj (%p). Aborting unref.", ref_obj, obj);
1008         return;
1009      }
1010 #else
1011    (void) ref_obj;
1012 #endif
1013    eo_unref(obj);
1014 }
1015
1016 EAPI Eo *
1017 eo_ref(Eo *obj)
1018 {
1019    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, obj);
1020
1021    obj->refcount++;
1022    return obj;
1023 }
1024
1025 static void
1026 _eo_del_internal(Eo *obj)
1027 {
1028    if (obj->delete)
1029       return;
1030    /* We need that for the event callbacks that may ref/unref. */
1031    obj->refcount++;
1032
1033    eo_event_callback_call(obj, EO_EV_DEL, NULL);
1034    obj->delete = EINA_TRUE;
1035
1036    obj->refcount--;
1037
1038    const Eo_Class *klass = eo_class_get(obj);
1039    _eo_kls_itr_init(obj, EO_NOOP);
1040    eo_constructor_error_unset(obj);
1041    _eo_destructor(obj, klass);
1042    if (eo_constructor_error_get(obj))
1043      {
1044         ERR("Type '%s' - One of the object destructors have failed.", klass->desc->name);
1045      }
1046
1047    if (!_eo_kls_itr_reached_end(obj))
1048      {
1049         ERR("Type '%s' - Not all of the object destructors have been executed.", klass->desc->name);
1050      }
1051    _eo_kls_itr_end(obj, EO_NOOP);
1052    /*FIXME: add eo_class_unref(klass) ? - just to clear the caches. */
1053
1054    /* If for some reason it's not empty, clear it. */
1055    while (obj->kls_itr)
1056      {
1057         WRN("Kls_Itr is not empty, possibly a bug, please report. - An error will be reported for each kls_itr in the stack.");
1058         Eina_Inlist *nitr = obj->kls_itr->next;
1059         free(EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eo_Kls_Itr_Node));
1060         obj->kls_itr = nitr;
1061      }
1062
1063    Eina_List *itr, *itr_n;
1064    Eo *emb_obj;
1065    EINA_LIST_FOREACH_SAFE(obj->composite_objects, itr, itr_n, emb_obj)
1066      {
1067         eo_composite_object_detach(obj, emb_obj);
1068      }
1069 }
1070
1071 EAPI void
1072 eo_unref(Eo *obj)
1073 {
1074    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1075
1076    if (--(obj->refcount) == 0)
1077      {
1078         _eo_del_internal(obj);
1079         /* We need that for the event callbacks that may ref/unref. */
1080         obj->refcount++;
1081
1082         eo_event_callback_call(obj, EO_EV_FREE, NULL);
1083
1084         obj->refcount--;
1085
1086 #ifndef NDEBUG
1087    /* If for some reason it's not empty, clear it. */
1088    while (obj->xrefs)
1089      {
1090         WRN("obj->xrefs is not empty, possibly a bug, please report. - An error will be reported for each xref in the stack.");
1091         Eina_Inlist *nitr = obj->xrefs->next;
1092         free(EINA_INLIST_CONTAINER_GET(obj->xrefs, Eo_Kls_Itr_Node));
1093         obj->xrefs = nitr;
1094      }
1095 #endif
1096
1097         _eo_callback_remove_all(obj);
1098
1099         free(obj);
1100      }
1101 }
1102
1103 EAPI int
1104 eo_ref_get(const Eo *obj)
1105 {
1106    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, 0);
1107
1108    return obj->refcount;
1109 }
1110
1111 EAPI void
1112 eo_del(Eo *obj)
1113 {
1114    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1115
1116    _eo_del_internal(obj);
1117    eo_unref(obj);
1118 }
1119
1120 EAPI Eo *
1121 eo_parent_get(Eo *obj)
1122 {
1123    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1124
1125    return obj->parent;
1126 }
1127
1128 EAPI void
1129 eo_constructor_error_set(Eo *obj)
1130 {
1131    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1132
1133    obj->construct_error = EINA_TRUE;
1134 }
1135
1136 static void
1137 eo_constructor_error_unset(Eo *obj)
1138 {
1139    obj->construct_error = EINA_FALSE;
1140 }
1141
1142 EAPI Eina_Bool
1143 eo_constructor_error_get(const Eo *obj)
1144 {
1145    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_TRUE);
1146
1147    return obj->construct_error;
1148 }
1149
1150 static inline void
1151 _eo_constructor_default(Eo *obj)
1152 {
1153    eo_constructor_super(obj);
1154 }
1155
1156 static inline void
1157 _eo_destructor_default(Eo *obj)
1158 {
1159    eo_destructor_super(obj);
1160 }
1161
1162 static void
1163 _eo_constructor(Eo *obj, const Eo_Class *klass)
1164 {
1165    if (!klass)
1166       return;
1167
1168    if (klass->desc->constructor)
1169       klass->desc->constructor(obj, eo_data_get(obj, klass));
1170    else
1171       _eo_constructor_default(obj);
1172 }
1173
1174 static void
1175 _eo_destructor(Eo *obj, const Eo_Class *klass)
1176 {
1177    if (!klass)
1178       return;
1179
1180    if (klass->desc->destructor)
1181       klass->desc->destructor(obj, eo_data_get(obj, klass));
1182    else
1183       _eo_destructor_default(obj);
1184 }
1185
1186 EAPI void
1187 eo_constructor_super(Eo *obj)
1188 {
1189    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1190
1191    _eo_constructor(obj, _eo_kls_itr_next(obj, EO_NOOP));
1192 }
1193
1194 EAPI void
1195 eo_destructor_super(Eo *obj)
1196 {
1197    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1198
1199    _eo_destructor(obj, _eo_kls_itr_next(obj, EO_NOOP));
1200 }
1201
1202 EAPI void *
1203 eo_data_get(const Eo *obj, const Eo_Class *klass)
1204 {
1205    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1206
1207 #ifndef NDEBUG
1208    if (!_eo_class_mro_has(obj->klass, klass))
1209      {
1210         ERR("Tried getting data of class '%s' from object of class '%s', but the former is not a direct inheritance of the latter.", klass->desc->name, obj->klass->desc->name);
1211         return NULL;
1212      }
1213 #endif
1214    if (EINA_LIKELY(klass->desc->data_size > 0))
1215      {
1216         if (EINA_UNLIKELY(klass->desc->type == EO_CLASS_TYPE_MIXIN))
1217           {
1218              Eo_Extension_Data_Offset *doff_itr =
1219                 eo_class_get(obj)->extn_data_off;
1220
1221              if (!doff_itr)
1222                 return NULL;
1223
1224              while (doff_itr->klass)
1225                {
1226                   if (doff_itr->klass == klass)
1227                      return ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj)) +
1228                            doff_itr->offset;
1229                   doff_itr++;
1230                }
1231           }
1232         else
1233           {
1234           return ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj)) +
1235              klass->data_offset;
1236           }
1237      }
1238
1239    return NULL;
1240 }
1241
1242 EAPI Eina_Bool
1243 eo_init(void)
1244 {
1245    const char *log_dom = "eo";
1246    if (_eo_init_count++ > 0)
1247       return EINA_TRUE;
1248
1249    eina_init();
1250
1251    _eo_classes = NULL;
1252    _eo_classes_last_id = 0;
1253    _eo_log_dom = eina_log_domain_register(log_dom, EINA_COLOR_LIGHTBLUE);
1254    if (_eo_log_dom < 0)
1255      {
1256         EINA_LOG_ERR("Could not register log domain: %s", log_dom);
1257         return EINA_FALSE;
1258      }
1259
1260    if (!eina_lock_new(&_eo_class_creation_lock))
1261      {
1262         EINA_LOG_ERR("Could not init lock.");
1263         return EINA_FALSE;
1264      }
1265
1266    eina_magic_string_static_set(EO_EINA_MAGIC, EO_EINA_MAGIC_STR);
1267    eina_magic_string_static_set(EO_CLASS_EINA_MAGIC,
1268          EO_CLASS_EINA_MAGIC_STR);
1269
1270    return EINA_TRUE;
1271 }
1272
1273 EAPI Eina_Bool
1274 eo_shutdown(void)
1275 {
1276    int i;
1277    Eo_Class **cls_itr = _eo_classes;
1278
1279    if (--_eo_init_count > 0)
1280       return EINA_TRUE;
1281
1282    for (i = 0 ; i < _eo_classes_last_id ; i++, cls_itr++)
1283      {
1284         if (*cls_itr)
1285            eo_class_free(*cls_itr);
1286      }
1287
1288    if (_eo_classes)
1289       free(_eo_classes);
1290
1291    eina_lock_free(&_eo_class_creation_lock);
1292
1293    eina_log_domain_unregister(_eo_log_dom);
1294    _eo_log_dom = -1;
1295
1296    eina_shutdown();
1297    return EINA_TRUE;
1298 }
1299
1300 EAPI void
1301 eo_composite_object_attach(Eo *obj, Eo *emb_obj)
1302 {
1303    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1304    EO_MAGIC_RETURN(emb_obj, EO_EINA_MAGIC);
1305
1306    eo_xref(emb_obj, obj);
1307    obj->composite_objects = eina_list_prepend(obj->composite_objects, emb_obj);
1308 }
1309
1310 EAPI void
1311 eo_composite_object_detach(Eo *obj, Eo *emb_obj)
1312 {
1313    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1314    EO_MAGIC_RETURN(emb_obj, EO_EINA_MAGIC);
1315
1316    obj->composite_objects = eina_list_remove(obj->composite_objects, emb_obj);
1317    eo_xunref(emb_obj, obj);
1318 }
1319
1320 EAPI Eina_Bool
1321 eo_composite_is(Eo *emb_obj)
1322 {
1323    if (!EINA_MAGIC_CHECK(emb_obj, EO_EINA_MAGIC))
1324      {
1325         EINA_MAGIC_FAIL(emb_obj, EO_EINA_MAGIC);
1326         return EINA_FALSE;
1327      }
1328
1329    Eo *obj = eo_parent_get(emb_obj);
1330    Eina_List *itr;
1331    Eo *tmp;
1332
1333    if (!obj)
1334       return EINA_FALSE;
1335
1336    EINA_LIST_FOREACH(obj->composite_objects, itr, tmp)
1337      {
1338         if (tmp == emb_obj)
1339            return EINA_TRUE;
1340      }
1341
1342    return EINA_FALSE;
1343 }
1344
1345 /* Callbacks */
1346 struct _Eo_Callback_Description
1347 {
1348    EINA_INLIST;
1349    const Eo_Event_Description *event;
1350    Eo_Event_Cb func;
1351    void *func_data;
1352    Eo_Callback_Priority priority;
1353    Eina_Bool delete_me : 1;
1354 };
1355
1356 /* Actually remove, doesn't care about walking list, or delete_me */
1357 static void
1358 _eo_callback_remove(Eo *obj, Eo_Callback_Description *cb)
1359 {
1360    obj->callbacks = eina_inlist_remove(obj->callbacks,
1361          EINA_INLIST_GET(cb));
1362    free(cb);
1363 }
1364
1365 /* Actually remove, doesn't care about walking list, or delete_me */
1366 static void
1367 _eo_callback_remove_all(Eo *obj)
1368 {
1369    Eina_Inlist *initr;
1370    Eo_Callback_Description *cb = NULL;
1371    EINA_INLIST_FOREACH_SAFE(obj->callbacks, initr, cb)
1372      {
1373         _eo_callback_remove(obj, cb);
1374      }
1375 }
1376
1377 static void
1378 _eo_callbacks_clear(Eo *obj)
1379 {
1380    Eina_Inlist *itn;
1381    Eo_Callback_Description *cb = NULL;
1382
1383    /* Abort if we are currently walking the list. */
1384    if (obj->walking_list > 0)
1385       return;
1386
1387    EINA_INLIST_FOREACH_SAFE(obj->callbacks, itn, cb)
1388      {
1389         if (cb->delete_me)
1390           {
1391              _eo_callback_remove(obj, cb);
1392           }
1393      }
1394 }
1395
1396 static int
1397 _callback_priority_cmp(const void *_a, const void *_b)
1398 {
1399    const Eo_Callback_Description *a, *b;
1400    a = (const Eo_Callback_Description *) _a;
1401    b = (const Eo_Callback_Description *) _b;
1402    if (a->priority < b->priority)
1403       return -1;
1404    else
1405       return 1;
1406 }
1407
1408 EAPI Eina_Bool
1409 eo_event_callback_priority_add(Eo *obj,
1410       const Eo_Event_Description *desc,
1411       Eo_Callback_Priority priority,
1412       Eo_Event_Cb func,
1413       const void *data)
1414 {
1415    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1416
1417    Eo_Callback_Description *cb = calloc(1, sizeof(*cb));
1418    cb->event = desc;
1419    cb->func = func;
1420    cb->func_data = (void *) data;
1421    cb->priority = priority;
1422    obj->callbacks = eina_inlist_sorted_insert(obj->callbacks,
1423          EINA_INLIST_GET(cb), _callback_priority_cmp);
1424
1425    eo_event_callback_call(obj, EO_EV_CALLBACK_ADD, desc);
1426
1427    return EINA_TRUE;
1428 }
1429
1430 EAPI void *
1431 eo_event_callback_del_lazy(Eo *obj, const Eo_Event_Description *desc, Eo_Event_Cb func)
1432 {
1433    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1434
1435    void *ret = NULL;
1436    Eo_Callback_Description *cb;
1437    EINA_INLIST_FOREACH(obj->callbacks, cb)
1438      {
1439         if ((cb->event == desc) && (cb->func == func))
1440           {
1441              void *data;
1442
1443              data = cb->func_data;
1444              cb->delete_me = EINA_TRUE;
1445              _eo_callbacks_clear(obj);
1446              ret = data;
1447              goto found;
1448           }
1449      }
1450
1451    return NULL;
1452
1453 found:
1454    eo_event_callback_call(obj, EO_EV_CALLBACK_DEL, desc);
1455    return ret;
1456 }
1457
1458 EAPI void *
1459 eo_event_callback_del(Eo *obj, const Eo_Event_Description *desc, Eo_Event_Cb func, const void *user_data)
1460 {
1461    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1462
1463    void *ret = NULL;
1464    Eo_Callback_Description *cb;
1465    EINA_INLIST_FOREACH(obj->callbacks, cb)
1466      {
1467         if ((cb->event == desc) && (cb->func == func) &&
1468               (cb->func_data == user_data))
1469           {
1470              void *data;
1471
1472              data = cb->func_data;
1473              cb->delete_me = EINA_TRUE;
1474              _eo_callbacks_clear(obj);
1475              ret = data;
1476              goto found;
1477           }
1478      }
1479
1480    return NULL;
1481
1482 found:
1483    eo_event_callback_call(obj, EO_EV_CALLBACK_DEL, desc);
1484    return ret;
1485 }
1486
1487 EAPI Eina_Bool
1488 eo_event_callback_call(Eo *obj, const Eo_Event_Description *desc,
1489       const void *event_info)
1490 {
1491    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1492
1493    Eina_Bool ret = EINA_TRUE;
1494    Eo_Callback_Description *cb;
1495
1496    eo_ref(obj);
1497    obj->walking_list++;
1498
1499    EINA_INLIST_FOREACH(obj->callbacks, cb)
1500      {
1501         if (!cb->delete_me && (cb->event == desc))
1502           {
1503              /* Abort callback calling if the func says so. */
1504              if (!cb->func((void *) cb->func_data, obj, desc,
1505                       (void *) event_info))
1506                {
1507                   ret = EINA_FALSE;
1508                   break;
1509                }
1510           }
1511         if (obj->delete)
1512           break;
1513      }
1514    obj->walking_list--;
1515    _eo_callbacks_clear(obj);
1516    eo_unref(obj);
1517
1518    return ret;
1519 }
1520
1521 static Eina_Bool
1522 _eo_event_forwarder_callback(void *data, Eo *obj, const Eo_Event_Description *desc, void *event_info)
1523 {
1524    (void) obj;
1525    Eo *new_obj = (Eo *) data;
1526    return eo_event_callback_call(new_obj, desc, event_info);
1527 }
1528
1529 /* FIXME: Change default priority? Maybe call later? */
1530 EAPI Eina_Bool
1531 eo_event_callback_forwarder_add(Eo *obj, const Eo_Event_Description *desc, Eo *new_obj)
1532 {
1533    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1534    EO_MAGIC_RETURN_VAL(new_obj, EO_EINA_MAGIC, EINA_FALSE);
1535
1536    return eo_event_callback_add(obj, desc, _eo_event_forwarder_callback, new_obj);
1537 }
1538
1539 EAPI Eina_Bool
1540 eo_event_callback_forwarder_del(Eo *obj, const Eo_Event_Description *desc, Eo *new_obj)
1541 {
1542    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1543    EO_MAGIC_RETURN_VAL(new_obj, EO_EINA_MAGIC, EINA_FALSE);
1544
1545    eo_event_callback_del(obj, desc, _eo_event_forwarder_callback, new_obj);
1546    return EINA_TRUE;
1547 }
1548