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