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);
24 typedef struct _Eo_Callback_Description Eo_Callback_Description;
29 const Eo_Class **kls_itr;
32 #define EO_EINA_MAGIC 0xa186bc32 /* Nothing magical about this number. */
33 #define EO_EINA_MAGIC_STR "Eo"
34 #define EO_CLASS_EINA_MAGIC 0xa186bb32 /* Nothing magical about this number. */
35 #define EO_CLASS_EINA_MAGIC_STR "Eo Class"
37 #define EO_MAGIC_RETURN_VAL(d, magic, ret) \
39 if (!EINA_MAGIC_CHECK(d, magic)) \
41 EINA_MAGIC_FAIL(d, magic); \
46 #define EO_MAGIC_RETURN(d, magic) \
48 if (!EINA_MAGIC_CHECK(d, magic)) \
50 EINA_MAGIC_FAIL(d, magic); \
58 const Eo_Class *klass;
64 Eina_List *composite_objects;
66 Eina_Inlist *callbacks;
72 Eina_Bool construct_error:1;
76 /* Dich search, split to 0xff 0xff 0xffff */
78 #define DICH_CHAIN1_MASK (0xffff)
79 #define DICH_CHAIN_LAST_MASK (0xffff)
80 #define DICH_CHAIN1(x) (((x) >> 16) & DICH_CHAIN1_MASK)
81 #define DICH_CHAIN_LAST(x) ((x) & DICH_CHAIN_LAST_MASK)
83 #define OP_CLASS_OFFSET 16
84 #define OP_CLASS_OFFSET_GET(x) (((x) >> OP_CLASS_OFFSET) & 0xffff)
85 #define OP_CLASS_GET(op) ({ \
86 Eo_Class_Id tmp = OP_CLASS_OFFSET_GET(op); \
89 #define OP_SUB_ID_GET(op) ((op) & 0xffff)
91 #define ID_CLASS_GET(id) ({ \
92 (Eo_Class *) ((id <= _eo_classes_last_id) && (id > 0)) ? \
93 (_eo_classes[id - 1]) : NULL; \
96 #define EO_ALIGN_SIZE(size) \
97 ((size) + (sizeof(void *) - ((size) % sizeof(void *))))
99 /* Structure of Eo_Op is:
104 typedef struct _Dich_Chain1 Dich_Chain1;
108 eo_op_func_type func;
113 op_type_funcs *funcs;
119 const Eo_Class *klass;
124 const Eo_Class *klass;
126 } Eo_Extension_Data_Offset;
131 Eo_Class_Id class_id;
132 const Eo_Class *parent;
133 const Eo_Class_Description *desc;
134 Dich_Chain1 *chain; /**< The size is class_id */
135 Eina_Inlist *extensions;
137 Eo_Extension_Data_Offset *extn_data_off;
138 size_t extn_data_size;
140 const Eo_Class **mro;
142 size_t data_offset; /* < Offset of the data within object data. */
144 Eina_Bool constructed : 1;
147 static inline eo_op_func_type
148 dich_func_get(const Eo_Class *klass, Eo_Op op)
150 if (!klass->chain) return NULL;
152 size_t idx1 = DICH_CHAIN1(op) - 1;
153 if (idx1 >= klass->class_id) return NULL;
154 const Dich_Chain1 *chain1 = &klass->chain[idx1];
155 if (!chain1->funcs) return NULL;
157 size_t idxl = DICH_CHAIN_LAST(op);
158 /* num_ops is calculated from the class. */
159 const Eo_Class *op_klass = ID_CLASS_GET(idx1 + 1);
160 if (!op_klass || (idxl >= op_klass->desc->ops.count))
163 return chain1->funcs[idxl].func;
167 dich_func_set(Eo_Class *klass, Eo_Op op, eo_op_func_type func)
169 const Eo_Class *op_klass = OP_CLASS_GET(op);
172 /* Verify op is valid. */
175 /* num_ops is calculated from the class. */
176 num_ops = op_klass->desc->ops.count;
177 if (DICH_CHAIN_LAST(op) >= num_ops)
179 ERR("OP %x is too big for the domain '%s', expected value < %x.",
180 op, op_klass->desc->name, op_klass->desc->ops.count);
186 ERR("OP %x is from an illegal class.", op);
192 klass->chain = calloc(klass->class_id, sizeof(*klass->chain));
195 size_t idx1 = DICH_CHAIN1(op) - 1;
196 Dich_Chain1 *chain1 = &klass->chain[idx1];
199 chain1->funcs = calloc(num_ops, sizeof(*(chain1->funcs)));
202 chain1->funcs[DICH_CHAIN_LAST(op)].func = func;
206 dich_func_clean_all(Eo_Class *klass)
209 Dich_Chain1 *chain1 = klass->chain;
214 for (i = 0 ; i < klass->class_id ; i++, chain1++)
225 static const Eo_Op_Description noop_desc =
226 EO_OP_DESCRIPTION(EO_NOOP, "", "No operation.");
228 static const Eo_Op_Description *
229 _eo_op_id_desc_get(Eo_Op op)
231 const Eo_Class *klass = OP_CLASS_GET(op);
232 Eo_Op sub_id = OP_SUB_ID_GET(op);
237 if (klass && (sub_id < klass->desc->ops.count))
238 return klass->desc->ops.descs + sub_id;
244 _eo_op_id_name_get(Eo_Op op)
246 const Eo_Op_Description *desc = _eo_op_id_desc_get(op);
247 return (desc) ? desc->name : NULL;
251 _eo_kls_itr_init(Eo *obj, Eo_Op op, Eo_Kls_Itr *prev_state)
253 prev_state->op = obj->mro_itr.op;
254 prev_state->kls_itr = obj->mro_itr.kls_itr;
256 /* If we are in a constructor/destructor or we changed an op - init. */
257 if ((op == EO_NOOP) || (obj->mro_itr.op != op))
259 obj->mro_itr.op = op;
260 obj->mro_itr.kls_itr = obj->klass->mro;
265 _eo_kls_itr_end(Eo *obj, Eo_Kls_Itr *prev_state)
267 if (obj->mro_itr.op != prev_state->op)
269 obj->mro_itr.op = prev_state->op;
270 obj->mro_itr.kls_itr = prev_state->kls_itr;
274 static inline const Eo_Class *
275 _eo_kls_itr_get(Eo *obj)
277 return (obj->mro_itr.kls_itr) ? *(obj->mro_itr.kls_itr) : NULL;
280 static inline const Eo_Class *
281 _eo_kls_itr_next(Eo *obj, Eo_Op op)
283 if (obj->mro_itr.op != op)
285 Eo_Op node_op = obj->mro_itr.op;
286 ERR("Called with op %d ('%s') while expecting: %d ('%s'). This probaly means you called eo_*_super functions from a wrong place.",
287 op, _eo_op_id_name_get(op),
288 node_op, _eo_op_id_name_get(node_op));
292 const Eo_Class **kls_itr = obj->mro_itr.kls_itr;
296 obj->mro_itr.kls_itr = kls_itr;
305 static inline Eina_Bool
306 _eo_kls_itr_reached_end(const Eo *obj)
308 const Eo_Class **kls_itr = obj->mro_itr.kls_itr;
309 return !(*kls_itr && *(kls_itr + 1));
313 _eo_op_internal(Eo *obj, Eina_Bool constant, Eo_Op op, va_list *p_list)
315 const Eo_Class *klass;
316 Eina_Bool ret = EINA_FALSE;
318 const Eo_Op_Description *op_desc = _eo_op_id_desc_get(op);
321 ((constant == EINA_TRUE) && (op_desc->constant == EINA_FALSE)))
323 ERR("Tried calling non-const or non-existant op '%s' (%d) from a const (query) function.", (op_desc) ? op_desc->name : NULL, op);
327 Eo_Kls_Itr prev_state;
328 _eo_kls_itr_init(obj, op, &prev_state);
329 klass = _eo_kls_itr_get(obj);
332 eo_op_func_type func = dich_func_get(klass, op);
336 func(obj, _eo_data_get(obj, klass), p_list);
341 klass = _eo_kls_itr_next(obj, op);
344 /* Try composite objects */
348 EINA_LIST_FOREACH(obj->composite_objects, itr, emb_obj)
350 if (_eo_op_internal(emb_obj, constant, op, p_list))
359 _eo_kls_itr_end(obj, &prev_state);
364 eo_do_internal(Eo *obj, Eina_Bool constant, ...)
366 Eina_Bool ret = EINA_TRUE;
370 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
374 va_start(p_list, constant);
376 op = va_arg(p_list, Eo_Op);
379 if (!_eo_op_internal(obj, constant, op, &p_list))
381 const Eo_Class *op_klass = OP_CLASS_GET(op);
382 const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL;
383 ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.",
384 op, _eo_op_id_name_get(op), _dom_name,
385 obj->klass->desc->name);
389 op = va_arg(p_list, Eo_Op);
399 eo_do_super_internal(Eo *obj, Eina_Bool constant, Eo_Op op, ...)
401 const Eo_Class *obj_klass;
402 Eina_Bool ret = EINA_TRUE;
404 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
406 /* Advance the kls itr. */
407 obj_klass = _eo_kls_itr_next(obj, op);
414 va_start(p_list, op);
415 if (!_eo_op_internal(obj, constant, op, &p_list))
417 const Eo_Class *op_klass = OP_CLASS_GET(op);
418 const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL;
419 ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.",
420 op, _eo_op_id_name_get(op), _dom_name,
421 (obj_klass) ? obj_klass->desc->name : NULL);
429 EAPI const Eo_Class *
430 eo_class_get(const Eo *obj)
432 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
438 eo_class_name_get(const Eo_Class *klass)
440 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, NULL);
442 return klass->desc->name;
446 _eo_class_base_op_init(Eo_Class *klass)
448 const Eo_Class_Description *desc = klass->desc;
449 if (!desc || !desc->ops.base_op_id)
452 /* FIXME: Depends on values defined above! */
453 *(desc->ops.base_op_id) = klass->class_id << OP_CLASS_OFFSET;
457 _eo_class_mro_has(const Eo_Class *klass, const Eo_Class *find)
459 const Eo_Class **itr;
460 for (itr = klass->mro ; *itr ; itr++)
472 _eo_class_mro_add(Eina_List *mro, const Eo_Class *klass)
474 Eina_List *extn_pos = NULL;
475 Eina_Bool check_consistency = !mro;
479 mro = eina_list_append(mro, klass);
481 /* Recursively add extenions. */
483 Eo_Extension_Node *extn;
484 EINA_INLIST_FOREACH(klass->extensions, extn)
486 mro = _eo_class_mro_add(mro, extn->klass);
487 /* Not possible: if (!mro) return NULL; */
489 if (check_consistency)
491 extn_pos = eina_list_append(extn_pos, eina_list_last(mro));
496 /* Check if we can create a consistent mro. We only do it for the class
497 * we are working on (i.e no parents). */
498 if (check_consistency)
500 Eo_Extension_Node *extn;
502 Eina_List *itr = extn_pos;
503 EINA_INLIST_FOREACH(klass->extensions, extn)
505 /* Get the first one after the extension. */
506 Eina_List *extn_list = eina_list_next(eina_list_data_get(itr));
508 /* If we found the extension again. */
509 if (eina_list_data_find_list(extn_list, extn->klass))
512 ERR("Cannot create a consistent method resolution order for class '%s' because of '%s'.", klass->desc->name, extn->klass->desc->name);
516 itr = eina_list_next(itr);
521 mro = _eo_class_mro_add(mro, klass->parent);
527 _eo_class_mro_init(Eo_Class *klass)
529 Eina_List *mro = NULL;
531 DBG("Started creating MRO for class '%s'", klass->desc->name);
532 mro = _eo_class_mro_add(mro, klass);
537 /* Remove duplicates and make them the right order. */
539 Eina_List *itr1, *itr2, *itr2n;
541 itr1 = eina_list_last(mro);
544 itr2 = eina_list_prev(itr1);
548 itr2n = eina_list_prev(itr2);
550 if (eina_list_data_get(itr1) == eina_list_data_get(itr2))
552 mro = eina_list_remove_list(mro, itr2);
558 itr1 = eina_list_prev(itr1);
562 /* Copy the mro and free the list. */
564 const Eo_Class *kls_itr;
565 const Eo_Class **mro_itr;
566 klass->mro = calloc(sizeof(*klass->mro), eina_list_count(mro) + 1);
568 mro_itr = klass->mro;
570 EINA_LIST_FREE(mro, kls_itr)
572 *(mro_itr++) = kls_itr;
574 DBG("Added '%s' to MRO", kls_itr->desc->name);
579 DBG("Finished creating MRO for class '%s'", klass->desc->name);
585 _eo_class_constructor(Eo_Class *klass)
587 if (klass->constructed)
590 klass->constructed = EINA_TRUE;
592 if (klass->desc->class_constructor)
593 klass->desc->class_constructor(klass);
597 eo_class_funcs_set(Eo_Class *klass, const Eo_Op_Func_Description *func_descs)
599 EO_MAGIC_RETURN(klass, EO_CLASS_EINA_MAGIC);
601 const Eo_Op_Func_Description *itr;
605 for ( ; itr->op != 0 ; itr++)
607 const Eo_Op_Description *op_desc = _eo_op_id_desc_get(itr->op);
609 if (EINA_LIKELY(!op_desc || (itr->constant == op_desc->constant)))
611 dich_func_set(klass, itr->op, itr->func);
615 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);
622 eo_class_free(Eo_Class *klass)
624 if (klass->constructed)
626 if (klass->desc->class_destructor)
627 klass->desc->class_destructor(klass);
629 dich_func_clean_all(klass);
634 Eo_Extension_Node *extn = NULL;
635 EINA_INLIST_FOREACH_SAFE(klass->extensions, itrn, extn)
644 if (klass->extn_data_off)
645 free(klass->extn_data_off);
652 _eo_class_check_op_descs(const Eo_Class *klass)
654 const Eo_Class_Description *desc = klass->desc;
655 const Eo_Op_Description *itr;
658 if (desc->ops.count > 0)
660 if (!desc->ops.base_op_id)
662 ERR("Class '%s' has a non-zero ops count, but base_id is NULL.",
667 if (!desc->ops.descs)
669 ERR("Class '%s' has a non-zero ops count, but there are no descs.",
675 itr = desc->ops.descs;
676 for (i = 0 ; i < desc->ops.count ; i++, itr++)
678 if (itr->sub_op != i)
682 ERR("Wrong order in Ops description for class '%s'. Expected %d and got %d", desc->name, i, itr->sub_op);
686 ERR("Found too few Ops description for class '%s'. Expected %d descriptions, but found %d.", desc->name, desc->ops.count, i);
692 if (itr && itr->name)
694 ERR("Found extra Ops description for class '%s'. Expected %d descriptions, but found more.", desc->name, desc->ops.count);
701 EAPI const Eo_Class *
702 eo_class_new(const Eo_Class_Description *desc, const Eo_Class *parent, ...)
707 if (parent && !EINA_MAGIC_CHECK(parent, EO_CLASS_EINA_MAGIC))
709 EINA_MAGIC_FAIL(parent, EO_CLASS_EINA_MAGIC);
713 va_start(p_list, parent);
715 EINA_SAFETY_ON_NULL_RETURN_VAL(desc, NULL);
716 EINA_SAFETY_ON_NULL_RETURN_VAL(desc->name, NULL);
718 /* Check restrictions on Interface types. */
719 if (desc->type == EO_CLASS_TYPE_INTERFACE)
721 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->constructor, NULL);
722 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->destructor, NULL);
723 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->class_constructor, NULL);
724 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->class_destructor, NULL);
725 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->data_size, NULL);
728 klass = calloc(1, sizeof(Eo_Class));
729 klass->parent = parent;
731 /* Handle class extensions */
733 Eo_Class *extn = NULL;
735 extn = va_arg(p_list, Eo_Class *);
738 switch (extn->desc->type)
740 case EO_CLASS_TYPE_REGULAR:
741 case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
742 /* Use it like an interface. */
743 case EO_CLASS_TYPE_INTERFACE:
745 case EO_CLASS_TYPE_MIXIN:
747 Eo_Extension_Node *node = calloc(1, sizeof(*node));
750 eina_inlist_append(klass->extensions,
751 EINA_INLIST_GET(node));
756 extn = va_arg(p_list, Eo_Class *);
762 /* Handle the inheritance */
765 /* Verify the inheritance is allowed. */
766 switch (klass->desc->type)
768 case EO_CLASS_TYPE_REGULAR:
769 case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
770 if ((klass->parent->desc->type != EO_CLASS_TYPE_REGULAR) &&
771 (klass->parent->desc->type != EO_CLASS_TYPE_REGULAR_NO_INSTANT))
773 ERR("Regular classes ('%s') aren't allowed to inherit from non-regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
777 case EO_CLASS_TYPE_INTERFACE:
778 case EO_CLASS_TYPE_MIXIN:
779 if ((klass->parent->desc->type != EO_CLASS_TYPE_INTERFACE) &&
780 (klass->parent->desc->type != EO_CLASS_TYPE_MIXIN))
782 ERR("Non-regular classes ('%s') aren't allowed to inherit from regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
789 /* Update the current offset. */
790 /* FIXME: Make sure this alignment is enough. */
791 klass->data_offset = klass->parent->data_offset +
792 EO_ALIGN_SIZE(klass->parent->desc->data_size);
795 if (!_eo_class_check_op_descs(klass))
800 if (!_eo_class_mro_init(klass))
805 /* create MIXIN offset table. */
807 const Eo_Class **mro_itr = klass->mro;
808 Eo_Extension_Data_Offset *extn_data_itr;
810 size_t extn_data_off = klass->data_offset +
811 EO_ALIGN_SIZE(klass->desc->data_size);
813 /* FIXME: Make faster... */
816 if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
817 ((*mro_itr)->desc->data_size > 0))
824 klass->extn_data_off = calloc(extn_num + 1,
825 sizeof(*klass->extn_data_off));
827 extn_data_itr = klass->extn_data_off;
828 mro_itr = klass->mro;
831 if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
832 ((*mro_itr)->desc->data_size > 0))
834 extn_data_itr->klass = *mro_itr;
835 extn_data_itr->offset = extn_data_off;
837 extn_data_off += EO_ALIGN_SIZE(extn_data_itr->klass->desc->data_size);
843 klass->extn_data_size = extn_data_off;
846 eina_lock_take(&_eo_class_creation_lock);
847 klass->class_id = ++_eo_classes_last_id;
849 /* FIXME: Handle errors. */
851 tmp = realloc(_eo_classes, _eo_classes_last_id * sizeof(*_eo_classes));
853 _eo_classes[klass->class_id - 1] = klass;
855 eina_lock_release(&_eo_class_creation_lock);
857 EINA_MAGIC_SET(klass, EO_CLASS_EINA_MAGIC);
859 _eo_class_base_op_init(klass);
861 _eo_class_constructor(klass);
868 eo_class_free(klass);
873 eo_add(const Eo_Class *klass, Eo *parent)
875 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, NULL);
877 if (parent) EO_MAGIC_RETURN_VAL(parent, EO_EINA_MAGIC, NULL);
879 if (EINA_UNLIKELY(klass->desc->type != EO_CLASS_TYPE_REGULAR))
881 ERR("Class '%s' is not instantiate-able. Aborting.", klass->desc->name);
885 Eo *obj = calloc(1, EO_ALIGN_SIZE(sizeof(*obj)) +
886 (klass->data_offset + EO_ALIGN_SIZE(klass->desc->data_size)) +
887 klass->extn_data_size);
889 obj->parent = parent;
893 Eo_Kls_Itr prev_state;
895 _eo_kls_itr_init(obj, EO_NOOP, &prev_state);
896 eo_constructor_error_unset(obj);
898 EINA_MAGIC_SET(obj, EO_EINA_MAGIC);
900 _eo_constructor(obj, klass);
902 if (EINA_UNLIKELY(eo_constructor_error_get(obj)))
904 ERR("Type '%s' - One of the object constructors have failed.", klass->desc->name);
908 if (EINA_UNLIKELY(!_eo_kls_itr_reached_end(obj)))
910 ERR("Type '%s' - Not all of the object constructors have been executed.", klass->desc->name);
913 _eo_kls_itr_end(obj, &prev_state);
919 _eo_kls_itr_end(obj, &prev_state);
920 /* Unref twice, once for the ref above, and once for the basic object ref. */
935 eo_xref_internal(Eo *obj, const Eo *ref_obj, const char *file, int line)
937 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, obj);
942 Eo_Xref_Node *xref = calloc(1, sizeof(*xref));
943 xref->ref_obj = ref_obj;
947 /* FIXME: Make it sorted. */
948 obj->xrefs = eina_inlist_prepend(obj->xrefs, EINA_INLIST_GET(xref));
959 eo_xunref(Eo *obj, const Eo *ref_obj)
961 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
963 Eo_Xref_Node *xref = NULL;
964 EINA_INLIST_FOREACH(obj->xrefs, xref)
966 if (xref->ref_obj == ref_obj)
972 obj->xrefs = eina_inlist_remove(obj->xrefs, EINA_INLIST_GET(xref));
977 ERR("ref_obj (%p) does not reference obj (%p). Aborting unref.", ref_obj, obj);
989 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, obj);
996 _eo_del_internal(Eo *obj)
1000 /* We need that for the event callbacks that may ref/unref. */
1003 eo_event_callback_call(obj, EO_EV_DEL, NULL);
1004 obj->delete = EINA_TRUE;
1008 const Eo_Class *klass = eo_class_get(obj);
1009 Eo_Kls_Itr prev_state;
1011 _eo_kls_itr_init(obj, EO_NOOP, &prev_state);
1012 eo_constructor_error_unset(obj);
1013 _eo_destructor(obj, klass);
1014 if (eo_constructor_error_get(obj))
1016 ERR("Type '%s' - One of the object destructors have failed.", klass->desc->name);
1019 if (!_eo_kls_itr_reached_end(obj))
1021 ERR("Type '%s' - Not all of the object destructors have been executed.", klass->desc->name);
1023 _eo_kls_itr_end(obj, &prev_state);
1024 /*FIXME: add eo_class_unref(klass) ? - just to clear the caches. */
1026 Eina_List *itr, *itr_n;
1028 EINA_LIST_FOREACH_SAFE(obj->composite_objects, itr, itr_n, emb_obj)
1030 eo_composite_object_detach(obj, emb_obj);
1037 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1039 if (--(obj->refcount) == 0)
1041 _eo_del_internal(obj);
1042 /* We need that for the event callbacks that may ref/unref. */
1045 eo_event_callback_call(obj, EO_EV_FREE, NULL);
1050 /* If for some reason it's not empty, clear it. */
1053 WRN("obj->xrefs is not empty, possibly a bug, please report. - An error will be reported for each xref in the stack.");
1054 Eina_Inlist *nitr = obj->xrefs->next;
1055 free(EINA_INLIST_CONTAINER_GET(obj->xrefs, Eo_Xref_Node));
1060 _eo_callback_remove_all(obj);
1067 eo_ref_get(const Eo *obj)
1069 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, 0);
1071 return obj->refcount;
1077 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1079 _eo_del_internal(obj);
1084 eo_parent_get(Eo *obj)
1086 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1092 eo_constructor_error_set(Eo *obj)
1094 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1096 obj->construct_error = EINA_TRUE;
1100 eo_constructor_error_unset(Eo *obj)
1102 obj->construct_error = EINA_FALSE;
1106 eo_constructor_error_get(const Eo *obj)
1108 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_TRUE);
1110 return obj->construct_error;
1114 _eo_constructor_default(Eo *obj)
1116 eo_constructor_super(obj);
1120 _eo_destructor_default(Eo *obj)
1122 eo_destructor_super(obj);
1126 _eo_constructor(Eo *obj, const Eo_Class *klass)
1131 if (klass->desc->constructor)
1132 klass->desc->constructor(obj, _eo_data_get(obj, klass));
1134 _eo_constructor_default(obj);
1138 _eo_destructor(Eo *obj, const Eo_Class *klass)
1143 if (klass->desc->destructor)
1144 klass->desc->destructor(obj, _eo_data_get(obj, klass));
1146 _eo_destructor_default(obj);
1150 eo_constructor_super(Eo *obj)
1152 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1154 _eo_constructor(obj, _eo_kls_itr_next(obj, EO_NOOP));
1158 eo_destructor_super(Eo *obj)
1160 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1162 _eo_destructor(obj, _eo_kls_itr_next(obj, EO_NOOP));
1165 static inline void *
1166 _eo_data_get(const Eo *obj, const Eo_Class *klass)
1168 if (EINA_LIKELY(klass->desc->data_size > 0))
1170 if (EINA_UNLIKELY(klass->desc->type == EO_CLASS_TYPE_MIXIN))
1172 Eo_Extension_Data_Offset *doff_itr =
1173 eo_class_get(obj)->extn_data_off;
1178 while (doff_itr->klass)
1180 if (doff_itr->klass == klass)
1181 return ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj)) +
1188 return ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj)) +
1197 eo_data_get(const Eo *obj, const Eo_Class *klass)
1200 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1203 if (!_eo_class_mro_has(obj->klass, klass))
1205 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);
1210 ret = _eo_data_get(obj, klass);
1212 if (!ret && (klass->desc->data_size == 0))
1214 ERR("Tried getting data of class '%s', but it has none..", klass->desc->name);
1223 const char *log_dom = "eo";
1224 if (_eo_init_count++ > 0)
1230 _eo_classes_last_id = 0;
1231 _eo_log_dom = eina_log_domain_register(log_dom, EINA_COLOR_LIGHTBLUE);
1232 if (_eo_log_dom < 0)
1234 EINA_LOG_ERR("Could not register log domain: %s", log_dom);
1238 if (!eina_lock_new(&_eo_class_creation_lock))
1240 EINA_LOG_ERR("Could not init lock.");
1244 eina_magic_string_static_set(EO_EINA_MAGIC, EO_EINA_MAGIC_STR);
1245 eina_magic_string_static_set(EO_CLASS_EINA_MAGIC,
1246 EO_CLASS_EINA_MAGIC_STR);
1255 Eo_Class **cls_itr = _eo_classes;
1257 if (--_eo_init_count > 0)
1260 for (i = 0 ; i < _eo_classes_last_id ; i++, cls_itr++)
1263 eo_class_free(*cls_itr);
1269 eina_lock_free(&_eo_class_creation_lock);
1271 eina_log_domain_unregister(_eo_log_dom);
1279 eo_composite_object_attach(Eo *obj, Eo *emb_obj)
1281 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1282 EO_MAGIC_RETURN(emb_obj, EO_EINA_MAGIC);
1284 eo_xref(emb_obj, obj);
1285 obj->composite_objects = eina_list_prepend(obj->composite_objects, emb_obj);
1289 eo_composite_object_detach(Eo *obj, Eo *emb_obj)
1291 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1292 EO_MAGIC_RETURN(emb_obj, EO_EINA_MAGIC);
1294 obj->composite_objects = eina_list_remove(obj->composite_objects, emb_obj);
1295 eo_xunref(emb_obj, obj);
1299 eo_composite_is(Eo *emb_obj)
1301 if (!EINA_MAGIC_CHECK(emb_obj, EO_EINA_MAGIC))
1303 EINA_MAGIC_FAIL(emb_obj, EO_EINA_MAGIC);
1307 Eo *obj = eo_parent_get(emb_obj);
1314 EINA_LIST_FOREACH(obj->composite_objects, itr, tmp)
1324 struct _Eo_Callback_Description
1327 const Eo_Event_Description *event;
1330 Eo_Callback_Priority priority;
1331 Eina_Bool delete_me : 1;
1334 /* Actually remove, doesn't care about walking list, or delete_me */
1336 _eo_callback_remove(Eo *obj, Eo_Callback_Description *cb)
1338 obj->callbacks = eina_inlist_remove(obj->callbacks,
1339 EINA_INLIST_GET(cb));
1343 /* Actually remove, doesn't care about walking list, or delete_me */
1345 _eo_callback_remove_all(Eo *obj)
1348 Eo_Callback_Description *cb = NULL;
1349 EINA_INLIST_FOREACH_SAFE(obj->callbacks, initr, cb)
1351 _eo_callback_remove(obj, cb);
1356 _eo_callbacks_clear(Eo *obj)
1359 Eo_Callback_Description *cb = NULL;
1361 /* Abort if we are currently walking the list. */
1362 if (obj->walking_list > 0)
1365 EINA_INLIST_FOREACH_SAFE(obj->callbacks, itn, cb)
1369 _eo_callback_remove(obj, cb);
1375 _callback_priority_cmp(const void *_a, const void *_b)
1377 const Eo_Callback_Description *a, *b;
1378 a = (const Eo_Callback_Description *) _a;
1379 b = (const Eo_Callback_Description *) _b;
1380 if (a->priority < b->priority)
1387 eo_event_callback_priority_add(Eo *obj,
1388 const Eo_Event_Description *desc,
1389 Eo_Callback_Priority priority,
1393 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1395 Eo_Callback_Description *cb = calloc(1, sizeof(*cb));
1398 cb->func_data = (void *) data;
1399 cb->priority = priority;
1400 obj->callbacks = eina_inlist_sorted_insert(obj->callbacks,
1401 EINA_INLIST_GET(cb), _callback_priority_cmp);
1403 eo_event_callback_call(obj, EO_EV_CALLBACK_ADD, desc);
1409 eo_event_callback_del_lazy(Eo *obj, const Eo_Event_Description *desc, Eo_Event_Cb func)
1411 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1414 Eo_Callback_Description *cb;
1415 EINA_INLIST_FOREACH(obj->callbacks, cb)
1417 if ((cb->event == desc) && (cb->func == func))
1421 data = cb->func_data;
1422 cb->delete_me = EINA_TRUE;
1423 _eo_callbacks_clear(obj);
1432 eo_event_callback_call(obj, EO_EV_CALLBACK_DEL, desc);
1437 eo_event_callback_del(Eo *obj, const Eo_Event_Description *desc, Eo_Event_Cb func, const void *user_data)
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) &&
1446 (cb->func_data == user_data))
1450 data = cb->func_data;
1451 cb->delete_me = EINA_TRUE;
1452 _eo_callbacks_clear(obj);
1461 eo_event_callback_call(obj, EO_EV_CALLBACK_DEL, desc);
1466 eo_event_callback_call(Eo *obj, const Eo_Event_Description *desc,
1467 const void *event_info)
1469 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1471 Eina_Bool ret = EINA_TRUE;
1472 Eo_Callback_Description *cb;
1475 obj->walking_list++;
1477 EINA_INLIST_FOREACH(obj->callbacks, cb)
1479 if (!cb->delete_me && (cb->event == desc))
1481 /* Abort callback calling if the func says so. */
1482 if (!cb->func((void *) cb->func_data, obj, desc,
1483 (void *) event_info))
1492 obj->walking_list--;
1493 _eo_callbacks_clear(obj);
1500 _eo_event_forwarder_callback(void *data, Eo *obj, const Eo_Event_Description *desc, void *event_info)
1503 Eo *new_obj = (Eo *) data;
1504 return eo_event_callback_call(new_obj, desc, event_info);
1507 /* FIXME: Change default priority? Maybe call later? */
1509 eo_event_callback_forwarder_add(Eo *obj, const Eo_Event_Description *desc, Eo *new_obj)
1511 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1512 EO_MAGIC_RETURN_VAL(new_obj, EO_EINA_MAGIC, EINA_FALSE);
1514 return eo_event_callback_add(obj, desc, _eo_event_forwarder_callback, new_obj);
1518 eo_event_callback_forwarder_del(Eo *obj, const Eo_Event_Description *desc, Eo *new_obj)
1520 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1521 EO_MAGIC_RETURN_VAL(new_obj, EO_EINA_MAGIC, EINA_FALSE);
1523 eo_event_callback_del(obj, desc, _eo_event_forwarder_callback, new_obj);