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