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