4 #include "eo_private.h"
8 typedef size_t 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);
22 static inline void *_eo_data_get(const Eo *obj, const Eo_Class *klass);
23 static inline Eo *_eo_ref(Eo *obj);
24 static inline void _eo_unref(Eo *obj);
26 typedef struct _Eo_Callback_Description Eo_Callback_Description;
31 const Eo_Class **kls_itr;
34 #define EO_EINA_MAGIC 0xa186bc32 /* Nothing magical about this number. */
35 #define EO_EINA_MAGIC_STR "Eo"
36 #define EO_CLASS_EINA_MAGIC 0xa186bb32 /* Nothing magical about this number. */
37 #define EO_CLASS_EINA_MAGIC_STR "Eo Class"
39 #define EO_MAGIC_RETURN_VAL(d, magic, ret) \
41 if (!EINA_MAGIC_CHECK(d, magic)) \
43 EINA_MAGIC_FAIL(d, magic); \
48 #define EO_MAGIC_RETURN(d, magic) \
50 if (!EINA_MAGIC_CHECK(d, magic)) \
52 EINA_MAGIC_FAIL(d, magic); \
60 const Eo_Class *klass;
66 Eina_List *composite_objects;
68 Eina_Inlist *callbacks;
74 Eina_Bool construct_error:1;
78 /* Dich search, split to 0xff 0xff 0xffff */
80 #define DICH_CHAIN1_MASK (0xffff)
81 #define DICH_CHAIN_LAST_MASK (0xffff)
82 #define DICH_CHAIN1(x) (((x) >> 16) & DICH_CHAIN1_MASK)
83 #define DICH_CHAIN_LAST(x) ((x) & DICH_CHAIN_LAST_MASK)
85 #define OP_CLASS_OFFSET 16
86 #define OP_CLASS_OFFSET_GET(x) (((x) >> OP_CLASS_OFFSET) & 0xffff)
87 #define OP_CLASS_GET(op) ({ \
88 Eo_Class_Id tmp = OP_CLASS_OFFSET_GET(op); \
91 #define OP_SUB_ID_GET(op) ((op) & 0xffff)
93 #define ID_CLASS_GET(id) ({ \
94 (Eo_Class *) ((id <= _eo_classes_last_id) && (id > 0)) ? \
95 (_eo_classes[id - 1]) : NULL; \
98 #define EO_ALIGN_SIZE(size) \
99 ((size) + (sizeof(void *) - ((size) % sizeof(void *))))
101 /* Structure of Eo_Op is:
106 typedef struct _Dich_Chain1 Dich_Chain1;
110 eo_op_func_type func;
115 op_type_funcs *funcs;
121 const Eo_Class *klass;
126 const Eo_Class *klass;
128 } Eo_Extension_Data_Offset;
133 Eo_Class_Id class_id;
134 const Eo_Class *parent;
135 const Eo_Class_Description *desc;
136 Dich_Chain1 *chain; /**< The size is class_id */
137 Eina_Inlist *extensions;
139 Eo_Extension_Data_Offset *extn_data_off;
140 size_t extn_data_size;
142 const Eo_Class **mro;
144 size_t data_offset; /* < Offset of the data within object data. */
146 Eina_Bool constructed : 1;
149 static inline eo_op_func_type
150 dich_func_get(const Eo_Class *klass, Eo_Op op)
152 if (!klass->chain) return NULL;
154 size_t idx1 = DICH_CHAIN1(op) - 1;
155 if (idx1 >= klass->class_id) return NULL;
156 const Dich_Chain1 *chain1 = &klass->chain[idx1];
157 if (!chain1->funcs) return NULL;
159 size_t idxl = DICH_CHAIN_LAST(op);
160 /* num_ops is calculated from the class. */
161 const Eo_Class *op_klass = ID_CLASS_GET(idx1 + 1);
162 if (!op_klass || (idxl >= op_klass->desc->ops.count))
165 return chain1->funcs[idxl].func;
169 dich_func_set(Eo_Class *klass, Eo_Op op, eo_op_func_type func)
171 const Eo_Class *op_klass = OP_CLASS_GET(op);
174 /* Verify op is valid. */
177 /* num_ops is calculated from the class. */
178 num_ops = op_klass->desc->ops.count;
179 if (DICH_CHAIN_LAST(op) >= num_ops)
181 ERR("OP %x is too big for the domain '%s', expected value < %x.",
182 op, op_klass->desc->name, op_klass->desc->ops.count);
188 ERR("OP %x is from an illegal class.", op);
194 klass->chain = calloc(klass->class_id, sizeof(*klass->chain));
197 size_t idx1 = DICH_CHAIN1(op) - 1;
198 Dich_Chain1 *chain1 = &klass->chain[idx1];
201 chain1->funcs = calloc(num_ops, sizeof(*(chain1->funcs)));
204 chain1->funcs[DICH_CHAIN_LAST(op)].func = func;
208 dich_func_clean_all(Eo_Class *klass)
211 Dich_Chain1 *chain1 = klass->chain;
216 for (i = 0 ; i < klass->class_id ; i++, chain1++)
227 static const Eo_Op_Description noop_desc =
228 EO_OP_DESCRIPTION(EO_NOOP, "", "No operation.");
230 static const Eo_Op_Description *
231 _eo_op_id_desc_get(Eo_Op op)
233 const Eo_Class *klass = OP_CLASS_GET(op);
234 Eo_Op sub_id = OP_SUB_ID_GET(op);
239 if (klass && (sub_id < klass->desc->ops.count))
240 return klass->desc->ops.descs + sub_id;
246 _eo_op_id_name_get(Eo_Op op)
248 const Eo_Op_Description *desc = _eo_op_id_desc_get(op);
249 return (desc) ? desc->name : NULL;
253 _eo_kls_itr_init(Eo *obj, Eo_Op op, Eo_Kls_Itr *prev_state)
255 prev_state->op = obj->mro_itr.op;
256 prev_state->kls_itr = obj->mro_itr.kls_itr;
258 /* If we are in a constructor/destructor or we changed an op - init. */
259 if ((op == EO_NOOP) || (obj->mro_itr.op != op))
261 obj->mro_itr.op = op;
262 obj->mro_itr.kls_itr = obj->klass->mro;
267 _eo_kls_itr_end(Eo *obj, Eo_Kls_Itr *prev_state)
269 if (obj->mro_itr.op != prev_state->op)
271 obj->mro_itr.op = prev_state->op;
272 obj->mro_itr.kls_itr = prev_state->kls_itr;
276 static inline const Eo_Class *
277 _eo_kls_itr_get(Eo *obj)
279 return (obj->mro_itr.kls_itr) ? *(obj->mro_itr.kls_itr) : NULL;
282 static inline const Eo_Class *
283 _eo_kls_itr_next(Eo *obj, Eo_Op op)
285 if (obj->mro_itr.op != op)
287 Eo_Op node_op = obj->mro_itr.op;
288 ERR("Called with op %d ('%s') while expecting: %d ('%s'). This probaly means you called eo_*_super functions from a wrong place.",
289 op, _eo_op_id_name_get(op),
290 node_op, _eo_op_id_name_get(node_op));
294 const Eo_Class **kls_itr = obj->mro_itr.kls_itr;
298 obj->mro_itr.kls_itr = kls_itr;
307 static inline Eina_Bool
308 _eo_kls_itr_reached_end(const Eo *obj)
310 const Eo_Class **kls_itr = obj->mro_itr.kls_itr;
311 return !(*kls_itr && *(kls_itr + 1));
315 _eo_op_internal(Eo *obj, Eina_Bool constant, Eo_Op op, va_list *p_list)
317 const Eo_Class *klass;
318 Eina_Bool ret = EINA_FALSE;
320 const Eo_Op_Description *op_desc = _eo_op_id_desc_get(op);
323 ((constant == EINA_TRUE) && (op_desc->constant == EINA_FALSE)))
325 ERR("Tried calling non-const or non-existant op '%s' (%d) from a const (query) function.", (op_desc) ? op_desc->name : NULL, op);
329 Eo_Kls_Itr prev_state;
330 _eo_kls_itr_init(obj, op, &prev_state);
331 klass = _eo_kls_itr_get(obj);
334 eo_op_func_type func = dich_func_get(klass, op);
338 func(obj, _eo_data_get(obj, klass), p_list);
343 klass = _eo_kls_itr_next(obj, op);
346 /* Try composite objects */
350 EINA_LIST_FOREACH(obj->composite_objects, itr, emb_obj)
352 if (_eo_op_internal(emb_obj, constant, op, p_list))
361 _eo_kls_itr_end(obj, &prev_state);
366 eo_do_internal(Eo *obj, Eina_Bool constant, ...)
368 Eina_Bool ret = EINA_TRUE;
372 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
376 va_start(p_list, constant);
378 op = va_arg(p_list, Eo_Op);
381 if (!_eo_op_internal(obj, constant, op, &p_list))
383 const Eo_Class *op_klass = OP_CLASS_GET(op);
384 const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL;
385 ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.",
386 op, _eo_op_id_name_get(op), _dom_name,
387 obj->klass->desc->name);
391 op = va_arg(p_list, Eo_Op);
401 eo_do_super_internal(Eo *obj, Eina_Bool constant, Eo_Op op, ...)
403 const Eo_Class *obj_klass;
404 Eina_Bool ret = EINA_TRUE;
406 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
408 /* Advance the kls itr. */
409 obj_klass = _eo_kls_itr_next(obj, op);
416 va_start(p_list, op);
417 if (!_eo_op_internal(obj, constant, op, &p_list))
419 const Eo_Class *op_klass = OP_CLASS_GET(op);
420 const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL;
421 ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.",
422 op, _eo_op_id_name_get(op), _dom_name,
423 (obj_klass) ? obj_klass->desc->name : NULL);
431 EAPI const Eo_Class *
432 eo_class_get(const Eo *obj)
434 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
440 eo_class_name_get(const Eo_Class *klass)
442 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, NULL);
444 return klass->desc->name;
448 _eo_class_base_op_init(Eo_Class *klass)
450 const Eo_Class_Description *desc = klass->desc;
451 if (!desc || !desc->ops.base_op_id)
454 /* FIXME: Depends on values defined above! */
455 *(desc->ops.base_op_id) = klass->class_id << OP_CLASS_OFFSET;
460 _eo_class_mro_has(const Eo_Class *klass, const Eo_Class *find)
462 const Eo_Class **itr;
463 for (itr = klass->mro ; *itr ; itr++)
476 _eo_class_mro_add(Eina_List *mro, const Eo_Class *klass)
478 Eina_List *extn_pos = NULL;
479 Eina_Bool check_consistency = !mro;
483 mro = eina_list_append(mro, klass);
485 /* Recursively add extenions. */
487 Eo_Extension_Node *extn;
488 EINA_INLIST_FOREACH(klass->extensions, extn)
490 mro = _eo_class_mro_add(mro, extn->klass);
491 /* Not possible: if (!mro) return NULL; */
493 if (check_consistency)
495 extn_pos = eina_list_append(extn_pos, eina_list_last(mro));
500 /* Check if we can create a consistent mro. We only do it for the class
501 * we are working on (i.e no parents). */
502 if (check_consistency)
504 Eo_Extension_Node *extn;
506 Eina_List *itr = extn_pos;
507 EINA_INLIST_FOREACH(klass->extensions, extn)
509 /* Get the first one after the extension. */
510 Eina_List *extn_list = eina_list_next(eina_list_data_get(itr));
512 /* If we found the extension again. */
513 if (eina_list_data_find_list(extn_list, extn->klass))
516 ERR("Cannot create a consistent method resolution order for class '%s' because of '%s'.", klass->desc->name, extn->klass->desc->name);
520 itr = eina_list_next(itr);
525 mro = _eo_class_mro_add(mro, klass->parent);
531 _eo_class_mro_init(Eo_Class *klass)
533 Eina_List *mro = NULL;
535 DBG("Started creating MRO for class '%s'", klass->desc->name);
536 mro = _eo_class_mro_add(mro, klass);
541 /* Remove duplicates and make them the right order. */
543 Eina_List *itr1, *itr2, *itr2n;
545 itr1 = eina_list_last(mro);
548 itr2 = eina_list_prev(itr1);
552 itr2n = eina_list_prev(itr2);
554 if (eina_list_data_get(itr1) == eina_list_data_get(itr2))
556 mro = eina_list_remove_list(mro, itr2);
562 itr1 = eina_list_prev(itr1);
566 /* Copy the mro and free the list. */
568 const Eo_Class *kls_itr;
569 const Eo_Class **mro_itr;
570 klass->mro = calloc(sizeof(*klass->mro), eina_list_count(mro) + 1);
572 mro_itr = klass->mro;
574 EINA_LIST_FREE(mro, kls_itr)
576 *(mro_itr++) = kls_itr;
578 DBG("Added '%s' to MRO", kls_itr->desc->name);
583 DBG("Finished creating MRO for class '%s'", klass->desc->name);
589 _eo_class_constructor(Eo_Class *klass)
591 if (klass->constructed)
594 klass->constructed = EINA_TRUE;
596 if (klass->desc->class_constructor)
597 klass->desc->class_constructor(klass);
601 eo_class_funcs_set(Eo_Class *klass, const Eo_Op_Func_Description *func_descs)
603 EO_MAGIC_RETURN(klass, EO_CLASS_EINA_MAGIC);
605 const Eo_Op_Func_Description *itr;
609 for ( ; itr->op != 0 ; itr++)
611 const Eo_Op_Description *op_desc = _eo_op_id_desc_get(itr->op);
613 if (EINA_LIKELY(!op_desc || (itr->constant == op_desc->constant)))
615 dich_func_set(klass, itr->op, itr->func);
619 ERR("Set function's constant property (%d) is different than the one in the op description (%d) for op '%s' in class '%s'.", itr->constant, op_desc->constant, op_desc->name, klass->desc->name);
626 eo_class_free(Eo_Class *klass)
628 if (klass->constructed)
630 if (klass->desc->class_destructor)
631 klass->desc->class_destructor(klass);
633 dich_func_clean_all(klass);
638 Eo_Extension_Node *extn = NULL;
639 EINA_INLIST_FOREACH_SAFE(klass->extensions, itrn, extn)
648 if (klass->extn_data_off)
649 free(klass->extn_data_off);
656 _eo_class_check_op_descs(const Eo_Class *klass)
658 const Eo_Class_Description *desc = klass->desc;
659 const Eo_Op_Description *itr;
662 if (desc->ops.count > 0)
664 if (!desc->ops.base_op_id)
666 ERR("Class '%s' has a non-zero ops count, but base_id is NULL.",
671 if (!desc->ops.descs)
673 ERR("Class '%s' has a non-zero ops count, but there are no descs.",
679 itr = desc->ops.descs;
680 for (i = 0 ; i < desc->ops.count ; i++, itr++)
682 if (itr->sub_op != i)
686 ERR("Wrong order in Ops description for class '%s'. Expected %d and got %d", desc->name, i, itr->sub_op);
690 ERR("Found too few Ops description for class '%s'. Expected %d descriptions, but found %d.", desc->name, desc->ops.count, i);
696 if (itr && itr->name)
698 ERR("Found extra Ops description for class '%s'. Expected %d descriptions, but found more.", desc->name, desc->ops.count);
705 EAPI const Eo_Class *
706 eo_class_new(const Eo_Class_Description *desc, const Eo_Class *parent, ...)
711 if (parent && !EINA_MAGIC_CHECK(parent, EO_CLASS_EINA_MAGIC))
713 EINA_MAGIC_FAIL(parent, EO_CLASS_EINA_MAGIC);
717 va_start(p_list, parent);
719 EINA_SAFETY_ON_NULL_RETURN_VAL(desc, NULL);
720 EINA_SAFETY_ON_NULL_RETURN_VAL(desc->name, NULL);
722 /* Check restrictions on Interface types. */
723 if (desc->type == EO_CLASS_TYPE_INTERFACE)
725 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->constructor, NULL);
726 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->destructor, NULL);
727 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->class_constructor, NULL);
728 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->class_destructor, NULL);
729 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->data_size, NULL);
732 klass = calloc(1, sizeof(Eo_Class));
733 klass->parent = parent;
735 /* Handle class extensions */
737 Eo_Class *extn = NULL;
739 extn = va_arg(p_list, Eo_Class *);
742 switch (extn->desc->type)
744 case EO_CLASS_TYPE_REGULAR:
745 case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
746 /* Use it like an interface. */
747 case EO_CLASS_TYPE_INTERFACE:
749 case EO_CLASS_TYPE_MIXIN:
751 Eo_Extension_Node *node = calloc(1, sizeof(*node));
754 eina_inlist_append(klass->extensions,
755 EINA_INLIST_GET(node));
760 extn = va_arg(p_list, Eo_Class *);
766 /* Handle the inheritance */
769 /* Verify the inheritance is allowed. */
770 switch (klass->desc->type)
772 case EO_CLASS_TYPE_REGULAR:
773 case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
774 if ((klass->parent->desc->type != EO_CLASS_TYPE_REGULAR) &&
775 (klass->parent->desc->type != EO_CLASS_TYPE_REGULAR_NO_INSTANT))
777 ERR("Regular classes ('%s') aren't allowed to inherit from non-regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
781 case EO_CLASS_TYPE_INTERFACE:
782 case EO_CLASS_TYPE_MIXIN:
783 if ((klass->parent->desc->type != EO_CLASS_TYPE_INTERFACE) &&
784 (klass->parent->desc->type != EO_CLASS_TYPE_MIXIN))
786 ERR("Non-regular classes ('%s') aren't allowed to inherit from regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
793 /* Update the current offset. */
794 /* FIXME: Make sure this alignment is enough. */
795 klass->data_offset = klass->parent->data_offset +
796 EO_ALIGN_SIZE(klass->parent->desc->data_size);
799 if (!_eo_class_check_op_descs(klass))
804 if (!_eo_class_mro_init(klass))
809 /* create MIXIN offset table. */
811 const Eo_Class **mro_itr = klass->mro;
812 Eo_Extension_Data_Offset *extn_data_itr;
814 size_t extn_data_off = klass->data_offset +
815 EO_ALIGN_SIZE(klass->desc->data_size);
817 /* FIXME: Make faster... */
820 if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
821 ((*mro_itr)->desc->data_size > 0))
828 klass->extn_data_off = calloc(extn_num + 1,
829 sizeof(*klass->extn_data_off));
831 extn_data_itr = klass->extn_data_off;
832 mro_itr = klass->mro;
835 if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
836 ((*mro_itr)->desc->data_size > 0))
838 extn_data_itr->klass = *mro_itr;
839 extn_data_itr->offset = extn_data_off;
841 extn_data_off += EO_ALIGN_SIZE(extn_data_itr->klass->desc->data_size);
847 klass->extn_data_size = extn_data_off;
850 eina_lock_take(&_eo_class_creation_lock);
851 klass->class_id = ++_eo_classes_last_id;
853 /* FIXME: Handle errors. */
855 tmp = realloc(_eo_classes, _eo_classes_last_id * sizeof(*_eo_classes));
857 _eo_classes[klass->class_id - 1] = klass;
859 eina_lock_release(&_eo_class_creation_lock);
861 EINA_MAGIC_SET(klass, EO_CLASS_EINA_MAGIC);
863 _eo_class_base_op_init(klass);
865 _eo_class_constructor(klass);
872 eo_class_free(klass);
877 eo_add(const Eo_Class *klass, Eo *parent)
879 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, NULL);
881 if (parent) EO_MAGIC_RETURN_VAL(parent, EO_EINA_MAGIC, NULL);
883 if (EINA_UNLIKELY(klass->desc->type != EO_CLASS_TYPE_REGULAR))
885 ERR("Class '%s' is not instantiate-able. Aborting.", klass->desc->name);
889 Eo *obj = calloc(1, EO_ALIGN_SIZE(sizeof(*obj)) +
890 (klass->data_offset + EO_ALIGN_SIZE(klass->desc->data_size)) +
891 klass->extn_data_size);
893 obj->parent = parent;
897 Eo_Kls_Itr prev_state;
899 _eo_kls_itr_init(obj, EO_NOOP, &prev_state);
900 eo_constructor_error_unset(obj);
902 EINA_MAGIC_SET(obj, EO_EINA_MAGIC);
904 _eo_constructor(obj, klass);
906 if (EINA_UNLIKELY(eo_constructor_error_get(obj)))
908 ERR("Type '%s' - One of the object constructors have failed.", klass->desc->name);
912 if (EINA_UNLIKELY(!_eo_kls_itr_reached_end(obj)))
914 ERR("Type '%s' - Not all of the object constructors have been executed.", klass->desc->name);
917 _eo_kls_itr_end(obj, &prev_state);
923 _eo_kls_itr_end(obj, &prev_state);
924 /* Unref twice, once for the ref above, and once for the basic object ref. */
939 eo_xref_internal(Eo *obj, const Eo *ref_obj, const char *file, int line)
941 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, obj);
946 Eo_Xref_Node *xref = calloc(1, sizeof(*xref));
947 xref->ref_obj = ref_obj;
951 /* FIXME: Make it sorted. */
952 obj->xrefs = eina_inlist_prepend(obj->xrefs, EINA_INLIST_GET(xref));
963 eo_xunref(Eo *obj, const Eo *ref_obj)
965 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
967 Eo_Xref_Node *xref = NULL;
968 EINA_INLIST_FOREACH(obj->xrefs, xref)
970 if (xref->ref_obj == ref_obj)
976 obj->xrefs = eina_inlist_remove(obj->xrefs, EINA_INLIST_GET(xref));
981 ERR("ref_obj (%p) does not reference obj (%p). Aborting unref.", ref_obj, obj);
1000 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, obj);
1002 return _eo_ref(obj);
1006 _eo_del_internal(Eo *obj)
1010 /* We need that for the event callbacks that may ref/unref. */
1013 eo_event_callback_call(obj, EO_EV_DEL, NULL);
1014 obj->delete = EINA_TRUE;
1018 const Eo_Class *klass = eo_class_get(obj);
1019 Eo_Kls_Itr prev_state;
1021 _eo_kls_itr_init(obj, EO_NOOP, &prev_state);
1022 eo_constructor_error_unset(obj);
1023 _eo_destructor(obj, klass);
1024 if (eo_constructor_error_get(obj))
1026 ERR("Type '%s' - One of the object destructors have failed.", klass->desc->name);
1029 if (!_eo_kls_itr_reached_end(obj))
1031 ERR("Type '%s' - Not all of the object destructors have been executed.", klass->desc->name);
1033 _eo_kls_itr_end(obj, &prev_state);
1034 /*FIXME: add eo_class_unref(klass) ? - just to clear the caches. */
1036 Eina_List *itr, *itr_n;
1038 EINA_LIST_FOREACH_SAFE(obj->composite_objects, itr, itr_n, emb_obj)
1040 eo_composite_object_detach(obj, emb_obj);
1047 if (--(obj->refcount) == 0)
1049 _eo_del_internal(obj);
1050 /* We need that for the event callbacks that may ref/unref. */
1053 eo_event_callback_call(obj, EO_EV_FREE, NULL);
1058 /* If for some reason it's not empty, clear it. */
1061 WRN("obj->xrefs is not empty, possibly a bug, please report. - An error will be reported for each xref in the stack.");
1062 Eina_Inlist *nitr = obj->xrefs->next;
1063 free(EINA_INLIST_CONTAINER_GET(obj->xrefs, Eo_Xref_Node));
1068 _eo_callback_remove_all(obj);
1077 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1083 eo_ref_get(const Eo *obj)
1085 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, 0);
1087 return obj->refcount;
1093 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1095 _eo_del_internal(obj);
1100 eo_parent_get(const Eo *obj)
1102 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1108 eo_constructor_error_set(Eo *obj)
1110 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1112 obj->construct_error = EINA_TRUE;
1116 eo_constructor_error_unset(Eo *obj)
1118 obj->construct_error = EINA_FALSE;
1122 eo_constructor_error_get(const Eo *obj)
1124 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_TRUE);
1126 return obj->construct_error;
1130 _eo_constructor_default(Eo *obj)
1132 eo_constructor_super(obj);
1136 _eo_destructor_default(Eo *obj)
1138 eo_destructor_super(obj);
1142 _eo_constructor(Eo *obj, const Eo_Class *klass)
1147 if (klass->desc->constructor)
1148 klass->desc->constructor(obj, _eo_data_get(obj, klass));
1150 _eo_constructor_default(obj);
1154 _eo_destructor(Eo *obj, const Eo_Class *klass)
1159 if (klass->desc->destructor)
1160 klass->desc->destructor(obj, _eo_data_get(obj, klass));
1162 _eo_destructor_default(obj);
1166 eo_constructor_super(Eo *obj)
1168 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1170 _eo_constructor(obj, _eo_kls_itr_next(obj, EO_NOOP));
1174 eo_destructor_super(Eo *obj)
1176 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1178 _eo_destructor(obj, _eo_kls_itr_next(obj, EO_NOOP));
1181 static inline void *
1182 _eo_data_get(const Eo *obj, const Eo_Class *klass)
1184 if (EINA_LIKELY(klass->desc->data_size > 0))
1186 if (EINA_UNLIKELY(klass->desc->type == EO_CLASS_TYPE_MIXIN))
1188 Eo_Extension_Data_Offset *doff_itr =
1189 eo_class_get(obj)->extn_data_off;
1194 while (doff_itr->klass)
1196 if (doff_itr->klass == klass)
1197 return ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj)) +
1204 return ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj)) +
1213 eo_data_get(const Eo *obj, const Eo_Class *klass)
1216 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1219 if (!_eo_class_mro_has(obj->klass, klass))
1221 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);
1226 ret = _eo_data_get(obj, klass);
1228 if (!ret && (klass->desc->data_size == 0))
1230 ERR("Tried getting data of class '%s', but it has none..", klass->desc->name);
1239 const char *log_dom = "eo";
1240 if (_eo_init_count++ > 0)
1246 _eo_classes_last_id = 0;
1247 _eo_log_dom = eina_log_domain_register(log_dom, EINA_COLOR_LIGHTBLUE);
1248 if (_eo_log_dom < 0)
1250 EINA_LOG_ERR("Could not register log domain: %s", log_dom);
1254 if (!eina_lock_new(&_eo_class_creation_lock))
1256 EINA_LOG_ERR("Could not init lock.");
1260 eina_magic_string_static_set(EO_EINA_MAGIC, EO_EINA_MAGIC_STR);
1261 eina_magic_string_static_set(EO_CLASS_EINA_MAGIC,
1262 EO_CLASS_EINA_MAGIC_STR);
1271 Eo_Class **cls_itr = _eo_classes;
1273 if (--_eo_init_count > 0)
1276 for (i = 0 ; i < _eo_classes_last_id ; i++, cls_itr++)
1279 eo_class_free(*cls_itr);
1285 eina_lock_free(&_eo_class_creation_lock);
1287 eina_log_domain_unregister(_eo_log_dom);
1295 eo_composite_object_attach(Eo *obj, Eo *emb_obj)
1297 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1298 EO_MAGIC_RETURN(emb_obj, EO_EINA_MAGIC);
1300 eo_xref(emb_obj, obj);
1301 obj->composite_objects = eina_list_prepend(obj->composite_objects, emb_obj);
1305 eo_composite_object_detach(Eo *obj, Eo *emb_obj)
1307 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1308 EO_MAGIC_RETURN(emb_obj, EO_EINA_MAGIC);
1310 obj->composite_objects = eina_list_remove(obj->composite_objects, emb_obj);
1311 eo_xunref(emb_obj, obj);
1315 eo_composite_is(Eo *emb_obj)
1317 if (!EINA_MAGIC_CHECK(emb_obj, EO_EINA_MAGIC))
1319 EINA_MAGIC_FAIL(emb_obj, EO_EINA_MAGIC);
1323 Eo *obj = eo_parent_get(emb_obj);
1330 EINA_LIST_FOREACH(obj->composite_objects, itr, tmp)
1340 struct _Eo_Callback_Description
1343 const Eo_Event_Description *event;
1346 Eo_Callback_Priority priority;
1347 Eina_Bool delete_me : 1;
1350 /* Actually remove, doesn't care about walking list, or delete_me */
1352 _eo_callback_remove(Eo *obj, Eo_Callback_Description *cb)
1354 obj->callbacks = eina_inlist_remove(obj->callbacks,
1355 EINA_INLIST_GET(cb));
1359 /* Actually remove, doesn't care about walking list, or delete_me */
1361 _eo_callback_remove_all(Eo *obj)
1364 Eo_Callback_Description *cb = NULL;
1365 EINA_INLIST_FOREACH_SAFE(obj->callbacks, initr, cb)
1367 _eo_callback_remove(obj, cb);
1372 _eo_callbacks_clear(Eo *obj)
1375 Eo_Callback_Description *cb = NULL;
1377 /* Abort if we are currently walking the list. */
1378 if (obj->walking_list > 0)
1381 EINA_INLIST_FOREACH_SAFE(obj->callbacks, itn, cb)
1385 _eo_callback_remove(obj, cb);
1391 _callback_priority_cmp(const void *_a, const void *_b)
1393 const Eo_Callback_Description *a, *b;
1394 a = (const Eo_Callback_Description *) _a;
1395 b = (const Eo_Callback_Description *) _b;
1396 if (a->priority < b->priority)
1403 eo_event_callback_priority_add(Eo *obj,
1404 const Eo_Event_Description *desc,
1405 Eo_Callback_Priority priority,
1409 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1411 Eo_Callback_Description *cb = calloc(1, sizeof(*cb));
1414 cb->func_data = (void *) data;
1415 cb->priority = priority;
1416 obj->callbacks = eina_inlist_sorted_insert(obj->callbacks,
1417 EINA_INLIST_GET(cb), _callback_priority_cmp);
1419 eo_event_callback_call(obj, EO_EV_CALLBACK_ADD, desc);
1425 eo_event_callback_del_lazy(Eo *obj, const Eo_Event_Description *desc, Eo_Event_Cb func)
1427 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1430 Eo_Callback_Description *cb;
1431 EINA_INLIST_FOREACH(obj->callbacks, cb)
1433 if ((cb->event == desc) && (cb->func == func))
1437 data = cb->func_data;
1438 cb->delete_me = EINA_TRUE;
1439 _eo_callbacks_clear(obj);
1448 eo_event_callback_call(obj, EO_EV_CALLBACK_DEL, desc);
1453 eo_event_callback_del(Eo *obj, const Eo_Event_Description *desc, Eo_Event_Cb func, const void *user_data)
1455 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1458 Eo_Callback_Description *cb;
1459 EINA_INLIST_FOREACH(obj->callbacks, cb)
1461 if ((cb->event == desc) && (cb->func == func) &&
1462 (cb->func_data == user_data))
1466 data = cb->func_data;
1467 cb->delete_me = EINA_TRUE;
1468 _eo_callbacks_clear(obj);
1477 eo_event_callback_call(obj, EO_EV_CALLBACK_DEL, desc);
1482 eo_event_callback_call(Eo *obj, const Eo_Event_Description *desc,
1483 const void *event_info)
1485 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1487 Eina_Bool ret = EINA_TRUE;
1488 Eo_Callback_Description *cb;
1491 obj->walking_list++;
1493 EINA_INLIST_FOREACH(obj->callbacks, cb)
1495 if (!cb->delete_me && (cb->event == desc))
1497 /* Abort callback calling if the func says so. */
1498 if (!cb->func((void *) cb->func_data, obj, desc,
1499 (void *) event_info))
1508 obj->walking_list--;
1509 _eo_callbacks_clear(obj);
1516 _eo_event_forwarder_callback(void *data, Eo *obj, const Eo_Event_Description *desc, void *event_info)
1519 Eo *new_obj = (Eo *) data;
1520 return eo_event_callback_call(new_obj, desc, event_info);
1523 /* FIXME: Change default priority? Maybe call later? */
1525 eo_event_callback_forwarder_add(Eo *obj, const Eo_Event_Description *desc, Eo *new_obj)
1527 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1528 EO_MAGIC_RETURN_VAL(new_obj, EO_EINA_MAGIC, EINA_FALSE);
1530 return eo_event_callback_add(obj, desc, _eo_event_forwarder_callback, new_obj);
1534 eo_event_callback_forwarder_del(Eo *obj, const Eo_Event_Description *desc, Eo *new_obj)
1536 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1537 EO_MAGIC_RETURN_VAL(new_obj, EO_EINA_MAGIC, EINA_FALSE);
1539 eo_event_callback_del(obj, desc, _eo_event_forwarder_callback, new_obj);