7 static int _eobj_log_dom = -1;
9 static Eobj_Class **_eobj_classes;
10 static Eobj_Class_Id _eobj_classes_last_id;
11 static Eina_Bool _eobj_init_count = 0;
13 static void _eobj_callback_remove_all(Eobj *obj);
14 static void _eobj_generic_data_del_all(Eobj *obj);
15 static void eobj_class_constructor(Eobj *obj, const Eobj_Class *klass);
16 static void eobj_class_destructor(Eobj *obj, const Eobj_Class *klass);
17 static void eobj_constructor_error_unset(Eobj *obj);
22 #define CRITICAL(...) EINA_LOG_DOM_CRIT(_eobj_log_dom, __VA_ARGS__)
27 #define ERR(...) EINA_LOG_DOM_ERR(_eobj_log_dom, __VA_ARGS__)
32 #define WRN(...) EINA_LOG_DOM_WARN(_eobj_log_dom, __VA_ARGS__)
37 #define INF(...) EINA_LOG_DOM_INFO(_eobj_log_dom, __VA_ARGS__)
42 #define DBG(...) EINA_LOG_DOM_DBG(_eobj_log_dom, __VA_ARGS__)
44 typedef struct _Eobj_Callback_Description Eobj_Callback_Description;
49 const Eobj_Class *klass;
52 Eina_List *composite_objects;
54 Eina_Inlist *callbacks;
57 Eina_Inlist *generic_data;
62 Eina_Bool construct_error:1;
66 /* Dich search, split to 0xff 0xff 0xffff */
68 #define DICH_CHAIN1_MASK (0xff)
69 #define DICH_CHAIN2_MASK (0xff)
70 #define DICH_CHAIN_LAST_MASK (0xffff)
71 #define DICH_CHAIN1_SIZE (DICH_CHAIN1_MASK + 1)
72 #define DICH_CHAIN2_SIZE (DICH_CHAIN2_MASK + 1)
73 #define DICH_CHAIN_LAST_SIZE (DICH_CHAIN_LAST_MASK + 1)
74 #define DICH_CHAIN1(x) (((x) >> 24) & DICH_CHAIN1_MASK)
75 #define DICH_CHAIN2(x) (((x) >> 16) & DICH_CHAIN2_MASK)
76 #define DICH_CHAIN_LAST(x) ((x) & DICH_CHAIN_LAST_MASK)
78 #define OP_CLASS_OFFSET 16
79 #define OP_CLASS_OFFSET_GET(x) (((x) >> OP_CLASS_OFFSET) & 0xffff)
80 #define OP_CLASS_GET(op) ({ \
81 Eobj_Class_Id tmp = OP_CLASS_OFFSET_GET(op); \
82 (Eobj_Class *) ((tmp <= _eobj_classes_last_id) && (tmp > 0)) ? \
83 (_eobj_classes[tmp - 1]) : NULL; \
86 /* Structure of Eobj_Op is:
91 typedef struct _Dich_Chain1 Dich_Chain1;
95 eobj_op_func_type func;
100 op_type_funcs *funcs;
111 const Eobj_Class *klass;
112 } Eobj_Extension_Node;
116 Eobj_Class_Id class_id;
117 const Eobj_Class *parent;
118 const Eobj_Class_Description *desc;
119 Dich_Chain1 chain[DICH_CHAIN1_SIZE];
120 Eina_Inlist *extensions;
122 const Eobj_Class **mro;
124 size_t data_offset; /* < Offset of the data within object data. */
126 Eina_Bool constructed : 1;
129 static inline eobj_op_func_type
130 dich_func_get(const Eobj_Class *klass, Eobj_Op op)
132 const Dich_Chain1 *chain1 = &klass->chain[DICH_CHAIN1(op)];
133 if (!chain1) return NULL;
134 if (!chain1->chain) return NULL;
135 Dich_Chain2 *chain2 = &chain1->chain[DICH_CHAIN2(op)];
136 if (!chain2) return NULL;
137 if (!chain2->funcs) return NULL;
139 /* num_ops is calculated from the class. */
140 const Eobj_Class *op_klass = OP_CLASS_GET(op);
141 if (!op_klass || (DICH_CHAIN_LAST(op) >= op_klass->desc->ops.count))
144 return chain2->funcs[DICH_CHAIN_LAST(op)].func;
148 dich_func_set(Eobj_Class *klass, Eobj_Op op, eobj_op_func_type func)
150 const Eobj_Class *op_klass = OP_CLASS_GET(op);
153 /* Verify op is valid. */
156 /* num_ops is calculated from the class. */
157 num_ops = op_klass->desc->ops.count;
158 if (DICH_CHAIN_LAST(op) >= num_ops)
160 ERR("OP %x is too big for the domain '%s', expected value < %x.",
161 op, op_klass->desc->name, op_klass->desc->ops.count);
166 ERR("OP %x is from an illegal class.", op);
170 Dich_Chain1 *chain1 = &klass->chain[DICH_CHAIN1(op)];
173 klass->chain[DICH_CHAIN1(op)].chain =
175 calloc(DICH_CHAIN2_SIZE, sizeof(*(chain1->chain)));
178 Dich_Chain2 *chain2 = &chain1->chain[DICH_CHAIN2(op)];
181 chain2->funcs = chain1->chain[DICH_CHAIN2(op)].funcs =
182 calloc(num_ops, sizeof(*(chain2->funcs)));
185 chain2->funcs[DICH_CHAIN_LAST(op)].func = func;
189 dich_func_clean_all(Eobj_Class *klass)
192 Dich_Chain1 *chain1 = klass->chain;
194 for (i = 0 ; i < DICH_CHAIN1_SIZE ; i++, chain1++)
197 Dich_Chain2 *chain2 = chain1->chain;
202 for (j = 0 ; j < DICH_CHAIN2_SIZE ; j++, chain2++)
207 chain1->chain = NULL;
217 const Eobj_Class **kls_itr;
220 static inline Eina_Bool
221 _eobj_kls_itr_init(Eobj *obj, Eobj_Op op)
225 Eobj_Kls_Itr_Node *node =
226 EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
235 Eobj_Kls_Itr_Node *node = calloc(1, sizeof(*node));
237 node->kls_itr = obj->klass->mro;
238 obj->kls_itr = eina_inlist_prepend(obj->kls_itr,
239 EINA_INLIST_GET(node));
246 _eobj_kls_itr_end(Eobj *obj, Eobj_Op op)
248 Eobj_Kls_Itr_Node *node =
249 EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
254 obj->kls_itr = eina_inlist_remove(obj->kls_itr, obj->kls_itr);
258 static inline const Eobj_Class *
259 _eobj_kls_itr_get(Eobj *obj)
261 Eobj_Kls_Itr_Node *node =
262 EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
264 return (node) ? *(node->kls_itr) : NULL;
267 static inline const Eobj_Class *
268 _eobj_kls_itr_next(Eobj *obj)
270 Eobj_Kls_Itr_Node *node =
271 EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
272 const Eobj_Class **kls_itr = node->kls_itr;
276 node->kls_itr = kls_itr;
285 static inline Eina_Bool
286 _eobj_kls_itr_reached_end(const Eobj *obj)
288 Eobj_Kls_Itr_Node *node =
289 EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
290 const Eobj_Class **kls_itr = node->kls_itr;
291 return !(*kls_itr && *(kls_itr + 1));
294 static const Eobj_Op_Description *
295 _eobj_op_id_desc_get(Eobj_Op op)
297 const Eobj_Class *klass = OP_CLASS_GET(op);
299 if (!klass || !klass->desc->ops.base_op_id) return NULL;
301 Eobj_Op base_op_id = *klass->desc->ops.base_op_id;
303 const Eobj_Op_Description *desc = klass->desc->ops.descs;
305 for (i = 0 ; i < klass->desc->ops.count ; i++, desc++)
307 if ((base_op_id + desc->sub_op) == op)
315 _eobj_op_internal(Eobj *obj, Eobj_Op op, va_list *p_list)
317 const Eobj_Class *klass;
318 Eina_Bool ret = EINA_FALSE;
321 _itr_init = _eobj_kls_itr_init(obj, op);
322 klass = _eobj_kls_itr_get(obj);
325 eobj_op_func_type func = dich_func_get(klass, op);
329 func(obj, eobj_data_get(obj, klass), p_list);
334 klass = _eobj_kls_itr_next(obj);
337 /* Try composite objects */
341 EINA_LIST_FOREACH(obj->composite_objects, itr, emb_obj)
343 if (_eobj_op_internal(emb_obj, op, p_list))
353 if (_itr_init) _eobj_kls_itr_end(obj, op);
357 static inline Eina_Bool
358 _eobj_ops_internal(Eobj *obj, va_list *p_list)
360 Eina_Bool ret = EINA_TRUE;
363 op = va_arg(*p_list, Eobj_Op);
366 if (!_eobj_op_internal(obj, op, p_list))
368 const Eobj_Op_Description *desc = _eobj_op_id_desc_get(op);
369 const char *_id_name = (desc) ? desc->name : NULL;
370 const Eobj_Class *op_klass = OP_CLASS_GET(op);
371 const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL;
372 ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.",
373 op, _id_name, _dom_name,
374 obj->klass->desc->name);
378 op = va_arg(*p_list, Eobj_Op);
385 eobj_do_internal(Eobj *obj, ...)
390 va_start(p_list, obj);
391 ret = _eobj_ops_internal(obj, &p_list);
398 eobj_super_do(Eobj *obj, Eobj_Op op, ...)
400 const Eobj_Class *obj_klass;
401 Eina_Bool ret = EINA_TRUE;
404 va_start(p_list, op);
406 /* Advance the kls itr. */
407 obj_klass = _eobj_kls_itr_next(obj);
408 if (!_eobj_op_internal(obj, op, &p_list))
410 const Eobj_Op_Description *desc = _eobj_op_id_desc_get(op);
411 const char *_id_name = (desc) ? desc->name : NULL;
412 const Eobj_Class *op_klass = OP_CLASS_GET(op);
413 const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL;
414 ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.",
415 op, _id_name, _dom_name,
416 (obj_klass) ? obj_klass->desc->name : NULL);
424 EAPI const Eobj_Class *
425 eobj_class_get(Eobj *obj)
431 eobj_class_name_get(const Eobj_Class *klass)
433 return klass->desc->name;
437 _eobj_class_base_op_init(Eobj_Class *klass)
439 const Eobj_Class_Description *desc = klass->desc;
440 if (!desc || !desc->ops.base_op_id)
443 /* FIXME: Depends on values defined above! */
444 *(desc->ops.base_op_id) = klass->class_id << OP_CLASS_OFFSET;
448 _eobj_class_mro_add(Eina_List *mro, const Eobj_Class *klass)
453 mro = eina_list_append(mro, klass);
456 Eobj_Extension_Node *extn;
457 EINA_INLIST_FOREACH(klass->extensions, extn)
459 mro = _eobj_class_mro_add(mro, extn->klass);
463 mro = _eobj_class_mro_add(mro, klass->parent);
469 _eobj_class_mro_init(Eobj_Class *klass)
471 Eina_List *mro = NULL;
473 DBG("Started creating MRO for class '%s'", klass->desc->name);
474 mro = _eobj_class_mro_add(mro, klass);
477 Eina_List *itr1, *itr2, *itr2n;
479 itr1 = eina_list_last(mro);
482 itr2 = eina_list_prev(itr1);
486 itr2n = eina_list_prev(itr2);
488 if (eina_list_data_get(itr1) == eina_list_data_get(itr2))
490 mro = eina_list_remove_list(mro, itr2);
496 itr1 = eina_list_prev(itr1);
500 /* Copy the mro and free the list. */
502 const Eobj_Class *kls_itr;
503 const Eobj_Class **mro_itr;
504 klass->mro = calloc(sizeof(*klass->mro), eina_list_count(mro) + 1);
506 mro_itr = klass->mro;
508 EINA_LIST_FREE(mro, kls_itr)
510 *(mro_itr++) = kls_itr;
512 DBG("Added '%s' to MRO", kls_itr->desc->name);
517 DBG("Finished creating MRO for class '%s'", klass->desc->name);
521 _eobj_class_constructor(Eobj_Class *klass)
523 if (klass->constructed)
526 klass->constructed = EINA_TRUE;
528 if (klass->desc->class_constructor)
529 klass->desc->class_constructor(klass);
531 _eobj_class_mro_init(klass);
535 eobj_class_funcs_set(Eobj_Class *klass, const Eobj_Op_Func_Description *func_descs)
537 const Eobj_Op_Func_Description *itr;
541 for ( ; itr->op != 0 ; itr++)
543 dich_func_set(klass, itr->op, itr->func);
549 eobj_class_new(const Eobj_Class_Description *desc, const Eobj_Class *parent, ...)
554 va_start(p_list, parent);
556 #define _CLS_NEW_CHECK(x) \
561 ERR("%s can't be NULL! Aborting.", #x); \
567 _CLS_NEW_CHECK(desc);
568 _CLS_NEW_CHECK(desc->name);
570 klass = calloc(1, sizeof(Eobj_Class));
571 klass->parent = parent;
573 /* Handle class extensions */
575 Eobj_Class *extn = NULL;
577 extn = va_arg(p_list, Eobj_Class *);
580 switch (extn->desc->type)
582 case EOBJ_CLASS_TYPE_REGULAR:
583 case EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT:
584 /* Use it like an interface. */
585 case EOBJ_CLASS_TYPE_INTERFACE:
587 case EOBJ_CLASS_TYPE_MIXIN:
589 Eobj_Extension_Node *node = calloc(1, sizeof(*node));
592 eina_inlist_append(klass->extensions,
593 EINA_INLIST_GET(node));
598 extn = va_arg(p_list, Eobj_Class *);
604 /* Handle the inheritance */
607 /* Verify the inheritance is allowed. */
608 switch (klass->desc->type)
610 case EOBJ_CLASS_TYPE_REGULAR:
611 case EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT:
612 if ((klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR) &&
613 (klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT))
615 /* FIXME: Actually handle it. */
616 ERR("Regular classes ('%s') aren't allowed to inherit from non-regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
620 case EOBJ_CLASS_TYPE_INTERFACE:
621 case EOBJ_CLASS_TYPE_MIXIN:
622 if ((klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR) &&
623 (klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT))
625 /* FIXME: Actually handle it. */
626 ERR("Non-regular classes ('%s') aren't allowed to inherit from regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
633 /* Update the current offset. */
634 /* FIXME: Make sure this alignment is enough. */
635 klass->data_offset = klass->parent->data_offset +
636 klass->parent->desc->data_size +
638 (klass->parent->desc->data_size % sizeof(void *)));
641 klass->class_id = ++_eobj_classes_last_id;
643 /* FIXME: Handle errors. */
645 tmp = realloc(_eobj_classes, _eobj_classes_last_id * sizeof(*_eobj_classes));
647 _eobj_classes[klass->class_id - 1] = klass;
650 _eobj_class_base_op_init(klass);
652 /* FIXME: Shouldn't be called here - should be called from eobj_add. */
653 _eobj_class_constructor(klass);
660 eobj_class_free(klass);
663 #undef _CLS_NEW_CHECK
666 eobj_class_free(Eobj_Class *klass)
668 if (klass->constructed)
670 if (klass->desc->class_destructor)
671 klass->desc->class_destructor(klass);
673 dich_func_clean_all(klass);
678 Eobj_Extension_Node *extn;
679 EINA_INLIST_FOREACH_SAFE(klass->extensions, itrn, extn)
691 eobj_add(const Eobj_Class *klass, Eobj *parent)
693 if (klass->desc->type != EOBJ_CLASS_TYPE_REGULAR)
695 ERR("Class '%s' is not instantiate-able. Aborting.", klass->desc->name);
699 Eobj *obj = calloc(1, sizeof(*obj));
701 obj->parent = parent;
705 obj->data_blob = calloc(1, klass->data_offset + klass->desc->data_size);
707 _eobj_kls_itr_init(obj, EOBJ_NOOP);
708 eobj_constructor_error_unset(obj);
711 eobj_class_constructor(obj, klass);
713 if (eobj_constructor_error_get(obj))
715 ERR("Type '%s' - One of the object constructors have failed.", klass->desc->name);
719 if (!_eobj_kls_itr_reached_end(obj))
721 ERR("Type '%s' - Not all of the object constructors have been executed.", klass->desc->name);
724 _eobj_kls_itr_end(obj, EOBJ_NOOP);
730 /* Unref twice, once for the ref above, and once for the basic object ref. */
744 eobj_unref(Eobj *obj)
746 if (--(obj->refcount) == 0)
748 const Eobj_Class *klass = eobj_class_get(obj);
749 _eobj_kls_itr_init(obj, EOBJ_NOOP);
750 eobj_constructor_error_unset(obj);
751 eobj_class_destructor(obj, klass);
752 if (eobj_constructor_error_get(obj))
754 ERR("Type '%s' - One of the object destructors have failed.", klass->desc->name);
757 if (!_eobj_kls_itr_reached_end(obj))
759 ERR("Type '%s' - Not all of the object destructors have been executed.", klass->desc->name);
761 _eobj_kls_itr_end(obj, EOBJ_NOOP);
762 /*FIXME: add eobj_class_unref(klass) ? - just to clear the caches. */
764 /* If for some reason it's not empty, clear it. */
767 WRN("Kls_Itr is not empty, possibly a bug, please report. - An error will be reported for each kls_itr in the stack.");
768 Eina_Inlist *nitr = nitr->next;
769 free(EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node));
773 Eina_List *itr, *itr_n;
775 EINA_LIST_FOREACH_SAFE(obj->composite_objects, itr, itr_n, emb_obj)
777 /* FIXME: Should probably be unref. */
779 obj->composite_objects =
780 eina_list_remove_list(obj->composite_objects, itr);
783 _eobj_callback_remove_all(obj);
786 free(obj->data_blob);
788 _eobj_generic_data_del_all(obj);
795 eobj_ref_get(const Eobj *obj)
797 return obj->refcount;
803 obj->delete = EINA_TRUE;
808 eobj_parent_get(Eobj *obj)
814 eobj_constructor_error_set(Eobj *obj)
816 obj->construct_error = EINA_TRUE;
820 eobj_constructor_error_unset(Eobj *obj)
822 obj->construct_error = EINA_FALSE;
826 eobj_constructor_error_get(const Eobj *obj)
828 return obj->construct_error;
832 _eobj_constructor_default(Eobj *obj)
834 eobj_constructor_super(obj);
838 _eobj_destructor_default(Eobj *obj)
840 eobj_destructor_super(obj);
844 eobj_class_constructor(Eobj *obj, const Eobj_Class *klass)
849 if (klass->desc->constructor)
850 klass->desc->constructor(obj, eobj_data_get(obj, klass));
852 _eobj_constructor_default(obj);
856 eobj_class_destructor(Eobj *obj, const Eobj_Class *klass)
861 if (klass->desc->destructor)
862 klass->desc->destructor(obj, eobj_data_get(obj, klass));
864 _eobj_destructor_default(obj);
868 eobj_constructor_super(Eobj *obj)
870 eobj_class_constructor(obj, _eobj_kls_itr_next(obj));
874 eobj_destructor_super(Eobj *obj)
876 eobj_class_destructor(obj, _eobj_kls_itr_next(obj));
880 eobj_data_get(Eobj *obj, const Eobj_Class *klass)
882 /* FIXME: Add a check that this is of the right klass and we don't seg.
883 * Probably just return NULL. */
884 if (klass->desc->data_size > 0)
885 return ((char *) obj->data_blob) + klass->data_offset;
893 Eina_Stringshare *key;
895 } Eobj_Generic_Data_Node;
898 _eobj_generic_data_node_free(Eobj_Generic_Data_Node *node)
900 eina_stringshare_del(node->key);
905 _eobj_generic_data_del_all(Eobj *obj)
908 Eobj_Generic_Data_Node *node;
910 EINA_INLIST_FOREACH_SAFE(obj->generic_data, nnode, node)
912 obj->generic_data = eina_inlist_remove(obj->generic_data,
913 EINA_INLIST_GET(node));
915 _eobj_generic_data_node_free(node);
920 eobj_generic_data_set(Eobj *obj, const char *key, const void *data)
923 Eobj_Generic_Data_Node *node;
925 if (!key) return NULL;
926 if (!data) return NULL;
928 prev_data = eobj_generic_data_del(obj, key);
930 node = malloc(sizeof(Eobj_Generic_Data_Node));
931 node->key = eina_stringshare_add(key);
932 node->data = (void *) data;
933 obj->generic_data = eina_inlist_prepend(obj->generic_data,
934 EINA_INLIST_GET(node));
940 eobj_generic_data_get(const Eobj *obj, const char *key)
942 Eobj_Generic_Data_Node *node;
944 if (!key) return NULL;
946 EINA_INLIST_FOREACH(obj->generic_data, node)
948 if (!strcmp(node->key, key))
950 ((Eobj *) obj)->generic_data =
951 eina_inlist_promote(obj->generic_data, EINA_INLIST_GET(node));
959 eobj_generic_data_del(Eobj *obj, const char *key)
961 Eobj_Generic_Data_Node *node;
963 if (!key) return NULL;
965 EINA_INLIST_FOREACH(obj->generic_data, node)
967 if (!strcmp(node->key, key))
972 obj->generic_data = eina_inlist_remove(obj->generic_data,
973 EINA_INLIST_GET(node));
974 _eobj_generic_data_node_free(node);
984 const char *log_dom = "eobj";
985 if (_eobj_init_count++ > 0)
990 _eobj_classes = NULL;
991 _eobj_classes_last_id = 0;
992 _eobj_log_dom = eina_log_domain_register(log_dom, EINA_COLOR_LIGHTBLUE);
993 if (_eobj_log_dom < 0)
995 EINA_LOG_ERR("Could not register log domain: %s", log_dom);
1006 Eobj_Class **cls_itr = _eobj_classes;
1008 if (--_eobj_init_count > 0)
1011 for (i = 0 ; i < _eobj_classes_last_id ; i++, cls_itr++)
1014 eobj_class_free(*cls_itr);
1018 free(_eobj_classes);
1020 eina_log_domain_unregister(_eobj_log_dom);
1028 eobj_composite_object_attach(Eobj *obj, Eobj *emb_obj)
1031 obj->composite_objects = eina_list_prepend(obj->composite_objects, emb_obj);
1035 eobj_composite_object_detach(Eobj *obj, Eobj *emb_obj)
1037 obj->composite_objects = eina_list_remove(obj->composite_objects, emb_obj);
1038 eobj_unref(emb_obj);
1042 eobj_composite_is(Eobj *emb_obj)
1044 Eobj *obj = eobj_parent_get(emb_obj);
1051 EINA_LIST_FOREACH(obj->composite_objects, itr, tmp)
1061 struct _Eobj_Callback_Description
1064 const Eobj_Event_Description *event;
1067 Eobj_Callback_Priority priority;
1068 Eina_Bool delete_me : 1;
1071 /* Actually remove, doesn't care about walking list, or delete_me */
1073 _eobj_callback_remove(Eobj *obj, Eobj_Callback_Description *cb)
1075 obj->callbacks = eina_inlist_remove(obj->callbacks,
1076 EINA_INLIST_GET(cb));
1080 /* Actually remove, doesn't care about walking list, or delete_me */
1082 _eobj_callback_remove_all(Eobj *obj)
1085 Eobj_Callback_Description *cb;
1086 EINA_INLIST_FOREACH_SAFE(obj->callbacks, initr, cb)
1088 _eobj_callback_remove(obj, cb);
1093 _eobj_callbacks_clear(Eobj *obj)
1096 Eobj_Callback_Description *cb;
1098 /* Abort if we are currently walking the list. */
1099 if (obj->walking_list > 0)
1102 EINA_INLIST_FOREACH_SAFE(obj->callbacks, itn, cb)
1106 _eobj_callback_remove(obj, cb);
1112 _callback_priority_cmp(const void *_a, const void *_b)
1114 const Eobj_Callback_Description *a, *b;
1115 a = (const Eobj_Callback_Description *) _a;
1116 b = (const Eobj_Callback_Description *) _b;
1117 if (a->priority < b->priority)
1124 eobj_event_callback_priority_add(Eobj *obj,
1125 const Eobj_Event_Description *desc,
1126 Eobj_Callback_Priority priority,
1130 Eobj_Callback_Description *cb = calloc(1, sizeof(*cb));
1133 cb->func_data = (void *) data;
1134 cb->priority = priority;
1135 obj->callbacks = eina_inlist_sorted_insert(obj->callbacks,
1136 EINA_INLIST_GET(cb), _callback_priority_cmp);
1138 eobj_event_callback_call(obj, EOBJ_SIG_CALLBACK_ADD, desc);
1144 eobj_event_callback_del(Eobj *obj, const Eobj_Event_Description *desc, Eobj_Event_Cb func)
1147 Eobj_Callback_Description *cb;
1148 EINA_INLIST_FOREACH(obj->callbacks, cb)
1150 if ((cb->event == desc) && (cb->func == func))
1154 data = cb->func_data;
1155 cb->delete_me = EINA_TRUE;
1156 _eobj_callbacks_clear(obj);
1165 eobj_event_callback_call(obj, EOBJ_SIG_CALLBACK_DEL, desc);
1170 eobj_event_callback_del_full(Eobj *obj, const Eobj_Event_Description *desc, Eobj_Event_Cb func, const void *user_data)
1173 Eobj_Callback_Description *cb;
1174 EINA_INLIST_FOREACH(obj->callbacks, cb)
1176 if ((cb->event == desc) && (cb->func == func) &&
1177 (cb->func_data == user_data))
1181 data = cb->func_data;
1182 cb->delete_me = EINA_TRUE;
1183 _eobj_callbacks_clear(obj);
1192 eobj_event_callback_call(obj, EOBJ_SIG_CALLBACK_DEL, desc);
1197 eobj_event_callback_call(Eobj *obj, const Eobj_Event_Description *desc,
1198 const void *event_info)
1200 Eina_Bool ret = EINA_TRUE;
1201 Eobj_Callback_Description *cb;
1204 obj->walking_list++;
1206 EINA_INLIST_FOREACH(obj->callbacks, cb)
1208 if (!cb->delete_me && (cb->event == desc))
1210 /* Abort callback calling if the func says so. */
1211 if (!cb->func((void *) cb->func_data, obj, desc,
1212 (void *) event_info))
1221 obj->walking_list--;
1222 _eobj_callbacks_clear(obj);
1229 _eobj_event_forwarder_callback(void *data, Eobj *obj, const Eobj_Event_Description *desc, void *event_info)
1232 Eobj *new_obj = (Eobj *) data;
1233 return eobj_event_callback_call(new_obj, desc, event_info);
1236 /* FIXME: Change default priority? Maybe call later? */
1238 eobj_event_callback_forwarder_add(Eobj *obj, const Eobj_Event_Description *desc, Eobj *new_obj)
1240 return eobj_event_callback_add(obj, desc, _eobj_event_forwarder_callback, new_obj);
1244 eobj_event_callback_forwarder_del(Eobj *obj, const Eobj_Event_Description *desc, Eobj *new_obj)
1246 eobj_event_callback_del_full(obj, desc, _eobj_event_forwarder_callback, new_obj);
1250 /* EOBJ_CLASS_BASE stuff */
1251 static Eobj_Class *_my_class = NULL;
1253 /* FIXME: Set proper type descriptions. */
1254 EAPI const Eobj_Event_Description _EOBJ_SIG_CALLBACK_ADD =
1255 EOBJ_EVENT_DESCRIPTION("callback,add", "?", "Called when a callback was added.");
1256 EAPI const Eobj_Event_Description _EOBJ_SIG_CALLBACK_DEL =
1257 EOBJ_EVENT_DESCRIPTION("callback,del", "?", "Called when a callback was deleted.");
1260 _constructor(Eobj *obj, void *class_data __UNUSED__)
1262 DBG("%p - %s.", obj, _my_class->desc->name);
1266 _destructor(Eobj *obj, void *class_data __UNUSED__)
1268 DBG("%p - %s.", obj, _my_class->desc->name);
1271 EAPI const Eobj_Class *
1272 eobj_base_class_get(void)
1274 if (_my_class) return _my_class;
1276 static const Eobj_Class_Description class_desc = {
1278 EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT,
1279 EOBJ_CLASS_DESCRIPTION_OPS(NULL, NULL, 0),
1288 return _my_class = eobj_class_new(&class_desc, NULL, NULL);