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