Eobj: Minor changes in eina log registration.
[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 static const Eobj_Op_Description *
296 _eobj_op_id_desc_get(Eobj_Op op)
297 {
298    const Eobj_Class *klass = OP_CLASS_GET(op);
299
300    if (!klass || !klass->desc->ops.base_op_id) return NULL;
301
302    Eobj_Op base_op_id = *klass->desc->ops.base_op_id;
303
304    const Eobj_Op_Description *desc = klass->desc->ops.descs;
305    size_t i;
306    for (i = 0 ; i < klass->desc->ops.count ; i++, desc++)
307      {
308         if ((base_op_id + desc->sub_op) == op)
309            return desc;
310      }
311
312    return NULL;
313 }
314
315 static Eina_Bool
316 _eobj_op_internal(Eobj *obj, Eobj_Op op, va_list *p_list)
317 {
318    const Eobj_Class *klass;
319    Eina_Bool ret = EINA_FALSE;
320    Eina_Bool _itr_init;
321
322    _itr_init = _eobj_kls_itr_init(obj, op);
323    klass = _eobj_kls_itr_get(obj);
324    while (klass)
325      {
326         eobj_op_func_type func = dich_func_get(klass, op);
327
328         if (func)
329           {
330              func(obj, eobj_data_get(obj, klass), p_list);
331              ret = EINA_TRUE;
332              goto end;
333           }
334
335         klass = _eobj_kls_itr_next(obj);
336      }
337
338    /* Try composite objects */
339      {
340         Eina_List *itr;
341         Eobj *emb_obj;
342         EINA_LIST_FOREACH(obj->composite_objects, itr, emb_obj)
343           {
344              if (_eobj_op_internal(emb_obj, op, p_list))
345                {
346                   ret = EINA_TRUE;
347                   goto end;
348                }
349           }
350      }
351
352 end:
353
354    if (_itr_init) _eobj_kls_itr_end(obj, op);
355    return ret;
356 }
357
358 static inline Eina_Bool
359 _eobj_ops_internal(Eobj *obj, va_list *p_list)
360 {
361    Eina_Bool ret = EINA_TRUE;
362    Eobj_Op op = 0;
363
364    op = va_arg(*p_list, Eobj_Op);
365    while (op)
366      {
367         if (!_eobj_op_internal(obj, op, p_list))
368           {
369              const Eobj_Op_Description *desc = _eobj_op_id_desc_get(op);
370              const char *_id_name = (desc) ? desc->name : NULL;
371              const Eobj_Class *op_klass = OP_CLASS_GET(op);
372              const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL;
373              ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.",
374                    op, _id_name, _dom_name,
375                    obj->klass->desc->name);
376              ret = EINA_FALSE;
377              break;
378           }
379         op = va_arg(*p_list, Eobj_Op);
380      }
381
382    return ret;
383 }
384
385 EAPI Eina_Bool
386 eobj_do_internal(Eobj *obj, ...)
387 {
388    Eina_Bool ret;
389    va_list p_list;
390    eobj_ref(obj);
391    va_start(p_list, obj);
392    ret = _eobj_ops_internal(obj, &p_list);
393    va_end(p_list);
394    eobj_unref(obj);
395    return ret;
396 }
397
398 EAPI Eina_Bool
399 eobj_super_do(Eobj *obj, Eobj_Op op, ...)
400 {
401    const Eobj_Class *obj_klass;
402    Eina_Bool ret = EINA_TRUE;
403    va_list p_list;
404
405    va_start(p_list, op);
406
407    /* Advance the kls itr. */
408    obj_klass = _eobj_kls_itr_next(obj);
409    if (!_eobj_op_internal(obj, op, &p_list))
410      {
411         const Eobj_Op_Description *desc = _eobj_op_id_desc_get(op);
412         const char *_id_name = (desc) ? desc->name : NULL;
413         const Eobj_Class *op_klass = OP_CLASS_GET(op);
414         const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL;
415         ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.",
416               op, _id_name, _dom_name,
417               (obj_klass) ? obj_klass->desc->name : NULL);
418         ret = EINA_FALSE;
419      }
420    va_end(p_list);
421
422    return ret;
423 }
424
425 EAPI const Eobj_Class *
426 eobj_class_get(Eobj *obj)
427 {
428    return obj->klass;
429 }
430
431 EAPI const char *
432 eobj_class_name_get(const Eobj_Class *klass)
433 {
434    return klass->desc->name;
435 }
436
437 static void
438 _eobj_class_base_op_init(Eobj_Class *klass)
439 {
440    const Eobj_Class_Description *desc = klass->desc;
441    if (!desc || !desc->ops.base_op_id)
442       return;
443
444    /* FIXME: Depends on values defined above! */
445    *(desc->ops.base_op_id) = klass->class_id << OP_CLASS_OFFSET;
446 }
447
448 static Eina_List *
449 _eobj_class_mro_add(Eina_List *mro, const Eobj_Class *klass)
450 {
451    if (!klass)
452       return mro;
453
454    mro = eina_list_append(mro, klass);
455
456      {
457         Eobj_Extension_Node *extn;
458         EINA_INLIST_FOREACH(klass->extensions, extn)
459           {
460              mro = _eobj_class_mro_add(mro, extn->klass);
461           }
462      }
463
464    mro = _eobj_class_mro_add(mro, klass->parent);
465
466    return mro;
467 }
468
469 static void
470 _eobj_class_mro_init(Eobj_Class *klass)
471 {
472    Eina_List *mro = NULL;
473
474    DBG("Started creating MRO for class '%s'", klass->desc->name);
475    mro = _eobj_class_mro_add(mro, klass);
476
477      {
478         Eina_List *itr1, *itr2, *itr2n;
479
480         itr1 = eina_list_last(mro);
481         while (itr1)
482           {
483              itr2 = eina_list_prev(itr1);
484
485              while (itr2)
486                {
487                   itr2n = eina_list_prev(itr2);
488
489                   if (eina_list_data_get(itr1) == eina_list_data_get(itr2))
490                     {
491                        mro = eina_list_remove_list(mro, itr2);
492                     }
493
494                   itr2 = itr2n;
495                }
496
497              itr1 = eina_list_prev(itr1);
498           }
499      }
500
501    /* Copy the mro and free the list. */
502      {
503         const Eobj_Class *kls_itr;
504         const Eobj_Class **mro_itr;
505         klass->mro = calloc(sizeof(*klass->mro), eina_list_count(mro) + 1);
506
507         mro_itr = klass->mro;
508
509         EINA_LIST_FREE(mro, kls_itr)
510           {
511              *(mro_itr++) = kls_itr;
512
513              DBG("Added '%s' to MRO", kls_itr->desc->name);
514           }
515         *(mro_itr) = NULL;
516      }
517
518    DBG("Finished creating MRO for class '%s'", klass->desc->name);
519 }
520
521 static void
522 _eobj_class_constructor(Eobj_Class *klass)
523 {
524    if (klass->constructed)
525       return;
526
527    klass->constructed = EINA_TRUE;
528
529    if (klass->desc->class_constructor)
530       klass->desc->class_constructor(klass);
531
532    _eobj_class_mro_init(klass);
533 }
534
535 EAPI void
536 eobj_class_funcs_set(Eobj_Class *klass, const Eobj_Op_Func_Description *func_descs)
537 {
538    const Eobj_Op_Func_Description *itr;
539    itr = func_descs;
540    if (itr)
541      {
542         for ( ; itr->op != 0 ; itr++)
543           {
544              dich_func_set(klass, itr->op, itr->func);
545           }
546      }
547 }
548
549 EAPI Eobj_Class *
550 eobj_class_new(const Eobj_Class_Description *desc, const Eobj_Class *parent, ...)
551 {
552    Eobj_Class *klass;
553    va_list p_list;
554
555    va_start(p_list, parent);
556
557 #define _CLS_NEW_CHECK(x) \
558    do \
559      { \
560         if (!x) \
561           { \
562              ERR("%s can't be NULL! Aborting.", #x); \
563              return NULL; \
564           } \
565      } \
566    while(0)
567
568    _CLS_NEW_CHECK(desc);
569    _CLS_NEW_CHECK(desc->name);
570
571    klass = calloc(1, sizeof(Eobj_Class));
572    klass->parent = parent;
573
574    /* Handle class extensions */
575      {
576         Eobj_Class *extn = NULL;
577
578         extn = va_arg(p_list, Eobj_Class *);
579         while (extn)
580           {
581              switch (extn->desc->type)
582                {
583                 case EOBJ_CLASS_TYPE_REGULAR:
584                 case EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT:
585                    /* Use it like an interface. */
586                 case EOBJ_CLASS_TYPE_INTERFACE:
587                    break;
588                 case EOBJ_CLASS_TYPE_MIXIN:
589                      {
590                         Eobj_Extension_Node *node = calloc(1, sizeof(*node));
591                         node->klass = extn;
592                         klass->extensions =
593                            eina_inlist_append(klass->extensions,
594                                  EINA_INLIST_GET(node));
595                      }
596                    break;
597                }
598
599              extn = va_arg(p_list, Eobj_Class *);
600           }
601      }
602
603    klass->desc = desc;
604
605    /* Handle the inheritance */
606    if (klass->parent)
607      {
608         /* Verify the inheritance is allowed. */
609         switch (klass->desc->type)
610           {
611            case EOBJ_CLASS_TYPE_REGULAR:
612            case EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT:
613               if ((klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR) &&
614                     (klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT))
615                 {
616                    /* FIXME: Actually handle it. */
617                    ERR("Regular classes ('%s') aren't allowed to inherit from non-regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
618                    goto cleanup;
619                 }
620               break;
621            case EOBJ_CLASS_TYPE_INTERFACE:
622            case EOBJ_CLASS_TYPE_MIXIN:
623               if ((klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR) &&
624                     (klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT))
625                 {
626                    /* FIXME: Actually handle it. */
627                    ERR("Non-regular classes ('%s') aren't allowed to inherit from regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
628                    goto cleanup;
629                 }
630               break;
631           }
632
633
634         /* Update the current offset. */
635         /* FIXME: Make sure this alignment is enough. */
636         klass->data_offset = klass->parent->data_offset +
637            klass->parent->desc->data_size +
638            (sizeof(void *) -
639                   (klass->parent->desc->data_size % sizeof(void *)));
640      }
641
642    klass->class_id = ++_eobj_classes_last_id;
643      {
644         /* FIXME: Handle errors. */
645         Eobj_Class **tmp;
646         tmp = realloc(_eobj_classes, _eobj_classes_last_id * sizeof(*_eobj_classes));
647         _eobj_classes = tmp;
648         _eobj_classes[klass->class_id - 1] = klass;
649      }
650
651    _eobj_class_base_op_init(klass);
652
653    /* FIXME: Shouldn't be called here - should be called from eobj_add. */
654    _eobj_class_constructor(klass);
655
656    va_end(p_list);
657
658    return klass;
659
660 cleanup:
661    eobj_class_free(klass);
662    return NULL;
663 }
664 #undef _CLS_NEW_CHECK
665
666 EAPI void
667 eobj_class_free(Eobj_Class *klass)
668 {
669    if (klass->constructed)
670      {
671         if (klass->desc->class_destructor)
672            klass->desc->class_destructor(klass);
673
674         dich_func_clean_all(klass);
675      }
676
677      {
678         Eina_Inlist *itrn;
679         Eobj_Extension_Node *extn;
680         EINA_INLIST_FOREACH_SAFE(klass->extensions, itrn, extn)
681           {
682              free(extn);
683           }
684      }
685
686    free(klass->mro);
687
688    free(klass);
689 }
690
691 EAPI Eobj *
692 eobj_add(const Eobj_Class *klass, Eobj *parent)
693 {
694    if (klass->desc->type != EOBJ_CLASS_TYPE_REGULAR)
695      {
696         ERR("Class '%s' is not instantiate-able. Aborting.", klass->desc->name);
697         return NULL;
698      }
699
700    Eobj *obj = calloc(1, sizeof(*obj));
701    obj->klass = klass;
702    obj->parent = parent;
703
704    obj->refcount++;
705
706    obj->data_blob = calloc(1, klass->data_offset + klass->desc->data_size);
707
708    _eobj_kls_itr_init(obj, EOBJ_NOOP);
709    eobj_constructor_error_unset(obj);
710
711    eobj_ref(obj);
712    eobj_class_constructor(obj, klass);
713
714    if (eobj_constructor_error_get(obj))
715      {
716         ERR("Type '%s' - One of the object constructors have failed.", klass->desc->name);
717         goto fail;
718      }
719
720    if (!_eobj_kls_itr_reached_end(obj))
721      {
722         ERR("Type '%s' - Not all of the object constructors have been executed.", klass->desc->name);
723         goto fail;
724      }
725    _eobj_kls_itr_end(obj, EOBJ_NOOP);
726    eobj_unref(obj);
727
728    return obj;
729
730 fail:
731    /* Unref twice, once for the ref above, and once for the basic object ref. */
732    eobj_unref(obj);
733    eobj_unref(obj);
734    return NULL;
735 }
736
737 EAPI Eobj *
738 eobj_ref(Eobj *obj)
739 {
740    obj->refcount++;
741    return obj;
742 }
743
744 EAPI void
745 eobj_unref(Eobj *obj)
746 {
747    if (--(obj->refcount) == 0)
748      {
749         const Eobj_Class *klass = eobj_class_get(obj);
750         _eobj_kls_itr_init(obj, EOBJ_NOOP);
751         eobj_constructor_error_unset(obj);
752         eobj_class_destructor(obj, klass);
753         if (eobj_constructor_error_get(obj))
754           {
755              ERR("Type '%s' - One of the object destructors have failed.", klass->desc->name);
756           }
757
758         if (!_eobj_kls_itr_reached_end(obj))
759           {
760              ERR("Type '%s' - Not all of the object destructors have been executed.", klass->desc->name);
761           }
762         _eobj_kls_itr_end(obj, EOBJ_NOOP);
763         /*FIXME: add eobj_class_unref(klass) ? - just to clear the caches. */
764
765         /* If for some reason it's not empty, clear it. */
766         while (obj->kls_itr)
767           {
768              WRN("Kls_Itr is not empty, possibly a bug, please report. - An error will be reported for each kls_itr in the stack.");
769              Eina_Inlist *nitr = nitr->next;
770              free(EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node));
771              obj->kls_itr = nitr;
772           }
773
774         Eina_List *itr, *itr_n;
775         Eobj *emb_obj;
776         EINA_LIST_FOREACH_SAFE(obj->composite_objects, itr, itr_n, emb_obj)
777           {
778              /* FIXME: Should probably be unref. */
779              eobj_del(emb_obj);
780              obj->composite_objects =
781                 eina_list_remove_list(obj->composite_objects, itr);
782           }
783
784         _eobj_callback_remove_all(obj);
785
786         if (obj->data_blob)
787            free(obj->data_blob);
788
789         _eobj_generic_data_del_all(obj);
790
791         free(obj);
792      }
793 }
794
795 EAPI int
796 eobj_ref_get(const Eobj *obj)
797 {
798    return obj->refcount;
799 }
800
801 EAPI void
802 eobj_del(Eobj *obj)
803 {
804    obj->delete = EINA_TRUE;
805    eobj_unref(obj);
806 }
807
808 EAPI Eobj *
809 eobj_parent_get(Eobj *obj)
810 {
811    return obj->parent;
812 }
813
814 EAPI void
815 eobj_constructor_error_set(Eobj *obj)
816 {
817    eobj_generic_data_set(obj, CONSTRUCT_ERROR_KEY, (void *) EINA_TRUE);
818 }
819
820 static void
821 eobj_constructor_error_unset(Eobj *obj)
822 {
823    eobj_generic_data_del(obj, CONSTRUCT_ERROR_KEY);
824 }
825
826 EAPI Eina_Bool
827 eobj_constructor_error_get(const Eobj *obj)
828 {
829    return (intptr_t) eobj_generic_data_get(obj, CONSTRUCT_ERROR_KEY);
830 }
831
832 static inline void
833 _eobj_constructor_default(Eobj *obj)
834 {
835    eobj_constructor_super(obj);
836 }
837
838 static inline void
839 _eobj_destructor_default(Eobj *obj)
840 {
841    eobj_destructor_super(obj);
842 }
843
844 static void
845 eobj_class_constructor(Eobj *obj, const Eobj_Class *klass)
846 {
847    if (!klass)
848       return;
849
850    if (klass->desc->constructor)
851       klass->desc->constructor(obj, eobj_data_get(obj, klass));
852    else
853       _eobj_constructor_default(obj);
854 }
855
856 static void
857 eobj_class_destructor(Eobj *obj, const Eobj_Class *klass)
858 {
859    if (!klass)
860       return;
861
862    if (klass->desc->destructor)
863       klass->desc->destructor(obj, eobj_data_get(obj, klass));
864    else
865       _eobj_destructor_default(obj);
866 }
867
868 EAPI void
869 eobj_constructor_super(Eobj *obj)
870 {
871    eobj_class_constructor(obj, _eobj_kls_itr_next(obj));
872 }
873
874 EAPI void
875 eobj_destructor_super(Eobj *obj)
876 {
877    eobj_class_destructor(obj, _eobj_kls_itr_next(obj));
878 }
879
880 EAPI void *
881 eobj_data_get(Eobj *obj, const Eobj_Class *klass)
882 {
883    /* FIXME: Add a check that this is of the right klass and we don't seg.
884     * Probably just return NULL. */
885    if (klass->desc->data_size > 0)
886       return ((char *) obj->data_blob) + klass->data_offset;
887    else
888       return NULL;
889 }
890
891 typedef struct
892 {
893    EINA_INLIST;
894    Eina_Stringshare *key;
895    void *data;
896 } Eobj_Generic_Data_Node;
897
898 static void
899 _eobj_generic_data_node_free(Eobj_Generic_Data_Node *node)
900 {
901    eina_stringshare_del(node->key);
902    free(node);
903 }
904
905 static void
906 _eobj_generic_data_del_all(Eobj *obj)
907 {
908    Eina_Inlist *nnode;
909    Eobj_Generic_Data_Node *node;
910
911    EINA_INLIST_FOREACH_SAFE(obj->generic_data, nnode, node)
912      {
913         obj->generic_data = eina_inlist_remove(obj->generic_data,
914               EINA_INLIST_GET(node));
915
916         _eobj_generic_data_node_free(node);
917      }
918 }
919
920 EAPI void *
921 eobj_generic_data_set(Eobj *obj, const char *key, const void *data)
922 {
923    void *prev_data;
924    Eobj_Generic_Data_Node *node;
925
926    if (!key) return NULL;
927    if (!data) return NULL;
928
929    prev_data = eobj_generic_data_del(obj, key);
930
931    node = malloc(sizeof(Eobj_Generic_Data_Node));
932    node->key = eina_stringshare_add(key);
933    node->data = (void *) data;
934    obj->generic_data = eina_inlist_prepend(obj->generic_data,
935          EINA_INLIST_GET(node));
936
937    return prev_data;
938 }
939
940 EAPI void *
941 eobj_generic_data_get(const Eobj *obj, const char *key)
942 {
943    Eobj_Generic_Data_Node *node;
944
945    if (!key) return NULL;
946
947    EINA_INLIST_FOREACH(obj->generic_data, node)
948      {
949         if (!strcmp(node->key, key))
950           {
951              ((Eobj *) obj)->generic_data =
952                 eina_inlist_promote(obj->generic_data, EINA_INLIST_GET(node));
953              return node->data;
954           }
955      }
956    return NULL;
957 }
958
959 EAPI void *
960 eobj_generic_data_del(Eobj *obj, const char *key)
961 {
962    Eobj_Generic_Data_Node *node;
963
964    if (!key) return NULL;
965
966    EINA_INLIST_FOREACH(obj->generic_data, node)
967      {
968         if (!strcmp(node->key, key))
969           {
970              void *data;
971
972              data = node->data;
973              obj->generic_data = eina_inlist_remove(obj->generic_data,
974                    EINA_INLIST_GET(node));
975              _eobj_generic_data_node_free(node);
976              return data;
977           }
978      }
979    return NULL;
980 }
981
982 EAPI Eina_Bool
983 eobj_init(void)
984 {
985    const char *log_dom = "eobj";
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(log_dom, EINA_COLOR_LIGHTBLUE);
994    if (_eobj_log_dom < 0)
995      {
996         EINA_LOG_ERR("Could not register log domain: %s", log_dom);
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_priority_add(Eobj *obj,
1126       const Eobj_Event_Description *desc,
1127       Eobj_Callback_Priority priority,
1128       Eobj_Event_Cb func,
1129       const void *data)
1130 {
1131    Eobj_Callback_Description *cb = calloc(1, sizeof(*cb));
1132    cb->event = desc;
1133    cb->func = func;
1134    cb->func_data = (void *) data;
1135    cb->priority = priority;
1136    obj->callbacks = eina_inlist_sorted_insert(obj->callbacks,
1137          EINA_INLIST_GET(cb), _callback_priority_cmp);
1138
1139    eobj_event_callback_call(obj, EOBJ_SIG_CALLBACK_ADD, desc);
1140
1141    return EINA_TRUE;
1142 }
1143
1144 EAPI void *
1145 eobj_event_callback_del(Eobj *obj, const Eobj_Event_Description *desc, Eobj_Event_Cb func)
1146 {
1147    void *ret = NULL;
1148    Eobj_Callback_Description *cb;
1149    EINA_INLIST_FOREACH(obj->callbacks, cb)
1150      {
1151         if ((cb->event == desc) && (cb->func == func))
1152           {
1153              void *data;
1154
1155              data = cb->func_data;
1156              cb->delete_me = EINA_TRUE;
1157              _eobj_callbacks_clear(obj);
1158              ret = data;
1159              goto found;
1160           }
1161      }
1162
1163    return NULL;
1164
1165 found:
1166    eobj_event_callback_call(obj, EOBJ_SIG_CALLBACK_DEL, desc);
1167    return ret;
1168 }
1169
1170 EAPI void *
1171 eobj_event_callback_del_full(Eobj *obj, const Eobj_Event_Description *desc, Eobj_Event_Cb func, const void *user_data)
1172 {
1173    void *ret = NULL;
1174    Eobj_Callback_Description *cb;
1175    EINA_INLIST_FOREACH(obj->callbacks, cb)
1176      {
1177         if ((cb->event == desc) && (cb->func == func) &&
1178               (cb->func_data == user_data))
1179           {
1180              void *data;
1181
1182              data = cb->func_data;
1183              cb->delete_me = EINA_TRUE;
1184              _eobj_callbacks_clear(obj);
1185              ret = data;
1186              goto found;
1187           }
1188      }
1189
1190    return NULL;
1191
1192 found:
1193    eobj_event_callback_call(obj, EOBJ_SIG_CALLBACK_DEL, desc);
1194    return ret;
1195 }
1196
1197 EAPI Eina_Bool
1198 eobj_event_callback_call(Eobj *obj, const Eobj_Event_Description *desc,
1199       const void *event_info)
1200 {
1201    Eobj_Callback_Description *cb;
1202
1203    eobj_ref(obj);
1204    obj->walking_list++;
1205
1206    EINA_INLIST_FOREACH(obj->callbacks, cb)
1207      {
1208         if (!cb->delete_me  && (cb->event == desc))
1209           {
1210              /* Abort callback calling if the func says so. */
1211              if (!cb->func((void *) cb->func_data, obj, desc,
1212                       (void *) event_info))
1213                {
1214                   break;
1215                }
1216           }
1217         if (obj->delete)
1218           break;
1219      }
1220    obj->walking_list--;
1221    _eobj_callbacks_clear(obj);
1222    eobj_unref(obj);
1223
1224    return EINA_TRUE;
1225 }
1226
1227 static Eina_Bool
1228 _eobj_event_forwarder_callback(void *data, Eobj *obj, const Eobj_Event_Description *desc, void *event_info)
1229 {
1230    (void) obj;
1231    Eobj *new_obj = (Eobj *) data;
1232    return eobj_event_callback_call(new_obj, desc, event_info);
1233 }
1234
1235 /* FIXME: Change default priority? Maybe call later? */
1236 EAPI Eina_Bool
1237 eobj_event_callback_forwarder_add(Eobj *obj, const Eobj_Event_Description *desc, Eobj *new_obj)
1238 {
1239    return eobj_event_callback_add(obj, desc, _eobj_event_forwarder_callback, new_obj);
1240 }
1241
1242 EAPI Eina_Bool
1243 eobj_event_callback_forwarder_del(Eobj *obj, const Eobj_Event_Description *desc, Eobj *new_obj)
1244 {
1245    eobj_event_callback_del_full(obj, desc, _eobj_event_forwarder_callback, new_obj);
1246    return EINA_TRUE;
1247 }
1248
1249 /* EOBJ_CLASS_BASE stuff */
1250 static Eobj_Class *_my_class = NULL;
1251
1252 /* FIXME: Set proper type descriptions. */
1253 EAPI const Eobj_Event_Description _EOBJ_SIG_CALLBACK_ADD =
1254    EOBJ_EVENT_DESCRIPTION("callback,add", "?", "Called when a callback was added.");
1255 EAPI const Eobj_Event_Description _EOBJ_SIG_CALLBACK_DEL =
1256    EOBJ_EVENT_DESCRIPTION("callback,del", "?", "Called when a callback was deleted.");
1257
1258 static void
1259 _constructor(Eobj *obj, void *class_data __UNUSED__)
1260 {
1261    DBG("%p - %s.", obj, _my_class->desc->name);
1262 }
1263
1264 static void
1265 _destructor(Eobj *obj, void *class_data __UNUSED__)
1266 {
1267    DBG("%p - %s.", obj, _my_class->desc->name);
1268 }
1269
1270 EAPI const Eobj_Class *
1271 eobj_base_class_get(void)
1272 {
1273    if (_my_class) return _my_class;
1274
1275    static const Eobj_Class_Description class_desc = {
1276         "Eobj Base",
1277         EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT,
1278         EOBJ_CLASS_DESCRIPTION_OPS(NULL, NULL, 0),
1279         NULL,
1280         0,
1281         _constructor,
1282         _destructor,
1283         NULL,
1284         NULL
1285    };
1286
1287    return _my_class = eobj_class_new(&class_desc, NULL, NULL);
1288 }
1289