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 %x ('%s') while expecting: %x ('%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' (%x) 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' (%x) 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 instance op '%s' (%x) 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_UNLIKELY(!op_desc || (itr->op == EO_NOOP)))
761 ERR("Setting implementation for non-existent op %x for class '%s'. Func index: %d", itr->op, klass->desc->name, itr - func_descs);
763 else if (EINA_LIKELY(itr->op_type == op_desc->op_type))
765 _dich_func_set(klass, itr->op, itr->func);
769 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",
771 (op_desc) ? op_desc->op_type : EO_OP_TYPE_REGULAR,
772 (op_desc) ? op_desc->name : NULL,
781 eo_class_free(Eo_Class *klass)
783 if (klass->constructed)
785 if (klass->desc->class_destructor)
786 klass->desc->class_destructor(klass);
788 _dich_func_clean_all(klass);
793 Eo_Extension_Node *extn = NULL;
794 EINA_INLIST_FOREACH_SAFE(klass->extensions, itrn, extn)
803 if (klass->extn_data_off)
804 free(klass->extn_data_off);
811 _eo_class_check_op_descs(const Eo_Class *klass, Eo_Class_Id id)
813 const Eo_Class_Description *desc = klass->desc;
814 const Eo_Op_Description *itr;
817 if (desc->ops.count > 0)
819 if (((id == 0) || (id > EO_STATIC_IDS_LAST)) && !desc->ops.base_op_id)
821 ERR("Class '%s' has a non-zero ops count, but base_id is NULL.",
826 if (!desc->ops.descs)
828 ERR("Class '%s' has a non-zero ops count, but there are no descs.",
834 itr = desc->ops.descs;
835 for (i = 0 ; i < desc->ops.count ; i++, itr++)
837 if (itr->sub_op != i)
841 ERR("Wrong order in Ops description for class '%s'. Expected %x and got %x", desc->name, i, itr->sub_op);
845 ERR("Found too few Ops description for class '%s'. Expected %x descriptions, but found %x.", desc->name, desc->ops.count, i);
851 if (itr && itr->name)
853 ERR("Found extra Ops description for class '%s'. Expected %d descriptions, but found more.", desc->name, desc->ops.count);
860 EAPI const Eo_Class *
861 eo_class_new(const Eo_Class_Description *desc, Eo_Class_Id id, const Eo_Class *parent, ...)
866 if (parent && !EINA_MAGIC_CHECK(parent, EO_CLASS_EINA_MAGIC))
868 EINA_MAGIC_FAIL(parent, EO_CLASS_EINA_MAGIC);
872 if (id > EO_STATIC_IDS_LAST)
874 ERR("Tried creating a class with the static id %d while the maximum static id is %d. Aborting.", id, EO_STATIC_IDS_LAST);
878 va_start(p_list, parent);
880 EINA_SAFETY_ON_NULL_RETURN_VAL(desc, NULL);
881 EINA_SAFETY_ON_NULL_RETURN_VAL(desc->name, NULL);
883 /* Check restrictions on Interface types. */
884 if (desc->type == EO_CLASS_TYPE_INTERFACE)
886 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->constructor, NULL);
887 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->destructor, NULL);
888 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->class_constructor, NULL);
889 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->class_destructor, NULL);
890 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->data_size, NULL);
893 klass = calloc(1, sizeof(Eo_Class));
894 klass->parent = parent;
896 /* Handle class extensions */
898 Eo_Class *extn = NULL;
900 extn = va_arg(p_list, Eo_Class *);
903 switch (extn->desc->type)
905 case EO_CLASS_TYPE_REGULAR:
906 case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
907 /* Use it like an interface. */
908 case EO_CLASS_TYPE_INTERFACE:
910 case EO_CLASS_TYPE_MIXIN:
912 Eo_Extension_Node *node = calloc(1, sizeof(*node));
915 eina_inlist_append(klass->extensions,
916 EINA_INLIST_GET(node));
921 extn = va_arg(p_list, Eo_Class *);
927 /* Handle the inheritance */
930 /* Verify the inheritance is allowed. */
931 switch (klass->desc->type)
933 case EO_CLASS_TYPE_REGULAR:
934 case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
935 if ((klass->parent->desc->type != EO_CLASS_TYPE_REGULAR) &&
936 (klass->parent->desc->type != EO_CLASS_TYPE_REGULAR_NO_INSTANT))
938 ERR("Regular classes ('%s') aren't allowed to inherit from non-regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
942 case EO_CLASS_TYPE_INTERFACE:
943 case EO_CLASS_TYPE_MIXIN:
944 if ((klass->parent->desc->type != EO_CLASS_TYPE_INTERFACE) &&
945 (klass->parent->desc->type != EO_CLASS_TYPE_MIXIN))
947 ERR("Non-regular classes ('%s') aren't allowed to inherit from regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
954 /* Update the current offset. */
955 /* FIXME: Make sure this alignment is enough. */
956 klass->data_offset = klass->parent->data_offset +
957 EO_ALIGN_SIZE(klass->parent->desc->data_size);
960 if (!_eo_class_check_op_descs(klass, id))
965 if (!_eo_class_mro_init(klass))
970 /* create MIXIN offset table. */
972 const Eo_Class **mro_itr = klass->mro;
973 Eo_Extension_Data_Offset *extn_data_itr;
975 size_t extn_data_off = klass->data_offset +
976 EO_ALIGN_SIZE(klass->desc->data_size);
978 /* FIXME: Make faster... */
981 if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
982 ((*mro_itr)->desc->data_size > 0))
989 klass->extn_data_off = calloc(extn_num + 1,
990 sizeof(*klass->extn_data_off));
992 extn_data_itr = klass->extn_data_off;
993 mro_itr = klass->mro;
996 if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
997 ((*mro_itr)->desc->data_size > 0))
999 extn_data_itr->klass = *mro_itr;
1000 extn_data_itr->offset = extn_data_off;
1002 extn_data_off += EO_ALIGN_SIZE(extn_data_itr->klass->desc->data_size);
1008 klass->extn_data_size = extn_data_off;
1011 eina_lock_take(&_eo_class_creation_lock);
1015 klass->class_id = ++_eo_classes_last_id;
1020 if (_eo_classes && _eo_classes[id - 1])
1022 ERR("A class with id %d was already defined (%s). Aborting.", id,
1023 _eo_classes[id - 1]->desc->name);
1024 eina_lock_release(&_eo_class_creation_lock);
1028 klass->class_id = id;
1033 /* FIXME: Handle errors. */
1034 size_t arrsize = _eo_classes_last_id * sizeof(*_eo_classes);
1036 tmp = realloc(_eo_classes, arrsize);
1038 /* If it's the first allocation, memset. */
1040 memset(tmp, 0, arrsize);
1043 _eo_classes[klass->class_id - 1] = klass;
1045 eina_lock_release(&_eo_class_creation_lock);
1047 EINA_MAGIC_SET(klass, EO_CLASS_EINA_MAGIC);
1049 /* Flatten the function array */
1051 const Eo_Class **mro_itr = klass->mro;
1052 for ( ; *mro_itr ; mro_itr++)
1055 /* Skip ourselves. */
1056 for ( mro_itr-- ; mro_itr > klass->mro ; mro_itr--)
1058 _dich_copy_all(klass, *mro_itr);
1062 _eo_class_base_op_init(klass);
1064 _eo_class_constructor(klass);
1071 eo_class_free(klass);
1076 eo_parent_set(Eo *obj, const Eo *parent)
1078 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1080 EO_MAGIC_RETURN_VAL(parent, EO_EINA_MAGIC, EINA_FALSE);
1082 if (obj->parent == parent)
1087 if (eo_composite_is(obj))
1089 eo_composite_object_detach(obj->parent, obj);
1094 obj->parent->children =
1095 eina_inlist_remove(obj->parent->children, EINA_INLIST_GET(obj));
1096 eo_xunref(obj, obj->parent);
1099 obj->parent = (Eo *) parent;
1102 obj->parent->children =
1103 eina_inlist_append(obj->parent->children, EINA_INLIST_GET(obj));
1104 eo_xref(obj, obj->parent);
1113 eo_add(const Eo_Class *klass, Eo *parent)
1115 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, NULL);
1117 if (parent) EO_MAGIC_RETURN_VAL(parent, EO_EINA_MAGIC, NULL);
1119 if (EINA_UNLIKELY(klass->desc->type != EO_CLASS_TYPE_REGULAR))
1121 ERR("Class '%s' is not instantiate-able. Aborting.", klass->desc->name);
1125 Eo *obj = calloc(1, EO_ALIGN_SIZE(sizeof(*obj)) +
1126 (klass->data_offset + EO_ALIGN_SIZE(klass->desc->data_size)) +
1127 klass->extn_data_size);
1128 EINA_MAGIC_SET(obj, EO_EINA_MAGIC);
1132 eo_parent_set(obj, parent);
1134 Eo_Kls_Itr prev_state;
1136 _eo_kls_itr_init(klass, &obj->mro_itr, EO_NOOP, &prev_state);
1137 _eo_error_unset(obj);
1140 _eo_constructor(obj, klass);
1142 if (EINA_UNLIKELY(_eo_error_get(obj)))
1144 ERR("Type '%s' - One of the object constructors have failed.", klass->desc->name);
1148 if (EINA_UNLIKELY(!_eo_kls_itr_reached_end(&obj->mro_itr)))
1150 ERR("Type '%s' - Not all of the object constructors have been executed.", klass->desc->name);
1153 _eo_kls_itr_end(&obj->mro_itr, &prev_state);
1159 _eo_kls_itr_end(&obj->mro_itr, &prev_state);
1160 /* Unref twice, once for the ref above, and once for the basic object ref. */
1175 eo_xref_internal(Eo *obj, const Eo *ref_obj, const char *file, int line)
1177 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, obj);
1182 Eo_Xref_Node *xref = calloc(1, sizeof(*xref));
1183 xref->ref_obj = ref_obj;
1187 obj->xrefs = eina_inlist_prepend(obj->xrefs, EINA_INLIST_GET(xref));
1198 eo_xunref(Eo *obj, const Eo *ref_obj)
1200 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1202 Eo_Xref_Node *xref = NULL;
1203 EINA_INLIST_FOREACH(obj->xrefs, xref)
1205 if (xref->ref_obj == ref_obj)
1211 obj->xrefs = eina_inlist_remove(obj->xrefs, EINA_INLIST_GET(xref));
1216 ERR("ref_obj (%p) does not reference obj (%p). Aborting unref.", ref_obj, obj);
1233 eo_ref(const Eo *_obj)
1235 Eo *obj = (Eo *) _obj;
1236 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, obj);
1238 return _eo_ref(obj);
1242 _eo_del_internal(Eo *obj)
1244 /* We need that for the event callbacks that may ref/unref. */
1247 eo_do(obj, eo_event_callback_call(EO_EV_DEL, NULL, NULL));
1249 const Eo_Class *klass = eo_class_get(obj);
1250 Eo_Kls_Itr prev_state;
1252 _eo_kls_itr_init(klass, &obj->mro_itr, EO_NOOP, &prev_state);
1253 _eo_error_unset(obj);
1254 _eo_destructor(obj, klass);
1255 if (_eo_error_get(obj))
1257 ERR("Type '%s' - One of the object destructors have failed.", klass->desc->name);
1260 if (!_eo_kls_itr_reached_end(&obj->mro_itr))
1262 ERR("Type '%s' - Not all of the object destructors have been executed.", klass->desc->name);
1264 _eo_kls_itr_end(&obj->mro_itr, &prev_state);
1265 /*FIXME: add eo_class_unref(klass) ? - just to clear the caches. */
1268 Eina_List *itr, *itr_n;
1270 EINA_LIST_FOREACH_SAFE(obj->composite_objects, itr, itr_n, emb_obj)
1272 eo_composite_object_detach(obj, emb_obj);
1276 while (obj->children)
1278 eo_parent_set(EINA_INLIST_CONTAINER_GET(obj->children, Eo), NULL);
1281 obj->del = EINA_TRUE;
1288 EINA_MAGIC_SET(obj, EO_FREED_EINA_MAGIC);
1295 if (--(obj->refcount) == 0)
1297 _eo_del_internal(obj);
1300 /* If for some reason it's not empty, clear it. */
1303 WRN("obj->xrefs is not empty, possibly a bug, please report. - An error will be reported for each xref in the stack.");
1304 Eina_Inlist *nitr = obj->xrefs->next;
1305 free(EINA_INLIST_CONTAINER_GET(obj->xrefs, Eo_Xref_Node));
1310 if (!obj->manual_free)
1313 _eo_ref(obj); /* If we manual free, we keep a phantom ref. */
1318 eo_unref(const Eo *_obj)
1320 Eo *obj = (Eo *) _obj;
1321 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1327 eo_del(const Eo *obj)
1329 eo_parent_set((Eo *) obj, NULL);
1334 eo_ref_get(const Eo *obj)
1336 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, 0);
1338 return obj->refcount;
1342 eo_parent_get(const Eo *obj)
1344 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1350 eo_error_set_internal(const Eo *obj, const char *file, int line)
1352 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1354 ERR("Error with obj '%p' at %s:%d", obj, file, line);
1356 ((Eo *) obj)->construct_error = EINA_TRUE;
1360 _eo_error_unset(Eo *obj)
1362 obj->construct_error = EINA_FALSE;
1367 * @brief Check if there was an error when constructing, destructing or calling a function of the object.
1368 * @param obj the object to work on.
1369 * @return @c EINA_TRUE if there was an error.
1371 static inline Eina_Bool
1372 _eo_error_get(const Eo *obj)
1374 return obj->construct_error;
1378 _eo_constructor_default(Eo *obj)
1380 eo_constructor_super(obj);
1384 _eo_destructor_default(Eo *obj)
1386 eo_destructor_super(obj);
1390 _eo_constructor(Eo *obj, const Eo_Class *klass)
1395 if (klass->desc->constructor)
1396 klass->desc->constructor(obj, _eo_data_get(obj, klass));
1398 _eo_constructor_default(obj);
1402 _eo_destructor(Eo *obj, const Eo_Class *klass)
1407 if (klass->desc->destructor)
1408 klass->desc->destructor(obj, _eo_data_get(obj, klass));
1410 _eo_destructor_default(obj);
1414 eo_constructor_super(Eo *obj)
1416 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1418 _eo_constructor(obj, _eo_kls_itr_next(&obj->mro_itr, EO_NOOP));
1422 eo_destructor_super(Eo *obj)
1424 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1426 _eo_destructor(obj, _eo_kls_itr_next(&obj->mro_itr, EO_NOOP));
1429 static inline void *
1430 _eo_data_get(const Eo *obj, const Eo_Class *klass)
1432 if (EINA_LIKELY(klass->desc->data_size > 0))
1434 if (EINA_UNLIKELY(klass->desc->type == EO_CLASS_TYPE_MIXIN))
1436 Eo_Extension_Data_Offset *doff_itr =
1437 eo_class_get(obj)->extn_data_off;
1442 while (doff_itr->klass)
1444 if (doff_itr->klass == klass)
1445 return ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj)) +
1452 return ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj)) +
1461 eo_data_get(const Eo *obj, const Eo_Class *klass)
1464 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1467 if (!_eo_class_mro_has(obj->klass, klass))
1469 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);
1474 ret = _eo_data_get(obj, klass);
1477 if (!ret && (klass->desc->data_size == 0))
1479 ERR("Tried getting data of class '%s', but it has none..", klass->desc->name);
1489 const char *log_dom = "eo";
1490 if (_eo_init_count++ > 0)
1496 _eo_classes_last_id = EO_STATIC_IDS_LAST;
1497 _eo_log_dom = eina_log_domain_register(log_dom, EINA_COLOR_LIGHTBLUE);
1498 if (_eo_log_dom < 0)
1500 EINA_LOG_ERR("Could not register log domain: %s", log_dom);
1504 if (!eina_lock_new(&_eo_class_creation_lock))
1506 EINA_LOG_ERR("Could not init lock.");
1510 eina_magic_string_static_set(EO_EINA_MAGIC, EO_EINA_MAGIC_STR);
1511 eina_magic_string_static_set(EO_FREED_EINA_MAGIC,
1512 EO_FREED_EINA_MAGIC_STR);
1513 eina_magic_string_static_set(EO_CLASS_EINA_MAGIC,
1514 EO_CLASS_EINA_MAGIC_STR);
1523 Eo_Class **cls_itr = _eo_classes;
1525 if (--_eo_init_count > 0)
1528 for (i = 0 ; i < _eo_classes_last_id ; i++, cls_itr++)
1531 eo_class_free(*cls_itr);
1537 eina_lock_free(&_eo_class_creation_lock);
1539 eina_log_domain_unregister(_eo_log_dom);
1547 eo_composite_object_attach(Eo *obj, Eo *emb_obj)
1549 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1550 EO_MAGIC_RETURN(emb_obj, EO_EINA_MAGIC);
1552 emb_obj->composite = EINA_TRUE;
1553 eo_parent_set(emb_obj, obj);
1554 obj->composite_objects = eina_list_prepend(obj->composite_objects, emb_obj);
1558 eo_composite_object_detach(Eo *obj, Eo *emb_obj)
1560 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1561 EO_MAGIC_RETURN(emb_obj, EO_EINA_MAGIC);
1563 emb_obj->composite = EINA_FALSE;
1564 obj->composite_objects = eina_list_remove(obj->composite_objects, emb_obj);
1565 eo_parent_set(emb_obj, NULL);
1569 eo_composite_is(const Eo *emb_obj)
1571 if (!EINA_MAGIC_CHECK(emb_obj, EO_EINA_MAGIC))
1573 EINA_MAGIC_FAIL(emb_obj, EO_EINA_MAGIC);
1577 return emb_obj->composite;
1581 eo_manual_free_set(Eo *obj, Eina_Bool manual_free)
1583 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1584 obj->manual_free = manual_free;
1588 eo_manual_free(Eo *obj)
1590 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1592 if (EINA_FALSE == obj->manual_free)
1594 ERR("Tried to manually free the object %p while the option has not been set; see eo_manual_free_set for more information.", obj);
1600 ERR("Tried deleting the object %p while still referenced(%d).", obj, eo_ref_get(obj));