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