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