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