Eobj: Added eobj_ref_get.
[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 int
765 eobj_ref_get(const Eobj *obj)
766 {
767    return obj->refcount;
768 }
769
770 EAPI void
771 eobj_del(Eobj *obj)
772 {
773    obj->delete = EINA_TRUE;
774    eobj_unref(obj);
775 }
776
777 EAPI Eobj *
778 eobj_parent_get(Eobj *obj)
779 {
780    return obj->parent;
781 }
782
783 EAPI void
784 eobj_constructor_error_set(Eobj *obj)
785 {
786    eobj_generic_data_set(obj, CONSTRUCT_ERROR_KEY, (void *) EINA_TRUE);
787 }
788
789 EAPI Eina_Bool
790 eobj_constructor_error_get(const Eobj *obj)
791 {
792    return (intptr_t) eobj_generic_data_get(obj, CONSTRUCT_ERROR_KEY);
793 }
794
795 static inline void
796 _eobj_constructor_default(Eobj *obj)
797 {
798    eobj_constructor_super(obj);
799 }
800
801 static inline void
802 _eobj_destructor_default(Eobj *obj)
803 {
804    eobj_destructor_super(obj);
805 }
806
807 static void
808 eobj_class_constructor(Eobj *obj, const Eobj_Class *klass)
809 {
810    if (!klass)
811       return;
812
813    if (klass->desc->constructor)
814       klass->desc->constructor(obj);
815    else
816       _eobj_constructor_default(obj);
817 }
818
819 static void
820 eobj_class_destructor(Eobj *obj, const Eobj_Class *klass)
821 {
822    if (!klass)
823       return;
824
825    if (klass->desc->destructor)
826       klass->desc->destructor(obj);
827    else
828       _eobj_destructor_default(obj);
829 }
830
831 EAPI void
832 eobj_constructor_super(Eobj *obj)
833 {
834    eobj_class_constructor(obj, _eobj_kls_itr_next(obj));
835 }
836
837 EAPI void
838 eobj_destructor_super(Eobj *obj)
839 {
840    eobj_class_destructor(obj, _eobj_kls_itr_next(obj));
841 }
842
843 EAPI void *
844 eobj_data_get(Eobj *obj, const Eobj_Class *klass)
845 {
846    /* FIXME: Add a check that this is of the right klass and we don't seg.
847     * Probably just return NULL. */
848    return obj->datas[_eobj_class_count_parents(klass)];
849 }
850
851 typedef struct
852 {
853    EINA_INLIST;
854    Eina_Stringshare *key;
855    void *data;
856 } Eobj_Generic_Data_Node;
857
858 static void
859 _eobj_generic_data_node_free(Eobj_Generic_Data_Node *node)
860 {
861    eina_stringshare_del(node->key);
862    free(node);
863 }
864
865 static void
866 _eobj_generic_data_del_all(Eobj *obj)
867 {
868    Eina_Inlist *nnode;
869    Eobj_Generic_Data_Node *node;
870
871    EINA_INLIST_FOREACH_SAFE(obj->generic_data, nnode, node)
872      {
873         obj->generic_data = eina_inlist_remove(obj->generic_data,
874               EINA_INLIST_GET(node));
875
876         _eobj_generic_data_node_free(node);
877      }
878 }
879
880 EAPI void *
881 eobj_generic_data_set(Eobj *obj, const char *key, const void *data)
882 {
883    void *prev_data;
884    Eobj_Generic_Data_Node *node;
885
886    if (!key) return NULL;
887    if (!data) return NULL;
888
889    prev_data = eobj_generic_data_del(obj, key);
890
891    node = malloc(sizeof(Eobj_Generic_Data_Node));
892    node->key = eina_stringshare_add(key);
893    node->data = (void *) data;
894    obj->generic_data = eina_inlist_prepend(obj->generic_data,
895          EINA_INLIST_GET(node));
896
897    return prev_data;
898 }
899
900 EAPI void *
901 eobj_generic_data_get(const Eobj *obj, const char *key)
902 {
903    Eobj_Generic_Data_Node *node;
904
905    if (!key) return NULL;
906
907    EINA_INLIST_FOREACH(obj->generic_data, node)
908      {
909         if (!strcmp(node->key, key))
910           {
911              ((Eobj *) obj)->generic_data =
912                 eina_inlist_promote(obj->generic_data, EINA_INLIST_GET(node));
913              return node->data;
914           }
915      }
916    return NULL;
917 }
918
919 EAPI void *
920 eobj_generic_data_del(Eobj *obj, const char *key)
921 {
922    Eobj_Generic_Data_Node *node;
923
924    if (!key) return NULL;
925
926    EINA_INLIST_FOREACH(obj->generic_data, node)
927      {
928         if (!strcmp(node->key, key))
929           {
930              void *data;
931
932              data = node->data;
933              obj->generic_data = eina_inlist_remove(obj->generic_data,
934                    EINA_INLIST_GET(node));
935              _eobj_generic_data_node_free(node);
936              return data;
937           }
938      }
939    return NULL;
940 }
941
942 EAPI Eina_Bool
943 eobj_init(void)
944 {
945    if (_eobj_init_count++ > 0)
946       return EINA_TRUE;
947
948    eina_init();
949
950    _eobj_classes = NULL;
951    _eobj_classes_last_id = 0;
952    _eobj_log_dom = eina_log_domain_register("eobj", EINA_COLOR_LIGHTBLUE);
953    if (_eobj_log_dom < 0)
954      {
955         EINA_LOG_ERR("Could not register log domain: eobj");
956         return EINA_FALSE;
957      }
958
959    return EINA_TRUE;
960 }
961
962 EAPI Eina_Bool
963 eobj_shutdown(void)
964 {
965    int i;
966    Eobj_Class **cls_itr = _eobj_classes;
967
968    if (--_eobj_init_count > 0)
969       return EINA_TRUE;
970
971    for (i = 0 ; i < _eobj_classes_last_id ; i++, cls_itr++)
972      {
973         if (*cls_itr)
974            eobj_class_free(*cls_itr);
975      }
976
977    if (_eobj_classes)
978       free(_eobj_classes);
979
980    eina_log_domain_unregister(_eobj_log_dom);
981    _eobj_log_dom = -1;
982
983    eina_shutdown();
984    return EINA_TRUE;
985 }
986
987 EAPI void
988 eobj_composite_object_attach(Eobj *obj, Eobj *emb_obj)
989 {
990    eobj_ref(emb_obj);
991    obj->composite_objects = eina_list_prepend(obj->composite_objects, emb_obj);
992 }
993
994 EAPI void
995 eobj_composite_object_detach(Eobj *obj, Eobj *emb_obj)
996 {
997    obj->composite_objects = eina_list_remove(obj->composite_objects, emb_obj);
998    eobj_unref(emb_obj);
999 }
1000
1001 EAPI Eina_Bool
1002 eobj_composite_is(Eobj *emb_obj)
1003 {
1004    Eobj *obj = eobj_parent_get(emb_obj);
1005    Eina_List *itr;
1006    Eobj *tmp;
1007    EINA_LIST_FOREACH(obj->composite_objects, itr, tmp)
1008      {
1009         if (tmp == emb_obj)
1010            return EINA_TRUE;
1011      }
1012
1013    return EINA_FALSE;
1014 }
1015
1016 /* Callbacks */
1017 struct _Eobj_Callback_Description
1018 {
1019    EINA_INLIST;
1020    const Eobj_Event_Description *event;
1021    Eobj_Event_Cb func;
1022    void *func_data;
1023    Eobj_Callback_Priority priority;
1024    Eina_Bool delete_me : 1;
1025 };
1026
1027 /* Actually remove, doesn't care about walking list, or delete_me */
1028 static void
1029 _eobj_callback_remove(Eobj *obj, Eobj_Callback_Description *cb)
1030 {
1031    obj->callbacks = eina_inlist_remove(obj->callbacks,
1032          EINA_INLIST_GET(cb));
1033    free(cb);
1034 }
1035
1036 /* Actually remove, doesn't care about walking list, or delete_me */
1037 static void
1038 _eobj_callback_remove_all(Eobj *obj)
1039 {
1040    Eina_Inlist *initr;
1041    Eobj_Callback_Description *cb;
1042    EINA_INLIST_FOREACH_SAFE(obj->callbacks, initr, cb)
1043      {
1044         _eobj_callback_remove(obj, cb);
1045      }
1046 }
1047
1048 static void
1049 _eobj_callbacks_clear(Eobj *obj)
1050 {
1051    Eina_Inlist *itn;
1052    Eobj_Callback_Description *cb;
1053
1054    /* Abort if we are currently walking the list. */
1055    if (obj->walking_list > 0)
1056       return;
1057
1058    EINA_INLIST_FOREACH_SAFE(obj->callbacks, itn, cb)
1059      {
1060         if (cb->delete_me)
1061           {
1062              _eobj_callback_remove(obj, cb);
1063           }
1064      }
1065 }
1066
1067 static int
1068 _callback_priority_cmp(const void *_a, const void *_b)
1069 {
1070    const Eobj_Callback_Description *a, *b;
1071    a = (const Eobj_Callback_Description *) _a;
1072    b = (const Eobj_Callback_Description *) _b;
1073    if (a->priority < b->priority)
1074       return -1;
1075    else
1076       return 1;
1077 }
1078
1079 EAPI Eina_Bool
1080 eobj_event_callback_add(Eobj *obj,
1081       const Eobj_Event_Description *desc,
1082       Eobj_Event_Cb cb,
1083       const void *data)
1084 {
1085    return eobj_event_callback_priority_add(obj, desc,
1086          EOBJ_CALLBACK_PRIORITY_DEFAULT, cb, data);
1087 }
1088
1089 EAPI Eina_Bool
1090 eobj_event_callback_priority_add(Eobj *obj,
1091       const Eobj_Event_Description *desc,
1092       Eobj_Callback_Priority priority,
1093       Eobj_Event_Cb func,
1094       const void *data)
1095 {
1096    Eobj_Callback_Description *cb = calloc(1, sizeof(*cb));
1097    cb->event = desc;
1098    cb->func = func;
1099    cb->func_data = (void *) data;
1100    cb->priority = priority;
1101    obj->callbacks = eina_inlist_sorted_insert(obj->callbacks,
1102          EINA_INLIST_GET(cb), _callback_priority_cmp);
1103
1104    eobj_event_callback_call(obj, EOBJ_SIG_CALLBACK_ADD, desc);
1105
1106    return EINA_TRUE;
1107 }
1108
1109 EAPI void *
1110 eobj_event_callback_del(Eobj *obj, const Eobj_Event_Description *desc, Eobj_Event_Cb func)
1111 {
1112    void *ret = NULL;
1113    Eobj_Callback_Description *cb;
1114    EINA_INLIST_FOREACH(obj->callbacks, cb)
1115      {
1116         if ((cb->event == desc) && (cb->func == func))
1117           {
1118              void *data;
1119
1120              data = cb->func_data;
1121              cb->delete_me = EINA_TRUE;
1122              _eobj_callbacks_clear(obj);
1123              ret = data;
1124              goto end;
1125           }
1126      }
1127
1128 end:
1129    eobj_event_callback_call(obj, EOBJ_SIG_CALLBACK_DEL, desc);
1130    return ret;
1131 }
1132
1133 EAPI void *
1134 eobj_event_callback_del_full(Eobj *obj, const Eobj_Event_Description *desc, Eobj_Event_Cb func, const void *user_data)
1135 {
1136    void *ret = NULL;
1137    Eobj_Callback_Description *cb;
1138    EINA_INLIST_FOREACH(obj->callbacks, cb)
1139      {
1140         if ((cb->event == desc) && (cb->func == func) &&
1141               (cb->func_data == user_data))
1142           {
1143              void *data;
1144
1145              data = cb->func_data;
1146              cb->delete_me = EINA_TRUE;
1147              _eobj_callbacks_clear(obj);
1148              ret = data;
1149              goto end;
1150           }
1151      }
1152
1153 end:
1154    eobj_event_callback_call(obj, EOBJ_SIG_CALLBACK_DEL, desc);
1155    return ret;
1156 }
1157
1158 EAPI Eina_Bool
1159 eobj_event_callback_call(Eobj *obj, const Eobj_Event_Description *desc,
1160       const void *event_info)
1161 {
1162    Eobj_Callback_Description *cb;
1163
1164    obj->walking_list++;
1165
1166    EINA_INLIST_FOREACH(obj->callbacks, cb)
1167      {
1168         if (!cb->delete_me  && (cb->event == desc))
1169           {
1170              /* Abort callback calling if the func says so. */
1171              if (!cb->func((void *) cb->func_data, obj, desc,
1172                       (void *) event_info))
1173                {
1174                   break;
1175                }
1176           }
1177         if (obj->delete)
1178           break;
1179      }
1180    obj->walking_list--;
1181    _eobj_callbacks_clear(obj);
1182
1183    return EINA_TRUE;
1184 }
1185
1186 static Eina_Bool
1187 _eobj_event_forwarder_callback(void *data, Eobj *obj, const Eobj_Event_Description *desc, void *event_info)
1188 {
1189    (void) obj;
1190    Eobj *new_obj = (Eobj *) data;
1191    return eobj_event_callback_call(new_obj, desc, event_info);
1192 }
1193
1194 /* FIXME: Change default priority? Maybe call later? */
1195 EAPI Eina_Bool
1196 eobj_event_callback_forwarder_add(Eobj *obj, const Eobj_Event_Description *desc, Eobj *new_obj)
1197 {
1198    return eobj_event_callback_add(obj, desc, _eobj_event_forwarder_callback, new_obj);
1199 }
1200
1201 EAPI Eina_Bool
1202 eobj_event_callback_forwarder_del(Eobj *obj, const Eobj_Event_Description *desc, Eobj *new_obj)
1203 {
1204    eobj_event_callback_del_full(obj, desc, _eobj_event_forwarder_callback, new_obj);
1205    return EINA_TRUE;
1206 }
1207
1208 /* EOBJ_CLASS_BASE stuff */
1209 static Eobj_Class *_my_class = NULL;
1210
1211 /* FIXME: Set proper type descriptions. */
1212 EAPI const Eobj_Event_Description _EOBJ_SIG_CALLBACK_ADD =
1213    EOBJ_EVENT_DESCRIPTION("callback,add", "?", "Called when a callback was added.");
1214 EAPI const Eobj_Event_Description _EOBJ_SIG_CALLBACK_DEL =
1215    EOBJ_EVENT_DESCRIPTION("callback,del", "?", "Called when a callback was deleted.");
1216
1217 static void
1218 _constructor(Eobj *obj)
1219 {
1220    DBG("%p - %s.", obj, _my_class->desc->name);
1221 }
1222
1223 static void
1224 _destructor(Eobj *obj)
1225 {
1226    DBG("%p - %s.", obj, _my_class->desc->name);
1227 }
1228
1229 EAPI const Eobj_Class *
1230 eobj_base_class_get(void)
1231 {
1232    if (_my_class) return _my_class;
1233
1234    static const Eobj_Class_Description class_desc = {
1235         "Eobj Base",
1236         EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT,
1237         EOBJ_CLASS_DESCRIPTION_OPS(NULL, NULL, 0),
1238         NULL,
1239         0,
1240         _constructor,
1241         _destructor,
1242         NULL,
1243         NULL
1244    };
1245
1246    return _my_class = eobj_class_new(&class_desc, NULL, NULL);
1247 }
1248