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