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_constructor(Eo *obj, const Eo_Class *klass);
20 static void _eo_destructor(Eo *obj, const Eo_Class *klass);
21 static inline Eina_Bool _eo_error_get(const Eo *obj);
22 static inline void _eo_error_unset(Eo *obj);
23 static inline void *_eo_data_get(const Eo *obj, const Eo_Class *klass);
24 static inline Eo *_eo_ref(Eo *obj);
25 static inline void _eo_unref(Eo *obj);
30 const Eo_Class **kls_itr;
37 Eina_Inlist *children;
38 const Eo_Class *klass;
44 Eina_List *composite_objects;
49 Eina_Bool construct_error:1;
50 Eina_Bool manual_free:1;
54 /* Dich search, split to 0xff 0xff 0xffff */
56 #define DICH_CHAIN1_MASK (0xffff)
57 #define DICH_CHAIN_LAST_MASK (0xffff)
58 #define DICH_CHAIN1(x) (((x) >> 16) & DICH_CHAIN1_MASK)
59 #define DICH_CHAIN_LAST(x) ((x) & DICH_CHAIN_LAST_MASK)
61 #define OP_CLASS_OFFSET_GET(x) (((x) >> EO_OP_CLASS_OFFSET) & 0xffff)
62 #define OP_CLASS_GET(op) ({ \
63 Eo_Class_Id tmp = OP_CLASS_OFFSET_GET(op); \
66 #define OP_SUB_ID_GET(op) ((op) & 0xffff)
68 #define ID_CLASS_GET(id) ({ \
69 (Eo_Class *) ((id <= _eo_classes_last_id) && (id > 0)) ? \
70 (_eo_classes[id - 1]) : NULL; \
73 #define EO_ALIGN_SIZE(size) \
74 ((size) + (sizeof(void *) - ((size) % sizeof(void *))))
76 /* Structure of Eo_Op is:
81 typedef struct _Dich_Chain1 Dich_Chain1;
97 const Eo_Class *klass;
102 const Eo_Class *klass;
104 } Eo_Extension_Data_Offset;
109 Eo_Class_Id class_id;
110 const Eo_Class *parent;
111 const Eo_Class_Description *desc;
112 Dich_Chain1 *chain; /**< The size is class_id */
113 Eina_Inlist *extensions;
115 Eo_Extension_Data_Offset *extn_data_off;
116 size_t extn_data_size;
118 const Eo_Class **mro;
121 size_t data_offset; /* < Offset of the data within object data. */
123 Eina_Bool constructed : 1;
127 _dich_chain_alloc(Dich_Chain1 *chain1, size_t num_ops)
131 chain1->funcs = calloc(num_ops, sizeof(*(chain1->funcs)));
136 _dich_copy_all(Eo_Class *dst, const Eo_Class *src)
138 if (!src->chain) return;
142 dst->chain = calloc(dst->class_id, sizeof(*dst->chain));
146 const Dich_Chain1 *sc1 = src->chain;
147 Dich_Chain1 *dc1 = dst->chain;
148 for (i = 0 ; i < src->class_id ; i++, sc1++, dc1++)
153 const Eo_Class *op_klass = ID_CLASS_GET(i + 1);
154 /* Can be NULL because of future static classes. */
158 size_t num_ops = op_klass->desc->ops.count;
159 _dich_chain_alloc(dc1, num_ops);
161 const op_type_funcs *sf = sc1->funcs;
162 op_type_funcs *df = dc1->funcs;
163 for (j = 0 ; j < num_ops ; j++, df++, sf++)
167 memcpy(df, sf, sizeof(*df));
174 static inline const op_type_funcs *
175 _dich_func_get(const Eo_Class *klass, Eo_Op op)
177 if (!klass->chain) return NULL;
179 size_t idx1 = DICH_CHAIN1(op) - 1;
180 if (idx1 >= klass->class_id) return NULL;
181 const Dich_Chain1 *chain1 = &klass->chain[idx1];
182 if (!chain1->funcs) return NULL;
184 size_t idxl = DICH_CHAIN_LAST(op);
185 /* num_ops is calculated from the class. */
186 const Eo_Class *op_klass = ID_CLASS_GET(idx1 + 1);
187 if (!op_klass || (idxl >= op_klass->desc->ops.count))
190 return &chain1->funcs[idxl];
194 _dich_func_set(Eo_Class *klass, Eo_Op op, eo_op_func_type func)
196 const Eo_Class *op_klass = OP_CLASS_GET(op);
199 /* Verify op is valid. */
202 /* num_ops is calculated from the class. */
203 num_ops = op_klass->desc->ops.count;
204 if (DICH_CHAIN_LAST(op) >= num_ops)
206 ERR("OP %x is too big for the domain '%s', expected value < %x.",
207 op, op_klass->desc->name, op_klass->desc->ops.count);
213 ERR("OP %x is from an illegal class.", op);
219 klass->chain = calloc(klass->class_id, sizeof(*klass->chain));
222 size_t idx1 = DICH_CHAIN1(op) - 1;
223 Dich_Chain1 *chain1 = &klass->chain[idx1];
224 _dich_chain_alloc(chain1, num_ops);
225 chain1->funcs[DICH_CHAIN_LAST(op)].func = func;
226 chain1->funcs[DICH_CHAIN_LAST(op)].src = klass;
230 _dich_func_clean_all(Eo_Class *klass)
233 Dich_Chain1 *chain1 = klass->chain;
238 for (i = 0 ; i < klass->class_id ; i++, chain1++)
249 static const Eo_Op_Description noop_desc =
250 EO_OP_DESCRIPTION(EO_NOOP, "", "No operation.");
252 static const Eo_Op_Description *
253 _eo_op_id_desc_get(Eo_Op op)
255 const Eo_Class *klass = OP_CLASS_GET(op);
256 Eo_Op sub_id = OP_SUB_ID_GET(op);
261 if (klass && (sub_id < klass->desc->ops.count))
262 return klass->desc->ops.descs + sub_id;
268 _eo_op_id_name_get(Eo_Op op)
270 const Eo_Op_Description *desc = _eo_op_id_desc_get(op);
271 return (desc) ? desc->name : NULL;
275 _eo_kls_itr_init(const Eo_Class *obj_klass, Eo_Kls_Itr *cur, Eo_Op op, Eo_Kls_Itr *prev_state)
277 prev_state->op = cur->op;
278 prev_state->kls_itr = cur->kls_itr;
280 /* If we are in a constructor/destructor or we changed an op - init. */
281 if ((op == EO_NOOP) || (cur->op != op))
284 cur->kls_itr = obj_klass->mro;
289 _eo_kls_itr_end(Eo_Kls_Itr *cur, Eo_Kls_Itr *prev_state)
291 if (cur->op != prev_state->op)
293 cur->op = prev_state->op;
294 cur->kls_itr = prev_state->kls_itr;
298 static inline const Eo_Class *
299 _eo_kls_itr_get(Eo_Kls_Itr *cur)
301 return (cur->kls_itr) ? *(cur->kls_itr) : NULL;
304 static inline const Eo_Class *
305 _eo_kls_itr_next(Eo_Kls_Itr *cur, Eo_Op op)
309 Eo_Op node_op = cur->op;
310 ERR("Called with op %d ('%s') while expecting: %d ('%s'). This probaly means you called eo_*_super functions from a wrong place.",
311 op, _eo_op_id_name_get(op),
312 node_op, _eo_op_id_name_get(node_op));
316 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))
331 cur->kls_itr = kls_itr;
340 static inline Eina_Bool
341 _eo_kls_itr_reached_end(const Eo_Kls_Itr *cur)
343 const Eo_Class **kls_itr = cur->kls_itr;
344 return !(*kls_itr && *(kls_itr + 1));
347 static inline const op_type_funcs *
348 _eo_kls_itr_func_get(const Eo_Class *klass, Eo_Kls_Itr *mro_itr, Eo_Op op, Eo_Kls_Itr *prev_state)
350 _eo_kls_itr_init(klass, mro_itr, op, prev_state);
351 klass = _eo_kls_itr_get(mro_itr);
354 const op_type_funcs *func = _dich_func_get(klass, op);
356 if (func && func->func)
365 #define _EO_OP_ERR_NO_OP_PRINT(op, klass) \
368 const Eo_Class *op_klass = OP_CLASS_GET(op); \
369 const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL; \
370 ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.", \
371 op, _eo_op_id_name_get(op), _dom_name, \
372 (klass) ? klass->desc->name : NULL); \
377 _eo_op_internal(Eo *obj, Eo_Op_Type op_type, Eo_Op op, va_list *p_list)
379 Eina_Bool ret = EINA_FALSE;
382 const Eo_Op_Description *op_desc = _eo_op_id_desc_get(op);
386 if (op_desc->op_type == EO_OP_TYPE_CLASS)
388 ERR("Tried calling a class op '%s' (%d) from a non-class context.", (op_desc) ? op_desc->name : NULL, op);
391 else if ((op_type == EO_OP_TYPE_CONST) &&
392 (op_desc->op_type != EO_OP_TYPE_CONST))
394 ERR("Tried calling non-const or non-existant op '%s' (%d) from a const (query) function.", (op_desc) ? op_desc->name : NULL, op);
400 Eo_Kls_Itr prev_state;
403 const op_type_funcs *func =
404 _eo_kls_itr_func_get(obj->klass, &obj->mro_itr, op, &prev_state);
407 func->func(obj, _eo_data_get(obj, func->src), p_list);
413 /* Try composite objects */
417 EINA_LIST_FOREACH(obj->composite_objects, itr, emb_obj)
419 if (_eo_op_internal(emb_obj, op_type, op, p_list))
428 _eo_kls_itr_end(&obj->mro_itr, &prev_state);
433 eo_do_internal(Eo *obj, Eo_Op_Type op_type, ...)
435 Eina_Bool ret = EINA_TRUE;
439 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
443 va_start(p_list, op_type);
445 op = va_arg(p_list, Eo_Op);
448 if (!_eo_op_internal(obj, op_type, op, &p_list))
450 _EO_OP_ERR_NO_OP_PRINT(op, obj->klass);
454 op = va_arg(p_list, Eo_Op);
464 eo_do_super_internal(Eo *obj, Eo_Op_Type op_type, Eo_Op op, ...)
466 const Eo_Class *nklass;
467 Eina_Bool ret = EINA_TRUE;
469 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
471 /* Advance the kls itr. */
472 nklass = _eo_kls_itr_next(&obj->mro_itr, op);
474 if (obj->mro_itr.op != op)
477 va_start(p_list, op);
478 if (!_eo_op_internal(obj, op_type, op, &p_list))
480 _EO_OP_ERR_NO_OP_PRINT(op, nklass);
489 _eo_class_op_internal(Eo_Class *klass, Eo_Op op, va_list *p_list)
491 Eina_Bool ret = EINA_FALSE;
494 const Eo_Op_Description *op_desc = _eo_op_id_desc_get(op);
498 if (op_desc->op_type != EO_OP_TYPE_CLASS)
500 ERR("Tried calling an instant op '%s' (%d) from a class context.", (op_desc) ? op_desc->name : NULL, op);
506 Eo_Kls_Itr prev_state;
509 const op_type_funcs *func =
510 _eo_kls_itr_func_get(klass, &klass->mro_itr, op, &prev_state);
513 ((eo_op_func_type_class) func->func)(klass, p_list);
520 _eo_kls_itr_end(&klass->mro_itr, &prev_state);
525 eo_class_do_internal(const Eo_Class *klass, ...)
527 Eina_Bool ret = EINA_TRUE;
531 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, EINA_FALSE);
533 va_start(p_list, klass);
535 op = va_arg(p_list, Eo_Op);
538 if (!_eo_class_op_internal((Eo_Class *) klass, op, &p_list))
540 _EO_OP_ERR_NO_OP_PRINT(op, klass);
544 op = va_arg(p_list, Eo_Op);
553 eo_class_do_super_internal(const Eo_Class *klass, Eo_Op op, ...)
555 const Eo_Class *nklass;
556 Eina_Bool ret = EINA_TRUE;
558 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, EINA_FALSE);
560 /* Advance the kls itr. */
561 nklass = _eo_kls_itr_next(&((Eo_Class *) klass)->mro_itr, op);
563 if (klass->mro_itr.op != op)
566 va_start(p_list, op);
567 if (!_eo_class_op_internal((Eo_Class *) klass, op, &p_list))
569 _EO_OP_ERR_NO_OP_PRINT(op, nklass);
577 EAPI const Eo_Class *
578 eo_class_get(const Eo *obj)
580 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
586 eo_class_name_get(const Eo_Class *klass)
588 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, NULL);
590 return klass->desc->name;
594 _eo_class_base_op_init(Eo_Class *klass)
596 const Eo_Class_Description *desc = klass->desc;
597 if (!desc || !desc->ops.base_op_id)
600 *(desc->ops.base_op_id) = EO_CLASS_ID_TO_BASE_ID(klass->class_id);
605 _eo_class_mro_has(const Eo_Class *klass, const Eo_Class *find)
607 const Eo_Class **itr;
608 for (itr = klass->mro ; *itr ; itr++)
621 _eo_class_mro_add(Eina_List *mro, const Eo_Class *klass)
623 Eina_List *extn_pos = NULL;
624 Eina_Bool check_consistency = !mro;
628 mro = eina_list_append(mro, klass);
630 /* Recursively add extenions. */
632 Eo_Extension_Node *extn;
633 EINA_INLIST_FOREACH(klass->extensions, extn)
635 mro = _eo_class_mro_add(mro, extn->klass);
636 /* Not possible: if (!mro) return NULL; */
638 if (check_consistency)
640 extn_pos = eina_list_append(extn_pos, eina_list_last(mro));
645 /* Check if we can create a consistent mro. We only do it for the class
646 * we are working on (i.e no parents). */
647 if (check_consistency)
649 Eo_Extension_Node *extn;
651 Eina_List *itr = extn_pos;
652 EINA_INLIST_FOREACH(klass->extensions, extn)
654 /* Get the first one after the extension. */
655 Eina_List *extn_list = eina_list_next(eina_list_data_get(itr));
657 /* If we found the extension again. */
658 if (eina_list_data_find_list(extn_list, extn->klass))
661 ERR("Cannot create a consistent method resolution order for class '%s' because of '%s'.", klass->desc->name, extn->klass->desc->name);
665 itr = eina_list_next(itr);
670 mro = _eo_class_mro_add(mro, klass->parent);
676 _eo_class_mro_init(Eo_Class *klass)
678 Eina_List *mro = NULL;
680 DBG("Started creating MRO for class '%s'", klass->desc->name);
681 mro = _eo_class_mro_add(mro, klass);
686 /* Remove duplicates and make them the right order. */
688 Eina_List *itr1, *itr2, *itr2n;
690 itr1 = eina_list_last(mro);
693 itr2 = eina_list_prev(itr1);
697 itr2n = eina_list_prev(itr2);
699 if (eina_list_data_get(itr1) == eina_list_data_get(itr2))
701 mro = eina_list_remove_list(mro, itr2);
707 itr1 = eina_list_prev(itr1);
711 /* Copy the mro and free the list. */
713 const Eo_Class *kls_itr;
714 const Eo_Class **mro_itr;
715 klass->mro = calloc(sizeof(*klass->mro), eina_list_count(mro) + 1);
717 mro_itr = klass->mro;
719 EINA_LIST_FREE(mro, kls_itr)
721 *(mro_itr++) = kls_itr;
723 DBG("Added '%s' to MRO", kls_itr->desc->name);
728 DBG("Finished creating MRO for class '%s'", klass->desc->name);
734 _eo_class_constructor(Eo_Class *klass)
736 if (klass->constructed)
739 klass->constructed = EINA_TRUE;
741 if (klass->desc->class_constructor)
742 klass->desc->class_constructor(klass);
746 eo_class_funcs_set(Eo_Class *klass, const Eo_Op_Func_Description *func_descs)
748 EO_MAGIC_RETURN(klass, EO_CLASS_EINA_MAGIC);
750 const Eo_Op_Func_Description *itr;
754 for ( ; itr->op != 0 ; itr++)
756 const Eo_Op_Description *op_desc = _eo_op_id_desc_get(itr->op);
758 if (EINA_LIKELY(!op_desc || (itr->op_type == op_desc->op_type)))
760 _dich_func_set(klass, itr->op, itr->func);
764 ERR("Set function's op type (%d) is different than the one in the op description (%d) for op '%s' in class '%s'.", itr->op_type, op_desc->op_type, op_desc->name, klass->desc->name);
771 eo_class_free(Eo_Class *klass)
773 if (klass->constructed)
775 if (klass->desc->class_destructor)
776 klass->desc->class_destructor(klass);
778 _dich_func_clean_all(klass);
783 Eo_Extension_Node *extn = NULL;
784 EINA_INLIST_FOREACH_SAFE(klass->extensions, itrn, extn)
793 if (klass->extn_data_off)
794 free(klass->extn_data_off);
801 _eo_class_check_op_descs(const Eo_Class *klass, Eo_Class_Id id)
803 const Eo_Class_Description *desc = klass->desc;
804 const Eo_Op_Description *itr;
807 if (desc->ops.count > 0)
809 if (((id == 0) || (id > EO_STATIC_IDS_LAST)) && !desc->ops.base_op_id)
811 ERR("Class '%s' has a non-zero ops count, but base_id is NULL.",
816 if (!desc->ops.descs)
818 ERR("Class '%s' has a non-zero ops count, but there are no descs.",
824 itr = desc->ops.descs;
825 for (i = 0 ; i < desc->ops.count ; i++, itr++)
827 if (itr->sub_op != i)
831 ERR("Wrong order in Ops description for class '%s'. Expected %d and got %d", desc->name, i, itr->sub_op);
835 ERR("Found too few Ops description for class '%s'. Expected %d descriptions, but found %d.", desc->name, desc->ops.count, i);
841 if (itr && itr->name)
843 ERR("Found extra Ops description for class '%s'. Expected %d descriptions, but found more.", desc->name, desc->ops.count);
850 EAPI const Eo_Class *
851 eo_class_new(const Eo_Class_Description *desc, Eo_Class_Id id, const Eo_Class *parent, ...)
856 if (parent && !EINA_MAGIC_CHECK(parent, EO_CLASS_EINA_MAGIC))
858 EINA_MAGIC_FAIL(parent, EO_CLASS_EINA_MAGIC);
862 if (id > EO_STATIC_IDS_LAST)
864 ERR("Tried creating a class with the static id %d while the maximum static id is %d. Aborting.", id, EO_STATIC_IDS_LAST);
868 va_start(p_list, parent);
870 EINA_SAFETY_ON_NULL_RETURN_VAL(desc, NULL);
871 EINA_SAFETY_ON_NULL_RETURN_VAL(desc->name, NULL);
873 /* Check restrictions on Interface types. */
874 if (desc->type == EO_CLASS_TYPE_INTERFACE)
876 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->constructor, NULL);
877 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->destructor, NULL);
878 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->class_constructor, NULL);
879 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->class_destructor, NULL);
880 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->data_size, NULL);
883 klass = calloc(1, sizeof(Eo_Class));
884 klass->parent = parent;
886 /* Handle class extensions */
888 Eo_Class *extn = NULL;
890 extn = va_arg(p_list, Eo_Class *);
893 switch (extn->desc->type)
895 case EO_CLASS_TYPE_REGULAR:
896 case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
897 /* Use it like an interface. */
898 case EO_CLASS_TYPE_INTERFACE:
900 case EO_CLASS_TYPE_MIXIN:
902 Eo_Extension_Node *node = calloc(1, sizeof(*node));
905 eina_inlist_append(klass->extensions,
906 EINA_INLIST_GET(node));
911 extn = va_arg(p_list, Eo_Class *);
917 /* Handle the inheritance */
920 /* Verify the inheritance is allowed. */
921 switch (klass->desc->type)
923 case EO_CLASS_TYPE_REGULAR:
924 case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
925 if ((klass->parent->desc->type != EO_CLASS_TYPE_REGULAR) &&
926 (klass->parent->desc->type != EO_CLASS_TYPE_REGULAR_NO_INSTANT))
928 ERR("Regular classes ('%s') aren't allowed to inherit from non-regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
932 case EO_CLASS_TYPE_INTERFACE:
933 case EO_CLASS_TYPE_MIXIN:
934 if ((klass->parent->desc->type != EO_CLASS_TYPE_INTERFACE) &&
935 (klass->parent->desc->type != EO_CLASS_TYPE_MIXIN))
937 ERR("Non-regular classes ('%s') aren't allowed to inherit from regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
944 /* Update the current offset. */
945 /* FIXME: Make sure this alignment is enough. */
946 klass->data_offset = klass->parent->data_offset +
947 EO_ALIGN_SIZE(klass->parent->desc->data_size);
950 if (!_eo_class_check_op_descs(klass, id))
955 if (!_eo_class_mro_init(klass))
960 /* create MIXIN offset table. */
962 const Eo_Class **mro_itr = klass->mro;
963 Eo_Extension_Data_Offset *extn_data_itr;
965 size_t extn_data_off = klass->data_offset +
966 EO_ALIGN_SIZE(klass->desc->data_size);
968 /* FIXME: Make faster... */
971 if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
972 ((*mro_itr)->desc->data_size > 0))
979 klass->extn_data_off = calloc(extn_num + 1,
980 sizeof(*klass->extn_data_off));
982 extn_data_itr = klass->extn_data_off;
983 mro_itr = klass->mro;
986 if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
987 ((*mro_itr)->desc->data_size > 0))
989 extn_data_itr->klass = *mro_itr;
990 extn_data_itr->offset = extn_data_off;
992 extn_data_off += EO_ALIGN_SIZE(extn_data_itr->klass->desc->data_size);
998 klass->extn_data_size = extn_data_off;
1001 eina_lock_take(&_eo_class_creation_lock);
1005 klass->class_id = ++_eo_classes_last_id;
1010 if (_eo_classes && _eo_classes[id - 1])
1012 ERR("A class with id %d was already defined (%s). Aborting.", id,
1013 _eo_classes[id - 1]->desc->name);
1014 eina_lock_release(&_eo_class_creation_lock);
1018 klass->class_id = id;
1023 /* FIXME: Handle errors. */
1024 size_t arrsize = _eo_classes_last_id * sizeof(*_eo_classes);
1026 tmp = realloc(_eo_classes, arrsize);
1028 /* If it's the first allocation, memset. */
1030 memset(tmp, 0, arrsize);
1033 _eo_classes[klass->class_id - 1] = klass;
1035 eina_lock_release(&_eo_class_creation_lock);
1037 EINA_MAGIC_SET(klass, EO_CLASS_EINA_MAGIC);
1039 /* Flatten the function array */
1041 const Eo_Class **mro_itr = klass->mro;
1042 for ( ; *mro_itr ; mro_itr++)
1045 /* Skip ourselves. */
1046 for ( mro_itr-- ; mro_itr > klass->mro ; mro_itr--)
1048 _dich_copy_all(klass, *mro_itr);
1052 _eo_class_base_op_init(klass);
1054 _eo_class_constructor(klass);
1061 eo_class_free(klass);
1066 eo_parent_set(Eo *obj, const Eo *parent)
1068 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1070 EO_MAGIC_RETURN_VAL(parent, EO_EINA_MAGIC, EINA_FALSE);
1072 if (obj->parent == parent)
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);
1125 _eo_constructor(obj, klass);
1127 if (EINA_UNLIKELY(_eo_error_get(obj)))
1129 ERR("Type '%s' - One of the object constructors have failed.", klass->desc->name);
1133 if (EINA_UNLIKELY(!_eo_kls_itr_reached_end(&obj->mro_itr)))
1135 ERR("Type '%s' - Not all of the object constructors have been executed.", klass->desc->name);
1138 _eo_kls_itr_end(&obj->mro_itr, &prev_state);
1144 _eo_kls_itr_end(&obj->mro_itr, &prev_state);
1145 /* Unref twice, once for the ref above, and once for the basic object ref. */
1160 eo_xref_internal(Eo *obj, const Eo *ref_obj, const char *file, int line)
1162 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, obj);
1167 Eo_Xref_Node *xref = calloc(1, sizeof(*xref));
1168 xref->ref_obj = ref_obj;
1172 obj->xrefs = eina_inlist_prepend(obj->xrefs, EINA_INLIST_GET(xref));
1183 eo_xunref(Eo *obj, const Eo *ref_obj)
1185 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1187 Eo_Xref_Node *xref = NULL;
1188 EINA_INLIST_FOREACH(obj->xrefs, xref)
1190 if (xref->ref_obj == ref_obj)
1196 obj->xrefs = eina_inlist_remove(obj->xrefs, EINA_INLIST_GET(xref));
1201 ERR("ref_obj (%p) does not reference obj (%p). Aborting unref.", ref_obj, obj);
1218 eo_ref(const Eo *_obj)
1220 Eo *obj = (Eo *) _obj;
1221 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, obj);
1223 return _eo_ref(obj);
1227 _eo_del_internal(Eo *obj)
1229 /* We need that for the event callbacks that may ref/unref. */
1232 eo_do(obj, eo_event_callback_call(EO_EV_DEL, NULL, NULL));
1234 const Eo_Class *klass = eo_class_get(obj);
1235 Eo_Kls_Itr prev_state;
1237 _eo_kls_itr_init(klass, &obj->mro_itr, EO_NOOP, &prev_state);
1238 _eo_error_unset(obj);
1239 _eo_destructor(obj, klass);
1240 if (_eo_error_get(obj))
1242 ERR("Type '%s' - One of the object destructors have failed.", klass->desc->name);
1245 if (!_eo_kls_itr_reached_end(&obj->mro_itr))
1247 ERR("Type '%s' - Not all of the object destructors have been executed.", klass->desc->name);
1249 _eo_kls_itr_end(&obj->mro_itr, &prev_state);
1250 /*FIXME: add eo_class_unref(klass) ? - just to clear the caches. */
1253 Eina_List *itr, *itr_n;
1255 EINA_LIST_FOREACH_SAFE(obj->composite_objects, itr, itr_n, emb_obj)
1257 eo_composite_object_detach(obj, emb_obj);
1261 eo_parent_set(obj, NULL);
1262 while (obj->children)
1264 eo_parent_set(EINA_INLIST_CONTAINER_GET(obj->children, Eo), NULL);
1267 obj->del = EINA_TRUE;
1274 EINA_MAGIC_SET(obj, EO_FREED_EINA_MAGIC);
1281 if (--(obj->refcount) == 0)
1283 _eo_del_internal(obj);
1286 /* If for some reason it's not empty, clear it. */
1289 WRN("obj->xrefs is not empty, possibly a bug, please report. - An error will be reported for each xref in the stack.");
1290 Eina_Inlist *nitr = obj->xrefs->next;
1291 free(EINA_INLIST_CONTAINER_GET(obj->xrefs, Eo_Xref_Node));
1296 if (!obj->manual_free)
1299 _eo_ref(obj); /* If we manual free, we keep a phantom ref. */
1304 eo_unref(const Eo *_obj)
1306 Eo *obj = (Eo *) _obj;
1307 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1313 eo_del(const Eo *obj)
1315 eo_parent_set((Eo *) obj, NULL);
1320 eo_ref_get(const Eo *obj)
1322 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, 0);
1324 return obj->refcount;
1328 eo_parent_get(const Eo *obj)
1330 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1336 eo_error_set_internal(const Eo *obj, const char *file, int line)
1338 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1340 ERR("Error with obj '%p' at %s:%d", obj, file, line);
1342 ((Eo *) obj)->construct_error = EINA_TRUE;
1346 _eo_error_unset(Eo *obj)
1348 obj->construct_error = EINA_FALSE;
1353 * @brief Check if there was an error when constructing, destructing or calling a function of the object.
1354 * @param obj the object to work on.
1355 * @return @c EINA_TRUE if there was an error.
1357 static inline Eina_Bool
1358 _eo_error_get(const Eo *obj)
1360 return obj->construct_error;
1364 _eo_constructor_default(Eo *obj)
1366 eo_constructor_super(obj);
1370 _eo_destructor_default(Eo *obj)
1372 eo_destructor_super(obj);
1376 _eo_constructor(Eo *obj, const Eo_Class *klass)
1381 if (klass->desc->constructor)
1382 klass->desc->constructor(obj, _eo_data_get(obj, klass));
1384 _eo_constructor_default(obj);
1388 _eo_destructor(Eo *obj, const Eo_Class *klass)
1393 if (klass->desc->destructor)
1394 klass->desc->destructor(obj, _eo_data_get(obj, klass));
1396 _eo_destructor_default(obj);
1400 eo_constructor_super(Eo *obj)
1402 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1404 _eo_constructor(obj, _eo_kls_itr_next(&obj->mro_itr, EO_NOOP));
1408 eo_destructor_super(Eo *obj)
1410 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1412 _eo_destructor(obj, _eo_kls_itr_next(&obj->mro_itr, EO_NOOP));
1415 static inline void *
1416 _eo_data_get(const Eo *obj, const Eo_Class *klass)
1418 if (EINA_LIKELY(klass->desc->data_size > 0))
1420 if (EINA_UNLIKELY(klass->desc->type == EO_CLASS_TYPE_MIXIN))
1422 Eo_Extension_Data_Offset *doff_itr =
1423 eo_class_get(obj)->extn_data_off;
1428 while (doff_itr->klass)
1430 if (doff_itr->klass == klass)
1431 return ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj)) +
1438 return ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj)) +
1447 eo_data_get(const Eo *obj, const Eo_Class *klass)
1450 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1453 if (!_eo_class_mro_has(obj->klass, klass))
1455 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);
1460 ret = _eo_data_get(obj, klass);
1463 if (!ret && (klass->desc->data_size == 0))
1465 ERR("Tried getting data of class '%s', but it has none..", klass->desc->name);
1475 const char *log_dom = "eo";
1476 if (_eo_init_count++ > 0)
1482 _eo_classes_last_id = EO_STATIC_IDS_LAST;
1483 _eo_log_dom = eina_log_domain_register(log_dom, EINA_COLOR_LIGHTBLUE);
1484 if (_eo_log_dom < 0)
1486 EINA_LOG_ERR("Could not register log domain: %s", log_dom);
1490 if (!eina_lock_new(&_eo_class_creation_lock))
1492 EINA_LOG_ERR("Could not init lock.");
1496 eina_magic_string_static_set(EO_EINA_MAGIC, EO_EINA_MAGIC_STR);
1497 eina_magic_string_static_set(EO_FREED_EINA_MAGIC,
1498 EO_FREED_EINA_MAGIC_STR);
1499 eina_magic_string_static_set(EO_CLASS_EINA_MAGIC,
1500 EO_CLASS_EINA_MAGIC_STR);
1509 Eo_Class **cls_itr = _eo_classes;
1511 if (--_eo_init_count > 0)
1514 for (i = 0 ; i < _eo_classes_last_id ; i++, cls_itr++)
1517 eo_class_free(*cls_itr);
1523 eina_lock_free(&_eo_class_creation_lock);
1525 eina_log_domain_unregister(_eo_log_dom);
1533 eo_composite_object_attach(Eo *obj, Eo *emb_obj)
1535 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1536 EO_MAGIC_RETURN(emb_obj, EO_EINA_MAGIC);
1538 eo_xref(emb_obj, obj);
1539 obj->composite_objects = eina_list_prepend(obj->composite_objects, emb_obj);
1543 eo_composite_object_detach(Eo *obj, Eo *emb_obj)
1545 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1546 EO_MAGIC_RETURN(emb_obj, EO_EINA_MAGIC);
1548 obj->composite_objects = eina_list_remove(obj->composite_objects, emb_obj);
1549 eo_xunref(emb_obj, obj);
1553 eo_composite_is(const Eo *emb_obj)
1555 if (!EINA_MAGIC_CHECK(emb_obj, EO_EINA_MAGIC))
1557 EINA_MAGIC_FAIL(emb_obj, EO_EINA_MAGIC);
1561 Eo *obj = eo_parent_get(emb_obj);
1568 EINA_LIST_FOREACH(obj->composite_objects, itr, tmp)
1578 eo_manual_free_set(Eo *obj, Eina_Bool manual_free)
1580 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1581 obj->manual_free = manual_free;
1585 eo_manual_free(Eo *obj)
1587 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1589 if (EINA_FALSE == obj->manual_free)
1591 ERR("Tried to manually free the object %p while the option has not been set; see eo_manual_free_set for more information.", obj);
1597 ERR("Tried deleting the object %p while still referenced(%d).", obj, eo_ref_get(obj));