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;
48 Eina_Bool composite:1;
50 Eina_Bool construct_error:1;
51 Eina_Bool manual_free:1;
55 /* Dich search, split to 0xff 0xff 0xffff */
57 #define DICH_CHAIN1_MASK (0xffff)
58 #define DICH_CHAIN_LAST_MASK (0xffff)
59 #define DICH_CHAIN1(x) (((x) >> 16) & DICH_CHAIN1_MASK)
60 #define DICH_CHAIN_LAST(x) ((x) & DICH_CHAIN_LAST_MASK)
62 #define OP_CLASS_OFFSET_GET(x) (((x) >> EO_OP_CLASS_OFFSET) & 0xffff)
63 #define OP_CLASS_GET(op) ({ \
64 Eo_Class_Id tmp = OP_CLASS_OFFSET_GET(op); \
67 #define OP_SUB_ID_GET(op) ((op) & 0xffff)
69 #define ID_CLASS_GET(id) ({ \
70 (Eo_Class *) ((id <= _eo_classes_last_id) && (id > 0)) ? \
71 (_eo_classes[id - 1]) : NULL; \
74 #define EO_ALIGN_SIZE(size) \
75 ((size) + (sizeof(void *) - ((size) % sizeof(void *))))
77 /* Structure of Eo_Op is:
82 typedef struct _Dich_Chain1 Dich_Chain1;
98 const Eo_Class *klass;
103 const Eo_Class *klass;
105 } Eo_Extension_Data_Offset;
110 Eo_Class_Id class_id;
111 const Eo_Class *parent;
112 const Eo_Class_Description *desc;
113 Dich_Chain1 *chain; /**< The size is class_id */
114 Eina_Inlist *extensions;
116 Eo_Extension_Data_Offset *extn_data_off;
117 size_t extn_data_size;
119 const Eo_Class **mro;
122 size_t data_offset; /* < Offset of the data within object data. */
124 Eina_Bool constructed : 1;
128 _dich_chain_alloc(Dich_Chain1 *chain1, size_t num_ops)
132 chain1->funcs = calloc(num_ops, sizeof(*(chain1->funcs)));
137 _dich_copy_all(Eo_Class *dst, const Eo_Class *src)
139 if (!src->chain) return;
143 dst->chain = calloc(dst->class_id, sizeof(*dst->chain));
147 const Dich_Chain1 *sc1 = src->chain;
148 Dich_Chain1 *dc1 = dst->chain;
149 for (i = 0 ; i < src->class_id ; i++, sc1++, dc1++)
154 const Eo_Class *op_klass = ID_CLASS_GET(i + 1);
155 /* Can be NULL because of future static classes. */
159 size_t num_ops = op_klass->desc->ops.count;
160 _dich_chain_alloc(dc1, num_ops);
162 const op_type_funcs *sf = sc1->funcs;
163 op_type_funcs *df = dc1->funcs;
164 for (j = 0 ; j < num_ops ; j++, df++, sf++)
168 memcpy(df, sf, sizeof(*df));
175 static inline const op_type_funcs *
176 _dich_func_get(const Eo_Class *klass, Eo_Op op)
178 if (!klass->chain) return NULL;
180 size_t idx1 = DICH_CHAIN1(op) - 1;
181 if (idx1 >= klass->class_id) return NULL;
182 const Dich_Chain1 *chain1 = &klass->chain[idx1];
183 if (!chain1->funcs) return NULL;
185 size_t idxl = DICH_CHAIN_LAST(op);
186 /* num_ops is calculated from the class. */
187 const Eo_Class *op_klass = ID_CLASS_GET(idx1 + 1);
188 if (!op_klass || (idxl >= op_klass->desc->ops.count))
191 return &chain1->funcs[idxl];
195 _dich_func_set(Eo_Class *klass, Eo_Op op, eo_op_func_type func)
197 const Eo_Class *op_klass = OP_CLASS_GET(op);
200 /* Verify op is valid. */
203 /* num_ops is calculated from the class. */
204 num_ops = op_klass->desc->ops.count;
205 if (DICH_CHAIN_LAST(op) >= num_ops)
207 ERR("OP %x is too big for the domain '%s', expected value < %x.",
208 op, op_klass->desc->name, op_klass->desc->ops.count);
214 ERR("OP %x is from an illegal class.", op);
220 klass->chain = calloc(klass->class_id, sizeof(*klass->chain));
223 size_t idx1 = DICH_CHAIN1(op) - 1;
224 Dich_Chain1 *chain1 = &klass->chain[idx1];
225 _dich_chain_alloc(chain1, num_ops);
226 chain1->funcs[DICH_CHAIN_LAST(op)].func = func;
227 chain1->funcs[DICH_CHAIN_LAST(op)].src = klass;
231 _dich_func_clean_all(Eo_Class *klass)
234 Dich_Chain1 *chain1 = klass->chain;
239 for (i = 0 ; i < klass->class_id ; i++, chain1++)
250 static const Eo_Op_Description noop_desc =
251 EO_OP_DESCRIPTION(EO_NOOP, "", "No operation.");
253 static const Eo_Op_Description *
254 _eo_op_id_desc_get(Eo_Op op)
256 const Eo_Class *klass = OP_CLASS_GET(op);
257 Eo_Op sub_id = OP_SUB_ID_GET(op);
262 if (klass && (sub_id < klass->desc->ops.count))
263 return klass->desc->ops.descs + sub_id;
269 _eo_op_id_name_get(Eo_Op op)
271 const Eo_Op_Description *desc = _eo_op_id_desc_get(op);
272 return (desc) ? desc->name : NULL;
276 _eo_kls_itr_init(const Eo_Class *obj_klass, Eo_Kls_Itr *cur, Eo_Op op, Eo_Kls_Itr *prev_state)
278 prev_state->op = cur->op;
279 prev_state->kls_itr = cur->kls_itr;
281 /* If we are in a constructor/destructor or we changed an op - init. */
282 if ((op == EO_NOOP) || (cur->op != op))
285 cur->kls_itr = obj_klass->mro;
290 _eo_kls_itr_end(Eo_Kls_Itr *cur, Eo_Kls_Itr *prev_state)
292 if (cur->op != prev_state->op)
294 cur->op = prev_state->op;
295 cur->kls_itr = prev_state->kls_itr;
299 static inline const Eo_Class *
300 _eo_kls_itr_get(Eo_Kls_Itr *cur)
302 return (cur->kls_itr) ? *(cur->kls_itr) : NULL;
305 static inline const Eo_Class *
306 _eo_kls_itr_next(Eo_Kls_Itr *cur, Eo_Op op)
310 Eo_Op node_op = cur->op;
311 ERR("Called with op %d ('%s') while expecting: %d ('%s'). This probaly means you called eo_*_super functions from a wrong place.",
312 op, _eo_op_id_name_get(op),
313 node_op, _eo_op_id_name_get(node_op));
317 const Eo_Class **kls_itr = cur->kls_itr;
322 const op_type_funcs *fsrc = _dich_func_get(*kls_itr, op);
324 while (*kls_itr && (*(kls_itr++) != fsrc->src))
332 cur->kls_itr = kls_itr;
341 static inline Eina_Bool
342 _eo_kls_itr_reached_end(const Eo_Kls_Itr *cur)
344 const Eo_Class **kls_itr = cur->kls_itr;
345 return !(*kls_itr && *(kls_itr + 1));
348 static inline const op_type_funcs *
349 _eo_kls_itr_func_get(const Eo_Class *klass, Eo_Kls_Itr *mro_itr, Eo_Op op, Eo_Kls_Itr *prev_state)
351 _eo_kls_itr_init(klass, mro_itr, op, prev_state);
352 klass = _eo_kls_itr_get(mro_itr);
355 const op_type_funcs *func = _dich_func_get(klass, op);
357 if (func && func->func)
366 #define _EO_OP_ERR_NO_OP_PRINT(op, klass) \
369 const Eo_Class *op_klass = OP_CLASS_GET(op); \
370 const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL; \
371 ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.", \
372 op, _eo_op_id_name_get(op), _dom_name, \
373 (klass) ? klass->desc->name : NULL); \
378 _eo_op_internal(Eo *obj, Eo_Op_Type op_type, Eo_Op op, va_list *p_list)
380 Eina_Bool ret = EINA_FALSE;
383 const Eo_Op_Description *op_desc = _eo_op_id_desc_get(op);
387 if (op_desc->op_type == EO_OP_TYPE_CLASS)
389 ERR("Tried calling a class op '%s' (%d) from a non-class context.", (op_desc) ? op_desc->name : NULL, op);
392 else if ((op_type == EO_OP_TYPE_CONST) &&
393 (op_desc->op_type != EO_OP_TYPE_CONST))
395 ERR("Tried calling non-const or non-existant op '%s' (%d) from a const (query) function.", (op_desc) ? op_desc->name : NULL, op);
401 Eo_Kls_Itr prev_state;
404 const op_type_funcs *func =
405 _eo_kls_itr_func_get(obj->klass, &obj->mro_itr, op, &prev_state);
408 func->func(obj, _eo_data_get(obj, func->src), p_list);
414 /* Try composite objects */
418 EINA_LIST_FOREACH(obj->composite_objects, itr, emb_obj)
420 if (_eo_op_internal(emb_obj, op_type, op, p_list))
429 _eo_kls_itr_end(&obj->mro_itr, &prev_state);
434 eo_do_internal(Eo *obj, Eo_Op_Type op_type, ...)
436 Eina_Bool ret = EINA_TRUE;
440 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
444 va_start(p_list, op_type);
446 op = va_arg(p_list, Eo_Op);
449 if (!_eo_op_internal(obj, op_type, op, &p_list))
451 _EO_OP_ERR_NO_OP_PRINT(op, obj->klass);
455 op = va_arg(p_list, Eo_Op);
465 eo_do_super_internal(Eo *obj, Eo_Op_Type op_type, Eo_Op op, ...)
467 const Eo_Class *nklass;
468 Eina_Bool ret = EINA_TRUE;
470 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
472 /* Advance the kls itr. */
473 nklass = _eo_kls_itr_next(&obj->mro_itr, op);
475 if (obj->mro_itr.op != op)
478 va_start(p_list, op);
479 if (!_eo_op_internal(obj, op_type, op, &p_list))
481 _EO_OP_ERR_NO_OP_PRINT(op, nklass);
490 _eo_class_op_internal(Eo_Class *klass, Eo_Op op, va_list *p_list)
492 Eina_Bool ret = EINA_FALSE;
495 const Eo_Op_Description *op_desc = _eo_op_id_desc_get(op);
499 if (op_desc->op_type != EO_OP_TYPE_CLASS)
501 ERR("Tried calling an instant op '%s' (%d) from a class context.", (op_desc) ? op_desc->name : NULL, op);
507 Eo_Kls_Itr prev_state;
510 const op_type_funcs *func =
511 _eo_kls_itr_func_get(klass, &klass->mro_itr, op, &prev_state);
514 ((eo_op_func_type_class) func->func)(klass, p_list);
521 _eo_kls_itr_end(&klass->mro_itr, &prev_state);
526 eo_class_do_internal(const Eo_Class *klass, ...)
528 Eina_Bool ret = EINA_TRUE;
532 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, EINA_FALSE);
534 va_start(p_list, klass);
536 op = va_arg(p_list, Eo_Op);
539 if (!_eo_class_op_internal((Eo_Class *) klass, op, &p_list))
541 _EO_OP_ERR_NO_OP_PRINT(op, klass);
545 op = va_arg(p_list, Eo_Op);
554 eo_class_do_super_internal(const Eo_Class *klass, Eo_Op op, ...)
556 const Eo_Class *nklass;
557 Eina_Bool ret = EINA_TRUE;
559 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, EINA_FALSE);
561 /* Advance the kls itr. */
562 nklass = _eo_kls_itr_next(&((Eo_Class *) klass)->mro_itr, op);
564 if (klass->mro_itr.op != op)
567 va_start(p_list, op);
568 if (!_eo_class_op_internal((Eo_Class *) klass, op, &p_list))
570 _EO_OP_ERR_NO_OP_PRINT(op, nklass);
578 EAPI const Eo_Class *
579 eo_class_get(const Eo *obj)
581 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
587 eo_class_name_get(const Eo_Class *klass)
589 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, NULL);
591 return klass->desc->name;
595 _eo_class_base_op_init(Eo_Class *klass)
597 const Eo_Class_Description *desc = klass->desc;
598 if (!desc || !desc->ops.base_op_id)
601 *(desc->ops.base_op_id) = EO_CLASS_ID_TO_BASE_ID(klass->class_id);
606 _eo_class_mro_has(const Eo_Class *klass, const Eo_Class *find)
608 const Eo_Class **itr;
609 for (itr = klass->mro ; *itr ; itr++)
622 _eo_class_mro_add(Eina_List *mro, const Eo_Class *klass)
624 Eina_List *extn_pos = NULL;
625 Eina_Bool check_consistency = !mro;
629 mro = eina_list_append(mro, klass);
631 /* Recursively add extenions. */
633 Eo_Extension_Node *extn;
634 EINA_INLIST_FOREACH(klass->extensions, extn)
636 mro = _eo_class_mro_add(mro, extn->klass);
637 /* Not possible: if (!mro) return NULL; */
639 if (check_consistency)
641 extn_pos = eina_list_append(extn_pos, eina_list_last(mro));
646 /* Check if we can create a consistent mro. We only do it for the class
647 * we are working on (i.e no parents). */
648 if (check_consistency)
650 Eo_Extension_Node *extn;
652 Eina_List *itr = extn_pos;
653 EINA_INLIST_FOREACH(klass->extensions, extn)
655 /* Get the first one after the extension. */
656 Eina_List *extn_list = eina_list_next(eina_list_data_get(itr));
658 /* If we found the extension again. */
659 if (eina_list_data_find_list(extn_list, extn->klass))
662 ERR("Cannot create a consistent method resolution order for class '%s' because of '%s'.", klass->desc->name, extn->klass->desc->name);
666 itr = eina_list_next(itr);
671 mro = _eo_class_mro_add(mro, klass->parent);
677 _eo_class_mro_init(Eo_Class *klass)
679 Eina_List *mro = NULL;
681 DBG("Started creating MRO for class '%s'", klass->desc->name);
682 mro = _eo_class_mro_add(mro, klass);
687 /* Remove duplicates and make them the right order. */
689 Eina_List *itr1, *itr2, *itr2n;
691 itr1 = eina_list_last(mro);
694 itr2 = eina_list_prev(itr1);
698 itr2n = eina_list_prev(itr2);
700 if (eina_list_data_get(itr1) == eina_list_data_get(itr2))
702 mro = eina_list_remove_list(mro, itr2);
708 itr1 = eina_list_prev(itr1);
712 /* Copy the mro and free the list. */
714 const Eo_Class *kls_itr;
715 const Eo_Class **mro_itr;
716 klass->mro = calloc(sizeof(*klass->mro), eina_list_count(mro) + 1);
718 mro_itr = klass->mro;
720 EINA_LIST_FREE(mro, kls_itr)
722 *(mro_itr++) = kls_itr;
724 DBG("Added '%s' to MRO", kls_itr->desc->name);
729 DBG("Finished creating MRO for class '%s'", klass->desc->name);
735 _eo_class_constructor(Eo_Class *klass)
737 if (klass->constructed)
740 klass->constructed = EINA_TRUE;
742 if (klass->desc->class_constructor)
743 klass->desc->class_constructor(klass);
747 eo_class_funcs_set(Eo_Class *klass, const Eo_Op_Func_Description *func_descs)
749 EO_MAGIC_RETURN(klass, EO_CLASS_EINA_MAGIC);
751 const Eo_Op_Func_Description *itr;
755 for ( ; itr->op_type != EO_OP_TYPE_INVALID ; itr++)
757 const Eo_Op_Description *op_desc = _eo_op_id_desc_get(itr->op);
759 if (EINA_LIKELY(op_desc && (itr->op_type == op_desc->op_type)))
761 _dich_func_set(klass, itr->op, itr->func);
765 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,
766 (op_desc) ? op_desc->op_type : EO_OP_TYPE_REGULAR,
767 (op_desc) ? op_desc->name : NULL,
775 eo_class_free(Eo_Class *klass)
777 if (klass->constructed)
779 if (klass->desc->class_destructor)
780 klass->desc->class_destructor(klass);
782 _dich_func_clean_all(klass);
787 Eo_Extension_Node *extn = NULL;
788 EINA_INLIST_FOREACH_SAFE(klass->extensions, itrn, extn)
797 if (klass->extn_data_off)
798 free(klass->extn_data_off);
805 _eo_class_check_op_descs(const Eo_Class *klass, Eo_Class_Id id)
807 const Eo_Class_Description *desc = klass->desc;
808 const Eo_Op_Description *itr;
811 if (desc->ops.count > 0)
813 if (((id == 0) || (id > EO_STATIC_IDS_LAST)) && !desc->ops.base_op_id)
815 ERR("Class '%s' has a non-zero ops count, but base_id is NULL.",
820 if (!desc->ops.descs)
822 ERR("Class '%s' has a non-zero ops count, but there are no descs.",
828 itr = desc->ops.descs;
829 for (i = 0 ; i < desc->ops.count ; i++, itr++)
831 if (itr->sub_op != i)
835 ERR("Wrong order in Ops description for class '%s'. Expected %d and got %d", desc->name, i, itr->sub_op);
839 ERR("Found too few Ops description for class '%s'. Expected %d descriptions, but found %d.", desc->name, desc->ops.count, i);
845 if (itr && itr->name)
847 ERR("Found extra Ops description for class '%s'. Expected %d descriptions, but found more.", desc->name, desc->ops.count);
854 EAPI const Eo_Class *
855 eo_class_new(const Eo_Class_Description *desc, Eo_Class_Id id, const Eo_Class *parent, ...)
860 if (parent && !EINA_MAGIC_CHECK(parent, EO_CLASS_EINA_MAGIC))
862 EINA_MAGIC_FAIL(parent, EO_CLASS_EINA_MAGIC);
866 if (id > EO_STATIC_IDS_LAST)
868 ERR("Tried creating a class with the static id %d while the maximum static id is %d. Aborting.", id, EO_STATIC_IDS_LAST);
872 va_start(p_list, parent);
874 EINA_SAFETY_ON_NULL_RETURN_VAL(desc, NULL);
875 EINA_SAFETY_ON_NULL_RETURN_VAL(desc->name, NULL);
877 /* Check restrictions on Interface types. */
878 if (desc->type == EO_CLASS_TYPE_INTERFACE)
880 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->constructor, NULL);
881 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->destructor, NULL);
882 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->class_constructor, NULL);
883 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->class_destructor, NULL);
884 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->data_size, NULL);
887 klass = calloc(1, sizeof(Eo_Class));
888 klass->parent = parent;
890 /* Handle class extensions */
892 Eo_Class *extn = NULL;
894 extn = va_arg(p_list, Eo_Class *);
897 switch (extn->desc->type)
899 case EO_CLASS_TYPE_REGULAR:
900 case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
901 /* Use it like an interface. */
902 case EO_CLASS_TYPE_INTERFACE:
904 case EO_CLASS_TYPE_MIXIN:
906 Eo_Extension_Node *node = calloc(1, sizeof(*node));
909 eina_inlist_append(klass->extensions,
910 EINA_INLIST_GET(node));
915 extn = va_arg(p_list, Eo_Class *);
921 /* Handle the inheritance */
924 /* Verify the inheritance is allowed. */
925 switch (klass->desc->type)
927 case EO_CLASS_TYPE_REGULAR:
928 case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
929 if ((klass->parent->desc->type != EO_CLASS_TYPE_REGULAR) &&
930 (klass->parent->desc->type != EO_CLASS_TYPE_REGULAR_NO_INSTANT))
932 ERR("Regular classes ('%s') aren't allowed to inherit from non-regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
936 case EO_CLASS_TYPE_INTERFACE:
937 case EO_CLASS_TYPE_MIXIN:
938 if ((klass->parent->desc->type != EO_CLASS_TYPE_INTERFACE) &&
939 (klass->parent->desc->type != EO_CLASS_TYPE_MIXIN))
941 ERR("Non-regular classes ('%s') aren't allowed to inherit from regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
948 /* Update the current offset. */
949 /* FIXME: Make sure this alignment is enough. */
950 klass->data_offset = klass->parent->data_offset +
951 EO_ALIGN_SIZE(klass->parent->desc->data_size);
954 if (!_eo_class_check_op_descs(klass, id))
959 if (!_eo_class_mro_init(klass))
964 /* create MIXIN offset table. */
966 const Eo_Class **mro_itr = klass->mro;
967 Eo_Extension_Data_Offset *extn_data_itr;
969 size_t extn_data_off = klass->data_offset +
970 EO_ALIGN_SIZE(klass->desc->data_size);
972 /* FIXME: Make faster... */
975 if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
976 ((*mro_itr)->desc->data_size > 0))
983 klass->extn_data_off = calloc(extn_num + 1,
984 sizeof(*klass->extn_data_off));
986 extn_data_itr = klass->extn_data_off;
987 mro_itr = klass->mro;
990 if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
991 ((*mro_itr)->desc->data_size > 0))
993 extn_data_itr->klass = *mro_itr;
994 extn_data_itr->offset = extn_data_off;
996 extn_data_off += EO_ALIGN_SIZE(extn_data_itr->klass->desc->data_size);
1002 klass->extn_data_size = extn_data_off;
1005 eina_lock_take(&_eo_class_creation_lock);
1009 klass->class_id = ++_eo_classes_last_id;
1014 if (_eo_classes && _eo_classes[id - 1])
1016 ERR("A class with id %d was already defined (%s). Aborting.", id,
1017 _eo_classes[id - 1]->desc->name);
1018 eina_lock_release(&_eo_class_creation_lock);
1022 klass->class_id = id;
1027 /* FIXME: Handle errors. */
1028 size_t arrsize = _eo_classes_last_id * sizeof(*_eo_classes);
1030 tmp = realloc(_eo_classes, arrsize);
1032 /* If it's the first allocation, memset. */
1034 memset(tmp, 0, arrsize);
1037 _eo_classes[klass->class_id - 1] = klass;
1039 eina_lock_release(&_eo_class_creation_lock);
1041 EINA_MAGIC_SET(klass, EO_CLASS_EINA_MAGIC);
1043 /* Flatten the function array */
1045 const Eo_Class **mro_itr = klass->mro;
1046 for ( ; *mro_itr ; mro_itr++)
1049 /* Skip ourselves. */
1050 for ( mro_itr-- ; mro_itr > klass->mro ; mro_itr--)
1052 _dich_copy_all(klass, *mro_itr);
1056 _eo_class_base_op_init(klass);
1058 _eo_class_constructor(klass);
1065 eo_class_free(klass);
1070 eo_parent_set(Eo *obj, const Eo *parent)
1072 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1074 EO_MAGIC_RETURN_VAL(parent, EO_EINA_MAGIC, EINA_FALSE);
1076 if (obj->parent == parent)
1081 if (eo_composite_is(obj))
1083 eo_composite_object_detach(obj->parent, obj);
1088 obj->parent->children =
1089 eina_inlist_remove(obj->parent->children, EINA_INLIST_GET(obj));
1090 eo_xunref(obj, obj->parent);
1093 obj->parent = (Eo *) parent;
1096 obj->parent->children =
1097 eina_inlist_append(obj->parent->children, EINA_INLIST_GET(obj));
1098 eo_xref(obj, obj->parent);
1107 eo_add(const Eo_Class *klass, Eo *parent)
1109 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, NULL);
1111 if (parent) EO_MAGIC_RETURN_VAL(parent, EO_EINA_MAGIC, NULL);
1113 if (EINA_UNLIKELY(klass->desc->type != EO_CLASS_TYPE_REGULAR))
1115 ERR("Class '%s' is not instantiate-able. Aborting.", klass->desc->name);
1119 Eo *obj = calloc(1, EO_ALIGN_SIZE(sizeof(*obj)) +
1120 (klass->data_offset + EO_ALIGN_SIZE(klass->desc->data_size)) +
1121 klass->extn_data_size);
1122 EINA_MAGIC_SET(obj, EO_EINA_MAGIC);
1126 eo_parent_set(obj, parent);
1128 Eo_Kls_Itr prev_state;
1130 _eo_kls_itr_init(klass, &obj->mro_itr, EO_NOOP, &prev_state);
1131 _eo_error_unset(obj);
1134 _eo_constructor(obj, klass);
1136 if (EINA_UNLIKELY(_eo_error_get(obj)))
1138 ERR("Type '%s' - One of the object constructors have failed.", klass->desc->name);
1142 if (EINA_UNLIKELY(!_eo_kls_itr_reached_end(&obj->mro_itr)))
1144 ERR("Type '%s' - Not all of the object constructors have been executed.", klass->desc->name);
1147 _eo_kls_itr_end(&obj->mro_itr, &prev_state);
1153 _eo_kls_itr_end(&obj->mro_itr, &prev_state);
1154 /* Unref twice, once for the ref above, and once for the basic object ref. */
1169 eo_xref_internal(Eo *obj, const Eo *ref_obj, const char *file, int line)
1171 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, obj);
1176 Eo_Xref_Node *xref = calloc(1, sizeof(*xref));
1177 xref->ref_obj = ref_obj;
1181 obj->xrefs = eina_inlist_prepend(obj->xrefs, EINA_INLIST_GET(xref));
1192 eo_xunref(Eo *obj, const Eo *ref_obj)
1194 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1196 Eo_Xref_Node *xref = NULL;
1197 EINA_INLIST_FOREACH(obj->xrefs, xref)
1199 if (xref->ref_obj == ref_obj)
1205 obj->xrefs = eina_inlist_remove(obj->xrefs, EINA_INLIST_GET(xref));
1210 ERR("ref_obj (%p) does not reference obj (%p). Aborting unref.", ref_obj, obj);
1227 eo_ref(const Eo *_obj)
1229 Eo *obj = (Eo *) _obj;
1230 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, obj);
1232 return _eo_ref(obj);
1236 _eo_del_internal(Eo *obj)
1238 /* We need that for the event callbacks that may ref/unref. */
1241 eo_do(obj, eo_event_callback_call(EO_EV_DEL, NULL, NULL));
1243 const Eo_Class *klass = eo_class_get(obj);
1244 Eo_Kls_Itr prev_state;
1246 _eo_kls_itr_init(klass, &obj->mro_itr, EO_NOOP, &prev_state);
1247 _eo_error_unset(obj);
1248 _eo_destructor(obj, klass);
1249 if (_eo_error_get(obj))
1251 ERR("Type '%s' - One of the object destructors have failed.", klass->desc->name);
1254 if (!_eo_kls_itr_reached_end(&obj->mro_itr))
1256 ERR("Type '%s' - Not all of the object destructors have been executed.", klass->desc->name);
1258 _eo_kls_itr_end(&obj->mro_itr, &prev_state);
1259 /*FIXME: add eo_class_unref(klass) ? - just to clear the caches. */
1262 Eina_List *itr, *itr_n;
1264 EINA_LIST_FOREACH_SAFE(obj->composite_objects, itr, itr_n, emb_obj)
1266 eo_composite_object_detach(obj, emb_obj);
1270 while (obj->children)
1272 eo_parent_set(EINA_INLIST_CONTAINER_GET(obj->children, Eo), NULL);
1275 obj->del = EINA_TRUE;
1282 EINA_MAGIC_SET(obj, EO_FREED_EINA_MAGIC);
1289 if (--(obj->refcount) == 0)
1291 _eo_del_internal(obj);
1294 /* If for some reason it's not empty, clear it. */
1297 WRN("obj->xrefs is not empty, possibly a bug, please report. - An error will be reported for each xref in the stack.");
1298 Eina_Inlist *nitr = obj->xrefs->next;
1299 free(EINA_INLIST_CONTAINER_GET(obj->xrefs, Eo_Xref_Node));
1304 if (!obj->manual_free)
1307 _eo_ref(obj); /* If we manual free, we keep a phantom ref. */
1312 eo_unref(const Eo *_obj)
1314 Eo *obj = (Eo *) _obj;
1315 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1321 eo_del(const Eo *obj)
1323 eo_parent_set((Eo *) obj, NULL);
1328 eo_ref_get(const Eo *obj)
1330 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, 0);
1332 return obj->refcount;
1336 eo_parent_get(const Eo *obj)
1338 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1344 eo_error_set_internal(const Eo *obj, const char *file, int line)
1346 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1348 ERR("Error with obj '%p' at %s:%d", obj, file, line);
1350 ((Eo *) obj)->construct_error = EINA_TRUE;
1354 _eo_error_unset(Eo *obj)
1356 obj->construct_error = EINA_FALSE;
1361 * @brief Check if there was an error when constructing, destructing or calling a function of the object.
1362 * @param obj the object to work on.
1363 * @return @c EINA_TRUE if there was an error.
1365 static inline Eina_Bool
1366 _eo_error_get(const Eo *obj)
1368 return obj->construct_error;
1372 _eo_constructor_default(Eo *obj)
1374 eo_constructor_super(obj);
1378 _eo_destructor_default(Eo *obj)
1380 eo_destructor_super(obj);
1384 _eo_constructor(Eo *obj, const Eo_Class *klass)
1389 if (klass->desc->constructor)
1390 klass->desc->constructor(obj, _eo_data_get(obj, klass));
1392 _eo_constructor_default(obj);
1396 _eo_destructor(Eo *obj, const Eo_Class *klass)
1401 if (klass->desc->destructor)
1402 klass->desc->destructor(obj, _eo_data_get(obj, klass));
1404 _eo_destructor_default(obj);
1408 eo_constructor_super(Eo *obj)
1410 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1412 _eo_constructor(obj, _eo_kls_itr_next(&obj->mro_itr, EO_NOOP));
1416 eo_destructor_super(Eo *obj)
1418 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1420 _eo_destructor(obj, _eo_kls_itr_next(&obj->mro_itr, EO_NOOP));
1423 static inline void *
1424 _eo_data_get(const Eo *obj, const Eo_Class *klass)
1426 if (EINA_LIKELY(klass->desc->data_size > 0))
1428 if (EINA_UNLIKELY(klass->desc->type == EO_CLASS_TYPE_MIXIN))
1430 Eo_Extension_Data_Offset *doff_itr =
1431 eo_class_get(obj)->extn_data_off;
1436 while (doff_itr->klass)
1438 if (doff_itr->klass == klass)
1439 return ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj)) +
1446 return ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj)) +
1455 eo_data_get(const Eo *obj, const Eo_Class *klass)
1458 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1461 if (!_eo_class_mro_has(obj->klass, klass))
1463 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);
1468 ret = _eo_data_get(obj, klass);
1471 if (!ret && (klass->desc->data_size == 0))
1473 ERR("Tried getting data of class '%s', but it has none..", klass->desc->name);
1483 const char *log_dom = "eo";
1484 if (_eo_init_count++ > 0)
1490 _eo_classes_last_id = EO_STATIC_IDS_LAST;
1491 _eo_log_dom = eina_log_domain_register(log_dom, EINA_COLOR_LIGHTBLUE);
1492 if (_eo_log_dom < 0)
1494 EINA_LOG_ERR("Could not register log domain: %s", log_dom);
1498 if (!eina_lock_new(&_eo_class_creation_lock))
1500 EINA_LOG_ERR("Could not init lock.");
1504 eina_magic_string_static_set(EO_EINA_MAGIC, EO_EINA_MAGIC_STR);
1505 eina_magic_string_static_set(EO_FREED_EINA_MAGIC,
1506 EO_FREED_EINA_MAGIC_STR);
1507 eina_magic_string_static_set(EO_CLASS_EINA_MAGIC,
1508 EO_CLASS_EINA_MAGIC_STR);
1517 Eo_Class **cls_itr = _eo_classes;
1519 if (--_eo_init_count > 0)
1522 for (i = 0 ; i < _eo_classes_last_id ; i++, cls_itr++)
1525 eo_class_free(*cls_itr);
1531 eina_lock_free(&_eo_class_creation_lock);
1533 eina_log_domain_unregister(_eo_log_dom);
1541 eo_composite_object_attach(Eo *obj, Eo *emb_obj)
1543 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1544 EO_MAGIC_RETURN(emb_obj, EO_EINA_MAGIC);
1546 emb_obj->composite = EINA_TRUE;
1547 eo_parent_set(emb_obj, obj);
1548 obj->composite_objects = eina_list_prepend(obj->composite_objects, emb_obj);
1552 eo_composite_object_detach(Eo *obj, Eo *emb_obj)
1554 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1555 EO_MAGIC_RETURN(emb_obj, EO_EINA_MAGIC);
1557 emb_obj->composite = EINA_FALSE;
1558 obj->composite_objects = eina_list_remove(obj->composite_objects, emb_obj);
1559 eo_parent_set(emb_obj, NULL);
1563 eo_composite_is(const Eo *emb_obj)
1565 if (!EINA_MAGIC_CHECK(emb_obj, EO_EINA_MAGIC))
1567 EINA_MAGIC_FAIL(emb_obj, EO_EINA_MAGIC);
1571 return emb_obj->composite;
1575 eo_manual_free_set(Eo *obj, Eina_Bool manual_free)
1577 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1578 obj->manual_free = manual_free;
1582 eo_manual_free(Eo *obj)
1584 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1586 if (EINA_FALSE == obj->manual_free)
1588 ERR("Tried to manually free the object %p while the option has not been set; see eo_manual_free_set for more information.", obj);
1594 ERR("Tried deleting the object %p while still referenced(%d).", obj, eo_ref_get(obj));