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