4 #include "eo_private.h"
8 /* The last id that should be reserved for statically allocated classes. */
9 #define EO_STATIC_IDS_LAST 10
10 #define EO_OP_IDS_FIRST 1
12 /* Used inside the class_get functions of classes, see #EO_DEFINE_CLASS */
13 EAPI Eina_Lock _eo_class_creation_lock;
16 static Eo_Class **_eo_classes;
17 static Eo_Class_Id _eo_classes_last_id;
18 static Eina_Bool _eo_init_count = 0;
19 static Eo_Op _eo_ops_last_id = 0;
21 static void _eo_condtor_reset(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);
28 const Eo_Class **kls_itr;
29 Eina_Bool insuper_context:1;
36 Eina_Inlist *children;
37 const Eo_Class *klass;
43 Eina_List *composite_objects;
48 Eina_Bool condtor_done:1;
50 Eina_Bool composite:1;
52 Eina_Bool manual_free:1;
57 /* How we search and store the implementations in classes. */
58 #define DICH_CHAIN_LAST_BITS 5
59 #define DICH_CHAIN_LAST_SIZE (1 << DICH_CHAIN_LAST_BITS)
60 #define DICH_CHAIN1(x) ((x) / DICH_CHAIN_LAST_SIZE)
61 #define DICH_CHAIN_LAST(x) ((x) % DICH_CHAIN_LAST_SIZE)
63 #define OP_CLASS_OFFSET_GET(x) (((x) >> EO_OP_CLASS_OFFSET) & 0xffff)
65 #define ID_CLASS_GET(id) ({ \
66 (Eo_Class *) ((id <= _eo_classes_last_id) && (id > 0)) ? \
67 (_eo_classes[id - 1]) : NULL; \
70 #define EO_ALIGN_SIZE(size) \
71 ((size) + (sizeof(void *) - ((size) % sizeof(void *))))
73 typedef struct _Dich_Chain1 Dich_Chain1;
88 const Eo_Class *klass;
90 } Eo_Extension_Data_Offset;
96 const Eo_Class *parent;
97 const Eo_Class_Description *desc;
98 Dich_Chain1 *chain; /**< The size is class_id */
102 const Eo_Class **extensions;
104 Eo_Extension_Data_Offset *extn_data_off;
105 size_t extn_data_size;
107 const Eo_Class **mro;
110 size_t data_offset; /* < Offset of the data within object data. */
112 Eina_Bool constructed : 1;
116 _dich_chain_alloc(Dich_Chain1 *chain1)
120 chain1->funcs = calloc(DICH_CHAIN_LAST_SIZE, sizeof(*(chain1->funcs)));
125 _dich_copy_all(Eo_Class *dst, const Eo_Class *src)
128 const Dich_Chain1 *sc1 = src->chain;
129 Dich_Chain1 *dc1 = dst->chain;
130 for (i = 0 ; i <= DICH_CHAIN1(src->base_id) ; i++, sc1++, dc1++)
136 _dich_chain_alloc(dc1);
138 const op_type_funcs *sf = sc1->funcs;
139 op_type_funcs *df = dc1->funcs;
140 for (j = 0 ; j < DICH_CHAIN_LAST_SIZE ; j++, df++, sf++)
144 memcpy(df, sf, sizeof(*df));
151 static inline const op_type_funcs *
152 _dich_func_get(const Eo_Class *klass, Eo_Op op)
154 size_t idx1 = DICH_CHAIN1(op);
155 if (EINA_UNLIKELY(idx1 >= klass->chain_size))
157 Dich_Chain1 *chain1 = &klass->chain[idx1];
158 if (EINA_UNLIKELY(!chain1->funcs))
160 return &chain1->funcs[DICH_CHAIN_LAST(op)];
164 _dich_func_set(Eo_Class *klass, Eo_Op op, eo_op_func_type func)
166 size_t idx1 = DICH_CHAIN1(op);
167 Dich_Chain1 *chain1 = &klass->chain[idx1];
168 _dich_chain_alloc(chain1);
169 chain1->funcs[DICH_CHAIN_LAST(op)].func = func;
170 chain1->funcs[DICH_CHAIN_LAST(op)].src = klass;
174 _dich_func_clean_all(Eo_Class *klass)
177 Dich_Chain1 *chain1 = klass->chain;
179 for (i = 0 ; i <= DICH_CHAIN1(klass->base_id) ; i++, chain1++)
190 static const Eo_Op_Description noop_desc =
191 EO_OP_DESCRIPTION(EO_NOOP, "No operation.");
193 static const Eo_Class *
194 _eo_op_class_get(Eo_Op op)
196 /* FIXME: Make it fast. */
197 const Eo_Class *klass = NULL;
198 Eo_Class **itr = _eo_classes;
200 for (i = 0 ; i < _eo_classes_last_id ; i++, itr++)
202 if (*itr && ((*itr)->base_id <= op) &&
203 (op <= (*itr)->base_id + (*itr)->desc->ops.count))
213 static const Eo_Op_Description *
214 _eo_op_id_desc_get(Eo_Op op)
216 const Eo_Class *klass;
221 klass = _eo_op_class_get(op);
225 Eo_Op sub_id = op - klass->base_id;
226 if (sub_id < klass->desc->ops.count)
227 return klass->desc->ops.descs + sub_id;
234 _eo_op_id_name_get(Eo_Op op)
236 const Eo_Op_Description *desc = _eo_op_id_desc_get(op);
237 return (desc) ? desc->name : NULL;
241 _eo_kls_itr_init(const Eo_Class *obj_klass, Eo_Kls_Itr *cur, Eo_Kls_Itr *prev_state)
243 if (cur->insuper_context)
245 memcpy(prev_state, cur, sizeof(*cur));
246 cur->kls_itr = obj_klass->mro;
251 _eo_kls_itr_end(Eo_Kls_Itr *cur, Eo_Kls_Itr *prev_state)
253 if (cur->insuper_context)
255 memcpy(cur, prev_state, sizeof(*cur));
259 static inline const Eo_Class *
260 _eo_kls_itr_get(Eo_Kls_Itr *cur)
262 return *(cur->kls_itr);
265 static inline const Eo_Class *
266 _eo_kls_itr_next(Eo_Kls_Itr *cur, Eo_Kls_Itr *prev_state, Eo_Op op)
268 const Eo_Class **kls_itr = cur->kls_itr;
269 memcpy(prev_state, cur, sizeof(*cur));
270 cur->insuper_context = EINA_TRUE;
273 const op_type_funcs *fsrc = _dich_func_get(*kls_itr, op);
275 while (*kls_itr && (*(kls_itr++) != fsrc->src))
278 cur->kls_itr = kls_itr;
287 static inline const op_type_funcs *
288 _eo_kls_itr_func_get(Eo_Kls_Itr *mro_itr, Eo_Op op)
290 const Eo_Class *klass = _eo_kls_itr_get(mro_itr);
293 const op_type_funcs *func = _dich_func_get(klass, op);
295 if (func && func->func)
304 #define _EO_OP_ERR_NO_OP_PRINT(op, klass) \
307 const Eo_Class *op_klass = _eo_op_class_get(op); \
308 const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL; \
309 ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.", \
310 op, _eo_op_id_name_get(op), _dom_name, \
311 (klass) ? klass->desc->name : NULL); \
316 _eo_op_internal(Eo *obj, Eo_Op_Type op_type, Eo_Op op, va_list *p_list)
319 const Eo_Op_Description *op_desc = _eo_op_id_desc_get(op);
323 if (op_desc->op_type == EO_OP_TYPE_CLASS)
325 ERR("Tried calling a class op '%s' (%x) from a non-class context.", (op_desc) ? op_desc->name : NULL, op);
332 const op_type_funcs *func =
333 _eo_kls_itr_func_get(&obj->mro_itr, op);
336 func->func(obj, _eo_data_get(obj, func->src), p_list);
341 /* Try composite objects */
345 EINA_LIST_FOREACH(obj->composite_objects, itr, emb_obj)
347 if (_eo_op_internal(emb_obj, op_type, op, p_list))
357 eo_do_internal(Eo *obj, Eo_Op_Type op_type, ...)
359 Eina_Bool prev_error;
360 Eina_Bool ret = EINA_TRUE;
362 Eo_Kls_Itr prev_state;
365 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
367 prev_error = obj->do_error;
369 _eo_kls_itr_init(obj->klass, &obj->mro_itr, &prev_state);
371 va_start(p_list, op_type);
373 op = va_arg(p_list, Eo_Op);
376 if (!_eo_op_internal(obj, op_type, op, &p_list))
378 _EO_OP_ERR_NO_OP_PRINT(op, obj->klass);
382 op = va_arg(p_list, Eo_Op);
392 obj->do_error = prev_error;
394 _eo_kls_itr_end(&obj->mro_itr, &prev_state);
399 eo_do_super_internal(Eo *obj, Eo_Op_Type op_type, Eo_Op op, ...)
401 const Eo_Class *nklass;
402 Eina_Bool ret = EINA_TRUE;
404 Eo_Kls_Itr prev_state;
405 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
407 /* Advance the kls itr. */
408 nklass = _eo_kls_itr_next(&obj->mro_itr, &prev_state, op);
410 va_start(p_list, op);
411 if (!_eo_op_internal(obj, op_type, op, &p_list))
413 _EO_OP_ERR_NO_OP_PRINT(op, nklass);
421 _eo_kls_itr_end(&obj->mro_itr, &prev_state);
426 _eo_class_op_internal(Eo_Class *klass, Eo_Op op, va_list *p_list)
429 const Eo_Op_Description *op_desc = _eo_op_id_desc_get(op);
433 if (op_desc->op_type != EO_OP_TYPE_CLASS)
435 ERR("Tried calling an instance op '%s' (%x) from a class context.", (op_desc) ? op_desc->name : NULL, op);
442 const op_type_funcs *func =
443 _eo_kls_itr_func_get(&klass->mro_itr, op);
446 ((eo_op_func_type_class) func->func)(klass, p_list);
455 eo_class_do_internal(const Eo_Class *klass, ...)
457 Eina_Bool ret = EINA_TRUE;
459 Eo_Kls_Itr prev_state;
462 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, EINA_FALSE);
464 _eo_kls_itr_init(klass, &((Eo_Class *) klass)->mro_itr, &prev_state);
466 va_start(p_list, klass);
468 op = va_arg(p_list, Eo_Op);
471 if (!_eo_class_op_internal((Eo_Class *) klass, op, &p_list))
473 _EO_OP_ERR_NO_OP_PRINT(op, klass);
477 op = va_arg(p_list, Eo_Op);
482 _eo_kls_itr_end(&((Eo_Class *) klass)->mro_itr, &prev_state);
487 eo_class_do_super_internal(const Eo_Class *klass, Eo_Op op, ...)
489 const Eo_Class *nklass;
490 Eina_Bool ret = EINA_TRUE;
492 Eo_Kls_Itr prev_state;
493 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, EINA_FALSE);
495 /* Advance the kls itr. */
496 nklass = _eo_kls_itr_next(&((Eo_Class *) klass)->mro_itr, &prev_state, op);
498 va_start(p_list, op);
499 if (!_eo_class_op_internal((Eo_Class *) klass, op, &p_list))
501 _EO_OP_ERR_NO_OP_PRINT(op, nklass);
506 _eo_kls_itr_end(&((Eo_Class *) klass)->mro_itr, &prev_state);
510 EAPI const Eo_Class *
511 eo_class_get(const Eo *obj)
513 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
519 eo_class_name_get(const Eo_Class *klass)
521 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, NULL);
523 return klass->desc->name;
527 _eo_class_base_op_init(Eo_Class *klass)
529 const Eo_Class_Description *desc = klass->desc;
531 klass->base_id = _eo_ops_last_id;
533 if (desc && desc->ops.base_op_id)
534 *(desc->ops.base_op_id) = klass->base_id;
536 _eo_ops_last_id += desc->ops.count + 1;
538 klass->chain_size = DICH_CHAIN1(_eo_ops_last_id) + 1;
539 klass->chain = calloc(klass->chain_size, sizeof(*klass->chain));
544 _eo_class_mro_has(const Eo_Class *klass, const Eo_Class *find)
546 const Eo_Class **itr;
547 for (itr = klass->mro ; *itr ; itr++)
559 _eo_class_mro_add(Eina_List *mro, const Eo_Class *klass)
561 Eina_List *extn_pos = NULL;
562 Eina_Bool check_consistency = !mro;
566 mro = eina_list_append(mro, klass);
568 /* ONLY ADD MIXINS! */
570 /* Recursively add extenions. */
572 const Eo_Class **extn_itr;
574 for (extn_itr = klass->extensions ; *extn_itr ; extn_itr++)
576 const Eo_Class *extn = *extn_itr;
577 if (extn->desc->type != EO_CLASS_TYPE_MIXIN)
580 mro = _eo_class_mro_add(mro, extn);
581 /* Not possible: if (!mro) return NULL; */
583 if (check_consistency)
585 extn_pos = eina_list_append(extn_pos, eina_list_last(mro));
590 /* Check if we can create a consistent mro. We only do it for the class
591 * we are working on (i.e no parents). */
592 if (check_consistency)
594 const Eo_Class **extn_itr;
596 Eina_List *itr = extn_pos;
597 for (extn_itr = klass->extensions ; *extn_itr ; extn_itr++)
599 const Eo_Class *extn = *extn_itr;
600 if (extn->desc->type != EO_CLASS_TYPE_MIXIN)
603 /* Get the first one after the extension. */
604 Eina_List *extn_list = eina_list_next(eina_list_data_get(itr));
606 /* If we found the extension again. */
607 if (eina_list_data_find_list(extn_list, extn))
610 ERR("Cannot create a consistent method resolution order for class '%s' because of '%s'.", klass->desc->name, extn->desc->name);
614 itr = eina_list_next(itr);
619 mro = _eo_class_mro_add(mro, klass->parent);
625 _eo_class_mro_init(Eo_Class *klass)
627 Eina_List *mro = NULL;
629 DBG("Started creating MRO for class '%s'", klass->desc->name);
630 mro = _eo_class_mro_add(mro, klass);
635 /* Remove duplicates and make them the right order. */
637 Eina_List *itr1, *itr2, *itr2n;
639 itr1 = eina_list_last(mro);
642 itr2 = eina_list_prev(itr1);
646 itr2n = eina_list_prev(itr2);
648 if (eina_list_data_get(itr1) == eina_list_data_get(itr2))
650 mro = eina_list_remove_list(mro, itr2);
656 itr1 = eina_list_prev(itr1);
660 /* Copy the mro and free the list. */
662 const Eo_Class *kls_itr;
663 const Eo_Class **mro_itr;
664 klass->mro = calloc(sizeof(*klass->mro), eina_list_count(mro) + 1);
666 mro_itr = klass->mro;
668 EINA_LIST_FREE(mro, kls_itr)
670 *(mro_itr++) = kls_itr;
672 DBG("Added '%s' to MRO", kls_itr->desc->name);
677 DBG("Finished creating MRO for class '%s'", klass->desc->name);
683 _eo_class_constructor(Eo_Class *klass)
685 if (klass->constructed)
688 klass->constructed = EINA_TRUE;
690 if (klass->desc->class_constructor)
691 klass->desc->class_constructor(klass);
695 eo_class_funcs_set(Eo_Class *klass, const Eo_Op_Func_Description *func_descs)
697 EO_MAGIC_RETURN(klass, EO_CLASS_EINA_MAGIC);
699 const Eo_Op_Func_Description *itr;
703 for ( ; itr->op_type != EO_OP_TYPE_INVALID ; itr++)
705 const Eo_Op_Description *op_desc = _eo_op_id_desc_get(itr->op);
707 if (EINA_UNLIKELY(!op_desc || (itr->op == EO_NOOP)))
709 ERR("Setting implementation for non-existent op %x for class '%s'. Func index: %d", itr->op, klass->desc->name, itr - func_descs);
711 else if (EINA_LIKELY(itr->op_type == op_desc->op_type))
713 _dich_func_set(klass, itr->op, itr->func);
717 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",
719 (op_desc) ? op_desc->op_type : EO_OP_TYPE_REGULAR,
720 (op_desc) ? op_desc->name : NULL,
729 eo_class_free(Eo_Class *klass)
731 if (klass->constructed)
733 if (klass->desc->class_destructor)
734 klass->desc->class_destructor(klass);
736 _dich_func_clean_all(klass);
739 free(klass->extensions);
744 if (klass->extn_data_off)
745 free(klass->extn_data_off);
752 _eo_class_check_op_descs(const Eo_Class *klass, Eo_Class_Id id)
754 const Eo_Class_Description *desc = klass->desc;
755 const Eo_Op_Description *itr;
758 if (desc->ops.count > 0)
760 if (((id == 0) || (id > EO_STATIC_IDS_LAST)) && !desc->ops.base_op_id)
762 ERR("Class '%s' has a non-zero ops count, but base_id is NULL.",
767 if (!desc->ops.descs)
769 ERR("Class '%s' has a non-zero ops count, but there are no descs.",
775 itr = desc->ops.descs;
776 for (i = 0 ; i < desc->ops.count ; i++, itr++)
778 if (itr->sub_op != i)
782 ERR("Wrong order in Ops description for class '%s'. Expected %x and got %x", desc->name, i, itr->sub_op);
786 ERR("Found too few Ops description for class '%s'. Expected %x descriptions, but found %x.", desc->name, desc->ops.count, i);
792 if (itr && itr->name)
794 ERR("Found extra Ops description for class '%s'. Expected %d descriptions, but found more.", desc->name, desc->ops.count);
801 /* Not really called, just used for the ptr... */
803 _eo_class_isa_func(Eo *obj EINA_UNUSED, void *class_data EINA_UNUSED, va_list *list EINA_UNUSED)
808 EAPI const Eo_Class *
809 eo_class_new(const Eo_Class_Description *desc, Eo_Class_Id id, const Eo_Class *parent, ...)
814 if (parent && !EINA_MAGIC_CHECK(parent, EO_CLASS_EINA_MAGIC))
816 EINA_MAGIC_FAIL(parent, EO_CLASS_EINA_MAGIC);
820 if (id > EO_STATIC_IDS_LAST)
822 ERR("Tried creating a class with the static id %d while the maximum static id is %d. Aborting.", id, EO_STATIC_IDS_LAST);
826 va_start(p_list, parent);
828 EINA_SAFETY_ON_NULL_RETURN_VAL(desc, NULL);
829 EINA_SAFETY_ON_NULL_RETURN_VAL(desc->name, NULL);
831 /* Check restrictions on Interface types. */
832 if (desc->type == EO_CLASS_TYPE_INTERFACE)
834 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->data_size, NULL);
837 klass = calloc(1, sizeof(Eo_Class));
838 klass->parent = parent;
840 /* Handle class extensions */
842 Eina_List *extn_list = NULL;
843 const Eo_Class *extn = NULL;
844 const Eo_Class **extn_itr = NULL;
846 extn = va_arg(p_list, Eo_Class *);
849 switch (extn->desc->type)
851 case EO_CLASS_TYPE_REGULAR:
852 case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
853 case EO_CLASS_TYPE_INTERFACE:
854 case EO_CLASS_TYPE_MIXIN:
855 extn_list = eina_list_append(extn_list, extn);
859 extn = va_arg(p_list, Eo_Class *);
862 klass->extensions = calloc(sizeof(*klass->extensions),
863 eina_list_count(extn_list) + 1);
865 extn_itr = klass->extensions;
866 EINA_LIST_FREE(extn_list, extn)
868 *(extn_itr++) = extn;
874 /* Handle the inheritance */
877 /* Verify the inheritance is allowed. */
878 switch (klass->desc->type)
880 case EO_CLASS_TYPE_REGULAR:
881 case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
882 if ((klass->parent->desc->type != EO_CLASS_TYPE_REGULAR) &&
883 (klass->parent->desc->type != EO_CLASS_TYPE_REGULAR_NO_INSTANT))
885 ERR("Regular classes ('%s') aren't allowed to inherit from non-regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
889 case EO_CLASS_TYPE_INTERFACE:
890 case EO_CLASS_TYPE_MIXIN:
891 if ((klass->parent->desc->type != EO_CLASS_TYPE_INTERFACE) &&
892 (klass->parent->desc->type != EO_CLASS_TYPE_MIXIN))
894 ERR("Non-regular classes ('%s') aren't allowed to inherit from regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
901 /* Update the current offset. */
902 /* FIXME: Make sure this alignment is enough. */
903 klass->data_offset = klass->parent->data_offset +
904 EO_ALIGN_SIZE(klass->parent->desc->data_size);
907 if (!_eo_class_check_op_descs(klass, id))
912 if (!_eo_class_mro_init(klass))
917 /* create MIXIN offset table. */
919 const Eo_Class **mro_itr = klass->mro;
920 Eo_Extension_Data_Offset *extn_data_itr;
922 size_t extn_data_off = klass->data_offset +
923 EO_ALIGN_SIZE(klass->desc->data_size);
925 /* FIXME: Make faster... */
928 if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
929 ((*mro_itr)->desc->data_size > 0))
936 klass->extn_data_off = calloc(extn_num + 1,
937 sizeof(*klass->extn_data_off));
939 extn_data_itr = klass->extn_data_off;
940 mro_itr = klass->mro;
943 if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
944 ((*mro_itr)->desc->data_size > 0))
946 extn_data_itr->klass = *mro_itr;
947 extn_data_itr->offset = extn_data_off;
949 extn_data_off += EO_ALIGN_SIZE(extn_data_itr->klass->desc->data_size);
955 klass->extn_data_size = extn_data_off;
958 eina_lock_take(&_eo_class_creation_lock);
962 klass->class_id = ++_eo_classes_last_id;
967 if (_eo_classes && _eo_classes[id - 1])
969 ERR("A class with id %d was already defined (%s). Aborting.", id,
970 _eo_classes[id - 1]->desc->name);
971 eina_lock_release(&_eo_class_creation_lock);
975 klass->class_id = id;
980 /* FIXME: Handle errors. */
981 size_t arrsize = _eo_classes_last_id * sizeof(*_eo_classes);
983 tmp = realloc(_eo_classes, arrsize);
985 /* If it's the first allocation, memset. */
987 memset(tmp, 0, arrsize);
990 _eo_classes[klass->class_id - 1] = klass;
992 eina_lock_release(&_eo_class_creation_lock);
994 EINA_MAGIC_SET(klass, EO_CLASS_EINA_MAGIC);
996 _eo_class_base_op_init(klass);
997 /* Flatten the function array */
999 const Eo_Class **mro_itr = klass->mro;
1000 for ( ; *mro_itr ; mro_itr++)
1003 /* Skip ourselves. */
1004 for ( mro_itr-- ; mro_itr > klass->mro ; mro_itr--)
1006 _dich_copy_all(klass, *mro_itr);
1010 /* Mark which classes we implement */
1012 const Eo_Class **extn_itr;
1014 for (extn_itr = klass->extensions ; *extn_itr ; extn_itr++)
1016 const Eo_Class *extn = *extn_itr;
1017 /* Set it in the dich. */
1018 _dich_func_set(klass, extn->base_id +
1019 extn->desc->ops.count, _eo_class_isa_func);
1022 _dich_func_set(klass, klass->base_id + klass->desc->ops.count,
1023 _eo_class_isa_func);
1027 _dich_func_set(klass,
1028 klass->parent->base_id + klass->parent->desc->ops.count,
1029 _eo_class_isa_func);
1033 klass->mro_itr.kls_itr = klass->mro;
1035 _eo_class_constructor(klass);
1042 eo_class_free(klass);
1047 eo_isa(const Eo *obj, const Eo_Class *klass)
1049 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1050 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, EINA_FALSE);
1051 const op_type_funcs *func = _dich_func_get(obj->klass,
1052 klass->base_id + klass->desc->ops.count);
1054 /* Currently implemented by reusing the LAST op id. Just marking it with
1055 * _eo_class_isa_func. */
1056 return (func && (func->func == _eo_class_isa_func));
1060 eo_parent_set(Eo *obj, const Eo *parent)
1062 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1064 EO_MAGIC_RETURN_VAL(parent, EO_EINA_MAGIC, EINA_FALSE);
1066 if (obj->parent == parent)
1071 if (eo_composite_is(obj))
1073 eo_composite_detach(obj, obj->parent);
1078 obj->parent->children =
1079 eina_inlist_remove(obj->parent->children, EINA_INLIST_GET(obj));
1080 eo_xunref(obj, obj->parent);
1083 obj->parent = (Eo *) parent;
1086 obj->parent->children =
1087 eina_inlist_append(obj->parent->children, EINA_INLIST_GET(obj));
1088 eo_xref(obj, obj->parent);
1097 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);
1116 obj->mro_itr.kls_itr = obj->klass->mro;
1118 eo_parent_set(obj, parent);
1120 _eo_condtor_reset(obj);
1123 do_err = !eo_do(obj, eo_constructor());
1125 if (EINA_UNLIKELY(do_err))
1127 ERR("Object of class '%s' - One of the object constructors have failed.", klass->desc->name);
1131 if (!obj->condtor_done)
1133 ERR("Object of class '%s' - Not all of the object constructors have been executed.", klass->desc->name);
1141 /* Unref twice, once for the ref above, and once for the basic object ref. */
1156 eo_xref_internal(Eo *obj, const Eo *ref_obj, const char *file, int line)
1158 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, obj);
1163 Eo_Xref_Node *xref = calloc(1, sizeof(*xref));
1164 xref->ref_obj = ref_obj;
1168 obj->xrefs = eina_inlist_prepend(obj->xrefs, EINA_INLIST_GET(xref));
1179 eo_xunref(Eo *obj, const Eo *ref_obj)
1181 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1183 Eo_Xref_Node *xref = NULL;
1184 EINA_INLIST_FOREACH(obj->xrefs, xref)
1186 if (xref->ref_obj == ref_obj)
1192 obj->xrefs = eina_inlist_remove(obj->xrefs, EINA_INLIST_GET(xref));
1197 ERR("ref_obj (%p) does not reference obj (%p). Aborting unref.", ref_obj, obj);
1214 eo_ref(const Eo *_obj)
1216 Eo *obj = (Eo *) _obj;
1217 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, obj);
1219 return _eo_ref(obj);
1223 _eo_del_internal(Eo *obj)
1226 /* We need that for the event callbacks that may ref/unref. */
1229 eo_do(obj, eo_event_callback_call(EO_EV_DEL, NULL, NULL));
1231 const Eo_Class *klass = eo_class_get(obj);
1233 _eo_condtor_reset(obj);
1235 do_err = eo_do(obj, eo_destructor());
1236 if (EINA_UNLIKELY(!do_err))
1238 ERR("Object of class '%s' - One of the object destructors have failed.", klass->desc->name);
1241 if (!obj->condtor_done)
1243 ERR("Object of class '%s' - Not all of the object destructors have been executed.", klass->desc->name);
1245 /*FIXME: add eo_class_unref(klass) ? - just to clear the caches. */
1248 Eina_List *itr, *itr_n;
1250 EINA_LIST_FOREACH_SAFE(obj->composite_objects, itr, itr_n, emb_obj)
1252 eo_composite_detach(emb_obj, obj);
1256 while (obj->children)
1258 eo_parent_set(EINA_INLIST_CONTAINER_GET(obj->children, Eo), NULL);
1261 obj->del = EINA_TRUE;
1268 EINA_MAGIC_SET(obj, EO_FREED_EINA_MAGIC);
1275 if (--(obj->refcount) == 0)
1279 ERR("Object %p already deleted.", obj);
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)->do_error = EINA_TRUE;
1346 _eo_condtor_done(Eo *obj)
1348 if (obj->condtor_done)
1350 ERR("Object %p is already constructed at this point.", obj);
1354 obj->condtor_done = EINA_TRUE;
1358 _eo_condtor_reset(Eo *obj)
1360 obj->condtor_done = EINA_FALSE;
1363 static inline void *
1364 _eo_data_get(const Eo *obj, const Eo_Class *klass)
1366 if (EINA_LIKELY(klass->desc->data_size > 0))
1368 if (EINA_UNLIKELY(klass->desc->type == EO_CLASS_TYPE_MIXIN))
1370 Eo_Extension_Data_Offset *doff_itr =
1371 eo_class_get(obj)->extn_data_off;
1376 while (doff_itr->klass)
1378 if (doff_itr->klass == klass)
1379 return ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj)) +
1386 return ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj)) +
1395 eo_data_get(const Eo *obj, const Eo_Class *klass)
1398 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1399 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, NULL);
1402 if (!_eo_class_mro_has(obj->klass, klass))
1404 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);
1409 ret = _eo_data_get(obj, klass);
1412 if (!ret && (klass->desc->data_size == 0))
1414 ERR("Tried getting data of class '%s', but it has none..", klass->desc->name);
1424 const char *log_dom = "eo";
1425 if (_eo_init_count++ > 0)
1431 _eo_classes_last_id = EO_STATIC_IDS_LAST;
1432 _eo_ops_last_id = EO_OP_IDS_FIRST;
1433 _eo_log_dom = eina_log_domain_register(log_dom, EINA_COLOR_LIGHTBLUE);
1434 if (_eo_log_dom < 0)
1436 EINA_LOG_ERR("Could not register log domain: %s", log_dom);
1440 if (!eina_lock_new(&_eo_class_creation_lock))
1442 EINA_LOG_ERR("Could not init lock.");
1446 eina_magic_string_static_set(EO_EINA_MAGIC, EO_EINA_MAGIC_STR);
1447 eina_magic_string_static_set(EO_FREED_EINA_MAGIC,
1448 EO_FREED_EINA_MAGIC_STR);
1449 eina_magic_string_static_set(EO_CLASS_EINA_MAGIC,
1450 EO_CLASS_EINA_MAGIC_STR);
1453 /* Call it just for coverage purposes. Ugly I know, but I like it better than
1454 * casting everywhere else. */
1455 _eo_class_isa_func(NULL, NULL, NULL);
1465 Eo_Class **cls_itr = _eo_classes;
1467 if (--_eo_init_count > 0)
1470 for (i = 0 ; i < _eo_classes_last_id ; i++, cls_itr++)
1473 eo_class_free(*cls_itr);
1479 eina_lock_free(&_eo_class_creation_lock);
1481 eina_log_domain_unregister(_eo_log_dom);
1489 eo_composite_attach(Eo *comp_obj, Eo *parent)
1491 EO_MAGIC_RETURN(comp_obj, EO_EINA_MAGIC);
1492 EO_MAGIC_RETURN(parent, EO_EINA_MAGIC);
1494 comp_obj->composite = EINA_TRUE;
1495 eo_parent_set(comp_obj, parent);
1496 parent->composite_objects = eina_list_prepend(parent->composite_objects, comp_obj);
1500 eo_composite_detach(Eo *comp_obj, Eo *parent)
1502 EO_MAGIC_RETURN(comp_obj, EO_EINA_MAGIC);
1503 EO_MAGIC_RETURN(parent, EO_EINA_MAGIC);
1505 comp_obj->composite = EINA_FALSE;
1506 parent->composite_objects = eina_list_remove(parent->composite_objects, comp_obj);
1507 eo_parent_set(comp_obj, NULL);
1511 eo_composite_is(const Eo *comp_obj)
1513 if (!EINA_MAGIC_CHECK(comp_obj, EO_EINA_MAGIC))
1515 EINA_MAGIC_FAIL(comp_obj, EO_EINA_MAGIC);
1519 return comp_obj->composite;
1523 eo_manual_free_set(Eo *obj, Eina_Bool manual_free)
1525 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1526 obj->manual_free = manual_free;
1530 eo_manual_free(Eo *obj)
1532 EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1534 if (EINA_FALSE == obj->manual_free)
1536 ERR("Tried to manually free the object %p while the option has not been set; see eo_manual_free_set for more information.", obj);
1542 ERR("Tried deleting the object %p while still referenced(%d).", obj, eo_ref_get(obj));