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