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