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