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