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