4 #include "eo_private.h"
8 /* The last id that should be reserved for statically allocated classes. */
9 #define EO_STATIC_IDS_LAST 10
11 /* Used inside the class_get functions of classes, see #EO_DEFINE_CLASS */
12 EAPI Eina_Lock _eo_class_creation_lock;
15 static Eo_Class **_eo_classes;
16 static Eo_Class_Id _eo_classes_last_id;
17 static Eina_Bool _eo_init_count = 0;
19 static void _eo_condtor_reset(Eo *obj);
20 static inline Eina_Bool _eo_error_get(const Eo *obj);
21 static inline void _eo_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);
29 const Eo_Class **kls_itr;
36 Eina_Inlist *children;
37 const Eo_Class *klass;
43 Eina_List *composite_objects;
47 Eina_Bool construct_error:1;
48 Eina_Bool condtor_done:1;
50 Eina_Bool composite:1;
52 Eina_Bool manual_free:1;
56 /* Dich search, split to 0xff 0xff 0xffff */
58 #define DICH_CHAIN1_MASK (0xffff)
59 #define DICH_CHAIN_LAST_MASK (0xffff)
60 #define DICH_CHAIN1(x) (((x) >> 16) & DICH_CHAIN1_MASK)
61 #define DICH_CHAIN_LAST(x) ((x) & DICH_CHAIN_LAST_MASK)
63 #define OP_CLASS_OFFSET_GET(x) (((x) >> EO_OP_CLASS_OFFSET) & 0xffff)
64 #define OP_CLASS_GET(op) ({ \
65 Eo_Class_Id tmp = OP_CLASS_OFFSET_GET(op); \
68 #define OP_SUB_ID_GET(op) ((op) & 0xffff)
70 #define ID_CLASS_GET(id) ({ \
71 (Eo_Class *) ((id <= _eo_classes_last_id) && (id > 0)) ? \
72 (_eo_classes[id - 1]) : NULL; \
75 #define EO_ALIGN_SIZE(size) \
76 ((size) + (sizeof(void *) - ((size) % sizeof(void *))))
78 /* Structure of Eo_Op is:
83 typedef struct _Dich_Chain1 Dich_Chain1;
99 const Eo_Class *klass;
104 const Eo_Class *klass;
106 } Eo_Extension_Data_Offset;
111 Eo_Class_Id class_id;
112 const Eo_Class *parent;
113 const Eo_Class_Description *desc;
114 Dich_Chain1 *chain; /**< The size is class_id */
115 Eina_Inlist *extensions;
117 Eo_Extension_Data_Offset *extn_data_off;
118 size_t extn_data_size;
120 const Eo_Class **mro;
123 size_t data_offset; /* < Offset of the data within object data. */
125 Eina_Bool constructed : 1;
129 _dich_chain_alloc(Dich_Chain1 *chain1, size_t num_ops)
133 chain1->funcs = calloc(num_ops, sizeof(*(chain1->funcs)));
138 _dich_copy_all(Eo_Class *dst, const Eo_Class *src)
140 if (!src->chain) return;
144 dst->chain = calloc(dst->class_id, sizeof(*dst->chain));
148 const Dich_Chain1 *sc1 = src->chain;
149 Dich_Chain1 *dc1 = dst->chain;
150 for (i = 0 ; i < src->class_id ; i++, sc1++, dc1++)
155 const Eo_Class *op_klass = ID_CLASS_GET(i + 1);
156 /* Can be NULL because of future static classes. */
160 size_t num_ops = op_klass->desc->ops.count;
161 _dich_chain_alloc(dc1, num_ops);
163 const op_type_funcs *sf = sc1->funcs;
164 op_type_funcs *df = dc1->funcs;
165 for (j = 0 ; j < num_ops ; j++, df++, sf++)
169 memcpy(df, sf, sizeof(*df));
176 static inline const op_type_funcs *
177 _dich_func_get(const Eo_Class *klass, Eo_Op op)
179 if (!klass->chain) return NULL;
181 size_t idx1 = DICH_CHAIN1(op) - 1;
182 if (idx1 >= klass->class_id) return NULL;
183 const Dich_Chain1 *chain1 = &klass->chain[idx1];
184 if (!chain1->funcs) return NULL;
186 size_t idxl = DICH_CHAIN_LAST(op);
187 /* num_ops is calculated from the class. */
188 const Eo_Class *op_klass = ID_CLASS_GET(idx1 + 1);
189 if (!op_klass || (idxl >= op_klass->desc->ops.count))
192 return &chain1->funcs[idxl];
196 _dich_func_set(Eo_Class *klass, Eo_Op op, eo_op_func_type func)
198 const Eo_Class *op_klass = OP_CLASS_GET(op);
201 /* Verify op is valid. */
204 /* num_ops is calculated from the class. */
205 num_ops = op_klass->desc->ops.count;
206 if (DICH_CHAIN_LAST(op) >= num_ops)
208 ERR("OP %x is too big for the domain '%s', expected value < %x.",
209 op, op_klass->desc->name, op_klass->desc->ops.count);
215 ERR("OP %x is from an illegal class.", op);
221 klass->chain = calloc(klass->class_id, sizeof(*klass->chain));
224 size_t idx1 = DICH_CHAIN1(op) - 1;
225 Dich_Chain1 *chain1 = &klass->chain[idx1];
226 _dich_chain_alloc(chain1, num_ops);
227 chain1->funcs[DICH_CHAIN_LAST(op)].func = func;
228 chain1->funcs[DICH_CHAIN_LAST(op)].src = klass;
232 _dich_func_clean_all(Eo_Class *klass)
235 Dich_Chain1 *chain1 = klass->chain;
240 for (i = 0 ; i < klass->class_id ; i++, chain1++)
251 static const Eo_Op_Description noop_desc =
252 EO_OP_DESCRIPTION(EO_NOOP, "No operation.");
254 static const Eo_Op_Description *
255 _eo_op_id_desc_get(Eo_Op op)
257 const Eo_Class *klass = OP_CLASS_GET(op);
258 Eo_Op sub_id = OP_SUB_ID_GET(op);
263 if (klass && (sub_id < klass->desc->ops.count))
264 return klass->desc->ops.descs + sub_id;
270 _eo_op_id_name_get(Eo_Op op)
272 const Eo_Op_Description *desc = _eo_op_id_desc_get(op);
273 return (desc) ? desc->name : NULL;
277 _eo_kls_itr_init(const Eo_Class *obj_klass, Eo_Kls_Itr *cur, Eo_Op op, Eo_Kls_Itr *prev_state)
279 prev_state->op = cur->op;
280 prev_state->kls_itr = cur->kls_itr;
282 /* If we are in a constructor/destructor or we changed an op - init. */
283 if ((cur->op == EO_NOOP) || (cur->op != op))
286 cur->kls_itr = obj_klass->mro;
291 _eo_kls_itr_end(Eo_Kls_Itr *cur, Eo_Kls_Itr *prev_state)
293 if (cur->op != prev_state->op)
295 cur->op = prev_state->op;
296 cur->kls_itr = prev_state->kls_itr;
300 static inline const Eo_Class *
301 _eo_kls_itr_get(Eo_Kls_Itr *cur)
303 return (cur->kls_itr) ? *(cur->kls_itr) : NULL;
306 static inline const Eo_Class *
307 _eo_kls_itr_next(Eo_Kls_Itr *cur, Eo_Op op)
311 Eo_Op node_op = cur->op;
312 ERR("Called with op %x ('%s') while expecting: %x ('%s'). This probaly means you called eo_*_super functions from a wrong place.",
313 op, _eo_op_id_name_get(op),
314 node_op, _eo_op_id_name_get(node_op));
318 const Eo_Class **kls_itr = cur->kls_itr;
321 const op_type_funcs *fsrc = _dich_func_get(*kls_itr, op);
323 while (*kls_itr && (*(kls_itr++) != fsrc->src))
326 cur->kls_itr = kls_itr;
335 static inline const op_type_funcs *
336 _eo_kls_itr_func_get(const Eo_Class *klass, Eo_Kls_Itr *mro_itr, Eo_Op op, Eo_Kls_Itr *prev_state)
338 _eo_kls_itr_init(klass, mro_itr, op, prev_state);
339 klass = _eo_kls_itr_get(mro_itr);
342 const op_type_funcs *func = _dich_func_get(klass, op);
344 if (func && func->func)
353 #define _EO_OP_ERR_NO_OP_PRINT(op, klass) \
356 const Eo_Class *op_klass = OP_CLASS_GET(op); \
357 const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL; \
358 ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.", \
359 op, _eo_op_id_name_get(op), _dom_name, \
360 (klass) ? klass->desc->name : NULL); \
365 _eo_op_internal(Eo *obj, Eo_Op_Type op_type, Eo_Op op, va_list *p_list)
367 Eina_Bool ret = EINA_FALSE;
370 const Eo_Op_Description *op_desc = _eo_op_id_desc_get(op);
374 if (op_desc->op_type == EO_OP_TYPE_CLASS)
376 ERR("Tried calling a class op '%s' (%x) from a non-class context.", (op_desc) ? op_desc->name : NULL, op);
379 else if ((op_type == EO_OP_TYPE_CONST) &&
380 (op_desc->op_type != EO_OP_TYPE_CONST))
382 ERR("Tried calling non-const or non-existant op '%s' (%x) from a const (query) function.", (op_desc) ? op_desc->name : NULL, op);
388 Eo_Kls_Itr prev_state;
391 const op_type_funcs *func =
392 _eo_kls_itr_func_get(obj->klass, &obj->mro_itr, op, &prev_state);
395 func->func(obj, _eo_data_get(obj, func->src), p_list);
401 /* Try composite objects */
405 EINA_LIST_FOREACH(obj->composite_objects, itr, emb_obj)
407 if (_eo_op_internal(emb_obj, op_type, op, p_list))
416 _eo_kls_itr_end(&obj->mro_itr, &prev_state);
421 eo_do_internal(Eo *obj, Eo_Op_Type op_type, ...)
423 Eina_Bool ret = EINA_TRUE;
427 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
431 va_start(p_list, op_type);
433 op = va_arg(p_list, Eo_Op);
436 if (!_eo_op_internal(obj, op_type, op, &p_list))
438 _EO_OP_ERR_NO_OP_PRINT(op, obj->klass);
442 op = va_arg(p_list, Eo_Op);
452 eo_do_super_internal(Eo *obj, Eo_Op_Type op_type, Eo_Op op, ...)
454 const Eo_Class *nklass;
455 Eina_Bool ret = EINA_TRUE;
457 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
459 /* Advance the kls itr. */
460 nklass = _eo_kls_itr_next(&obj->mro_itr, op);
462 if (obj->mro_itr.op != op)
465 va_start(p_list, op);
466 if (!_eo_op_internal(obj, op_type, op, &p_list))
468 _EO_OP_ERR_NO_OP_PRINT(op, nklass);
477 _eo_class_op_internal(Eo_Class *klass, Eo_Op op, va_list *p_list)
479 Eina_Bool ret = EINA_FALSE;
482 const Eo_Op_Description *op_desc = _eo_op_id_desc_get(op);
486 if (op_desc->op_type != EO_OP_TYPE_CLASS)
488 ERR("Tried calling an instance op '%s' (%x) from a class context.", (op_desc) ? op_desc->name : NULL, op);
494 Eo_Kls_Itr prev_state;
497 const op_type_funcs *func =
498 _eo_kls_itr_func_get(klass, &klass->mro_itr, op, &prev_state);
501 ((eo_op_func_type_class) func->func)(klass, p_list);
508 _eo_kls_itr_end(&klass->mro_itr, &prev_state);
513 eo_class_do_internal(const Eo_Class *klass, ...)
515 Eina_Bool ret = EINA_TRUE;
519 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, EINA_FALSE);
521 va_start(p_list, klass);
523 op = va_arg(p_list, Eo_Op);
526 if (!_eo_class_op_internal((Eo_Class *) klass, op, &p_list))
528 _EO_OP_ERR_NO_OP_PRINT(op, klass);
532 op = va_arg(p_list, Eo_Op);
541 eo_class_do_super_internal(const Eo_Class *klass, Eo_Op op, ...)
543 const Eo_Class *nklass;
544 Eina_Bool ret = EINA_TRUE;
546 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, EINA_FALSE);
548 /* Advance the kls itr. */
549 nklass = _eo_kls_itr_next(&((Eo_Class *) klass)->mro_itr, op);
551 if (klass->mro_itr.op != op)
554 va_start(p_list, op);
555 if (!_eo_class_op_internal((Eo_Class *) klass, op, &p_list))
557 _EO_OP_ERR_NO_OP_PRINT(op, nklass);
565 EAPI const Eo_Class *
566 eo_class_get(const Eo *obj)
568 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
574 eo_class_name_get(const Eo_Class *klass)
576 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, NULL);
578 return klass->desc->name;
582 _eo_class_base_op_init(Eo_Class *klass)
584 const Eo_Class_Description *desc = klass->desc;
585 if (!desc || !desc->ops.base_op_id)
588 *(desc->ops.base_op_id) = EO_CLASS_ID_TO_BASE_ID(klass->class_id);
593 _eo_class_mro_has(const Eo_Class *klass, const Eo_Class *find)
595 const Eo_Class **itr;
596 for (itr = klass->mro ; *itr ; itr++)
609 _eo_class_mro_add(Eina_List *mro, const Eo_Class *klass)
611 Eina_List *extn_pos = NULL;
612 Eina_Bool check_consistency = !mro;
616 mro = eina_list_append(mro, klass);
618 /* Recursively add extenions. */
620 Eo_Extension_Node *extn;
621 EINA_INLIST_FOREACH(klass->extensions, extn)
623 mro = _eo_class_mro_add(mro, extn->klass);
624 /* Not possible: if (!mro) return NULL; */
626 if (check_consistency)
628 extn_pos = eina_list_append(extn_pos, eina_list_last(mro));
633 /* Check if we can create a consistent mro. We only do it for the class
634 * we are working on (i.e no parents). */
635 if (check_consistency)
637 Eo_Extension_Node *extn;
639 Eina_List *itr = extn_pos;
640 EINA_INLIST_FOREACH(klass->extensions, extn)
642 /* Get the first one after the extension. */
643 Eina_List *extn_list = eina_list_next(eina_list_data_get(itr));
645 /* If we found the extension again. */
646 if (eina_list_data_find_list(extn_list, extn->klass))
649 ERR("Cannot create a consistent method resolution order for class '%s' because of '%s'.", klass->desc->name, extn->klass->desc->name);
653 itr = eina_list_next(itr);
658 mro = _eo_class_mro_add(mro, klass->parent);
664 _eo_class_mro_init(Eo_Class *klass)
666 Eina_List *mro = NULL;
668 DBG("Started creating MRO for class '%s'", klass->desc->name);
669 mro = _eo_class_mro_add(mro, klass);
674 /* Remove duplicates and make them the right order. */
676 Eina_List *itr1, *itr2, *itr2n;
678 itr1 = eina_list_last(mro);
681 itr2 = eina_list_prev(itr1);
685 itr2n = eina_list_prev(itr2);
687 if (eina_list_data_get(itr1) == eina_list_data_get(itr2))
689 mro = eina_list_remove_list(mro, itr2);
695 itr1 = eina_list_prev(itr1);
699 /* Copy the mro and free the list. */
701 const Eo_Class *kls_itr;
702 const Eo_Class **mro_itr;
703 klass->mro = calloc(sizeof(*klass->mro), eina_list_count(mro) + 1);
705 mro_itr = klass->mro;
707 EINA_LIST_FREE(mro, kls_itr)
709 *(mro_itr++) = kls_itr;
711 DBG("Added '%s' to MRO", kls_itr->desc->name);
716 DBG("Finished creating MRO for class '%s'", klass->desc->name);
722 _eo_class_constructor(Eo_Class *klass)
724 if (klass->constructed)
727 klass->constructed = EINA_TRUE;
729 if (klass->desc->class_constructor)
730 klass->desc->class_constructor(klass);
734 eo_class_funcs_set(Eo_Class *klass, const Eo_Op_Func_Description *func_descs)
736 EO_MAGIC_RETURN(klass, EO_CLASS_EINA_MAGIC);
738 const Eo_Op_Func_Description *itr;
742 for ( ; itr->op_type != EO_OP_TYPE_INVALID ; itr++)
744 const Eo_Op_Description *op_desc = _eo_op_id_desc_get(itr->op);
746 if (EINA_UNLIKELY(!op_desc || (itr->op == EO_NOOP)))
748 ERR("Setting implementation for non-existent op %x for class '%s'. Func index: %d", itr->op, klass->desc->name, itr - func_descs);
750 else if (EINA_LIKELY(itr->op_type == op_desc->op_type))
752 _dich_func_set(klass, itr->op, itr->func);
756 ERR("Set function's op type (%x) is different than the one in the op description (%d) for op '%s' in class '%s'. Func index: %d",
758 (op_desc) ? op_desc->op_type : EO_OP_TYPE_REGULAR,
759 (op_desc) ? op_desc->name : NULL,
768 eo_class_free(Eo_Class *klass)
770 if (klass->constructed)
772 if (klass->desc->class_destructor)
773 klass->desc->class_destructor(klass);
775 _dich_func_clean_all(klass);
780 Eo_Extension_Node *extn = NULL;
781 EINA_INLIST_FOREACH_SAFE(klass->extensions, itrn, extn)
790 if (klass->extn_data_off)
791 free(klass->extn_data_off);
798 _eo_class_check_op_descs(const Eo_Class *klass, Eo_Class_Id id)
800 const Eo_Class_Description *desc = klass->desc;
801 const Eo_Op_Description *itr;
804 if (desc->ops.count > 0)
806 if (((id == 0) || (id > EO_STATIC_IDS_LAST)) && !desc->ops.base_op_id)
808 ERR("Class '%s' has a non-zero ops count, but base_id is NULL.",
813 if (!desc->ops.descs)
815 ERR("Class '%s' has a non-zero ops count, but there are no descs.",
821 itr = desc->ops.descs;
822 for (i = 0 ; i < desc->ops.count ; i++, itr++)
824 if (itr->sub_op != i)
828 ERR("Wrong order in Ops description for class '%s'. Expected %x and got %x", desc->name, i, itr->sub_op);
832 ERR("Found too few Ops description for class '%s'. Expected %x descriptions, but found %x.", desc->name, desc->ops.count, i);
838 if (itr && itr->name)
840 ERR("Found extra Ops description for class '%s'. Expected %d descriptions, but found more.", desc->name, desc->ops.count);
847 EAPI const Eo_Class *
848 eo_class_new(const Eo_Class_Description *desc, Eo_Class_Id id, const Eo_Class *parent, ...)
853 if (parent && !EINA_MAGIC_CHECK(parent, EO_CLASS_EINA_MAGIC))
855 EINA_MAGIC_FAIL(parent, EO_CLASS_EINA_MAGIC);
859 if (id > EO_STATIC_IDS_LAST)
861 ERR("Tried creating a class with the static id %d while the maximum static id is %d. Aborting.", id, EO_STATIC_IDS_LAST);
865 va_start(p_list, parent);
867 EINA_SAFETY_ON_NULL_RETURN_VAL(desc, NULL);
868 EINA_SAFETY_ON_NULL_RETURN_VAL(desc->name, NULL);
870 /* Check restrictions on Interface types. */
871 if (desc->type == EO_CLASS_TYPE_INTERFACE)
873 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->class_constructor, NULL);
874 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->class_destructor, NULL);
875 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->data_size, NULL);
878 klass = calloc(1, sizeof(Eo_Class));
879 klass->parent = parent;
881 /* Handle class extensions */
883 Eo_Class *extn = NULL;
885 extn = va_arg(p_list, Eo_Class *);
888 switch (extn->desc->type)
890 case EO_CLASS_TYPE_REGULAR:
891 case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
892 /* Use it like an interface. */
893 case EO_CLASS_TYPE_INTERFACE:
895 case EO_CLASS_TYPE_MIXIN:
897 Eo_Extension_Node *node = calloc(1, sizeof(*node));
900 eina_inlist_append(klass->extensions,
901 EINA_INLIST_GET(node));
906 extn = va_arg(p_list, Eo_Class *);
912 /* Handle the inheritance */
915 /* Verify the inheritance is allowed. */
916 switch (klass->desc->type)
918 case EO_CLASS_TYPE_REGULAR:
919 case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
920 if ((klass->parent->desc->type != EO_CLASS_TYPE_REGULAR) &&
921 (klass->parent->desc->type != EO_CLASS_TYPE_REGULAR_NO_INSTANT))
923 ERR("Regular classes ('%s') aren't allowed to inherit from non-regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
927 case EO_CLASS_TYPE_INTERFACE:
928 case EO_CLASS_TYPE_MIXIN:
929 if ((klass->parent->desc->type != EO_CLASS_TYPE_INTERFACE) &&
930 (klass->parent->desc->type != EO_CLASS_TYPE_MIXIN))
932 ERR("Non-regular classes ('%s') aren't allowed to inherit from regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
939 /* Update the current offset. */
940 /* FIXME: Make sure this alignment is enough. */
941 klass->data_offset = klass->parent->data_offset +
942 EO_ALIGN_SIZE(klass->parent->desc->data_size);
945 if (!_eo_class_check_op_descs(klass, id))
950 if (!_eo_class_mro_init(klass))
955 /* create MIXIN offset table. */
957 const Eo_Class **mro_itr = klass->mro;
958 Eo_Extension_Data_Offset *extn_data_itr;
960 size_t extn_data_off = klass->data_offset +
961 EO_ALIGN_SIZE(klass->desc->data_size);
963 /* FIXME: Make faster... */
966 if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
967 ((*mro_itr)->desc->data_size > 0))
974 klass->extn_data_off = calloc(extn_num + 1,
975 sizeof(*klass->extn_data_off));
977 extn_data_itr = klass->extn_data_off;
978 mro_itr = klass->mro;
981 if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
982 ((*mro_itr)->desc->data_size > 0))
984 extn_data_itr->klass = *mro_itr;
985 extn_data_itr->offset = extn_data_off;
987 extn_data_off += EO_ALIGN_SIZE(extn_data_itr->klass->desc->data_size);
993 klass->extn_data_size = extn_data_off;
996 eina_lock_take(&_eo_class_creation_lock);
1000 klass->class_id = ++_eo_classes_last_id;
1005 if (_eo_classes && _eo_classes[id - 1])
1007 ERR("A class with id %d was already defined (%s). Aborting.", id,
1008 _eo_classes[id - 1]->desc->name);
1009 eina_lock_release(&_eo_class_creation_lock);
1013 klass->class_id = id;
1018 /* FIXME: Handle errors. */
1019 size_t arrsize = _eo_classes_last_id * sizeof(*_eo_classes);
1021 tmp = realloc(_eo_classes, arrsize);
1023 /* If it's the first allocation, memset. */
1025 memset(tmp, 0, arrsize);
1028 _eo_classes[klass->class_id - 1] = klass;
1030 eina_lock_release(&_eo_class_creation_lock);
1032 EINA_MAGIC_SET(klass, EO_CLASS_EINA_MAGIC);
1034 /* Flatten the function array */
1036 const Eo_Class **mro_itr = klass->mro;
1037 for ( ; *mro_itr ; mro_itr++)
1040 /* Skip ourselves. */
1041 for ( mro_itr-- ; mro_itr > klass->mro ; mro_itr--)
1043 _dich_copy_all(klass, *mro_itr);
1047 _eo_class_base_op_init(klass);
1049 _eo_class_constructor(klass);
1056 eo_class_free(klass);
1061 eo_parent_set(Eo *obj, const Eo *parent)
1063 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1065 EO_MAGIC_RETURN_VAL(parent, EO_EINA_MAGIC, EINA_FALSE);
1067 if (obj->parent == parent)
1072 if (eo_composite_is(obj))
1074 eo_composite_object_detach(obj->parent, obj);
1079 obj->parent->children =
1080 eina_inlist_remove(obj->parent->children, EINA_INLIST_GET(obj));
1081 eo_xunref(obj, obj->parent);
1084 obj->parent = (Eo *) parent;
1087 obj->parent->children =
1088 eina_inlist_append(obj->parent->children, EINA_INLIST_GET(obj));
1089 eo_xref(obj, obj->parent);
1098 eo_add(const Eo_Class *klass, Eo *parent)
1100 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, NULL);
1102 if (parent) EO_MAGIC_RETURN_VAL(parent, EO_EINA_MAGIC, NULL);
1104 if (EINA_UNLIKELY(klass->desc->type != EO_CLASS_TYPE_REGULAR))
1106 ERR("Class '%s' is not instantiate-able. Aborting.", klass->desc->name);
1110 Eo *obj = calloc(1, EO_ALIGN_SIZE(sizeof(*obj)) +
1111 (klass->data_offset + EO_ALIGN_SIZE(klass->desc->data_size)) +
1112 klass->extn_data_size);
1113 EINA_MAGIC_SET(obj, EO_EINA_MAGIC);
1117 eo_parent_set(obj, parent);
1119 Eo_Kls_Itr prev_state;
1121 _eo_kls_itr_init(klass, &obj->mro_itr, EO_NOOP, &prev_state);
1122 _eo_error_unset(obj);
1123 _eo_condtor_reset(obj);
1126 eo_do(obj, eo_constructor());
1128 if (EINA_UNLIKELY(_eo_error_get(obj)))
1130 ERR("Object of class '%s' - One of the object constructors have failed.", klass->desc->name);
1134 if (!obj->condtor_done)
1136 const Eo_Class *cur_klass = _eo_kls_itr_get(&obj->mro_itr);
1137 ERR("Object of class '%s' - Not all of the object constructors have been executed, last constructor was of class: '%s'", klass->desc->name, cur_klass->desc->name);
1140 _eo_kls_itr_end(&obj->mro_itr, &prev_state);
1146 _eo_kls_itr_end(&obj->mro_itr, &prev_state);
1147 /* Unref twice, once for the ref above, and once for the basic object ref. */
1162 eo_xref_internal(Eo *obj, const Eo *ref_obj, const char *file, int line)
1164 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, obj);
1169 Eo_Xref_Node *xref = calloc(1, sizeof(*xref));
1170 xref->ref_obj = ref_obj;
1174 obj->xrefs = eina_inlist_prepend(obj->xrefs, EINA_INLIST_GET(xref));
1185 eo_xunref(Eo *obj, const Eo *ref_obj)
1187 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1189 Eo_Xref_Node *xref = NULL;
1190 EINA_INLIST_FOREACH(obj->xrefs, xref)
1192 if (xref->ref_obj == ref_obj)
1198 obj->xrefs = eina_inlist_remove(obj->xrefs, EINA_INLIST_GET(xref));
1203 ERR("ref_obj (%p) does not reference obj (%p). Aborting unref.", ref_obj, obj);
1220 eo_ref(const Eo *_obj)
1222 Eo *obj = (Eo *) _obj;
1223 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, obj);
1225 return _eo_ref(obj);
1229 _eo_del_internal(Eo *obj)
1231 /* We need that for the event callbacks that may ref/unref. */
1234 eo_do(obj, eo_event_callback_call(EO_EV_DEL, NULL, NULL));
1236 const Eo_Class *klass = eo_class_get(obj);
1237 Eo_Kls_Itr prev_state;
1239 _eo_kls_itr_init(klass, &obj->mro_itr, EO_NOOP, &prev_state);
1240 _eo_error_unset(obj);
1241 _eo_condtor_reset(obj);
1243 eo_do(obj, eo_destructor());
1244 if (_eo_error_get(obj))
1246 ERR("Object of class '%s' - One of the object destructors have failed.", klass->desc->name);
1249 if (!obj->condtor_done)
1251 const Eo_Class *cur_klass = _eo_kls_itr_get(&obj->mro_itr);
1252 ERR("Object of class '%s' - Not all of the object destructors have been executed, last destructor was of class: '%s'", klass->desc->name, cur_klass->desc->name);
1254 _eo_kls_itr_end(&obj->mro_itr, &prev_state);
1255 /*FIXME: add eo_class_unref(klass) ? - just to clear the caches. */
1258 Eina_List *itr, *itr_n;
1260 EINA_LIST_FOREACH_SAFE(obj->composite_objects, itr, itr_n, emb_obj)
1262 eo_composite_object_detach(obj, emb_obj);
1266 while (obj->children)
1268 eo_parent_set(EINA_INLIST_CONTAINER_GET(obj->children, Eo), NULL);
1271 obj->del = EINA_TRUE;
1278 EINA_MAGIC_SET(obj, EO_FREED_EINA_MAGIC);
1285 if (--(obj->refcount) == 0)
1289 ERR("Object %p already deleted.", obj);
1293 _eo_del_internal(obj);
1296 /* If for some reason it's not empty, clear it. */
1299 WRN("obj->xrefs is not empty, possibly a bug, please report. - An error will be reported for each xref in the stack.");
1300 Eina_Inlist *nitr = obj->xrefs->next;
1301 free(EINA_INLIST_CONTAINER_GET(obj->xrefs, Eo_Xref_Node));
1306 if (!obj->manual_free)
1309 _eo_ref(obj); /* If we manual free, we keep a phantom ref. */
1314 eo_unref(const Eo *_obj)
1316 Eo *obj = (Eo *) _obj;
1317 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1323 eo_del(const Eo *obj)
1325 eo_parent_set((Eo *) obj, NULL);
1330 eo_ref_get(const Eo *obj)
1332 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, 0);
1334 return obj->refcount;
1338 eo_parent_get(const Eo *obj)
1340 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1346 eo_error_set_internal(const Eo *obj, const char *file, int line)
1348 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1350 ERR("Error with obj '%p' at %s:%d", obj, file, line);
1352 ((Eo *) obj)->construct_error = EINA_TRUE;
1356 _eo_error_unset(Eo *obj)
1358 obj->construct_error = EINA_FALSE;
1362 _eo_condtor_done(Eo *obj)
1364 if (obj->condtor_done)
1366 ERR("Object %p is already constructed at this point.", obj);
1370 obj->condtor_done = EINA_TRUE;
1374 _eo_condtor_reset(Eo *obj)
1376 obj->condtor_done = EINA_FALSE;
1381 * @brief Check if there was an error when constructing, destructing or calling a function of the object.
1382 * @param obj the object to work on.
1383 * @return @c EINA_TRUE if there was an error.
1385 static inline Eina_Bool
1386 _eo_error_get(const Eo *obj)
1388 return obj->construct_error;
1391 static inline void *
1392 _eo_data_get(const Eo *obj, const Eo_Class *klass)
1394 if (EINA_LIKELY(klass->desc->data_size > 0))
1396 if (EINA_UNLIKELY(klass->desc->type == EO_CLASS_TYPE_MIXIN))
1398 Eo_Extension_Data_Offset *doff_itr =
1399 eo_class_get(obj)->extn_data_off;
1404 while (doff_itr->klass)
1406 if (doff_itr->klass == klass)
1407 return ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj)) +
1414 return ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj)) +
1423 eo_data_get(const Eo *obj, const Eo_Class *klass)
1426 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1429 if (!_eo_class_mro_has(obj->klass, klass))
1431 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);
1436 ret = _eo_data_get(obj, klass);
1439 if (!ret && (klass->desc->data_size == 0))
1441 ERR("Tried getting data of class '%s', but it has none..", klass->desc->name);
1451 const char *log_dom = "eo";
1452 if (_eo_init_count++ > 0)
1458 _eo_classes_last_id = EO_STATIC_IDS_LAST;
1459 _eo_log_dom = eina_log_domain_register(log_dom, EINA_COLOR_LIGHTBLUE);
1460 if (_eo_log_dom < 0)
1462 EINA_LOG_ERR("Could not register log domain: %s", log_dom);
1466 if (!eina_lock_new(&_eo_class_creation_lock))
1468 EINA_LOG_ERR("Could not init lock.");
1472 eina_magic_string_static_set(EO_EINA_MAGIC, EO_EINA_MAGIC_STR);
1473 eina_magic_string_static_set(EO_FREED_EINA_MAGIC,
1474 EO_FREED_EINA_MAGIC_STR);
1475 eina_magic_string_static_set(EO_CLASS_EINA_MAGIC,
1476 EO_CLASS_EINA_MAGIC_STR);
1485 Eo_Class **cls_itr = _eo_classes;
1487 if (--_eo_init_count > 0)
1490 for (i = 0 ; i < _eo_classes_last_id ; i++, cls_itr++)
1493 eo_class_free(*cls_itr);
1499 eina_lock_free(&_eo_class_creation_lock);
1501 eina_log_domain_unregister(_eo_log_dom);
1509 eo_composite_object_attach(Eo *comp_obj, Eo *parent)
1511 EO_MAGIC_RETURN(comp_obj, EO_EINA_MAGIC);
1512 EO_MAGIC_RETURN(parent, EO_EINA_MAGIC);
1514 comp_obj->composite = EINA_TRUE;
1515 eo_parent_set(comp_obj, parent);
1516 parent->composite_objects = eina_list_prepend(parent->composite_objects, comp_obj);
1520 eo_composite_object_detach(Eo *comp_obj, Eo *parent)
1522 EO_MAGIC_RETURN(comp_obj, EO_EINA_MAGIC);
1523 EO_MAGIC_RETURN(parent, EO_EINA_MAGIC);
1525 comp_obj->composite = EINA_FALSE;
1526 parent->composite_objects = eina_list_remove(parent->composite_objects, comp_obj);
1527 eo_parent_set(comp_obj, NULL);
1531 eo_composite_is(const Eo *comp_obj)
1533 if (!EINA_MAGIC_CHECK(comp_obj, EO_EINA_MAGIC))
1535 EINA_MAGIC_FAIL(comp_obj, EO_EINA_MAGIC);
1539 return comp_obj->composite;
1543 eo_manual_free_set(Eo *obj, Eina_Bool manual_free)
1545 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1546 obj->manual_free = manual_free;
1550 eo_manual_free(Eo *obj)
1552 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1554 if (EINA_FALSE == obj->manual_free)
1556 ERR("Tried to manually free the object %p while the option has not been set; see eo_manual_free_set for more information.", obj);
1562 ERR("Tried deleting the object %p while still referenced(%d).", obj, eo_ref_get(obj));