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