4 #include "eo_private.h"
8 typedef int Eo_Class_Id;
10 /* Used inside the class_get functions of classes, see #EO_DEFINE_CLASS */
11 EAPI Eina_Lock _eo_class_creation_lock;
14 static Eo_Class **_eo_classes;
15 static Eo_Class_Id _eo_classes_last_id;
16 static Eina_Bool _eo_init_count = 0;
18 static void _eo_callback_remove_all(Eo *obj);
19 static void _eo_constructor(Eo *obj, const Eo_Class *klass);
20 static void _eo_destructor(Eo *obj, const Eo_Class *klass);
21 static void eo_constructor_error_unset(Eo *obj);
23 typedef struct _Eo_Callback_Description Eo_Callback_Description;
25 #define EO_EINA_MAGIC 0xa186bc32 /* Nothing magical about this number. */
26 #define EO_EINA_MAGIC_STR "Eo"
27 #define EO_CLASS_EINA_MAGIC 0xa186bb32 /* Nothing magical about this number. */
28 #define EO_CLASS_EINA_MAGIC_STR "Eo Class"
30 #define EO_MAGIC_RETURN_VAL(d, magic, ret) \
32 if (!EINA_MAGIC_CHECK(d, magic)) \
34 EINA_MAGIC_FAIL(d, magic); \
39 #define EO_MAGIC_RETURN(d, magic) \
41 if (!EINA_MAGIC_CHECK(d, magic)) \
43 EINA_MAGIC_FAIL(d, magic); \
51 const Eo_Class *klass;
57 Eina_List *composite_objects;
59 Eina_Inlist *callbacks;
65 Eina_Bool construct_error:1;
69 /* Dich search, split to 0xff 0xff 0xffff */
71 #define DICH_CHAIN1_MASK (0xff)
72 #define DICH_CHAIN2_MASK (0xff)
73 #define DICH_CHAIN_LAST_MASK (0xffff)
74 #define DICH_CHAIN1_SIZE (DICH_CHAIN1_MASK + 1)
75 #define DICH_CHAIN2_SIZE (DICH_CHAIN2_MASK + 1)
76 #define DICH_CHAIN_LAST_SIZE (DICH_CHAIN_LAST_MASK + 1)
77 #define DICH_CHAIN1(x) (((x) >> 24) & DICH_CHAIN1_MASK)
78 #define DICH_CHAIN2(x) (((x) >> 16) & DICH_CHAIN2_MASK)
79 #define DICH_CHAIN_LAST(x) ((x) & DICH_CHAIN_LAST_MASK)
81 #define OP_CLASS_OFFSET 16
82 #define OP_CLASS_OFFSET_GET(x) (((x) >> OP_CLASS_OFFSET) & 0xffff)
83 #define OP_CLASS_GET(op) ({ \
84 Eo_Class_Id tmp = OP_CLASS_OFFSET_GET(op); \
85 (Eo_Class *) ((tmp <= _eo_classes_last_id) && (tmp > 0)) ? \
86 (_eo_classes[tmp - 1]) : NULL; \
88 #define OP_SUB_ID_GET(op) ((op) & 0xffff)
90 #define EO_ALIGN_SIZE(size) \
91 ((size) + (sizeof(void *) - ((size) % sizeof(void *))))
93 /* Structure of Eo_Op is:
98 typedef struct _Dich_Chain1 Dich_Chain1;
102 eo_op_func_type func;
107 op_type_funcs *funcs;
118 const Eo_Class *klass;
123 const Eo_Class *klass;
125 } Eo_Extension_Data_Offset;
130 Eo_Class_Id class_id;
131 const Eo_Class *parent;
132 const Eo_Class_Description *desc;
133 Dich_Chain1 chain[DICH_CHAIN1_SIZE];
134 Eina_Inlist *extensions;
136 Eo_Extension_Data_Offset *extn_data_off;
137 size_t extn_data_size;
139 const Eo_Class **mro;
141 size_t data_offset; /* < Offset of the data within object data. */
143 Eina_Bool constructed : 1;
146 static inline eo_op_func_type
147 dich_func_get(const Eo_Class *klass, Eo_Op op)
149 const Dich_Chain1 *chain1 = &klass->chain[DICH_CHAIN1(op)];
150 if (!chain1) return NULL;
151 if (!chain1->chain) return NULL;
152 Dich_Chain2 *chain2 = &chain1->chain[DICH_CHAIN2(op)];
153 if (!chain2) return NULL;
154 if (!chain2->funcs) return NULL;
156 /* num_ops is calculated from the class. */
157 const Eo_Class *op_klass = OP_CLASS_GET(op);
158 if (!op_klass || (DICH_CHAIN_LAST(op) >= op_klass->desc->ops.count))
161 return chain2->funcs[DICH_CHAIN_LAST(op)].func;
165 dich_func_set(Eo_Class *klass, Eo_Op op, eo_op_func_type func)
167 const Eo_Class *op_klass = OP_CLASS_GET(op);
170 /* Verify op is valid. */
173 /* num_ops is calculated from the class. */
174 num_ops = op_klass->desc->ops.count;
175 if (DICH_CHAIN_LAST(op) >= num_ops)
177 ERR("OP %x is too big for the domain '%s', expected value < %x.",
178 op, op_klass->desc->name, op_klass->desc->ops.count);
184 ERR("OP %x is from an illegal class.", op);
188 Dich_Chain1 *chain1 = &klass->chain[DICH_CHAIN1(op)];
191 chain1->chain = calloc(DICH_CHAIN2_SIZE, sizeof(*(chain1->chain)));
194 Dich_Chain2 *chain2 = &chain1->chain[DICH_CHAIN2(op)];
197 chain2->funcs = calloc(num_ops, sizeof(*(chain2->funcs)));
200 chain2->funcs[DICH_CHAIN_LAST(op)].func = func;
204 dich_func_clean_all(Eo_Class *klass)
207 Dich_Chain1 *chain1 = klass->chain;
209 for (i = 0 ; i < DICH_CHAIN1_SIZE ; i++, chain1++)
212 Dich_Chain2 *chain2 = chain1->chain;
217 for (j = 0 ; j < DICH_CHAIN2_SIZE ; j++, chain2++)
222 chain1->chain = NULL;
228 static const Eo_Op_Description noop_desc =
229 EO_OP_DESCRIPTION(EO_NOOP, "", "No operation.");
231 static const Eo_Op_Description *
232 _eo_op_id_desc_get(Eo_Op op)
234 const Eo_Class *klass = OP_CLASS_GET(op);
235 Eo_Op sub_id = OP_SUB_ID_GET(op);
240 if (klass && (sub_id < klass->desc->ops.count))
241 return klass->desc->ops.descs + sub_id;
247 _eo_op_id_name_get(Eo_Op op)
249 const Eo_Op_Description *desc = _eo_op_id_desc_get(op);
250 return (desc) ? desc->name : NULL;
257 const Eo_Class **kls_itr;
260 static inline Eina_Bool
261 _eo_kls_itr_init(Eo *obj, Eo_Op op)
265 Eo_Kls_Itr_Node *node =
266 EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eo_Kls_Itr_Node);
275 Eo_Kls_Itr_Node *node = calloc(1, sizeof(*node));
277 node->kls_itr = obj->klass->mro;
278 obj->kls_itr = eina_inlist_prepend(obj->kls_itr,
279 EINA_INLIST_GET(node));
286 _eo_kls_itr_end(Eo *obj, Eo_Op op)
288 Eo_Kls_Itr_Node *node =
289 EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eo_Kls_Itr_Node);
294 obj->kls_itr = eina_inlist_remove(obj->kls_itr, obj->kls_itr);
298 static inline const Eo_Class *
299 _eo_kls_itr_get(Eo *obj)
301 Eo_Kls_Itr_Node *node =
302 EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eo_Kls_Itr_Node);
304 return (node) ? *(node->kls_itr) : NULL;
307 static inline const Eo_Class *
308 _eo_kls_itr_next(Eo *obj, Eo_Op op)
310 Eo_Kls_Itr_Node *node =
311 EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eo_Kls_Itr_Node);
313 if (!node || (node->op != op))
315 Eo_Op node_op = (node) ? node->op : EO_NOOP;
316 ERR("Called with op %d ('%s') while expecting: %d ('%s'). This probaly means you called eo_*_super functions from a wrong place.",
317 op, _eo_op_id_name_get(op),
318 node_op, _eo_op_id_name_get(node_op));
322 const Eo_Class **kls_itr = node->kls_itr;
326 node->kls_itr = kls_itr;
335 static inline Eina_Bool
336 _eo_kls_itr_reached_end(const Eo *obj)
338 Eo_Kls_Itr_Node *node =
339 EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eo_Kls_Itr_Node);
340 const Eo_Class **kls_itr = node->kls_itr;
341 return !(*kls_itr && *(kls_itr + 1));
345 _eo_op_internal(Eo *obj, Eina_Bool constant, Eo_Op op, va_list *p_list)
347 const Eo_Class *klass;
348 Eina_Bool ret = EINA_FALSE;
351 const Eo_Op_Description *op_desc = _eo_op_id_desc_get(op);
354 ((constant == EINA_TRUE) && (op_desc->constant == EINA_FALSE)))
356 ERR("Tried calling non-const or non-existant op '%s' (%d) from a const (query) function.", (op_desc) ? op_desc->name : NULL, op);
360 _itr_init = _eo_kls_itr_init(obj, op);
361 klass = _eo_kls_itr_get(obj);
364 eo_op_func_type func = dich_func_get(klass, op);
368 func(obj, eo_data_get(obj, klass), p_list);
373 klass = _eo_kls_itr_next(obj, op);
376 /* Try composite objects */
380 EINA_LIST_FOREACH(obj->composite_objects, itr, emb_obj)
382 if (_eo_op_internal(emb_obj, constant, op, p_list))
392 if (_itr_init) _eo_kls_itr_end(obj, op);
397 eo_do_internal(Eo *obj, Eina_Bool constant, ...)
399 Eina_Bool ret = EINA_TRUE;
403 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
407 va_start(p_list, constant);
409 op = va_arg(p_list, Eo_Op);
412 if (!_eo_op_internal(obj, constant, op, &p_list))
414 const Eo_Class *op_klass = OP_CLASS_GET(op);
415 const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL;
416 ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.",
417 op, _eo_op_id_name_get(op), _dom_name,
418 obj->klass->desc->name);
422 op = va_arg(p_list, Eo_Op);
432 eo_do_super_internal(Eo *obj, Eina_Bool constant, Eo_Op op, ...)
434 const Eo_Class *obj_klass;
435 Eina_Bool ret = EINA_TRUE;
437 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
439 /* Advance the kls itr. */
440 obj_klass = _eo_kls_itr_next(obj, op);
447 va_start(p_list, op);
448 if (!_eo_op_internal(obj, constant, op, &p_list))
450 const Eo_Class *op_klass = OP_CLASS_GET(op);
451 const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL;
452 ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.",
453 op, _eo_op_id_name_get(op), _dom_name,
454 (obj_klass) ? obj_klass->desc->name : NULL);
462 EAPI const Eo_Class *
463 eo_class_get(const Eo *obj)
465 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
471 eo_class_name_get(const Eo_Class *klass)
473 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, NULL);
475 return klass->desc->name;
479 _eo_class_base_op_init(Eo_Class *klass)
481 const Eo_Class_Description *desc = klass->desc;
482 if (!desc || !desc->ops.base_op_id)
485 /* FIXME: Depends on values defined above! */
486 *(desc->ops.base_op_id) = klass->class_id << OP_CLASS_OFFSET;
490 _eo_class_mro_has(const Eo_Class *klass, const Eo_Class *find)
492 const Eo_Class **itr;
493 for (itr = klass->mro ; *itr ; itr++)
505 _eo_class_mro_add(Eina_List *mro, const Eo_Class *klass)
507 Eina_List *extn_pos = NULL;
508 Eina_Bool check_consistency = !mro;
512 mro = eina_list_append(mro, klass);
514 /* Recursively add extenions. */
516 Eo_Extension_Node *extn;
517 EINA_INLIST_FOREACH(klass->extensions, extn)
519 mro = _eo_class_mro_add(mro, extn->klass);
520 /* Not possible: if (!mro) return NULL; */
522 if (check_consistency)
524 extn_pos = eina_list_append(extn_pos, eina_list_last(mro));
529 /* Check if we can create a consistent mro. We only do it for the class
530 * we are working on (i.e no parents). */
531 if (check_consistency)
533 Eo_Extension_Node *extn;
535 Eina_List *itr = extn_pos;
536 EINA_INLIST_FOREACH(klass->extensions, extn)
538 /* Get the first one after the extension. */
539 Eina_List *extn_list = eina_list_next(eina_list_data_get(itr));
541 /* If we found the extension again. */
542 if (eina_list_data_find_list(extn_list, extn->klass))
545 ERR("Cannot create a consistent method resolution order for class '%s' because of '%s'.", klass->desc->name, extn->klass->desc->name);
549 itr = eina_list_next(itr);
554 mro = _eo_class_mro_add(mro, klass->parent);
560 _eo_class_mro_init(Eo_Class *klass)
562 Eina_List *mro = NULL;
564 DBG("Started creating MRO for class '%s'", klass->desc->name);
565 mro = _eo_class_mro_add(mro, klass);
570 /* Remove duplicates and make them the right order. */
572 Eina_List *itr1, *itr2, *itr2n;
574 itr1 = eina_list_last(mro);
577 itr2 = eina_list_prev(itr1);
581 itr2n = eina_list_prev(itr2);
583 if (eina_list_data_get(itr1) == eina_list_data_get(itr2))
585 mro = eina_list_remove_list(mro, itr2);
591 itr1 = eina_list_prev(itr1);
595 /* Copy the mro and free the list. */
597 const Eo_Class *kls_itr;
598 const Eo_Class **mro_itr;
599 klass->mro = calloc(sizeof(*klass->mro), eina_list_count(mro) + 1);
601 mro_itr = klass->mro;
603 EINA_LIST_FREE(mro, kls_itr)
605 *(mro_itr++) = kls_itr;
607 DBG("Added '%s' to MRO", kls_itr->desc->name);
612 DBG("Finished creating MRO for class '%s'", klass->desc->name);
618 _eo_class_constructor(Eo_Class *klass)
620 if (klass->constructed)
623 klass->constructed = EINA_TRUE;
625 if (klass->desc->class_constructor)
626 klass->desc->class_constructor(klass);
630 eo_class_funcs_set(Eo_Class *klass, const Eo_Op_Func_Description *func_descs)
632 EO_MAGIC_RETURN(klass, EO_CLASS_EINA_MAGIC);
634 const Eo_Op_Func_Description *itr;
638 for ( ; itr->op != 0 ; itr++)
640 const Eo_Op_Description *op_desc = _eo_op_id_desc_get(itr->op);
642 if (EINA_LIKELY(!op_desc || (itr->constant == op_desc->constant)))
644 dich_func_set(klass, itr->op, itr->func);
648 ERR("Set function's constant property (%d) is different than the one in the op description (%d) for op '%s' in class '%s'.", itr->constant, op_desc->constant, op_desc->name, klass->desc->name);
655 eo_class_free(Eo_Class *klass)
657 if (klass->constructed)
659 if (klass->desc->class_destructor)
660 klass->desc->class_destructor(klass);
662 dich_func_clean_all(klass);
667 Eo_Extension_Node *extn = NULL;
668 EINA_INLIST_FOREACH_SAFE(klass->extensions, itrn, extn)
677 if (klass->extn_data_off)
678 free(klass->extn_data_off);
685 _eo_class_check_op_descs(const Eo_Class *klass)
687 const Eo_Class_Description *desc = klass->desc;
688 const Eo_Op_Description *itr;
691 if (desc->ops.count > 0)
693 if (!desc->ops.base_op_id)
695 ERR("Class '%s' has a non-zero ops count, but base_id is NULL.",
700 if (!desc->ops.descs)
702 ERR("Class '%s' has a non-zero ops count, but there are no descs.",
708 itr = desc->ops.descs;
709 for (i = 0 ; i < desc->ops.count ; i++, itr++)
711 if (itr->sub_op != i)
715 ERR("Wrong order in Ops description for class '%s'. Expected %d and got %d", desc->name, i, itr->sub_op);
719 ERR("Found too few Ops description for class '%s'. Expected %d descriptions, but found %d.", desc->name, desc->ops.count, i);
725 if (itr && itr->name)
727 ERR("Found extra Ops description for class '%s'. Expected %d descriptions, but found more.", desc->name, desc->ops.count);
734 EAPI const Eo_Class *
735 eo_class_new(const Eo_Class_Description *desc, const Eo_Class *parent, ...)
740 if (parent && !EINA_MAGIC_CHECK(parent, EO_CLASS_EINA_MAGIC))
742 EINA_MAGIC_FAIL(parent, EO_CLASS_EINA_MAGIC);
746 va_start(p_list, parent);
748 EINA_SAFETY_ON_NULL_RETURN_VAL(desc, NULL);
749 EINA_SAFETY_ON_NULL_RETURN_VAL(desc->name, NULL);
751 /* Check restrictions on Interface types. */
752 if (desc->type == EO_CLASS_TYPE_INTERFACE)
754 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->constructor, NULL);
755 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->destructor, NULL);
756 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->class_constructor, NULL);
757 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->class_destructor, NULL);
758 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->data_size, NULL);
761 klass = calloc(1, sizeof(Eo_Class));
762 klass->parent = parent;
764 /* Handle class extensions */
766 Eo_Class *extn = NULL;
768 extn = va_arg(p_list, Eo_Class *);
771 switch (extn->desc->type)
773 case EO_CLASS_TYPE_REGULAR:
774 case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
775 /* Use it like an interface. */
776 case EO_CLASS_TYPE_INTERFACE:
778 case EO_CLASS_TYPE_MIXIN:
780 Eo_Extension_Node *node = calloc(1, sizeof(*node));
783 eina_inlist_append(klass->extensions,
784 EINA_INLIST_GET(node));
789 extn = va_arg(p_list, Eo_Class *);
795 /* Handle the inheritance */
798 /* Verify the inheritance is allowed. */
799 switch (klass->desc->type)
801 case EO_CLASS_TYPE_REGULAR:
802 case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
803 if ((klass->parent->desc->type != EO_CLASS_TYPE_REGULAR) &&
804 (klass->parent->desc->type != EO_CLASS_TYPE_REGULAR_NO_INSTANT))
806 ERR("Regular classes ('%s') aren't allowed to inherit from non-regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
810 case EO_CLASS_TYPE_INTERFACE:
811 case EO_CLASS_TYPE_MIXIN:
812 if ((klass->parent->desc->type != EO_CLASS_TYPE_INTERFACE) &&
813 (klass->parent->desc->type != EO_CLASS_TYPE_MIXIN))
815 ERR("Non-regular classes ('%s') aren't allowed to inherit from regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
822 /* Update the current offset. */
823 /* FIXME: Make sure this alignment is enough. */
824 klass->data_offset = klass->parent->data_offset +
825 EO_ALIGN_SIZE(klass->parent->desc->data_size);
828 if (!_eo_class_check_op_descs(klass))
833 if (!_eo_class_mro_init(klass))
838 /* create MIXIN offset table. */
840 const Eo_Class **mro_itr = klass->mro;
841 Eo_Extension_Data_Offset *extn_data_itr;
843 size_t extn_data_off = klass->data_offset +
844 EO_ALIGN_SIZE(klass->desc->data_size);
846 /* FIXME: Make faster... */
849 if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
850 ((*mro_itr)->desc->data_size > 0))
857 klass->extn_data_off = calloc(extn_num + 1,
858 sizeof(*klass->extn_data_off));
860 extn_data_itr = klass->extn_data_off;
861 mro_itr = klass->mro;
864 if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
865 ((*mro_itr)->desc->data_size > 0))
867 extn_data_itr->klass = *mro_itr;
868 extn_data_itr->offset = extn_data_off;
870 extn_data_off += EO_ALIGN_SIZE(extn_data_itr->klass->desc->data_size);
876 klass->extn_data_size = extn_data_off;
879 eina_lock_take(&_eo_class_creation_lock);
880 klass->class_id = ++_eo_classes_last_id;
882 /* FIXME: Handle errors. */
884 tmp = realloc(_eo_classes, _eo_classes_last_id * sizeof(*_eo_classes));
886 _eo_classes[klass->class_id - 1] = klass;
888 eina_lock_release(&_eo_class_creation_lock);
890 EINA_MAGIC_SET(klass, EO_CLASS_EINA_MAGIC);
892 _eo_class_base_op_init(klass);
894 _eo_class_constructor(klass);
901 eo_class_free(klass);
906 eo_add(const Eo_Class *klass, Eo *parent)
908 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, NULL);
910 if (parent) EO_MAGIC_RETURN_VAL(parent, EO_EINA_MAGIC, NULL);
912 if (EINA_UNLIKELY(klass->desc->type != EO_CLASS_TYPE_REGULAR))
914 ERR("Class '%s' is not instantiate-able. Aborting.", klass->desc->name);
918 Eo *obj = calloc(1, EO_ALIGN_SIZE(sizeof(*obj)) +
919 (klass->data_offset + EO_ALIGN_SIZE(klass->desc->data_size)) +
920 klass->extn_data_size);
922 obj->parent = parent;
926 _eo_kls_itr_init(obj, EO_NOOP);
927 eo_constructor_error_unset(obj);
929 EINA_MAGIC_SET(obj, EO_EINA_MAGIC);
931 _eo_constructor(obj, klass);
933 if (EINA_UNLIKELY(eo_constructor_error_get(obj)))
935 ERR("Type '%s' - One of the object constructors have failed.", klass->desc->name);
939 if (EINA_UNLIKELY(!_eo_kls_itr_reached_end(obj)))
941 ERR("Type '%s' - Not all of the object constructors have been executed.", klass->desc->name);
944 _eo_kls_itr_end(obj, EO_NOOP);
950 /* Unref twice, once for the ref above, and once for the basic object ref. */
965 eo_xref_internal(Eo *obj, const Eo *ref_obj, const char *file, int line)
967 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, obj);
972 Eo_Xref_Node *xref = calloc(1, sizeof(*xref));
973 xref->ref_obj = ref_obj;
977 /* FIXME: Make it sorted. */
978 obj->xrefs = eina_inlist_prepend(obj->xrefs, EINA_INLIST_GET(xref));
989 eo_xunref(Eo *obj, const Eo *ref_obj)
991 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
993 Eo_Xref_Node *xref = NULL;
994 EINA_INLIST_FOREACH(obj->xrefs, xref)
996 if (xref->ref_obj == ref_obj)
1002 obj->xrefs = eina_inlist_remove(obj->xrefs, EINA_INLIST_GET(xref));
1007 ERR("ref_obj (%p) does not reference obj (%p). Aborting unref.", ref_obj, obj);
1019 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, obj);
1026 _eo_del_internal(Eo *obj)
1030 /* We need that for the event callbacks that may ref/unref. */
1033 eo_event_callback_call(obj, EO_EV_DEL, NULL);
1034 obj->delete = EINA_TRUE;
1038 const Eo_Class *klass = eo_class_get(obj);
1039 _eo_kls_itr_init(obj, EO_NOOP);
1040 eo_constructor_error_unset(obj);
1041 _eo_destructor(obj, klass);
1042 if (eo_constructor_error_get(obj))
1044 ERR("Type '%s' - One of the object destructors have failed.", klass->desc->name);
1047 if (!_eo_kls_itr_reached_end(obj))
1049 ERR("Type '%s' - Not all of the object destructors have been executed.", klass->desc->name);
1051 _eo_kls_itr_end(obj, EO_NOOP);
1052 /*FIXME: add eo_class_unref(klass) ? - just to clear the caches. */
1054 /* If for some reason it's not empty, clear it. */
1055 while (obj->kls_itr)
1057 WRN("Kls_Itr is not empty, possibly a bug, please report. - An error will be reported for each kls_itr in the stack.");
1058 Eina_Inlist *nitr = obj->kls_itr->next;
1059 free(EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eo_Kls_Itr_Node));
1060 obj->kls_itr = nitr;
1063 Eina_List *itr, *itr_n;
1065 EINA_LIST_FOREACH_SAFE(obj->composite_objects, itr, itr_n, emb_obj)
1067 eo_composite_object_detach(obj, emb_obj);
1074 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1076 if (--(obj->refcount) == 0)
1078 _eo_del_internal(obj);
1079 /* We need that for the event callbacks that may ref/unref. */
1082 eo_event_callback_call(obj, EO_EV_FREE, NULL);
1087 /* If for some reason it's not empty, clear it. */
1090 WRN("obj->xrefs is not empty, possibly a bug, please report. - An error will be reported for each xref in the stack.");
1091 Eina_Inlist *nitr = obj->xrefs->next;
1092 free(EINA_INLIST_CONTAINER_GET(obj->xrefs, Eo_Xref_Node));
1097 _eo_callback_remove_all(obj);
1104 eo_ref_get(const Eo *obj)
1106 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, 0);
1108 return obj->refcount;
1114 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1116 _eo_del_internal(obj);
1121 eo_parent_get(Eo *obj)
1123 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1129 eo_constructor_error_set(Eo *obj)
1131 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1133 obj->construct_error = EINA_TRUE;
1137 eo_constructor_error_unset(Eo *obj)
1139 obj->construct_error = EINA_FALSE;
1143 eo_constructor_error_get(const Eo *obj)
1145 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_TRUE);
1147 return obj->construct_error;
1151 _eo_constructor_default(Eo *obj)
1153 eo_constructor_super(obj);
1157 _eo_destructor_default(Eo *obj)
1159 eo_destructor_super(obj);
1163 _eo_constructor(Eo *obj, const Eo_Class *klass)
1168 if (klass->desc->constructor)
1169 klass->desc->constructor(obj, eo_data_get(obj, klass));
1171 _eo_constructor_default(obj);
1175 _eo_destructor(Eo *obj, const Eo_Class *klass)
1180 if (klass->desc->destructor)
1181 klass->desc->destructor(obj, eo_data_get(obj, klass));
1183 _eo_destructor_default(obj);
1187 eo_constructor_super(Eo *obj)
1189 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1191 _eo_constructor(obj, _eo_kls_itr_next(obj, EO_NOOP));
1195 eo_destructor_super(Eo *obj)
1197 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1199 _eo_destructor(obj, _eo_kls_itr_next(obj, EO_NOOP));
1203 eo_data_get(const Eo *obj, const Eo_Class *klass)
1205 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1208 if (!_eo_class_mro_has(obj->klass, klass))
1210 ERR("Tried getting data of class '%s' from object of class '%s', but the former is not a direct inheritance of the latter.", klass->desc->name, obj->klass->desc->name);
1214 if (EINA_LIKELY(klass->desc->data_size > 0))
1216 if (EINA_UNLIKELY(klass->desc->type == EO_CLASS_TYPE_MIXIN))
1218 Eo_Extension_Data_Offset *doff_itr =
1219 eo_class_get(obj)->extn_data_off;
1224 while (doff_itr->klass)
1226 if (doff_itr->klass == klass)
1227 return ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj)) +
1234 return ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj)) +
1240 /* FIXME: Enable this to only work when called from the outside.
1241 ERR("Tried getting data of class '%s', but it has none..", klass->desc->name);
1251 const char *log_dom = "eo";
1252 if (_eo_init_count++ > 0)
1258 _eo_classes_last_id = 0;
1259 _eo_log_dom = eina_log_domain_register(log_dom, EINA_COLOR_LIGHTBLUE);
1260 if (_eo_log_dom < 0)
1262 EINA_LOG_ERR("Could not register log domain: %s", log_dom);
1266 if (!eina_lock_new(&_eo_class_creation_lock))
1268 EINA_LOG_ERR("Could not init lock.");
1272 eina_magic_string_static_set(EO_EINA_MAGIC, EO_EINA_MAGIC_STR);
1273 eina_magic_string_static_set(EO_CLASS_EINA_MAGIC,
1274 EO_CLASS_EINA_MAGIC_STR);
1283 Eo_Class **cls_itr = _eo_classes;
1285 if (--_eo_init_count > 0)
1288 for (i = 0 ; i < _eo_classes_last_id ; i++, cls_itr++)
1291 eo_class_free(*cls_itr);
1297 eina_lock_free(&_eo_class_creation_lock);
1299 eina_log_domain_unregister(_eo_log_dom);
1307 eo_composite_object_attach(Eo *obj, Eo *emb_obj)
1309 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1310 EO_MAGIC_RETURN(emb_obj, EO_EINA_MAGIC);
1312 eo_xref(emb_obj, obj);
1313 obj->composite_objects = eina_list_prepend(obj->composite_objects, emb_obj);
1317 eo_composite_object_detach(Eo *obj, Eo *emb_obj)
1319 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1320 EO_MAGIC_RETURN(emb_obj, EO_EINA_MAGIC);
1322 obj->composite_objects = eina_list_remove(obj->composite_objects, emb_obj);
1323 eo_xunref(emb_obj, obj);
1327 eo_composite_is(Eo *emb_obj)
1329 if (!EINA_MAGIC_CHECK(emb_obj, EO_EINA_MAGIC))
1331 EINA_MAGIC_FAIL(emb_obj, EO_EINA_MAGIC);
1335 Eo *obj = eo_parent_get(emb_obj);
1342 EINA_LIST_FOREACH(obj->composite_objects, itr, tmp)
1352 struct _Eo_Callback_Description
1355 const Eo_Event_Description *event;
1358 Eo_Callback_Priority priority;
1359 Eina_Bool delete_me : 1;
1362 /* Actually remove, doesn't care about walking list, or delete_me */
1364 _eo_callback_remove(Eo *obj, Eo_Callback_Description *cb)
1366 obj->callbacks = eina_inlist_remove(obj->callbacks,
1367 EINA_INLIST_GET(cb));
1371 /* Actually remove, doesn't care about walking list, or delete_me */
1373 _eo_callback_remove_all(Eo *obj)
1376 Eo_Callback_Description *cb = NULL;
1377 EINA_INLIST_FOREACH_SAFE(obj->callbacks, initr, cb)
1379 _eo_callback_remove(obj, cb);
1384 _eo_callbacks_clear(Eo *obj)
1387 Eo_Callback_Description *cb = NULL;
1389 /* Abort if we are currently walking the list. */
1390 if (obj->walking_list > 0)
1393 EINA_INLIST_FOREACH_SAFE(obj->callbacks, itn, cb)
1397 _eo_callback_remove(obj, cb);
1403 _callback_priority_cmp(const void *_a, const void *_b)
1405 const Eo_Callback_Description *a, *b;
1406 a = (const Eo_Callback_Description *) _a;
1407 b = (const Eo_Callback_Description *) _b;
1408 if (a->priority < b->priority)
1415 eo_event_callback_priority_add(Eo *obj,
1416 const Eo_Event_Description *desc,
1417 Eo_Callback_Priority priority,
1421 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1423 Eo_Callback_Description *cb = calloc(1, sizeof(*cb));
1426 cb->func_data = (void *) data;
1427 cb->priority = priority;
1428 obj->callbacks = eina_inlist_sorted_insert(obj->callbacks,
1429 EINA_INLIST_GET(cb), _callback_priority_cmp);
1431 eo_event_callback_call(obj, EO_EV_CALLBACK_ADD, desc);
1437 eo_event_callback_del_lazy(Eo *obj, const Eo_Event_Description *desc, Eo_Event_Cb func)
1439 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1442 Eo_Callback_Description *cb;
1443 EINA_INLIST_FOREACH(obj->callbacks, cb)
1445 if ((cb->event == desc) && (cb->func == func))
1449 data = cb->func_data;
1450 cb->delete_me = EINA_TRUE;
1451 _eo_callbacks_clear(obj);
1460 eo_event_callback_call(obj, EO_EV_CALLBACK_DEL, desc);
1465 eo_event_callback_del(Eo *obj, const Eo_Event_Description *desc, Eo_Event_Cb func, const void *user_data)
1467 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1470 Eo_Callback_Description *cb;
1471 EINA_INLIST_FOREACH(obj->callbacks, cb)
1473 if ((cb->event == desc) && (cb->func == func) &&
1474 (cb->func_data == user_data))
1478 data = cb->func_data;
1479 cb->delete_me = EINA_TRUE;
1480 _eo_callbacks_clear(obj);
1489 eo_event_callback_call(obj, EO_EV_CALLBACK_DEL, desc);
1494 eo_event_callback_call(Eo *obj, const Eo_Event_Description *desc,
1495 const void *event_info)
1497 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1499 Eina_Bool ret = EINA_TRUE;
1500 Eo_Callback_Description *cb;
1503 obj->walking_list++;
1505 EINA_INLIST_FOREACH(obj->callbacks, cb)
1507 if (!cb->delete_me && (cb->event == desc))
1509 /* Abort callback calling if the func says so. */
1510 if (!cb->func((void *) cb->func_data, obj, desc,
1511 (void *) event_info))
1520 obj->walking_list--;
1521 _eo_callbacks_clear(obj);
1528 _eo_event_forwarder_callback(void *data, Eo *obj, const Eo_Event_Description *desc, void *event_info)
1531 Eo *new_obj = (Eo *) data;
1532 return eo_event_callback_call(new_obj, desc, event_info);
1535 /* FIXME: Change default priority? Maybe call later? */
1537 eo_event_callback_forwarder_add(Eo *obj, const Eo_Event_Description *desc, Eo *new_obj)
1539 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1540 EO_MAGIC_RETURN_VAL(new_obj, EO_EINA_MAGIC, EINA_FALSE);
1542 return eo_event_callback_add(obj, desc, _eo_event_forwarder_callback, new_obj);
1546 eo_event_callback_forwarder_del(Eo *obj, const Eo_Event_Description *desc, Eo *new_obj)
1548 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1549 EO_MAGIC_RETURN_VAL(new_obj, EO_EINA_MAGIC, EINA_FALSE);
1551 eo_event_callback_del(obj, desc, _eo_event_forwarder_callback, new_obj);