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