Eobj: Added EOBJ_SIG_CALLBACK_ADD/DEL to Eobj's Event desc.
[profile/ivi/eobj.git] / lib / eobj.c
1 #include <Eina.h>
2
3 #include "Eobj.h"
4
5 #include "config.h"
6
7 typedef int Eobj_Class_Id;
8
9 static int _eobj_log_dom = -1;
10
11 static Eobj_Class **_eobj_classes;
12 static Eobj_Class_Id _eobj_classes_last_id;
13 static Eina_Bool _eobj_init_count = 0;
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      EINA_MAGIC
50      Eobj *parent;
51      const Eobj_Class *klass;
52      void *data_blob;
53      int refcount;
54      Eina_List *composite_objects;
55
56      Eina_Inlist *callbacks;
57      int walking_list;
58
59      Eina_Inlist *generic_data;
60
61      Eina_Inlist *kls_itr;
62
63      Eina_Bool delete:1;
64      Eina_Bool construct_error:1;
65 };
66
67 /* Start of Dich */
68 /* Dich search, split to 0xff 0xff 0xffff */
69
70 #define DICH_CHAIN1_MASK (0xff)
71 #define DICH_CHAIN2_MASK (0xff)
72 #define DICH_CHAIN_LAST_MASK (0xffff)
73 #define DICH_CHAIN1_SIZE (DICH_CHAIN1_MASK + 1)
74 #define DICH_CHAIN2_SIZE (DICH_CHAIN2_MASK + 1)
75 #define DICH_CHAIN_LAST_SIZE (DICH_CHAIN_LAST_MASK + 1)
76 #define DICH_CHAIN1(x) (((x) >> 24) & DICH_CHAIN1_MASK)
77 #define DICH_CHAIN2(x) (((x) >> 16) & DICH_CHAIN2_MASK)
78 #define DICH_CHAIN_LAST(x) ((x) & DICH_CHAIN_LAST_MASK)
79
80 #define OP_CLASS_OFFSET 16
81 #define OP_CLASS_OFFSET_GET(x) (((x) >> OP_CLASS_OFFSET) & 0xffff)
82 #define OP_CLASS_GET(op) ({ \
83       Eobj_Class_Id tmp = OP_CLASS_OFFSET_GET(op); \
84       (Eobj_Class *) ((tmp <= _eobj_classes_last_id) && (tmp > 0)) ? \
85       (_eobj_classes[tmp - 1]) : NULL; \
86       })
87
88 /* Structure of Eobj_Op is:
89  * 16bit: class
90  * 16bit: op.
91  */
92
93 typedef struct _Dich_Chain1 Dich_Chain1;
94
95 typedef struct
96 {
97    eobj_op_func_type func;
98 } op_type_funcs;
99
100 typedef struct
101 {
102    op_type_funcs *funcs;
103 } Dich_Chain2;
104
105 struct _Dich_Chain1
106 {
107    Dich_Chain2 *chain;
108 };
109
110 typedef struct
111 {
112      EINA_INLIST;
113      const Eobj_Class *klass;
114 } Eobj_Extension_Node;
115
116 struct _Eobj_Class
117 {
118    Eobj_Class_Id class_id;
119    const Eobj_Class *parent;
120    const Eobj_Class_Description *desc;
121    Dich_Chain1 chain[DICH_CHAIN1_SIZE];
122    Eina_Inlist *extensions;
123
124    const Eobj_Class **mro;
125
126    size_t data_offset; /* < Offset of the data within object data. */
127
128    Eina_Bool constructed : 1;
129 };
130
131 static inline eobj_op_func_type
132 dich_func_get(const Eobj_Class *klass, Eobj_Op op)
133 {
134    const Dich_Chain1 *chain1 = &klass->chain[DICH_CHAIN1(op)];
135    if (!chain1) return NULL;
136    if (!chain1->chain) return NULL;
137    Dich_Chain2 *chain2 = &chain1->chain[DICH_CHAIN2(op)];
138    if (!chain2) return NULL;
139    if (!chain2->funcs) return NULL;
140
141    /* num_ops is calculated from the class. */
142    const Eobj_Class *op_klass = OP_CLASS_GET(op);
143    if (!op_klass || (DICH_CHAIN_LAST(op) >= op_klass->desc->ops.count))
144       return NULL;
145
146    return chain2->funcs[DICH_CHAIN_LAST(op)].func;
147 }
148
149 static inline void
150 dich_func_set(Eobj_Class *klass, Eobj_Op op, eobj_op_func_type func)
151 {
152    const Eobj_Class *op_klass = OP_CLASS_GET(op);
153    size_t num_ops;
154
155    /* Verify op is valid. */
156    if (op_klass)
157      {
158         /* num_ops is calculated from the class. */
159         num_ops = op_klass->desc->ops.count;
160         if (DICH_CHAIN_LAST(op) >= num_ops)
161           {
162              ERR("OP %x is too big for the domain '%s', expected value < %x.",
163                    op, op_klass->desc->name, op_klass->desc->ops.count);
164           }
165      }
166    else
167      {
168         ERR("OP %x is from an illegal class.", op);
169         return;
170      }
171
172    Dich_Chain1 *chain1 = &klass->chain[DICH_CHAIN1(op)];
173    if (!chain1->chain)
174      {
175         klass->chain[DICH_CHAIN1(op)].chain =
176            chain1->chain =
177            calloc(DICH_CHAIN2_SIZE, sizeof(*(chain1->chain)));
178      }
179
180    Dich_Chain2 *chain2 = &chain1->chain[DICH_CHAIN2(op)];
181    if (!chain2->funcs)
182      {
183         chain2->funcs = chain1->chain[DICH_CHAIN2(op)].funcs =
184            calloc(num_ops, sizeof(*(chain2->funcs)));
185      }
186
187    chain2->funcs[DICH_CHAIN_LAST(op)].func = func;
188 }
189
190 static inline void
191 dich_func_clean_all(Eobj_Class *klass)
192 {
193    int i;
194    Dich_Chain1 *chain1 = klass->chain;
195
196    for (i = 0 ; i < DICH_CHAIN1_SIZE ; i++, chain1++)
197      {
198         int j;
199         Dich_Chain2 *chain2 = chain1->chain;
200
201         if (!chain2)
202            continue;
203
204         for (j = 0 ; j < DICH_CHAIN2_SIZE ; j++, chain2++)
205           {
206              free(chain2->funcs);
207           }
208         free(chain1->chain);
209         chain1->chain = NULL;
210      }
211 }
212
213 /* END OF DICH */
214
215 typedef struct
216 {
217    EINA_INLIST;
218    Eobj_Op op;
219    const Eobj_Class **kls_itr;
220 } Eobj_Kls_Itr_Node;
221
222 static inline Eina_Bool
223 _eobj_kls_itr_init(Eobj *obj, Eobj_Op op)
224 {
225    if (obj->kls_itr)
226      {
227         Eobj_Kls_Itr_Node *node =
228            EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
229         if (node->op == op)
230           {
231              return EINA_FALSE;
232           }
233      }
234
235
236      {
237         Eobj_Kls_Itr_Node *node = calloc(1, sizeof(*node));
238         node->op = op;
239         node->kls_itr = obj->klass->mro;
240         obj->kls_itr = eina_inlist_prepend(obj->kls_itr,
241               EINA_INLIST_GET(node));
242
243         return EINA_TRUE;
244      }
245 }
246
247 static inline void
248 _eobj_kls_itr_end(Eobj *obj, Eobj_Op op)
249 {
250    Eobj_Kls_Itr_Node *node =
251       EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
252
253    if (node->op != op)
254       return;
255
256    obj->kls_itr = eina_inlist_remove(obj->kls_itr, obj->kls_itr);
257    free(node);
258 }
259
260 static inline const Eobj_Class *
261 _eobj_kls_itr_get(Eobj *obj)
262 {
263    Eobj_Kls_Itr_Node *node =
264       EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
265
266    return (node) ? *(node->kls_itr) : NULL;
267 }
268
269 static inline const Eobj_Class *
270 _eobj_kls_itr_next(Eobj *obj)
271 {
272    Eobj_Kls_Itr_Node *node =
273       EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
274    const Eobj_Class **kls_itr = node->kls_itr;
275    if (*kls_itr)
276      {
277         kls_itr++;
278         node->kls_itr = kls_itr;
279         return *kls_itr;
280      }
281    else
282      {
283         return NULL;
284      }
285 }
286
287 static inline Eina_Bool
288 _eobj_kls_itr_reached_end(const Eobj *obj)
289 {
290    Eobj_Kls_Itr_Node *node =
291       EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
292    const Eobj_Class **kls_itr = node->kls_itr;
293    return !(*kls_itr && *(kls_itr + 1));
294 }
295
296 static const Eobj_Op_Description *
297 _eobj_op_id_desc_get(Eobj_Op op)
298 {
299    const Eobj_Class *klass = OP_CLASS_GET(op);
300
301    if (!klass || !klass->desc->ops.base_op_id) return NULL;
302
303    Eobj_Op base_op_id = *klass->desc->ops.base_op_id;
304
305    const Eobj_Op_Description *desc = klass->desc->ops.descs;
306    size_t i;
307    for (i = 0 ; i < klass->desc->ops.count ; i++, desc++)
308      {
309         if ((base_op_id + desc->sub_op) == op)
310            return desc;
311      }
312
313    return NULL;
314 }
315
316 static Eina_Bool
317 _eobj_op_internal(Eobj *obj, Eobj_Op op, va_list *p_list)
318 {
319    const Eobj_Class *klass;
320    Eina_Bool ret = EINA_FALSE;
321    Eina_Bool _itr_init;
322
323    _itr_init = _eobj_kls_itr_init(obj, op);
324    klass = _eobj_kls_itr_get(obj);
325    while (klass)
326      {
327         eobj_op_func_type func = dich_func_get(klass, op);
328
329         if (func)
330           {
331              func(obj, eobj_data_get(obj, klass), p_list);
332              ret = EINA_TRUE;
333              goto end;
334           }
335
336         klass = _eobj_kls_itr_next(obj);
337      }
338
339    /* Try composite objects */
340      {
341         Eina_List *itr;
342         Eobj *emb_obj;
343         EINA_LIST_FOREACH(obj->composite_objects, itr, emb_obj)
344           {
345              if (_eobj_op_internal(emb_obj, op, p_list))
346                {
347                   ret = EINA_TRUE;
348                   goto end;
349                }
350           }
351      }
352
353 end:
354
355    if (_itr_init) _eobj_kls_itr_end(obj, op);
356    return ret;
357 }
358
359 static inline Eina_Bool
360 _eobj_ops_internal(Eobj *obj, va_list *p_list)
361 {
362    Eina_Bool ret = EINA_TRUE;
363    Eobj_Op op = 0;
364
365    op = va_arg(*p_list, Eobj_Op);
366    while (op)
367      {
368         if (!_eobj_op_internal(obj, op, p_list))
369           {
370              const Eobj_Op_Description *desc = _eobj_op_id_desc_get(op);
371              const char *_id_name = (desc) ? desc->name : NULL;
372              const Eobj_Class *op_klass = OP_CLASS_GET(op);
373              const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL;
374              ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.",
375                    op, _id_name, _dom_name,
376                    obj->klass->desc->name);
377              ret = EINA_FALSE;
378              break;
379           }
380         op = va_arg(*p_list, Eobj_Op);
381      }
382
383    return ret;
384 }
385
386 EAPI Eina_Bool
387 eobj_do_internal(Eobj *obj, ...)
388 {
389    Eina_Bool ret;
390    va_list p_list;
391    eobj_ref(obj);
392    va_start(p_list, obj);
393    ret = _eobj_ops_internal(obj, &p_list);
394    va_end(p_list);
395    eobj_unref(obj);
396    return ret;
397 }
398
399 EAPI Eina_Bool
400 eobj_super_do(Eobj *obj, Eobj_Op op, ...)
401 {
402    const Eobj_Class *obj_klass;
403    Eina_Bool ret = EINA_TRUE;
404    va_list p_list;
405
406    va_start(p_list, op);
407
408    /* Advance the kls itr. */
409    obj_klass = _eobj_kls_itr_next(obj);
410    if (!_eobj_op_internal(obj, op, &p_list))
411      {
412         const Eobj_Op_Description *desc = _eobj_op_id_desc_get(op);
413         const char *_id_name = (desc) ? desc->name : NULL;
414         const Eobj_Class *op_klass = OP_CLASS_GET(op);
415         const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL;
416         ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.",
417               op, _id_name, _dom_name,
418               (obj_klass) ? obj_klass->desc->name : NULL);
419         ret = EINA_FALSE;
420      }
421    va_end(p_list);
422
423    return ret;
424 }
425
426 EAPI const Eobj_Class *
427 eobj_class_get(const Eobj *obj)
428 {
429    return obj->klass;
430 }
431
432 EAPI const char *
433 eobj_class_name_get(const Eobj_Class *klass)
434 {
435    return klass->desc->name;
436 }
437
438 static void
439 _eobj_class_base_op_init(Eobj_Class *klass)
440 {
441    const Eobj_Class_Description *desc = klass->desc;
442    if (!desc || !desc->ops.base_op_id)
443       return;
444
445    /* FIXME: Depends on values defined above! */
446    *(desc->ops.base_op_id) = klass->class_id << OP_CLASS_OFFSET;
447 }
448
449 static Eina_List *
450 _eobj_class_mro_add(Eina_List *mro, const Eobj_Class *klass)
451 {
452    if (!klass)
453       return mro;
454
455    mro = eina_list_append(mro, klass);
456
457      {
458         Eobj_Extension_Node *extn;
459         EINA_INLIST_FOREACH(klass->extensions, extn)
460           {
461              mro = _eobj_class_mro_add(mro, extn->klass);
462           }
463      }
464
465    mro = _eobj_class_mro_add(mro, klass->parent);
466
467    return mro;
468 }
469
470 static void
471 _eobj_class_mro_init(Eobj_Class *klass)
472 {
473    Eina_List *mro = NULL;
474
475    DBG("Started creating MRO for class '%s'", klass->desc->name);
476    mro = _eobj_class_mro_add(mro, klass);
477
478      {
479         Eina_List *itr1, *itr2, *itr2n;
480
481         itr1 = eina_list_last(mro);
482         while (itr1)
483           {
484              itr2 = eina_list_prev(itr1);
485
486              while (itr2)
487                {
488                   itr2n = eina_list_prev(itr2);
489
490                   if (eina_list_data_get(itr1) == eina_list_data_get(itr2))
491                     {
492                        mro = eina_list_remove_list(mro, itr2);
493                     }
494
495                   itr2 = itr2n;
496                }
497
498              itr1 = eina_list_prev(itr1);
499           }
500      }
501
502    /* Copy the mro and free the list. */
503      {
504         const Eobj_Class *kls_itr;
505         const Eobj_Class **mro_itr;
506         klass->mro = calloc(sizeof(*klass->mro), eina_list_count(mro) + 1);
507
508         mro_itr = klass->mro;
509
510         EINA_LIST_FREE(mro, kls_itr)
511           {
512              *(mro_itr++) = kls_itr;
513
514              DBG("Added '%s' to MRO", kls_itr->desc->name);
515           }
516         *(mro_itr) = NULL;
517      }
518
519    DBG("Finished creating MRO for class '%s'", klass->desc->name);
520 }
521
522 static void
523 _eobj_class_constructor(Eobj_Class *klass)
524 {
525    if (klass->constructed)
526       return;
527
528    klass->constructed = EINA_TRUE;
529
530    if (klass->desc->class_constructor)
531       klass->desc->class_constructor(klass);
532
533    _eobj_class_mro_init(klass);
534 }
535
536 EAPI void
537 eobj_class_funcs_set(Eobj_Class *klass, const Eobj_Op_Func_Description *func_descs)
538 {
539    const Eobj_Op_Func_Description *itr;
540    itr = func_descs;
541    if (itr)
542      {
543         for ( ; itr->op != 0 ; itr++)
544           {
545              dich_func_set(klass, itr->op, itr->func);
546           }
547      }
548 }
549
550 void
551 eobj_class_free(Eobj_Class *klass)
552 {
553    if (klass->constructed)
554      {
555         if (klass->desc->class_destructor)
556            klass->desc->class_destructor(klass);
557
558         dich_func_clean_all(klass);
559      }
560
561      {
562         Eina_Inlist *itrn;
563         Eobj_Extension_Node *extn;
564         EINA_INLIST_FOREACH_SAFE(klass->extensions, itrn, extn)
565           {
566              free(extn);
567           }
568      }
569
570    free(klass->mro);
571
572    free(klass);
573 }
574
575 EAPI Eobj_Class *
576 eobj_class_new(const Eobj_Class_Description *desc, const Eobj_Class *parent, ...)
577 {
578    Eobj_Class *klass;
579    va_list p_list;
580
581    va_start(p_list, parent);
582
583 #define _CLS_NEW_CHECK(x) \
584    do \
585      { \
586         if (!x) \
587           { \
588              ERR("%s can't be NULL! Aborting.", #x); \
589              return NULL; \
590           } \
591      } \
592    while(0)
593
594    _CLS_NEW_CHECK(desc);
595    _CLS_NEW_CHECK(desc->name);
596
597    klass = calloc(1, sizeof(Eobj_Class));
598    klass->parent = parent;
599
600    /* Handle class extensions */
601      {
602         Eobj_Class *extn = NULL;
603
604         extn = va_arg(p_list, Eobj_Class *);
605         while (extn)
606           {
607              switch (extn->desc->type)
608                {
609                 case EOBJ_CLASS_TYPE_REGULAR:
610                 case EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT:
611                    /* Use it like an interface. */
612                 case EOBJ_CLASS_TYPE_INTERFACE:
613                    break;
614                 case EOBJ_CLASS_TYPE_MIXIN:
615                      {
616                         Eobj_Extension_Node *node = calloc(1, sizeof(*node));
617                         node->klass = extn;
618                         klass->extensions =
619                            eina_inlist_append(klass->extensions,
620                                  EINA_INLIST_GET(node));
621                      }
622                    break;
623                }
624
625              extn = va_arg(p_list, Eobj_Class *);
626           }
627      }
628
629    klass->desc = desc;
630
631    /* Handle the inheritance */
632    if (klass->parent)
633      {
634         /* Verify the inheritance is allowed. */
635         switch (klass->desc->type)
636           {
637            case EOBJ_CLASS_TYPE_REGULAR:
638            case EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT:
639               if ((klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR) &&
640                     (klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT))
641                 {
642                    /* FIXME: Actually handle it. */
643                    ERR("Regular classes ('%s') aren't allowed to inherit from non-regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
644                    goto cleanup;
645                 }
646               break;
647            case EOBJ_CLASS_TYPE_INTERFACE:
648            case EOBJ_CLASS_TYPE_MIXIN:
649               if ((klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR) &&
650                     (klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT))
651                 {
652                    /* FIXME: Actually handle it. */
653                    ERR("Non-regular classes ('%s') aren't allowed to inherit from regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
654                    goto cleanup;
655                 }
656               break;
657           }
658
659
660         /* Update the current offset. */
661         /* FIXME: Make sure this alignment is enough. */
662         klass->data_offset = klass->parent->data_offset +
663            klass->parent->desc->data_size +
664            (sizeof(void *) -
665                   (klass->parent->desc->data_size % sizeof(void *)));
666      }
667
668    klass->class_id = ++_eobj_classes_last_id;
669      {
670         /* FIXME: Handle errors. */
671         Eobj_Class **tmp;
672         tmp = realloc(_eobj_classes, _eobj_classes_last_id * sizeof(*_eobj_classes));
673         _eobj_classes = tmp;
674         _eobj_classes[klass->class_id - 1] = klass;
675      }
676
677    _eobj_class_base_op_init(klass);
678
679    /* FIXME: Shouldn't be called here - should be called from eobj_add. */
680    _eobj_class_constructor(klass);
681
682    va_end(p_list);
683
684    return klass;
685
686 cleanup:
687    eobj_class_free(klass);
688    return NULL;
689 }
690 #undef _CLS_NEW_CHECK
691
692 EAPI Eobj *
693 eobj_add(const Eobj_Class *klass, Eobj *parent)
694 {
695    if (klass->desc->type != EOBJ_CLASS_TYPE_REGULAR)
696      {
697         ERR("Class '%s' is not instantiate-able. Aborting.", klass->desc->name);
698         return NULL;
699      }
700
701    Eobj *obj = calloc(1, sizeof(*obj));
702    obj->klass = klass;
703    obj->parent = parent;
704
705    obj->refcount++;
706
707    obj->data_blob = calloc(1, klass->data_offset + klass->desc->data_size);
708
709    _eobj_kls_itr_init(obj, EOBJ_NOOP);
710    eobj_constructor_error_unset(obj);
711
712    eobj_ref(obj);
713    eobj_class_constructor(obj, klass);
714
715    if (eobj_constructor_error_get(obj))
716      {
717         ERR("Type '%s' - One of the object constructors have failed.", klass->desc->name);
718         goto fail;
719      }
720
721    if (!_eobj_kls_itr_reached_end(obj))
722      {
723         ERR("Type '%s' - Not all of the object constructors have been executed.", klass->desc->name);
724         goto fail;
725      }
726    _eobj_kls_itr_end(obj, EOBJ_NOOP);
727    eobj_unref(obj);
728
729    return obj;
730
731 fail:
732    /* Unref twice, once for the ref above, and once for the basic object ref. */
733    eobj_unref(obj);
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    obj->construct_error = EINA_TRUE;
819 }
820
821 static void
822 eobj_constructor_error_unset(Eobj *obj)
823 {
824    obj->construct_error = EINA_FALSE;
825 }
826
827 EAPI Eina_Bool
828 eobj_constructor_error_get(const Eobj *obj)
829 {
830    return obj->construct_error;
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    const char *log_dom = "eobj";
987    if (_eobj_init_count++ > 0)
988       return EINA_TRUE;
989
990    eina_init();
991
992    _eobj_classes = NULL;
993    _eobj_classes_last_id = 0;
994    _eobj_log_dom = eina_log_domain_register(log_dom, EINA_COLOR_LIGHTBLUE);
995    if (_eobj_log_dom < 0)
996      {
997         EINA_LOG_ERR("Could not register log domain: %s", log_dom);
998         return EINA_FALSE;
999      }
1000
1001    return EINA_TRUE;
1002 }
1003
1004 EAPI Eina_Bool
1005 eobj_shutdown(void)
1006 {
1007    int i;
1008    Eobj_Class **cls_itr = _eobj_classes;
1009
1010    if (--_eobj_init_count > 0)
1011       return EINA_TRUE;
1012
1013    for (i = 0 ; i < _eobj_classes_last_id ; i++, cls_itr++)
1014      {
1015         if (*cls_itr)
1016            eobj_class_free(*cls_itr);
1017      }
1018
1019    if (_eobj_classes)
1020       free(_eobj_classes);
1021
1022    eina_log_domain_unregister(_eobj_log_dom);
1023    _eobj_log_dom = -1;
1024
1025    eina_shutdown();
1026    return EINA_TRUE;
1027 }
1028
1029 EAPI void
1030 eobj_composite_object_attach(Eobj *obj, Eobj *emb_obj)
1031 {
1032    eobj_ref(emb_obj);
1033    obj->composite_objects = eina_list_prepend(obj->composite_objects, emb_obj);
1034 }
1035
1036 EAPI void
1037 eobj_composite_object_detach(Eobj *obj, Eobj *emb_obj)
1038 {
1039    obj->composite_objects = eina_list_remove(obj->composite_objects, emb_obj);
1040    eobj_unref(emb_obj);
1041 }
1042
1043 EAPI Eina_Bool
1044 eobj_composite_is(Eobj *emb_obj)
1045 {
1046    Eobj *obj = eobj_parent_get(emb_obj);
1047    Eina_List *itr;
1048    Eobj *tmp;
1049
1050    if (!obj)
1051       return EINA_FALSE;
1052
1053    EINA_LIST_FOREACH(obj->composite_objects, itr, tmp)
1054      {
1055         if (tmp == emb_obj)
1056            return EINA_TRUE;
1057      }
1058
1059    return EINA_FALSE;
1060 }
1061
1062 /* Callbacks */
1063 struct _Eobj_Callback_Description
1064 {
1065    EINA_INLIST;
1066    const Eobj_Event_Description *event;
1067    Eobj_Event_Cb func;
1068    void *func_data;
1069    Eobj_Callback_Priority priority;
1070    Eina_Bool delete_me : 1;
1071 };
1072
1073 /* Actually remove, doesn't care about walking list, or delete_me */
1074 static void
1075 _eobj_callback_remove(Eobj *obj, Eobj_Callback_Description *cb)
1076 {
1077    obj->callbacks = eina_inlist_remove(obj->callbacks,
1078          EINA_INLIST_GET(cb));
1079    free(cb);
1080 }
1081
1082 /* Actually remove, doesn't care about walking list, or delete_me */
1083 static void
1084 _eobj_callback_remove_all(Eobj *obj)
1085 {
1086    Eina_Inlist *initr;
1087    Eobj_Callback_Description *cb;
1088    EINA_INLIST_FOREACH_SAFE(obj->callbacks, initr, cb)
1089      {
1090         _eobj_callback_remove(obj, cb);
1091      }
1092 }
1093
1094 static void
1095 _eobj_callbacks_clear(Eobj *obj)
1096 {
1097    Eina_Inlist *itn;
1098    Eobj_Callback_Description *cb;
1099
1100    /* Abort if we are currently walking the list. */
1101    if (obj->walking_list > 0)
1102       return;
1103
1104    EINA_INLIST_FOREACH_SAFE(obj->callbacks, itn, cb)
1105      {
1106         if (cb->delete_me)
1107           {
1108              _eobj_callback_remove(obj, cb);
1109           }
1110      }
1111 }
1112
1113 static int
1114 _callback_priority_cmp(const void *_a, const void *_b)
1115 {
1116    const Eobj_Callback_Description *a, *b;
1117    a = (const Eobj_Callback_Description *) _a;
1118    b = (const Eobj_Callback_Description *) _b;
1119    if (a->priority < b->priority)
1120       return -1;
1121    else
1122       return 1;
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    Eina_Bool ret = EINA_TRUE;
1203    Eobj_Callback_Description *cb;
1204
1205    eobj_ref(obj);
1206    obj->walking_list++;
1207
1208    EINA_INLIST_FOREACH(obj->callbacks, cb)
1209      {
1210         if (!cb->delete_me  && (cb->event == desc))
1211           {
1212              /* Abort callback calling if the func says so. */
1213              if (!cb->func((void *) cb->func_data, obj, desc,
1214                       (void *) event_info))
1215                {
1216                   ret = EINA_FALSE;
1217                   break;
1218                }
1219           }
1220         if (obj->delete)
1221           break;
1222      }
1223    obj->walking_list--;
1224    _eobj_callbacks_clear(obj);
1225    eobj_unref(obj);
1226
1227    return ret;
1228 }
1229
1230 static Eina_Bool
1231 _eobj_event_forwarder_callback(void *data, Eobj *obj, const Eobj_Event_Description *desc, void *event_info)
1232 {
1233    (void) obj;
1234    Eobj *new_obj = (Eobj *) data;
1235    return eobj_event_callback_call(new_obj, desc, event_info);
1236 }
1237
1238 /* FIXME: Change default priority? Maybe call later? */
1239 EAPI Eina_Bool
1240 eobj_event_callback_forwarder_add(Eobj *obj, const Eobj_Event_Description *desc, Eobj *new_obj)
1241 {
1242    return eobj_event_callback_add(obj, desc, _eobj_event_forwarder_callback, new_obj);
1243 }
1244
1245 EAPI Eina_Bool
1246 eobj_event_callback_forwarder_del(Eobj *obj, const Eobj_Event_Description *desc, Eobj *new_obj)
1247 {
1248    eobj_event_callback_del_full(obj, desc, _eobj_event_forwarder_callback, new_obj);
1249    return EINA_TRUE;
1250 }
1251
1252 /* EOBJ_CLASS_BASE stuff */
1253 static Eobj_Class *_my_class = NULL;
1254
1255 /* FIXME: Set proper type descriptions. */
1256 EAPI const Eobj_Event_Description _EOBJ_SIG_CALLBACK_ADD =
1257    EOBJ_EVENT_DESCRIPTION("callback,add", "?", "Called when a callback was added.");
1258 EAPI const Eobj_Event_Description _EOBJ_SIG_CALLBACK_DEL =
1259    EOBJ_EVENT_DESCRIPTION("callback,del", "?", "Called when a callback was deleted.");
1260
1261 static void
1262 _constructor(Eobj *obj, void *class_data __UNUSED__)
1263 {
1264    DBG("%p - %s.", obj, _my_class->desc->name);
1265 }
1266
1267 static void
1268 _destructor(Eobj *obj, void *class_data __UNUSED__)
1269 {
1270    DBG("%p - %s.", obj, _my_class->desc->name);
1271 }
1272
1273 EAPI const Eobj_Class *
1274 eobj_base_class_get(void)
1275 {
1276    if (_my_class) return _my_class;
1277
1278    static const Eobj_Event_Description *event_desc[] = {
1279         EOBJ_SIG_CALLBACK_ADD,
1280         EOBJ_SIG_CALLBACK_DEL,
1281         NULL
1282    };
1283
1284    static const Eobj_Class_Description class_desc = {
1285         "Eobj Base",
1286         EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT,
1287         EOBJ_CLASS_DESCRIPTION_OPS(NULL, NULL, 0),
1288         event_desc,
1289         0,
1290         _constructor,
1291         _destructor,
1292         NULL,
1293         NULL
1294    };
1295
1296    return _my_class = eobj_class_new(&class_desc, NULL, NULL);
1297 }
1298