7 typedef int Eobj_Class_Id;
9 static int _eobj_log_dom = -1;
11 static Eobj_Class **_eobj_classes;
12 static Eobj_Class_Id _eobj_classes_last_id;
13 static Eina_Bool _eobj_init_count = 0;
15 static void _eobj_callback_remove_all(Eobj *obj);
16 static void _eobj_generic_data_del_all(Eobj *obj);
17 static void eobj_class_constructor(Eobj *obj, const Eobj_Class *klass);
18 static void eobj_class_destructor(Eobj *obj, const Eobj_Class *klass);
19 static void eobj_constructor_error_unset(Eobj *obj);
24 #define CRITICAL(...) EINA_LOG_DOM_CRIT(_eobj_log_dom, __VA_ARGS__)
29 #define ERR(...) EINA_LOG_DOM_ERR(_eobj_log_dom, __VA_ARGS__)
34 #define WRN(...) EINA_LOG_DOM_WARN(_eobj_log_dom, __VA_ARGS__)
39 #define INF(...) EINA_LOG_DOM_INFO(_eobj_log_dom, __VA_ARGS__)
44 #define DBG(...) EINA_LOG_DOM_DBG(_eobj_log_dom, __VA_ARGS__)
46 typedef struct _Eobj_Callback_Description Eobj_Callback_Description;
51 const Eobj_Class *klass;
54 Eina_List *composite_objects;
56 Eina_Inlist *callbacks;
59 Eina_Inlist *generic_data;
64 Eina_Bool construct_error:1;
68 /* Dich search, split to 0xff 0xff 0xffff */
70 #define DICH_CHAIN1_MASK (0xff)
71 #define DICH_CHAIN2_MASK (0xff)
72 #define DICH_CHAIN_LAST_MASK (0xffff)
73 #define DICH_CHAIN1_SIZE (DICH_CHAIN1_MASK + 1)
74 #define DICH_CHAIN2_SIZE (DICH_CHAIN2_MASK + 1)
75 #define DICH_CHAIN_LAST_SIZE (DICH_CHAIN_LAST_MASK + 1)
76 #define DICH_CHAIN1(x) (((x) >> 24) & DICH_CHAIN1_MASK)
77 #define DICH_CHAIN2(x) (((x) >> 16) & DICH_CHAIN2_MASK)
78 #define DICH_CHAIN_LAST(x) ((x) & DICH_CHAIN_LAST_MASK)
80 #define OP_CLASS_OFFSET 16
81 #define OP_CLASS_OFFSET_GET(x) (((x) >> OP_CLASS_OFFSET) & 0xffff)
82 #define OP_CLASS_GET(op) ({ \
83 Eobj_Class_Id tmp = OP_CLASS_OFFSET_GET(op); \
84 (Eobj_Class *) ((tmp <= _eobj_classes_last_id) && (tmp > 0)) ? \
85 (_eobj_classes[tmp - 1]) : NULL; \
88 /* Structure of Eobj_Op is:
93 typedef struct _Dich_Chain1 Dich_Chain1;
97 eobj_op_func_type func;
102 op_type_funcs *funcs;
113 const Eobj_Class *klass;
114 } Eobj_Extension_Node;
118 Eobj_Class_Id class_id;
119 const Eobj_Class *parent;
120 const Eobj_Class_Description *desc;
121 Dich_Chain1 chain[DICH_CHAIN1_SIZE];
122 Eina_Inlist *extensions;
124 const Eobj_Class **mro;
126 size_t data_offset; /* < Offset of the data within object data. */
128 Eina_Bool constructed : 1;
131 static inline eobj_op_func_type
132 dich_func_get(const Eobj_Class *klass, Eobj_Op op)
134 const Dich_Chain1 *chain1 = &klass->chain[DICH_CHAIN1(op)];
135 if (!chain1) return NULL;
136 if (!chain1->chain) return NULL;
137 Dich_Chain2 *chain2 = &chain1->chain[DICH_CHAIN2(op)];
138 if (!chain2) return NULL;
139 if (!chain2->funcs) return NULL;
141 /* num_ops is calculated from the class. */
142 const Eobj_Class *op_klass = OP_CLASS_GET(op);
143 if (!op_klass || (DICH_CHAIN_LAST(op) >= op_klass->desc->ops.count))
146 return chain2->funcs[DICH_CHAIN_LAST(op)].func;
150 dich_func_set(Eobj_Class *klass, Eobj_Op op, eobj_op_func_type func)
152 const Eobj_Class *op_klass = OP_CLASS_GET(op);
155 /* Verify op is valid. */
158 /* num_ops is calculated from the class. */
159 num_ops = op_klass->desc->ops.count;
160 if (DICH_CHAIN_LAST(op) >= num_ops)
162 ERR("OP %x is too big for the domain '%s', expected value < %x.",
163 op, op_klass->desc->name, op_klass->desc->ops.count);
168 ERR("OP %x is from an illegal class.", op);
172 Dich_Chain1 *chain1 = &klass->chain[DICH_CHAIN1(op)];
175 klass->chain[DICH_CHAIN1(op)].chain =
177 calloc(DICH_CHAIN2_SIZE, sizeof(*(chain1->chain)));
180 Dich_Chain2 *chain2 = &chain1->chain[DICH_CHAIN2(op)];
183 chain2->funcs = chain1->chain[DICH_CHAIN2(op)].funcs =
184 calloc(num_ops, sizeof(*(chain2->funcs)));
187 chain2->funcs[DICH_CHAIN_LAST(op)].func = func;
191 dich_func_clean_all(Eobj_Class *klass)
194 Dich_Chain1 *chain1 = klass->chain;
196 for (i = 0 ; i < DICH_CHAIN1_SIZE ; i++, chain1++)
199 Dich_Chain2 *chain2 = chain1->chain;
204 for (j = 0 ; j < DICH_CHAIN2_SIZE ; j++, chain2++)
209 chain1->chain = NULL;
219 const Eobj_Class **kls_itr;
222 static inline Eina_Bool
223 _eobj_kls_itr_init(Eobj *obj, Eobj_Op op)
227 Eobj_Kls_Itr_Node *node =
228 EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
237 Eobj_Kls_Itr_Node *node = calloc(1, sizeof(*node));
239 node->kls_itr = obj->klass->mro;
240 obj->kls_itr = eina_inlist_prepend(obj->kls_itr,
241 EINA_INLIST_GET(node));
248 _eobj_kls_itr_end(Eobj *obj, Eobj_Op op)
250 Eobj_Kls_Itr_Node *node =
251 EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
256 obj->kls_itr = eina_inlist_remove(obj->kls_itr, obj->kls_itr);
260 static inline const Eobj_Class *
261 _eobj_kls_itr_get(Eobj *obj)
263 Eobj_Kls_Itr_Node *node =
264 EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
266 return (node) ? *(node->kls_itr) : NULL;
269 static inline const Eobj_Class *
270 _eobj_kls_itr_next(Eobj *obj)
272 Eobj_Kls_Itr_Node *node =
273 EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
274 const Eobj_Class **kls_itr = node->kls_itr;
278 node->kls_itr = kls_itr;
287 static inline Eina_Bool
288 _eobj_kls_itr_reached_end(const Eobj *obj)
290 Eobj_Kls_Itr_Node *node =
291 EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
292 const Eobj_Class **kls_itr = node->kls_itr;
293 return !(*kls_itr && *(kls_itr + 1));
296 static const Eobj_Op_Description *
297 _eobj_op_id_desc_get(Eobj_Op op)
299 const Eobj_Class *klass = OP_CLASS_GET(op);
301 if (!klass || !klass->desc->ops.base_op_id) return NULL;
303 Eobj_Op base_op_id = *klass->desc->ops.base_op_id;
305 const Eobj_Op_Description *desc = klass->desc->ops.descs;
307 for (i = 0 ; i < klass->desc->ops.count ; i++, desc++)
309 if ((base_op_id + desc->sub_op) == op)
317 _eobj_op_internal(Eobj *obj, Eobj_Op op, va_list *p_list)
319 const Eobj_Class *klass;
320 Eina_Bool ret = EINA_FALSE;
323 _itr_init = _eobj_kls_itr_init(obj, op);
324 klass = _eobj_kls_itr_get(obj);
327 eobj_op_func_type func = dich_func_get(klass, op);
331 func(obj, eobj_data_get(obj, klass), p_list);
336 klass = _eobj_kls_itr_next(obj);
339 /* Try composite objects */
343 EINA_LIST_FOREACH(obj->composite_objects, itr, emb_obj)
345 if (_eobj_op_internal(emb_obj, op, p_list))
355 if (_itr_init) _eobj_kls_itr_end(obj, op);
359 static inline Eina_Bool
360 _eobj_ops_internal(Eobj *obj, va_list *p_list)
362 Eina_Bool ret = EINA_TRUE;
365 op = va_arg(*p_list, Eobj_Op);
368 if (!_eobj_op_internal(obj, op, p_list))
370 const Eobj_Op_Description *desc = _eobj_op_id_desc_get(op);
371 const char *_id_name = (desc) ? desc->name : NULL;
372 const Eobj_Class *op_klass = OP_CLASS_GET(op);
373 const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL;
374 ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.",
375 op, _id_name, _dom_name,
376 obj->klass->desc->name);
380 op = va_arg(*p_list, Eobj_Op);
387 eobj_do_internal(Eobj *obj, ...)
392 va_start(p_list, obj);
393 ret = _eobj_ops_internal(obj, &p_list);
400 eobj_super_do(Eobj *obj, Eobj_Op op, ...)
402 const Eobj_Class *obj_klass;
403 Eina_Bool ret = EINA_TRUE;
406 va_start(p_list, op);
408 /* Advance the kls itr. */
409 obj_klass = _eobj_kls_itr_next(obj);
410 if (!_eobj_op_internal(obj, op, &p_list))
412 const Eobj_Op_Description *desc = _eobj_op_id_desc_get(op);
413 const char *_id_name = (desc) ? desc->name : NULL;
414 const Eobj_Class *op_klass = OP_CLASS_GET(op);
415 const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL;
416 ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.",
417 op, _id_name, _dom_name,
418 (obj_klass) ? obj_klass->desc->name : NULL);
426 EAPI const Eobj_Class *
427 eobj_class_get(const Eobj *obj)
433 eobj_class_name_get(const Eobj_Class *klass)
435 return klass->desc->name;
439 _eobj_class_base_op_init(Eobj_Class *klass)
441 const Eobj_Class_Description *desc = klass->desc;
442 if (!desc || !desc->ops.base_op_id)
445 /* FIXME: Depends on values defined above! */
446 *(desc->ops.base_op_id) = klass->class_id << OP_CLASS_OFFSET;
450 _eobj_class_mro_add(Eina_List *mro, const Eobj_Class *klass)
455 mro = eina_list_append(mro, klass);
458 Eobj_Extension_Node *extn;
459 EINA_INLIST_FOREACH(klass->extensions, extn)
461 mro = _eobj_class_mro_add(mro, extn->klass);
465 mro = _eobj_class_mro_add(mro, klass->parent);
471 _eobj_class_mro_init(Eobj_Class *klass)
473 Eina_List *mro = NULL;
475 DBG("Started creating MRO for class '%s'", klass->desc->name);
476 mro = _eobj_class_mro_add(mro, klass);
479 Eina_List *itr1, *itr2, *itr2n;
481 itr1 = eina_list_last(mro);
484 itr2 = eina_list_prev(itr1);
488 itr2n = eina_list_prev(itr2);
490 if (eina_list_data_get(itr1) == eina_list_data_get(itr2))
492 mro = eina_list_remove_list(mro, itr2);
498 itr1 = eina_list_prev(itr1);
502 /* Copy the mro and free the list. */
504 const Eobj_Class *kls_itr;
505 const Eobj_Class **mro_itr;
506 klass->mro = calloc(sizeof(*klass->mro), eina_list_count(mro) + 1);
508 mro_itr = klass->mro;
510 EINA_LIST_FREE(mro, kls_itr)
512 *(mro_itr++) = kls_itr;
514 DBG("Added '%s' to MRO", kls_itr->desc->name);
519 DBG("Finished creating MRO for class '%s'", klass->desc->name);
523 _eobj_class_constructor(Eobj_Class *klass)
525 if (klass->constructed)
528 klass->constructed = EINA_TRUE;
530 if (klass->desc->class_constructor)
531 klass->desc->class_constructor(klass);
533 _eobj_class_mro_init(klass);
537 eobj_class_funcs_set(Eobj_Class *klass, const Eobj_Op_Func_Description *func_descs)
539 const Eobj_Op_Func_Description *itr;
543 for ( ; itr->op != 0 ; itr++)
545 dich_func_set(klass, itr->op, itr->func);
551 eobj_class_free(Eobj_Class *klass)
553 if (klass->constructed)
555 if (klass->desc->class_destructor)
556 klass->desc->class_destructor(klass);
558 dich_func_clean_all(klass);
563 Eobj_Extension_Node *extn;
564 EINA_INLIST_FOREACH_SAFE(klass->extensions, itrn, extn)
576 eobj_class_new(const Eobj_Class_Description *desc, const Eobj_Class *parent, ...)
581 va_start(p_list, parent);
583 #define _CLS_NEW_CHECK(x) \
588 ERR("%s can't be NULL! Aborting.", #x); \
594 _CLS_NEW_CHECK(desc);
595 _CLS_NEW_CHECK(desc->name);
597 klass = calloc(1, sizeof(Eobj_Class));
598 klass->parent = parent;
600 /* Handle class extensions */
602 Eobj_Class *extn = NULL;
604 extn = va_arg(p_list, Eobj_Class *);
607 switch (extn->desc->type)
609 case EOBJ_CLASS_TYPE_REGULAR:
610 case EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT:
611 /* Use it like an interface. */
612 case EOBJ_CLASS_TYPE_INTERFACE:
614 case EOBJ_CLASS_TYPE_MIXIN:
616 Eobj_Extension_Node *node = calloc(1, sizeof(*node));
619 eina_inlist_append(klass->extensions,
620 EINA_INLIST_GET(node));
625 extn = va_arg(p_list, Eobj_Class *);
631 /* Handle the inheritance */
634 /* Verify the inheritance is allowed. */
635 switch (klass->desc->type)
637 case EOBJ_CLASS_TYPE_REGULAR:
638 case EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT:
639 if ((klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR) &&
640 (klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT))
642 /* FIXME: Actually handle it. */
643 ERR("Regular classes ('%s') aren't allowed to inherit from non-regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
647 case EOBJ_CLASS_TYPE_INTERFACE:
648 case EOBJ_CLASS_TYPE_MIXIN:
649 if ((klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR) &&
650 (klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT))
652 /* FIXME: Actually handle it. */
653 ERR("Non-regular classes ('%s') aren't allowed to inherit from regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
660 /* Update the current offset. */
661 /* FIXME: Make sure this alignment is enough. */
662 klass->data_offset = klass->parent->data_offset +
663 klass->parent->desc->data_size +
665 (klass->parent->desc->data_size % sizeof(void *)));
668 klass->class_id = ++_eobj_classes_last_id;
670 /* FIXME: Handle errors. */
672 tmp = realloc(_eobj_classes, _eobj_classes_last_id * sizeof(*_eobj_classes));
674 _eobj_classes[klass->class_id - 1] = klass;
677 _eobj_class_base_op_init(klass);
679 /* FIXME: Shouldn't be called here - should be called from eobj_add. */
680 _eobj_class_constructor(klass);
687 eobj_class_free(klass);
690 #undef _CLS_NEW_CHECK
693 eobj_add(const Eobj_Class *klass, Eobj *parent)
695 if (klass->desc->type != EOBJ_CLASS_TYPE_REGULAR)
697 ERR("Class '%s' is not instantiate-able. Aborting.", klass->desc->name);
701 Eobj *obj = calloc(1, sizeof(*obj));
703 obj->parent = parent;
707 obj->data_blob = calloc(1, klass->data_offset + klass->desc->data_size);
709 _eobj_kls_itr_init(obj, EOBJ_NOOP);
710 eobj_constructor_error_unset(obj);
713 eobj_class_constructor(obj, klass);
715 if (eobj_constructor_error_get(obj))
717 ERR("Type '%s' - One of the object constructors have failed.", klass->desc->name);
721 if (!_eobj_kls_itr_reached_end(obj))
723 ERR("Type '%s' - Not all of the object constructors have been executed.", klass->desc->name);
726 _eobj_kls_itr_end(obj, EOBJ_NOOP);
732 /* Unref twice, once for the ref above, and once for the basic object ref. */
746 eobj_unref(Eobj *obj)
748 if (--(obj->refcount) == 0)
750 const Eobj_Class *klass = eobj_class_get(obj);
751 _eobj_kls_itr_init(obj, EOBJ_NOOP);
752 eobj_constructor_error_unset(obj);
753 eobj_class_destructor(obj, klass);
754 if (eobj_constructor_error_get(obj))
756 ERR("Type '%s' - One of the object destructors have failed.", klass->desc->name);
759 if (!_eobj_kls_itr_reached_end(obj))
761 ERR("Type '%s' - Not all of the object destructors have been executed.", klass->desc->name);
763 _eobj_kls_itr_end(obj, EOBJ_NOOP);
764 /*FIXME: add eobj_class_unref(klass) ? - just to clear the caches. */
766 /* If for some reason it's not empty, clear it. */
769 WRN("Kls_Itr is not empty, possibly a bug, please report. - An error will be reported for each kls_itr in the stack.");
770 Eina_Inlist *nitr = nitr->next;
771 free(EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node));
775 Eina_List *itr, *itr_n;
777 EINA_LIST_FOREACH_SAFE(obj->composite_objects, itr, itr_n, emb_obj)
779 /* FIXME: Should probably be unref. */
781 obj->composite_objects =
782 eina_list_remove_list(obj->composite_objects, itr);
785 _eobj_callback_remove_all(obj);
788 free(obj->data_blob);
790 _eobj_generic_data_del_all(obj);
797 eobj_ref_get(const Eobj *obj)
799 return obj->refcount;
805 obj->delete = EINA_TRUE;
810 eobj_parent_get(Eobj *obj)
816 eobj_constructor_error_set(Eobj *obj)
818 obj->construct_error = EINA_TRUE;
822 eobj_constructor_error_unset(Eobj *obj)
824 obj->construct_error = EINA_FALSE;
828 eobj_constructor_error_get(const Eobj *obj)
830 return obj->construct_error;
834 _eobj_constructor_default(Eobj *obj)
836 eobj_constructor_super(obj);
840 _eobj_destructor_default(Eobj *obj)
842 eobj_destructor_super(obj);
846 eobj_class_constructor(Eobj *obj, const Eobj_Class *klass)
851 if (klass->desc->constructor)
852 klass->desc->constructor(obj, eobj_data_get(obj, klass));
854 _eobj_constructor_default(obj);
858 eobj_class_destructor(Eobj *obj, const Eobj_Class *klass)
863 if (klass->desc->destructor)
864 klass->desc->destructor(obj, eobj_data_get(obj, klass));
866 _eobj_destructor_default(obj);
870 eobj_constructor_super(Eobj *obj)
872 eobj_class_constructor(obj, _eobj_kls_itr_next(obj));
876 eobj_destructor_super(Eobj *obj)
878 eobj_class_destructor(obj, _eobj_kls_itr_next(obj));
882 eobj_data_get(Eobj *obj, const Eobj_Class *klass)
884 /* FIXME: Add a check that this is of the right klass and we don't seg.
885 * Probably just return NULL. */
886 if (klass->desc->data_size > 0)
887 return ((char *) obj->data_blob) + klass->data_offset;
895 Eina_Stringshare *key;
897 } Eobj_Generic_Data_Node;
900 _eobj_generic_data_node_free(Eobj_Generic_Data_Node *node)
902 eina_stringshare_del(node->key);
907 _eobj_generic_data_del_all(Eobj *obj)
910 Eobj_Generic_Data_Node *node;
912 EINA_INLIST_FOREACH_SAFE(obj->generic_data, nnode, node)
914 obj->generic_data = eina_inlist_remove(obj->generic_data,
915 EINA_INLIST_GET(node));
917 _eobj_generic_data_node_free(node);
922 eobj_generic_data_set(Eobj *obj, const char *key, const void *data)
925 Eobj_Generic_Data_Node *node;
927 if (!key) return NULL;
928 if (!data) return NULL;
930 prev_data = eobj_generic_data_del(obj, key);
932 node = malloc(sizeof(Eobj_Generic_Data_Node));
933 node->key = eina_stringshare_add(key);
934 node->data = (void *) data;
935 obj->generic_data = eina_inlist_prepend(obj->generic_data,
936 EINA_INLIST_GET(node));
942 eobj_generic_data_get(const Eobj *obj, const char *key)
944 Eobj_Generic_Data_Node *node;
946 if (!key) return NULL;
948 EINA_INLIST_FOREACH(obj->generic_data, node)
950 if (!strcmp(node->key, key))
952 ((Eobj *) obj)->generic_data =
953 eina_inlist_promote(obj->generic_data, EINA_INLIST_GET(node));
961 eobj_generic_data_del(Eobj *obj, const char *key)
963 Eobj_Generic_Data_Node *node;
965 if (!key) return NULL;
967 EINA_INLIST_FOREACH(obj->generic_data, node)
969 if (!strcmp(node->key, key))
974 obj->generic_data = eina_inlist_remove(obj->generic_data,
975 EINA_INLIST_GET(node));
976 _eobj_generic_data_node_free(node);
986 const char *log_dom = "eobj";
987 if (_eobj_init_count++ > 0)
992 _eobj_classes = NULL;
993 _eobj_classes_last_id = 0;
994 _eobj_log_dom = eina_log_domain_register(log_dom, EINA_COLOR_LIGHTBLUE);
995 if (_eobj_log_dom < 0)
997 EINA_LOG_ERR("Could not register log domain: %s", log_dom);
1008 Eobj_Class **cls_itr = _eobj_classes;
1010 if (--_eobj_init_count > 0)
1013 for (i = 0 ; i < _eobj_classes_last_id ; i++, cls_itr++)
1016 eobj_class_free(*cls_itr);
1020 free(_eobj_classes);
1022 eina_log_domain_unregister(_eobj_log_dom);
1030 eobj_composite_object_attach(Eobj *obj, Eobj *emb_obj)
1033 obj->composite_objects = eina_list_prepend(obj->composite_objects, emb_obj);
1037 eobj_composite_object_detach(Eobj *obj, Eobj *emb_obj)
1039 obj->composite_objects = eina_list_remove(obj->composite_objects, emb_obj);
1040 eobj_unref(emb_obj);
1044 eobj_composite_is(Eobj *emb_obj)
1046 Eobj *obj = eobj_parent_get(emb_obj);
1053 EINA_LIST_FOREACH(obj->composite_objects, itr, tmp)
1063 struct _Eobj_Callback_Description
1066 const Eobj_Event_Description *event;
1069 Eobj_Callback_Priority priority;
1070 Eina_Bool delete_me : 1;
1073 /* Actually remove, doesn't care about walking list, or delete_me */
1075 _eobj_callback_remove(Eobj *obj, Eobj_Callback_Description *cb)
1077 obj->callbacks = eina_inlist_remove(obj->callbacks,
1078 EINA_INLIST_GET(cb));
1082 /* Actually remove, doesn't care about walking list, or delete_me */
1084 _eobj_callback_remove_all(Eobj *obj)
1087 Eobj_Callback_Description *cb;
1088 EINA_INLIST_FOREACH_SAFE(obj->callbacks, initr, cb)
1090 _eobj_callback_remove(obj, cb);
1095 _eobj_callbacks_clear(Eobj *obj)
1098 Eobj_Callback_Description *cb;
1100 /* Abort if we are currently walking the list. */
1101 if (obj->walking_list > 0)
1104 EINA_INLIST_FOREACH_SAFE(obj->callbacks, itn, cb)
1108 _eobj_callback_remove(obj, cb);
1114 _callback_priority_cmp(const void *_a, const void *_b)
1116 const Eobj_Callback_Description *a, *b;
1117 a = (const Eobj_Callback_Description *) _a;
1118 b = (const Eobj_Callback_Description *) _b;
1119 if (a->priority < b->priority)
1126 eobj_event_callback_priority_add(Eobj *obj,
1127 const Eobj_Event_Description *desc,
1128 Eobj_Callback_Priority priority,
1132 Eobj_Callback_Description *cb = calloc(1, sizeof(*cb));
1135 cb->func_data = (void *) data;
1136 cb->priority = priority;
1137 obj->callbacks = eina_inlist_sorted_insert(obj->callbacks,
1138 EINA_INLIST_GET(cb), _callback_priority_cmp);
1140 eobj_event_callback_call(obj, EOBJ_EV_CALLBACK_ADD, desc);
1146 eobj_event_callback_del(Eobj *obj, const Eobj_Event_Description *desc, Eobj_Event_Cb func)
1149 Eobj_Callback_Description *cb;
1150 EINA_INLIST_FOREACH(obj->callbacks, cb)
1152 if ((cb->event == desc) && (cb->func == func))
1156 data = cb->func_data;
1157 cb->delete_me = EINA_TRUE;
1158 _eobj_callbacks_clear(obj);
1167 eobj_event_callback_call(obj, EOBJ_EV_CALLBACK_DEL, desc);
1172 eobj_event_callback_del_full(Eobj *obj, const Eobj_Event_Description *desc, Eobj_Event_Cb func, const void *user_data)
1175 Eobj_Callback_Description *cb;
1176 EINA_INLIST_FOREACH(obj->callbacks, cb)
1178 if ((cb->event == desc) && (cb->func == func) &&
1179 (cb->func_data == user_data))
1183 data = cb->func_data;
1184 cb->delete_me = EINA_TRUE;
1185 _eobj_callbacks_clear(obj);
1194 eobj_event_callback_call(obj, EOBJ_EV_CALLBACK_DEL, desc);
1199 eobj_event_callback_call(Eobj *obj, const Eobj_Event_Description *desc,
1200 const void *event_info)
1202 Eina_Bool ret = EINA_TRUE;
1203 Eobj_Callback_Description *cb;
1206 obj->walking_list++;
1208 EINA_INLIST_FOREACH(obj->callbacks, cb)
1210 if (!cb->delete_me && (cb->event == desc))
1212 /* Abort callback calling if the func says so. */
1213 if (!cb->func((void *) cb->func_data, obj, desc,
1214 (void *) event_info))
1223 obj->walking_list--;
1224 _eobj_callbacks_clear(obj);
1231 _eobj_event_forwarder_callback(void *data, Eobj *obj, const Eobj_Event_Description *desc, void *event_info)
1234 Eobj *new_obj = (Eobj *) data;
1235 return eobj_event_callback_call(new_obj, desc, event_info);
1238 /* FIXME: Change default priority? Maybe call later? */
1240 eobj_event_callback_forwarder_add(Eobj *obj, const Eobj_Event_Description *desc, Eobj *new_obj)
1242 return eobj_event_callback_add(obj, desc, _eobj_event_forwarder_callback, new_obj);
1246 eobj_event_callback_forwarder_del(Eobj *obj, const Eobj_Event_Description *desc, Eobj *new_obj)
1248 eobj_event_callback_del_full(obj, desc, _eobj_event_forwarder_callback, new_obj);
1252 /* EOBJ_CLASS_BASE stuff */
1253 static Eobj_Class *_my_class = NULL;
1255 /* FIXME: Set proper type descriptions. */
1256 EAPI const Eobj_Event_Description _EOBJ_EV_CALLBACK_ADD =
1257 EOBJ_EVENT_DESCRIPTION("callback,add", "?", "Called when a callback was added.");
1258 EAPI const Eobj_Event_Description _EOBJ_EV_CALLBACK_DEL =
1259 EOBJ_EVENT_DESCRIPTION("callback,del", "?", "Called when a callback was deleted.");
1262 _constructor(Eobj *obj, void *class_data __UNUSED__)
1264 DBG("%p - %s.", obj, _my_class->desc->name);
1268 _destructor(Eobj *obj, void *class_data __UNUSED__)
1270 DBG("%p - %s.", obj, _my_class->desc->name);
1273 EAPI const Eobj_Class *
1274 eobj_base_class_get(void)
1276 if (_my_class) return _my_class;
1278 static const Eobj_Event_Description *event_desc[] = {
1279 EOBJ_EV_CALLBACK_ADD,
1280 EOBJ_EV_CALLBACK_DEL,
1284 static const Eobj_Class_Description class_desc = {
1286 EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT,
1287 EOBJ_CLASS_DESCRIPTION_OPS(NULL, NULL, 0),
1296 return _my_class = eobj_class_new(&class_desc, NULL, NULL);