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