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