4 #include "eo_private.h"
8 /* The last id that should be reserved for statically allocated classes. */
9 #define EO_CLASS_IDS_FIRST 1
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);
25 static const Eo_Class *_eo_op_class_get(Eo_Op op);
26 static const Eo_Op_Description *_eo_op_id_desc_get(Eo_Op op);
37 Eina_Inlist *children;
38 const Eo_Class *klass;
44 Eina_List *composite_objects;
49 Eina_Bool condtor_done:1;
51 Eina_Bool composite:1;
53 Eina_Bool manual_free:1;
58 /* How we search and store the implementations in classes. */
59 #define DICH_CHAIN_LAST_BITS 5
60 #define DICH_CHAIN_LAST_SIZE (1 << DICH_CHAIN_LAST_BITS)
61 #define DICH_CHAIN1(x) ((x) / DICH_CHAIN_LAST_SIZE)
62 #define DICH_CHAIN_LAST(x) ((x) % DICH_CHAIN_LAST_SIZE)
64 #define OP_CLASS_OFFSET_GET(x) (((x) >> EO_OP_CLASS_OFFSET) & 0xffff)
66 #define ID_CLASS_GET(id) ({ \
67 (Eo_Class *) ((id <= _eo_classes_last_id) && (id > 0)) ? \
68 (_eo_classes[id - 1]) : NULL; \
71 #define EO_ALIGN_SIZE(size) \
72 ((size) + (sizeof(void *) - ((size) % sizeof(void *))))
74 typedef struct _Dich_Chain1 Dich_Chain1;
89 const Eo_Class *klass;
91 } Eo_Extension_Data_Offset;
97 const Eo_Class *parent;
98 const Eo_Class_Description *desc;
99 Dich_Chain1 *chain; /**< The size is chain size */
103 const Eo_Class **extensions;
105 Eo_Extension_Data_Offset *extn_data_off;
106 size_t extn_data_size;
108 const Eo_Class **mro;
111 size_t data_offset; /* < Offset of the data within object data. */
113 Eina_Bool constructed : 1;
117 _dich_chain_alloc(Dich_Chain1 *chain1)
121 chain1->funcs = calloc(DICH_CHAIN_LAST_SIZE, sizeof(*(chain1->funcs)));
126 _dich_copy_all(Eo_Class *dst, const Eo_Class *src)
129 const Dich_Chain1 *sc1 = src->chain;
130 Dich_Chain1 *dc1 = dst->chain;
131 for (i = 0 ; i < src->chain_size ; i++, sc1++, dc1++)
137 _dich_chain_alloc(dc1);
139 const op_type_funcs *sf = sc1->funcs;
140 op_type_funcs *df = dc1->funcs;
141 for (j = 0 ; j < DICH_CHAIN_LAST_SIZE ; j++, df++, sf++)
145 memcpy(df, sf, sizeof(*df));
152 static inline const op_type_funcs *
153 _dich_func_get(const Eo_Class *klass, Eo_Op op)
155 size_t idx1 = DICH_CHAIN1(op);
156 if (EINA_UNLIKELY(idx1 >= klass->chain_size))
158 Dich_Chain1 *chain1 = &klass->chain[idx1];
159 if (EINA_UNLIKELY(!chain1->funcs))
161 return &chain1->funcs[DICH_CHAIN_LAST(op)];
165 _dich_func_set(Eo_Class *klass, Eo_Op op, eo_op_func_type func)
167 size_t idx1 = DICH_CHAIN1(op);
168 Dich_Chain1 *chain1 = &klass->chain[idx1];
169 _dich_chain_alloc(chain1);
170 if (chain1->funcs[DICH_CHAIN_LAST(op)].src == klass)
172 const Eo_Class *op_kls = _eo_op_class_get(op);
173 const Eo_Op_Description *op_desc = _eo_op_id_desc_get(op);
174 ERR("Already set function for op %x (%s:%s). Overriding with func %p",
175 op, op_kls->desc->name, op_desc->name, func);
178 chain1->funcs[DICH_CHAIN_LAST(op)].func = func;
179 chain1->funcs[DICH_CHAIN_LAST(op)].src = klass;
183 _dich_func_clean_all(Eo_Class *klass)
186 Dich_Chain1 *chain1 = klass->chain;
188 for (i = 0 ; i < klass->chain_size ; i++, chain1++)
199 static const Eo_Op_Description noop_desc =
200 EO_OP_DESCRIPTION(EO_NOOP, "No operation.");
202 static const Eo_Class *
203 _eo_op_class_get(Eo_Op op)
205 /* FIXME: Make it fast. */
206 const Eo_Class *klass = NULL;
207 Eo_Class **itr = _eo_classes;
209 for (i = 0 ; i < _eo_classes_last_id ; i++, itr++)
211 if (*itr && ((*itr)->base_id <= op) &&
212 (op <= (*itr)->base_id + (*itr)->desc->ops.count))
222 static const Eo_Op_Description *
223 _eo_op_id_desc_get(Eo_Op op)
225 const Eo_Class *klass;
230 klass = _eo_op_class_get(op);
234 Eo_Op sub_id = op - klass->base_id;
235 if (sub_id < klass->desc->ops.count)
236 return klass->desc->ops.descs + sub_id;
243 _eo_op_id_name_get(Eo_Op op)
245 const Eo_Op_Description *desc = _eo_op_id_desc_get(op);
246 return (desc) ? desc->name : NULL;
250 _eo_kls_itr_init(const Eo_Class *obj_klass, Eo_Kls_Itr *cur, Eo_Kls_Itr *prev_state)
252 memcpy(prev_state, cur, sizeof(*cur));
253 cur->kls = *obj_klass->mro;
257 _eo_kls_itr_end(Eo_Kls_Itr *cur, Eo_Kls_Itr *prev_state)
259 memcpy(cur, prev_state, sizeof(*cur));
262 static inline const Eo_Class *
263 _eo_kls_itr_get(Eo_Kls_Itr *cur)
269 _eo_kls_itr_set(Eo_Kls_Itr *cur, const Eo_Class *kls)
274 static inline const Eo_Class *
275 _eo_kls_itr_next(const Eo_Class *orig_kls, Eo_Kls_Itr *cur, Eo_Kls_Itr *prev_state, Eo_Op op)
277 const Eo_Class **kls_itr = NULL;
278 memcpy(prev_state, cur, sizeof(*cur));
280 /* Find the kls itr. */
281 kls_itr = orig_kls->mro;
282 while (*kls_itr && (*kls_itr != cur->kls))
290 const op_type_funcs *fsrc = _dich_func_get(*kls_itr, op);
291 if (!fsrc || !fsrc->func)
296 cur->kls = fsrc->src;
305 static inline const op_type_funcs *
306 _eo_kls_itr_func_get(Eo_Kls_Itr *mro_itr, Eo_Op op)
308 const Eo_Class *klass = _eo_kls_itr_get(mro_itr);
311 const op_type_funcs *func = _dich_func_get(klass, op);
313 if (func && func->func)
315 _eo_kls_itr_set(mro_itr, func->src);
320 _eo_kls_itr_set(mro_itr, NULL);
324 #define _EO_OP_ERR_NO_OP_PRINT(op, klass) \
327 const Eo_Class *op_klass = _eo_op_class_get(op); \
328 const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL; \
329 ERR("Can't find func for op %x (%s:%s) for class '%s'. Aborting.", \
330 op, _dom_name, _eo_op_id_name_get(op), \
331 (klass) ? klass->desc->name : NULL); \
336 _eo_op_internal(Eo *obj, Eo_Op_Type op_type, Eo_Op op, va_list *p_list)
339 const Eo_Op_Description *op_desc = _eo_op_id_desc_get(op);
343 if (op_desc->op_type == EO_OP_TYPE_CLASS)
345 ERR("Tried calling a class op '%s' (%x) from a non-class context.", (op_desc) ? op_desc->name : NULL, op);
352 const op_type_funcs *func =
353 _eo_kls_itr_func_get(&obj->mro_itr, op);
356 func->func(obj, _eo_data_get(obj, func->src), p_list);
361 /* Try composite objects */
365 EINA_LIST_FOREACH(obj->composite_objects, itr, emb_obj)
367 /* FIXME: Clean this up a bit. */
368 Eo_Kls_Itr prev_state;
369 _eo_kls_itr_init(emb_obj->klass, &emb_obj->mro_itr, &prev_state);
370 if (_eo_op_internal(emb_obj, op_type, op, p_list))
372 _eo_kls_itr_end(&emb_obj->mro_itr, &prev_state);
375 _eo_kls_itr_end(&emb_obj->mro_itr, &prev_state);
382 eo_do_internal(Eo *obj, Eo_Op_Type op_type, ...)
384 Eina_Bool prev_error;
385 Eina_Bool ret = EINA_TRUE;
387 Eo_Kls_Itr prev_state;
390 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
392 prev_error = obj->do_error;
395 va_start(p_list, op_type);
397 op = va_arg(p_list, Eo_Op);
400 _eo_kls_itr_init(obj->klass, &obj->mro_itr, &prev_state);
401 if (!_eo_op_internal(obj, op_type, op, &p_list))
403 _EO_OP_ERR_NO_OP_PRINT(op, obj->klass);
405 _eo_kls_itr_end(&obj->mro_itr, &prev_state);
408 op = va_arg(p_list, Eo_Op);
409 _eo_kls_itr_end(&obj->mro_itr, &prev_state);
419 obj->do_error = prev_error;
425 eo_do_super_internal(Eo *obj, Eo_Op_Type op_type, Eo_Op op, ...)
427 const Eo_Class *nklass;
428 Eina_Bool ret = EINA_TRUE;
430 Eo_Kls_Itr prev_state;
431 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
433 /* Advance the kls itr. */
434 nklass = _eo_kls_itr_next(obj->klass, &obj->mro_itr, &prev_state, op);
436 va_start(p_list, op);
437 if (!_eo_op_internal(obj, op_type, op, &p_list))
439 _EO_OP_ERR_NO_OP_PRINT(op, nklass);
447 _eo_kls_itr_end(&obj->mro_itr, &prev_state);
452 _eo_class_op_internal(Eo_Class *klass, Eo_Op op, va_list *p_list)
455 const Eo_Op_Description *op_desc = _eo_op_id_desc_get(op);
459 if (op_desc->op_type != EO_OP_TYPE_CLASS)
461 ERR("Tried calling an instance op '%s' (%x) from a class context.", (op_desc) ? op_desc->name : NULL, op);
468 const op_type_funcs *func =
469 _eo_kls_itr_func_get(&klass->mro_itr, op);
472 ((eo_op_func_type_class) func->func)(klass, p_list);
481 eo_class_do_internal(const Eo_Class *klass, ...)
483 Eina_Bool ret = EINA_TRUE;
485 Eo_Kls_Itr prev_state;
488 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, EINA_FALSE);
490 va_start(p_list, klass);
492 op = va_arg(p_list, Eo_Op);
495 _eo_kls_itr_init(klass, &((Eo_Class *) klass)->mro_itr, &prev_state);
496 if (!_eo_class_op_internal((Eo_Class *) klass, op, &p_list))
498 _EO_OP_ERR_NO_OP_PRINT(op, klass);
500 _eo_kls_itr_end(&((Eo_Class *) klass)->mro_itr, &prev_state);
503 _eo_kls_itr_end(&((Eo_Class *) klass)->mro_itr, &prev_state);
504 op = va_arg(p_list, Eo_Op);
513 eo_class_do_super_internal(const Eo_Class *klass, Eo_Op op, ...)
515 const Eo_Class *nklass;
516 Eina_Bool ret = EINA_TRUE;
518 Eo_Kls_Itr prev_state;
519 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, EINA_FALSE);
521 /* Advance the kls itr. */
522 nklass = _eo_kls_itr_next(klass, &((Eo_Class *) klass)->mro_itr, &prev_state, op);
524 va_start(p_list, op);
525 if (!_eo_class_op_internal((Eo_Class *) klass, op, &p_list))
527 _EO_OP_ERR_NO_OP_PRINT(op, nklass);
532 _eo_kls_itr_end(&((Eo_Class *) klass)->mro_itr, &prev_state);
536 EAPI const Eo_Class *
537 eo_class_get(const Eo *obj)
539 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
545 eo_class_name_get(const Eo_Class *klass)
547 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, NULL);
549 return klass->desc->name;
553 _eo_class_base_op_init(Eo_Class *klass)
555 const Eo_Class_Description *desc = klass->desc;
557 klass->base_id = _eo_ops_last_id;
559 if (desc && desc->ops.base_op_id)
560 *(desc->ops.base_op_id) = klass->base_id;
562 _eo_ops_last_id += desc->ops.count + 1;
564 klass->chain_size = DICH_CHAIN1(_eo_ops_last_id) + 1;
565 klass->chain = calloc(klass->chain_size, sizeof(*klass->chain));
570 _eo_class_mro_has(const Eo_Class *klass, const Eo_Class *find)
572 const Eo_Class **itr;
573 for (itr = klass->mro ; *itr ; itr++)
585 _eo_class_mro_add(Eina_List *mro, const Eo_Class *klass)
587 Eina_List *extn_pos = NULL;
588 Eina_Bool check_consistency = !mro;
592 mro = eina_list_append(mro, klass);
594 /* ONLY ADD MIXINS! */
596 /* Recursively add extenions. */
598 const Eo_Class **extn_itr;
600 for (extn_itr = klass->extensions ; *extn_itr ; extn_itr++)
602 const Eo_Class *extn = *extn_itr;
603 if (extn->desc->type != EO_CLASS_TYPE_MIXIN)
606 mro = _eo_class_mro_add(mro, extn);
607 /* Not possible: if (!mro) return NULL; */
609 if (check_consistency)
611 extn_pos = eina_list_append(extn_pos, eina_list_last(mro));
616 /* Check if we can create a consistent mro. We only do it for the class
617 * we are working on (i.e no parents). */
618 if (check_consistency)
620 const Eo_Class **extn_itr;
622 Eina_List *itr = extn_pos;
623 for (extn_itr = klass->extensions ; *extn_itr ; extn_itr++)
625 const Eo_Class *extn = *extn_itr;
626 if (extn->desc->type != EO_CLASS_TYPE_MIXIN)
629 /* Get the first one after the extension. */
630 Eina_List *extn_list = eina_list_next(eina_list_data_get(itr));
632 /* If we found the extension again. */
633 if (eina_list_data_find_list(extn_list, extn))
636 ERR("Cannot create a consistent method resolution order for class '%s' because of '%s'.", klass->desc->name, extn->desc->name);
640 itr = eina_list_next(itr);
645 mro = _eo_class_mro_add(mro, klass->parent);
651 _eo_class_mro_init(Eo_Class *klass)
653 Eina_List *mro = NULL;
655 DBG("Started creating MRO for class '%s'", klass->desc->name);
656 mro = _eo_class_mro_add(mro, klass);
661 /* Remove duplicates and make them the right order. */
663 Eina_List *itr1, *itr2, *itr2n;
665 itr1 = eina_list_last(mro);
668 itr2 = eina_list_prev(itr1);
672 itr2n = eina_list_prev(itr2);
674 if (eina_list_data_get(itr1) == eina_list_data_get(itr2))
676 mro = eina_list_remove_list(mro, itr2);
682 itr1 = eina_list_prev(itr1);
686 /* Copy the mro and free the list. */
688 const Eo_Class *kls_itr;
689 const Eo_Class **mro_itr;
690 klass->mro = calloc(sizeof(*klass->mro), eina_list_count(mro) + 1);
692 mro_itr = klass->mro;
694 EINA_LIST_FREE(mro, kls_itr)
696 *(mro_itr++) = kls_itr;
698 DBG("Added '%s' to MRO", kls_itr->desc->name);
703 DBG("Finished creating MRO for class '%s'", klass->desc->name);
709 _eo_class_constructor(Eo_Class *klass)
711 if (klass->constructed)
714 klass->constructed = EINA_TRUE;
716 if (klass->desc->class_constructor)
717 klass->desc->class_constructor(klass);
721 eo_class_funcs_set(Eo_Class *klass, const Eo_Op_Func_Description *func_descs)
723 EO_MAGIC_RETURN(klass, EO_CLASS_EINA_MAGIC);
725 const Eo_Op_Func_Description *itr;
729 for ( ; itr->op_type != EO_OP_TYPE_INVALID ; itr++)
731 const Eo_Op_Description *op_desc = _eo_op_id_desc_get(itr->op);
733 if (EINA_UNLIKELY(!op_desc || (itr->op == EO_NOOP)))
735 ERR("Setting implementation for non-existent op %x for class '%s'. Func index: %d", itr->op, klass->desc->name, itr - func_descs);
737 else if (EINA_LIKELY(itr->op_type == op_desc->op_type))
739 _dich_func_set(klass, itr->op, itr->func);
743 ERR("Set function's op type (%x) is different than the one in the op description (%d) for op '%s:%s'. Func index: %d",
745 (op_desc) ? op_desc->op_type : EO_OP_TYPE_REGULAR,
747 (op_desc) ? op_desc->name : NULL,
755 eo_class_free(Eo_Class *klass)
757 if (klass->constructed)
759 if (klass->desc->class_destructor)
760 klass->desc->class_destructor(klass);
762 _dich_func_clean_all(klass);
765 free(klass->extensions);
770 if (klass->extn_data_off)
771 free(klass->extn_data_off);
778 _eo_class_check_op_descs(const Eo_Class *klass)
780 const Eo_Class_Description *desc = klass->desc;
781 const Eo_Op_Description *itr;
784 if (desc->ops.count > 0)
786 if (!desc->ops.base_op_id)
788 ERR("Class '%s' has a non-zero ops count, but base_id is NULL.",
793 if (!desc->ops.descs)
795 ERR("Class '%s' has a non-zero ops count, but there are no descs.",
801 itr = desc->ops.descs;
802 for (i = 0 ; i < desc->ops.count ; i++, itr++)
804 if (itr->sub_op != i)
808 ERR("Wrong order in Ops description for class '%s'. Expected %x and got %x", desc->name, i, itr->sub_op);
812 ERR("Found too few Ops description for class '%s'. Expected %x descriptions, but found %x.", desc->name, desc->ops.count, i);
818 if (itr && itr->name)
820 ERR("Found extra Ops description for class '%s'. Expected %d descriptions, but found more.", desc->name, desc->ops.count);
827 /* Not really called, just used for the ptr... */
829 _eo_class_isa_func(Eo *obj EINA_UNUSED, void *class_data EINA_UNUSED, va_list *list EINA_UNUSED)
834 EAPI const Eo_Class *
835 eo_class_new(const Eo_Class_Description *desc, const Eo_Class *parent, ...)
840 if (parent && !EINA_MAGIC_CHECK(parent, EO_CLASS_EINA_MAGIC))
842 EINA_MAGIC_FAIL(parent, EO_CLASS_EINA_MAGIC);
846 va_start(p_list, parent);
848 EINA_SAFETY_ON_NULL_RETURN_VAL(desc, NULL);
849 EINA_SAFETY_ON_NULL_RETURN_VAL(desc->name, NULL);
851 /* Check restrictions on Interface types. */
852 if (desc->type == EO_CLASS_TYPE_INTERFACE)
854 EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->data_size, NULL);
857 klass = calloc(1, sizeof(Eo_Class));
858 klass->parent = parent;
860 /* Handle class extensions */
862 Eina_List *extn_list = NULL;
863 const Eo_Class *extn = NULL;
864 const Eo_Class **extn_itr = NULL;
866 extn = va_arg(p_list, Eo_Class *);
869 switch (extn->desc->type)
871 case EO_CLASS_TYPE_REGULAR:
872 case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
873 case EO_CLASS_TYPE_INTERFACE:
874 case EO_CLASS_TYPE_MIXIN:
875 extn_list = eina_list_append(extn_list, extn);
879 extn = va_arg(p_list, Eo_Class *);
882 klass->extensions = calloc(sizeof(*klass->extensions),
883 eina_list_count(extn_list) + 1);
885 extn_itr = klass->extensions;
886 EINA_LIST_FREE(extn_list, extn)
888 *(extn_itr++) = extn;
894 /* Handle the inheritance */
897 /* Verify the inheritance is allowed. */
898 switch (klass->desc->type)
900 case EO_CLASS_TYPE_REGULAR:
901 case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
902 if ((klass->parent->desc->type != EO_CLASS_TYPE_REGULAR) &&
903 (klass->parent->desc->type != EO_CLASS_TYPE_REGULAR_NO_INSTANT))
905 ERR("Regular classes ('%s') aren't allowed to inherit from non-regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
909 case EO_CLASS_TYPE_INTERFACE:
910 case EO_CLASS_TYPE_MIXIN:
911 if ((klass->parent->desc->type != EO_CLASS_TYPE_INTERFACE) &&
912 (klass->parent->desc->type != EO_CLASS_TYPE_MIXIN))
914 ERR("Non-regular classes ('%s') aren't allowed to inherit from regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
921 /* Update the current offset. */
922 /* FIXME: Make sure this alignment is enough. */
923 klass->data_offset = klass->parent->data_offset +
924 EO_ALIGN_SIZE(klass->parent->desc->data_size);
927 if (!_eo_class_check_op_descs(klass))
932 if (!_eo_class_mro_init(klass))
937 /* create MIXIN offset table. */
939 const Eo_Class **mro_itr = klass->mro;
940 Eo_Extension_Data_Offset *extn_data_itr;
942 size_t extn_data_off = klass->data_offset +
943 EO_ALIGN_SIZE(klass->desc->data_size);
945 /* FIXME: Make faster... */
948 if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
949 ((*mro_itr)->desc->data_size > 0))
956 klass->extn_data_off = calloc(extn_num + 1,
957 sizeof(*klass->extn_data_off));
959 extn_data_itr = klass->extn_data_off;
960 mro_itr = klass->mro;
963 if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
964 ((*mro_itr)->desc->data_size > 0))
966 extn_data_itr->klass = *mro_itr;
967 extn_data_itr->offset = extn_data_off;
969 extn_data_off += EO_ALIGN_SIZE(extn_data_itr->klass->desc->data_size);
975 klass->extn_data_size = extn_data_off;
978 eina_lock_take(&_eo_class_creation_lock);
980 klass->class_id = ++_eo_classes_last_id;
983 /* FIXME: Handle errors. */
984 size_t arrsize = _eo_classes_last_id * sizeof(*_eo_classes);
986 tmp = realloc(_eo_classes, arrsize);
988 /* If it's the first allocation, memset. */
990 memset(tmp, 0, arrsize);
993 _eo_classes[klass->class_id - 1] = klass;
995 eina_lock_release(&_eo_class_creation_lock);
997 EINA_MAGIC_SET(klass, EO_CLASS_EINA_MAGIC);
999 _eo_class_base_op_init(klass);
1000 /* Flatten the function array */
1002 const Eo_Class **mro_itr = klass->mro;
1003 for ( ; *mro_itr ; mro_itr++)
1006 /* Skip ourselves. */
1007 for ( mro_itr-- ; mro_itr > klass->mro ; mro_itr--)
1009 _dich_copy_all(klass, *mro_itr);
1013 /* Mark which classes we implement */
1015 const Eo_Class **extn_itr;
1017 for (extn_itr = klass->extensions ; *extn_itr ; extn_itr++)
1019 const Eo_Class *extn = *extn_itr;
1020 /* Set it in the dich. */
1021 _dich_func_set(klass, extn->base_id +
1022 extn->desc->ops.count, _eo_class_isa_func);
1025 _dich_func_set(klass, klass->base_id + klass->desc->ops.count,
1026 _eo_class_isa_func);
1030 _dich_func_set(klass,
1031 klass->parent->base_id + klass->parent->desc->ops.count,
1032 _eo_class_isa_func);
1036 _eo_class_constructor(klass);
1043 eo_class_free(klass);
1048 eo_isa(const Eo *obj, const Eo_Class *klass)
1050 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1051 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, EINA_FALSE);
1052 const op_type_funcs *func = _dich_func_get(obj->klass,
1053 klass->base_id + klass->desc->ops.count);
1055 /* Currently implemented by reusing the LAST op id. Just marking it with
1056 * _eo_class_isa_func. */
1057 return (func && (func->func == _eo_class_isa_func));
1061 eo_parent_set(Eo *obj, const Eo *parent)
1063 EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1065 EO_MAGIC_RETURN_VAL(parent, EO_EINA_MAGIC, EINA_FALSE);
1067 if (obj->parent == parent)
1072 if (eo_composite_is(obj))
1074 eo_composite_detach(obj, obj->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)
1101 EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, NULL);
1103 if (parent) EO_MAGIC_RETURN_VAL(parent, EO_EINA_MAGIC, NULL);
1105 if (EINA_UNLIKELY(klass->desc->type != EO_CLASS_TYPE_REGULAR))
1107 ERR("Class '%s' is not instantiate-able. Aborting.", klass->desc->name);
1111 Eo *obj = calloc(1, EO_ALIGN_SIZE(sizeof(*obj)) +
1112 (klass->data_offset + EO_ALIGN_SIZE(klass->desc->data_size)) +
1113 klass->extn_data_size);
1114 EINA_MAGIC_SET(obj, EO_EINA_MAGIC);
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_CLASS_IDS_FIRST - 1;
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));