161c7c9ac39b5e54a4b29e250952c0a29fbc7106
[profile/ivi/eobj.git] / lib / eobj.c
1 #include <Eina.h>
2
3 #include "Eobj.h"
4
5 static int _eobj_log_dom = -1;
6
7 static Eobj_Class **_eobj_classes;
8 static Eobj_Class_Id _eobj_classes_last_id;
9 static Eina_Bool _eobj_init_count = 0;
10
11 #define CONSTRUCT_ERROR_KEY "__construct_error"
12
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
18 #ifdef CRITICAL
19 #undef CRITICAL
20 #endif
21 #define CRITICAL(...) EINA_LOG_DOM_CRIT(_eobj_log_dom, __VA_ARGS__)
22
23 #ifdef ERR
24 #undef ERR
25 #endif
26 #define ERR(...) EINA_LOG_DOM_ERR(_eobj_log_dom, __VA_ARGS__)
27
28 #ifdef WRN
29 #undef WRN
30 #endif
31 #define WRN(...) EINA_LOG_DOM_WARN(_eobj_log_dom, __VA_ARGS__)
32
33 #ifdef INF
34 #undef INF
35 #endif
36 #define INF(...) EINA_LOG_DOM_INFO(_eobj_log_dom, __VA_ARGS__)
37
38 #ifdef DBG
39 #undef DBG
40 #endif
41 #define DBG(...) EINA_LOG_DOM_DBG(_eobj_log_dom, __VA_ARGS__)
42
43 typedef struct _Eobj_Callback_Description Eobj_Callback_Description;
44
45 struct _Eobj {
46      Eobj *parent;
47      const Eobj_Class *klass;
48      void *data_blob;
49      void **datas;
50      int refcount;
51      Eina_List *composite_objects;
52
53      Eina_Inlist *callbacks;
54      int walking_list;
55
56      Eina_Inlist *generic_data;
57
58      const Eobj_Class **kls_itr;
59
60      Eina_Bool delete:1;
61      EINA_MAGIC
62 };
63
64 /* Start of Dich */
65 /* Dich search, split to 0xff 0xff 0xffff */
66
67 #define DICH_CHAIN1_MASK (0xff)
68 #define DICH_CHAIN2_MASK (0xff)
69 #define DICH_CHAIN_LAST_MASK (0xffff)
70 #define DICH_CHAIN1_SIZE (DICH_CHAIN1_MASK + 1)
71 #define DICH_CHAIN2_SIZE (DICH_CHAIN2_MASK + 1)
72 #define DICH_CHAIN_LAST_SIZE (DICH_CHAIN_LAST_MASK + 1)
73 #define DICH_CHAIN1(x) (((x) >> 24) & DICH_CHAIN1_MASK)
74 #define DICH_CHAIN2(x) (((x) >> 16) & DICH_CHAIN2_MASK)
75 #define DICH_CHAIN_LAST(x) ((x) & DICH_CHAIN_LAST_MASK)
76
77 #define OP_CLASS_OFFSET 16
78 #define OP_CLASS_OFFSET_GET(x) (((x) >> OP_CLASS_OFFSET) & 0xffff)
79 #define OP_CLASS_GET(op) ({ \
80       Eobj_Class_Id tmp = OP_CLASS_OFFSET_GET(op); \
81       (Eobj_Class *) ((tmp <= _eobj_classes_last_id) && (tmp > 0)) ? \
82       (_eobj_classes[tmp - 1]) : NULL; \
83       })
84
85 /* Structure of Eobj_Op is:
86  * 16bit: class
87  * 16bit: op.
88  */
89
90 typedef struct _Dich_Chain1 Dich_Chain1;
91
92 typedef struct
93 {
94    eobj_op_func_type func;
95 } op_type_funcs;
96
97 typedef struct
98 {
99    op_type_funcs *funcs;
100 } Dich_Chain2;
101
102 struct _Dich_Chain1
103 {
104    Dich_Chain2 *chain;
105 };
106
107 typedef struct
108 {
109      EINA_INLIST;
110      const Eobj_Class *klass;
111      Eina_Bool exists : 1; /* < True if already exists in class (incl parents). */
112 } Eobj_Extension_Node;
113
114 struct _Eobj_Class
115 {
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;
121
122    const Eobj_Class **mro;
123
124    Eina_Bool constructed : 1;
125 };
126
127 static inline eobj_op_func_type
128 dich_func_get(const Eobj_Class *klass, Eobj_Op op)
129 {
130    const Dich_Chain1 *chain1 = &klass->chain[DICH_CHAIN1(op)];
131    if (!chain1) return NULL;
132    if (!chain1->chain) return NULL;
133    Dich_Chain2 *chain2 = &chain1->chain[DICH_CHAIN2(op)];
134    if (!chain2) return NULL;
135    if (!chain2->funcs) return NULL;
136
137    /* num_ops is calculated from the class. */
138    const Eobj_Class *op_klass = OP_CLASS_GET(op);
139    if (!op_klass || (DICH_CHAIN_LAST(op) >= op_klass->desc->ops.count))
140       return NULL;
141
142    return chain2->funcs[DICH_CHAIN_LAST(op)].func;
143 }
144
145 static inline void
146 dich_func_set(Eobj_Class *klass, Eobj_Op op, eobj_op_func_type func)
147 {
148    const Eobj_Class *op_klass = OP_CLASS_GET(op);
149    size_t num_ops;
150
151    /* Verify op is valid. */
152    if (op_klass)
153      {
154         /* num_ops is calculated from the class. */
155         num_ops = op_klass->desc->ops.count;
156         if (DICH_CHAIN_LAST(op) >= num_ops)
157           {
158              ERR("OP %x is too big for the domain '%s', expected value < %x.",
159                    op, op_klass->desc->name, op_klass->desc->ops.count);
160           }
161      }
162    else
163      {
164         ERR("OP %x is from an illegal class.", op);
165         return;
166      }
167
168    Dich_Chain1 *chain1 = &klass->chain[DICH_CHAIN1(op)];
169    if (!chain1->chain)
170      {
171         klass->chain[DICH_CHAIN1(op)].chain =
172            chain1->chain =
173            calloc(DICH_CHAIN2_SIZE, sizeof(*(chain1->chain)));
174      }
175
176    Dich_Chain2 *chain2 = &chain1->chain[DICH_CHAIN2(op)];
177    if (!chain2->funcs)
178      {
179         chain2->funcs = chain1->chain[DICH_CHAIN2(op)].funcs =
180            calloc(num_ops, sizeof(*(chain2->funcs)));
181      }
182
183    chain2->funcs[DICH_CHAIN_LAST(op)].func = func;
184 }
185
186 static inline void
187 dich_func_clean_all(Eobj_Class *klass)
188 {
189    int i;
190    Dich_Chain1 *chain1 = klass->chain;
191
192    for (i = 0 ; i < DICH_CHAIN1_SIZE ; i++, chain1++)
193      {
194         int j;
195         Dich_Chain2 *chain2 = chain1->chain;
196
197         if (!chain2)
198            continue;
199
200         for (j = 0 ; j < DICH_CHAIN2_SIZE ; j++, chain2++)
201           {
202              free(chain2->funcs);
203           }
204         free(chain1->chain);
205         chain1->chain = NULL;
206      }
207 }
208
209 /* END OF DICH */
210
211 /* FIXME: Decide if it should be fast, and if so, add a mapping.
212  * Otherwise, this is very slow. But since it's only for debugging... */
213 static const Eobj_Op_Description *
214 _eobj_op_id_desc_get(Eobj_Op op)
215 {
216    int i;
217    Eobj_Class **cls_itr = _eobj_classes;
218
219    for (i = 0 ; i < _eobj_classes_last_id ; i++, cls_itr++)
220      {
221         if (*cls_itr)
222           {
223              const Eobj_Op_Description *desc = (*cls_itr)->desc->ops.descs;
224              if (!desc)
225                 continue;
226
227              Eobj_Op base_op_id = *(*cls_itr)->desc->ops.base_op_id;
228              while (desc->sub_op)
229                {
230                   if ((base_op_id + desc->sub_op) == op)
231                      return desc;
232                   desc++;
233                }
234           }
235      }
236
237    return NULL;
238 }
239
240 static Eina_Bool
241 _eobj_op_internal(Eobj *obj, const Eobj_Class *obj_klass, Eobj_Op op, va_list *p_list)
242 {
243    const Eobj_Class *klass = obj_klass;
244    eobj_op_func_type func;
245
246    if (!obj_klass)
247       return EINA_FALSE;
248
249    while (klass)
250      {
251         func = dich_func_get(klass, op);
252
253         if (func)
254           {
255              func(obj, op, p_list);
256              return EINA_TRUE;
257           }
258         else
259           {
260              klass = klass->parent;
261           }
262      }
263
264    if (!klass)
265      {
266         klass = obj_klass;
267         /* FIXME: Should probably flatten everything. to be faster. */
268           {
269              /* Try MIXINS */
270              Eobj_Extension_Node *itr;
271              EINA_INLIST_FOREACH(klass->extensions, itr)
272                {
273                   if (_eobj_op_internal(obj, itr->klass, op, p_list))
274                     {
275                        return EINA_TRUE;
276                     }
277                }
278           }
279
280         /* Try composite objects */
281           {
282              Eina_List *itr;
283              Eobj *emb_obj;
284              EINA_LIST_FOREACH(obj->composite_objects, itr, emb_obj)
285                {
286                   if (_eobj_op_internal(emb_obj, eobj_class_get(emb_obj), op, p_list))
287                     {
288                        return EINA_TRUE;
289                     }
290                }
291           }
292
293         /* If haven't found anything, return FALSE */
294         return EINA_FALSE;
295      }
296
297    return EINA_TRUE;
298 }
299
300 static inline Eina_Bool
301 _eobj_ops_internal(Eobj *obj, const Eobj_Class *obj_klass, va_list *p_list)
302 {
303    Eina_Bool ret = EINA_TRUE;
304    Eobj_Op op = 0;
305
306    op = va_arg(*p_list, Eobj_Op);
307    while (op)
308      {
309         if (!_eobj_op_internal(obj, obj_klass, op, p_list))
310           {
311              const Eobj_Op_Description *desc = _eobj_op_id_desc_get(op);
312              const char *_id_name = (desc) ? desc->name : NULL;
313              const Eobj_Class *op_klass = OP_CLASS_GET(op);
314              const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL;
315              ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.",
316                    op, _id_name, _dom_name,
317                    obj_klass->desc->name);
318              ret = EINA_FALSE;
319              break;
320           }
321         op = va_arg(*p_list, Eobj_Op);
322      }
323
324    return ret;
325 }
326
327 EAPI Eina_Bool
328 eobj_do_internal(Eobj *obj, ...)
329 {
330    Eina_Bool ret;
331    va_list p_list;
332    va_start(p_list, obj);
333    ret = _eobj_ops_internal(obj, eobj_class_get(obj), &p_list);
334    va_end(p_list);
335    return ret;
336 }
337
338 EAPI Eina_Bool
339 eobj_class_do_internal(Eobj *obj, const Eobj_Class *klass, ...)
340 {
341    Eina_Bool ret;
342    va_list p_list;
343    va_start(p_list, klass);
344    ret = _eobj_ops_internal(obj, klass, &p_list);
345    va_end(p_list);
346    return ret;
347 }
348
349 EAPI const Eobj_Class *
350 eobj_class_get(Eobj *obj)
351 {
352    return obj->klass;
353 }
354
355 EAPI const Eobj_Class *
356 eobj_class_parent_get(const Eobj_Class *klass)
357 {
358    return klass->parent;
359 }
360
361 EAPI const char *
362 eobj_class_name_get(const Eobj_Class *klass)
363 {
364    return klass->desc->name;
365 }
366
367 static void
368 _eobj_class_base_op_init(Eobj_Class *klass)
369 {
370    const Eobj_Class_Description *desc = klass->desc;
371    if (!desc || !desc->ops.base_op_id)
372       return;
373
374    /* FIXME: Depends on values defined above! */
375    *(desc->ops.base_op_id) = klass->class_id << OP_CLASS_OFFSET;
376 }
377
378 static Eina_List *
379 _eobj_class_mro_add(Eina_List *mro, const Eobj_Class *klass)
380 {
381    if (!klass)
382       return mro;
383
384    mro = eina_list_append(mro, klass);
385
386      {
387         Eobj_Extension_Node *extn;
388         EINA_INLIST_FOREACH(klass->extensions, extn)
389           {
390              mro = _eobj_class_mro_add(mro, extn->klass);
391           }
392      }
393
394    mro = _eobj_class_mro_add(mro, klass->parent);
395
396    return mro;
397 }
398
399 static void
400 _eobj_class_mro_init(Eobj_Class *klass)
401 {
402    Eina_List *mro = NULL;
403
404    DBG("Started creating MRO for class '%s'", klass->desc->name);
405    mro = _eobj_class_mro_add(mro, klass);
406
407      {
408         Eina_List *itr1, *itr2, *itr2n;
409
410         itr1 = eina_list_last(mro);
411         while (itr1)
412           {
413              itr2 = eina_list_prev(itr1);
414
415              while (itr2)
416                {
417                   itr2n = eina_list_prev(itr2);
418
419                   if (eina_list_data_get(itr1) == eina_list_data_get(itr2))
420                     {
421                        mro = eina_list_remove_list(mro, itr2);
422                     }
423
424                   itr2 = itr2n;
425                }
426
427              itr1 = eina_list_prev(itr1);
428           }
429      }
430
431    /* Copy the mro and free the list. */
432      {
433         const Eobj_Class *kls_itr;
434         const Eobj_Class **mro_itr;
435         klass->mro = calloc(sizeof(*klass->mro), eina_list_count(mro) + 1);
436
437         mro_itr = klass->mro;
438
439         EINA_LIST_FREE(mro, kls_itr)
440           {
441              *(mro_itr++) = kls_itr;
442
443              DBG("Added '%s' to MRO", kls_itr->desc->name);
444           }
445         *(mro_itr) = NULL;
446      }
447
448    DBG("Finished creating MRO for class '%s'", klass->desc->name);
449 }
450
451 static void
452 _eobj_class_constructor(Eobj_Class *klass)
453 {
454    if (klass->constructed)
455       return;
456
457    klass->constructed = EINA_TRUE;
458
459    if (klass->desc->class_constructor)
460       klass->desc->class_constructor(klass);
461
462    _eobj_class_mro_init(klass);
463 }
464
465 EAPI void
466 eobj_class_funcs_set(Eobj_Class *klass, const Eobj_Op_Func_Description *func_descs)
467 {
468    const Eobj_Op_Func_Description *itr;
469    itr = func_descs;
470    if (itr)
471      {
472         for ( ; itr->op != 0 ; itr++)
473           {
474              dich_func_set(klass, itr->op, itr->func);
475           }
476      }
477 }
478
479 static Eina_Bool
480 _eobj_class_extn_exists(const Eobj_Class *klass, const Eobj_Class *extn_cls)
481 {
482    while (klass)
483      {
484         Eobj_Extension_Node *extn;
485         EINA_INLIST_FOREACH(klass->extensions, extn)
486           {
487              if (extn->klass == extn_cls)
488                 return EINA_TRUE;
489           }
490
491         klass = klass->parent;
492      }
493
494    return EINA_FALSE;
495 }
496
497 EAPI Eobj_Class *
498 eobj_class_new(const Eobj_Class_Description *desc, const Eobj_Class *parent, ...)
499 {
500    Eobj_Class *klass;
501    va_list p_list;
502
503    va_start(p_list, parent);
504
505 #define _CLS_NEW_CHECK(x) \
506    do \
507      { \
508         if (!x) \
509           { \
510              ERR("%s can't be NULL! Aborting.", #x); \
511              return NULL; \
512           } \
513      } \
514    while(0)
515
516    _CLS_NEW_CHECK(desc);
517    _CLS_NEW_CHECK(desc->name);
518
519    klass = calloc(1, sizeof(Eobj_Class));
520    klass->parent = parent;
521    klass->class_id = ++_eobj_classes_last_id;
522      {
523         /* FIXME: Handle errors. */
524         Eobj_Class **tmp;
525         tmp = realloc(_eobj_classes, _eobj_classes_last_id * sizeof(*_eobj_classes));
526         _eobj_classes = tmp;
527         _eobj_classes[klass->class_id - 1] = klass;
528      }
529
530    /* Handle class extensions */
531      {
532         Eobj_Class *extn = NULL;
533
534         extn = va_arg(p_list, Eobj_Class *);
535         while (extn)
536           {
537              switch (extn->desc->type)
538                {
539                 case EOBJ_CLASS_TYPE_REGULAR:
540                 case EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT:
541                    /* Use it like an interface. */
542                 case EOBJ_CLASS_TYPE_INTERFACE:
543                    break;
544                 case EOBJ_CLASS_TYPE_MIXIN:
545                      {
546                         Eobj_Extension_Node *node = calloc(1, sizeof(*node));
547                         node->klass = extn;
548                         node->exists = _eobj_class_extn_exists(klass, extn);
549                         klass->extensions =
550                            eina_inlist_append(klass->extensions,
551                                  EINA_INLIST_GET(node));
552                      }
553                    break;
554                }
555
556              extn = va_arg(p_list, Eobj_Class *);
557           }
558      }
559
560    klass->desc = desc;
561    _eobj_class_base_op_init(klass);
562
563    /* FIXME: Shouldn't be called here - should be called from eobj_add. */
564    _eobj_class_constructor(klass);
565
566    va_end(p_list);
567
568    return klass;
569 }
570 #undef _CLS_NEW_CHECK
571
572 EAPI void
573 eobj_class_free(Eobj_Class *klass)
574 {
575    if (klass->constructed)
576      {
577         if (klass->desc->class_destructor)
578            klass->desc->class_destructor(klass);
579
580         dich_func_clean_all(klass);
581      }
582
583      {
584         Eina_Inlist *itrn;
585         Eobj_Extension_Node *extn;
586         EINA_INLIST_FOREACH_SAFE(klass->extensions, itrn, extn)
587           {
588              free(extn);
589           }
590      }
591
592    free(klass->mro);
593
594    free(klass);
595 }
596
597 /* FIXME: Do I still need count parents? */
598 static int
599 _eobj_class_count_parents(const Eobj_Class *klass)
600 {
601    int count = 0;
602
603    for (count = 0 ; klass->parent ; klass = klass->parent)
604       count++;
605
606    return count;
607 }
608
609 static void
610 _eobj_kls_itr_init(Eobj *obj)
611 {
612    obj->kls_itr = obj->klass->mro;
613 }
614
615 EAPI Eobj *
616 eobj_add(const Eobj_Class *klass, Eobj *parent)
617 {
618    if (klass->desc->type != EOBJ_CLASS_TYPE_REGULAR)
619      {
620         ERR("Class '%s' is not instantiate-able. Aborting.", klass->desc->name);
621         return NULL;
622      }
623
624    Eobj *obj = calloc(1, sizeof(*obj));
625    obj->klass = klass;
626    obj->parent = parent;
627
628    obj->refcount++;
629      {
630         size_t datas_count = 0;
631         intptr_t offset = 0;
632         size_t i;
633         const Eobj_Class *kls_itr;
634         void **pvt_itr;
635         datas_count = _eobj_class_count_parents(klass) + 1;
636
637         obj->datas = calloc(datas_count, sizeof(*(obj->datas)));
638
639         /* Calculate all the offsets and set in the datas array. */
640         pvt_itr = obj->datas + datas_count - 1;
641         for (kls_itr = klass ; kls_itr->parent ; kls_itr = kls_itr->parent)
642            {
643               *pvt_itr = (void *) offset;
644
645               /* FIXME: Make sure this alignment is enough. */
646               offset += kls_itr->desc->private_size +
647                  (sizeof(void *) -
648                   (kls_itr->desc->private_size % sizeof(void *)));
649               pvt_itr--;
650            }
651
652         /* Allocate the datas blob and update the offsets. */
653         obj->data_blob = calloc(1, offset);
654
655         pvt_itr = obj->datas;
656         for (i = 0 ; i < datas_count ; i++)
657           {
658              *pvt_itr = ((char *) obj->data_blob) + (intptr_t) *pvt_itr;
659
660               pvt_itr++;
661           }
662      }
663
664    _eobj_kls_itr_init(obj);
665    eobj_class_constructor(obj, klass);
666    if (*obj->kls_itr && *(obj->kls_itr + 1))
667      {
668         ERR("Type '%s' - Not all of the object constructors have been executed.", klass->desc->name);
669         goto fail;
670      }
671
672    if (eobj_generic_data_get(obj, CONSTRUCT_ERROR_KEY))
673      {
674         ERR("Type '%s' - One of the object constructors have failed.", klass->desc->name);
675         goto fail;
676      }
677
678    return obj;
679
680 fail:
681    eobj_unref(obj);
682    return NULL;
683 }
684
685 EAPI Eobj *
686 eobj_ref(Eobj *obj)
687 {
688    obj->refcount++;
689    return obj;
690 }
691
692 EAPI void
693 eobj_unref(Eobj *obj)
694 {
695    if (--(obj->refcount) == 0)
696      {
697         const Eobj_Class *klass = eobj_class_get(obj);
698         _eobj_kls_itr_init(obj);
699         eobj_class_destructor(obj, klass);
700         /*FIXME: add eobj_class_unref(klass) ? - just to clear the caches. */
701
702         Eina_List *itr, *itr_n;
703         Eobj *emb_obj;
704         EINA_LIST_FOREACH_SAFE(obj->composite_objects, itr, itr_n, emb_obj)
705           {
706              eobj_del(emb_obj);
707              obj->composite_objects =
708                 eina_list_remove_list(obj->composite_objects, itr);
709           }
710
711         _eobj_callback_remove_all(obj);
712
713         if (obj->data_blob)
714            free(obj->data_blob);
715         free(obj->datas);
716
717         _eobj_generic_data_del_all(obj);
718
719         free(obj);
720      }
721 }
722
723 EAPI void
724 eobj_del(Eobj *obj)
725 {
726    obj->delete = EINA_TRUE;
727    eobj_unref(obj);
728 }
729
730 EAPI Eobj *
731 eobj_parent_get(Eobj *obj)
732 {
733    return obj->parent;
734 }
735
736 EAPI void
737 eobj_constructor_error_set(Eobj *obj)
738 {
739    eobj_generic_data_set(obj, CONSTRUCT_ERROR_KEY, (void *) EINA_TRUE);
740 }
741
742 EAPI Eina_Bool
743 eobj_constructor_error_get(const Eobj *obj)
744 {
745    return (intptr_t) eobj_generic_data_get(obj, CONSTRUCT_ERROR_KEY);
746 }
747
748 static inline void
749 _eobj_constructor_default(Eobj *obj)
750 {
751    eobj_constructor_super(obj);
752 }
753
754 static inline void
755 _eobj_destructor_default(Eobj *obj)
756 {
757    eobj_destructor_super(obj);
758 }
759
760 static void
761 eobj_class_constructor(Eobj *obj, const Eobj_Class *klass)
762 {
763    if (!klass)
764       return;
765
766    if (klass->desc->constructor)
767       klass->desc->constructor(obj);
768    else
769       _eobj_constructor_default(obj);
770 }
771
772 static void
773 eobj_class_destructor(Eobj *obj, const Eobj_Class *klass)
774 {
775    if (!klass)
776       return;
777
778    if (klass->desc->destructor)
779       klass->desc->destructor(obj);
780    else
781       _eobj_destructor_default(obj);
782 }
783
784 EAPI void
785 eobj_constructor_super(Eobj *obj)
786 {
787    if (*obj->kls_itr)
788       eobj_class_constructor(obj, *++(obj->kls_itr));
789 }
790
791 EAPI void
792 eobj_destructor_super(Eobj *obj)
793 {
794    if (*obj->kls_itr)
795       eobj_class_destructor(obj, *++(obj->kls_itr));
796 }
797
798 EAPI void *
799 eobj_data_get(Eobj *obj, const Eobj_Class *klass)
800 {
801    /* FIXME: Add a check that this is of the right klass and we don't seg.
802     * Probably just return NULL. */
803    return obj->datas[_eobj_class_count_parents(klass)];
804 }
805
806 typedef struct
807 {
808    EINA_INLIST;
809    Eina_Stringshare *key;
810    void *data;
811 } Eobj_Generic_Data_Node;
812
813 static void
814 _eobj_generic_data_node_free(Eobj_Generic_Data_Node *node)
815 {
816    eina_stringshare_del(node->key);
817    free(node);
818 }
819
820 static void
821 _eobj_generic_data_del_all(Eobj *obj)
822 {
823    Eina_Inlist *nnode;
824    Eobj_Generic_Data_Node *node;
825
826    EINA_INLIST_FOREACH_SAFE(obj->generic_data, nnode, node)
827      {
828         obj->generic_data = eina_inlist_remove(obj->generic_data,
829               EINA_INLIST_GET(node));
830
831         _eobj_generic_data_node_free(node);
832      }
833 }
834
835 EAPI void *
836 eobj_generic_data_set(Eobj *obj, const char *key, const void *data)
837 {
838    void *prev_data;
839    Eobj_Generic_Data_Node *node;
840
841    if (!key) return NULL;
842    if (!data) return NULL;
843
844    prev_data = eobj_generic_data_del(obj, key);
845
846    node = malloc(sizeof(Eobj_Generic_Data_Node));
847    node->key = eina_stringshare_add(key);
848    node->data = (void *) data;
849    obj->generic_data = eina_inlist_prepend(obj->generic_data,
850          EINA_INLIST_GET(node));
851
852    return prev_data;
853 }
854
855 EAPI void *
856 eobj_generic_data_get(const Eobj *obj, const char *key)
857 {
858    Eobj_Generic_Data_Node *node;
859
860    if (!key) return NULL;
861
862    EINA_INLIST_FOREACH(obj->generic_data, node)
863      {
864         if (!strcmp(node->key, key))
865           {
866              ((Eobj *) obj)->generic_data =
867                 eina_inlist_promote(obj->generic_data, EINA_INLIST_GET(node));
868              return node->data;
869           }
870      }
871    return NULL;
872 }
873
874 EAPI void *
875 eobj_generic_data_del(Eobj *obj, const char *key)
876 {
877    Eobj_Generic_Data_Node *node;
878
879    if (!key) return NULL;
880
881    EINA_INLIST_FOREACH(obj->generic_data, node)
882      {
883         if (!strcmp(node->key, key))
884           {
885              void *data;
886
887              data = node->data;
888              obj->generic_data = eina_inlist_remove(obj->generic_data,
889                    EINA_INLIST_GET(node));
890              _eobj_generic_data_node_free(node);
891              return data;
892           }
893      }
894    return NULL;
895 }
896
897 EAPI Eina_Bool
898 eobj_init(void)
899 {
900    if (_eobj_init_count++ > 0)
901       return EINA_TRUE;
902
903    eina_init();
904
905    _eobj_classes = NULL;
906    _eobj_classes_last_id = 0;
907    _eobj_log_dom = eina_log_domain_register("eobj", EINA_COLOR_LIGHTBLUE);
908    if (_eobj_log_dom < 0)
909      {
910         EINA_LOG_ERR("Could not register log domain: eobj");
911         return EINA_FALSE;
912      }
913
914    return EINA_TRUE;
915 }
916
917 EAPI Eina_Bool
918 eobj_shutdown(void)
919 {
920    int i;
921    Eobj_Class **cls_itr = _eobj_classes;
922
923    if (--_eobj_init_count > 0)
924       return EINA_TRUE;
925
926    for (i = 0 ; i < _eobj_classes_last_id ; i++, cls_itr++)
927      {
928         if (*cls_itr)
929            eobj_class_free(*cls_itr);
930      }
931
932    if (_eobj_classes)
933       free(_eobj_classes);
934
935    eina_log_domain_unregister(_eobj_log_dom);
936    _eobj_log_dom = -1;
937
938    eina_shutdown();
939    return EINA_TRUE;
940 }
941
942 EAPI void
943 eobj_composite_object_attach(Eobj *obj, Eobj *emb_obj)
944 {
945    eobj_ref(emb_obj);
946    obj->composite_objects = eina_list_prepend(obj->composite_objects, emb_obj);
947 }
948
949 EAPI void
950 eobj_composite_object_detach(Eobj *obj, Eobj *emb_obj)
951 {
952    obj->composite_objects = eina_list_remove(obj->composite_objects, emb_obj);
953    eobj_unref(emb_obj);
954 }
955
956 EAPI Eina_Bool
957 eobj_composite_is(Eobj *emb_obj)
958 {
959    Eobj *obj = eobj_parent_get(emb_obj);
960    Eina_List *itr;
961    Eobj *tmp;
962    EINA_LIST_FOREACH(obj->composite_objects, itr, tmp)
963      {
964         if (tmp == emb_obj)
965            return EINA_TRUE;
966      }
967
968    return EINA_FALSE;
969 }
970
971 /* Callbacks */
972 struct _Eobj_Callback_Description
973 {
974    EINA_INLIST;
975    const Eobj_Event_Description *event;
976    Eobj_Event_Cb func;
977    void *func_data;
978    Eobj_Callback_Priority priority;
979    Eina_Bool delete_me : 1;
980 };
981
982 /* Actually remove, doesn't care about walking list, or delete_me */
983 static void
984 _eobj_callback_remove(Eobj *obj, Eobj_Callback_Description *cb)
985 {
986    obj->callbacks = eina_inlist_remove(obj->callbacks,
987          EINA_INLIST_GET(cb));
988    free(cb);
989 }
990
991 /* Actually remove, doesn't care about walking list, or delete_me */
992 static void
993 _eobj_callback_remove_all(Eobj *obj)
994 {
995    Eina_Inlist *initr;
996    Eobj_Callback_Description *cb;
997    EINA_INLIST_FOREACH_SAFE(obj->callbacks, initr, cb)
998      {
999         _eobj_callback_remove(obj, cb);
1000      }
1001 }
1002
1003 static void
1004 _eobj_callbacks_clear(Eobj *obj)
1005 {
1006    Eina_Inlist *itn;
1007    Eobj_Callback_Description *cb;
1008
1009    /* Abort if we are currently walking the list. */
1010    if (obj->walking_list > 0)
1011       return;
1012
1013    EINA_INLIST_FOREACH_SAFE(obj->callbacks, itn, cb)
1014      {
1015         if (cb->delete_me)
1016           {
1017              _eobj_callback_remove(obj, cb);
1018           }
1019      }
1020 }
1021
1022 static int
1023 _callback_priority_cmp(const void *_a, const void *_b)
1024 {
1025    const Eobj_Callback_Description *a, *b;
1026    a = (const Eobj_Callback_Description *) _a;
1027    b = (const Eobj_Callback_Description *) _b;
1028    if (a->priority < b->priority)
1029       return -1;
1030    else
1031       return 1;
1032 }
1033
1034 EAPI Eina_Bool
1035 eobj_event_callback_add(Eobj *obj,
1036       const Eobj_Event_Description *desc,
1037       Eobj_Event_Cb cb,
1038       const void *data)
1039 {
1040    return eobj_event_callback_priority_add(obj, desc,
1041          EOBJ_CALLBACK_PRIORITY_DEFAULT, cb, data);
1042 }
1043
1044 EAPI Eina_Bool
1045 eobj_event_callback_priority_add(Eobj *obj,
1046       const Eobj_Event_Description *desc,
1047       Eobj_Callback_Priority priority,
1048       Eobj_Event_Cb func,
1049       const void *data)
1050 {
1051    Eobj_Callback_Description *cb = calloc(1, sizeof(*cb));
1052    cb->event = desc;
1053    cb->func = func;
1054    cb->func_data = (void *) data;
1055    cb->priority = priority;
1056    obj->callbacks = eina_inlist_sorted_insert(obj->callbacks,
1057          EINA_INLIST_GET(cb), _callback_priority_cmp);
1058
1059    eobj_event_callback_call(obj, EOBJ_SIG_CALLBACK_ADD, desc);
1060
1061    return EINA_TRUE;
1062 }
1063
1064 EAPI void *
1065 eobj_event_callback_del(Eobj *obj, const Eobj_Event_Description *desc, Eobj_Event_Cb func)
1066 {
1067    void *ret = NULL;
1068    Eobj_Callback_Description *cb;
1069    EINA_INLIST_FOREACH(obj->callbacks, cb)
1070      {
1071         if ((cb->event == desc) && (cb->func == func))
1072           {
1073              void *data;
1074
1075              data = cb->func_data;
1076              cb->delete_me = EINA_TRUE;
1077              _eobj_callbacks_clear(obj);
1078              ret = data;
1079              goto end;
1080           }
1081      }
1082
1083 end:
1084    eobj_event_callback_call(obj, EOBJ_SIG_CALLBACK_DEL, desc);
1085    return ret;
1086 }
1087
1088 EAPI void *
1089 eobj_event_callback_del_full(Eobj *obj, const Eobj_Event_Description *desc, Eobj_Event_Cb func, const void *user_data)
1090 {
1091    void *ret = NULL;
1092    Eobj_Callback_Description *cb;
1093    EINA_INLIST_FOREACH(obj->callbacks, cb)
1094      {
1095         if ((cb->event == desc) && (cb->func == func) &&
1096               (cb->func_data == user_data))
1097           {
1098              void *data;
1099
1100              data = cb->func_data;
1101              cb->delete_me = EINA_TRUE;
1102              _eobj_callbacks_clear(obj);
1103              ret = data;
1104              goto end;
1105           }
1106      }
1107
1108 end:
1109    eobj_event_callback_call(obj, EOBJ_SIG_CALLBACK_DEL, desc);
1110    return ret;
1111 }
1112
1113 EAPI Eina_Bool
1114 eobj_event_callback_call(Eobj *obj, const Eobj_Event_Description *desc,
1115       const void *event_info)
1116 {
1117    Eobj_Callback_Description *cb;
1118
1119    obj->walking_list++;
1120
1121    EINA_INLIST_FOREACH(obj->callbacks, cb)
1122      {
1123         if (!cb->delete_me  && (cb->event == desc))
1124           {
1125              /* Abort callback calling if the func says so. */
1126              if (!cb->func((void *) cb->func_data, obj, desc,
1127                       (void *) event_info))
1128                {
1129                   break;
1130                }
1131           }
1132         if (obj->delete)
1133           break;
1134      }
1135    obj->walking_list--;
1136    _eobj_callbacks_clear(obj);
1137
1138    return EINA_TRUE;
1139 }
1140
1141 static Eina_Bool
1142 _eobj_event_forwarder_callback(void *data, Eobj *obj, const Eobj_Event_Description *desc, void *event_info)
1143 {
1144    (void) obj;
1145    Eobj *new_obj = (Eobj *) data;
1146    return eobj_event_callback_call(new_obj, desc, event_info);
1147 }
1148
1149 /* FIXME: Change default priority? Maybe call later? */
1150 EAPI Eina_Bool
1151 eobj_event_callback_forwarder_add(Eobj *obj, const Eobj_Event_Description *desc, Eobj *new_obj)
1152 {
1153    return eobj_event_callback_add(obj, desc, _eobj_event_forwarder_callback, new_obj);
1154 }
1155
1156 EAPI Eina_Bool
1157 eobj_event_callback_forwarder_del(Eobj *obj, const Eobj_Event_Description *desc, Eobj *new_obj)
1158 {
1159    eobj_event_callback_del_full(obj, desc, _eobj_event_forwarder_callback, new_obj);
1160    return EINA_TRUE;
1161 }
1162
1163 /* EOBJ_CLASS_BASE stuff */
1164 static Eobj_Class *_my_class = NULL;
1165
1166 /* FIXME: Set proper type descriptions. */
1167 EAPI const Eobj_Event_Description _EOBJ_SIG_CALLBACK_ADD =
1168    EOBJ_EVENT_DESCRIPTION("callback,add", "?", "Called when a callback was added.");
1169 EAPI const Eobj_Event_Description _EOBJ_SIG_CALLBACK_DEL =
1170    EOBJ_EVENT_DESCRIPTION("callback,del", "?", "Called when a callback was deleted.");
1171
1172 static void
1173 _constructor(Eobj *obj)
1174 {
1175    DBG("%p - %s.", obj, _my_class->desc->name);
1176 }
1177
1178 static void
1179 _destructor(Eobj *obj)
1180 {
1181    DBG("%p - %s.", obj, _my_class->desc->name);
1182 }
1183
1184 EAPI const Eobj_Class *
1185 eobj_base_class_get(void)
1186 {
1187    if (_my_class) return _my_class;
1188
1189    static const Eobj_Class_Description class_desc = {
1190         "Eobj Base",
1191         EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT,
1192         EOBJ_CLASS_DESCRIPTION_OPS(NULL, NULL, 0),
1193         NULL,
1194         0,
1195         _constructor,
1196         _destructor,
1197         NULL,
1198         NULL
1199    };
1200
1201    return _my_class = eobj_class_new(&class_desc, NULL, NULL);
1202 }
1203