Eobj: Ensure we don't allow calling super functions out of place.
[profile/ivi/eobj.git] / lib / eobj.c
1 #include <Eina.h>
2
3 #include "Eobj.h"
4 #include "eobj_private.h"
5
6 #include "config.h"
7
8 typedef int Eobj_Class_Id;
9
10 int _eobj_log_dom = -1;
11
12 static Eobj_Class **_eobj_classes;
13 static Eobj_Class_Id _eobj_classes_last_id;
14 static Eina_Bool _eobj_init_count = 0;
15
16 static void _eobj_callback_remove_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 typedef struct _Eobj_Callback_Description Eobj_Callback_Description;
22
23 struct _Eobj {
24      EINA_MAGIC
25      Eobj *parent;
26      const Eobj_Class *klass;
27      void *data_blob;
28      int refcount;
29 #ifndef NDEBUG
30      Eina_Inlist *xrefs;
31 #endif
32
33      Eina_List *composite_objects;
34
35      Eina_Inlist *callbacks;
36      int walking_list;
37
38      Eina_Inlist *kls_itr;
39
40      Eina_Bool delete:1;
41      Eina_Bool construct_error:1;
42 };
43
44 /* Start of Dich */
45 /* Dich search, split to 0xff 0xff 0xffff */
46
47 #define DICH_CHAIN1_MASK (0xff)
48 #define DICH_CHAIN2_MASK (0xff)
49 #define DICH_CHAIN_LAST_MASK (0xffff)
50 #define DICH_CHAIN1_SIZE (DICH_CHAIN1_MASK + 1)
51 #define DICH_CHAIN2_SIZE (DICH_CHAIN2_MASK + 1)
52 #define DICH_CHAIN_LAST_SIZE (DICH_CHAIN_LAST_MASK + 1)
53 #define DICH_CHAIN1(x) (((x) >> 24) & DICH_CHAIN1_MASK)
54 #define DICH_CHAIN2(x) (((x) >> 16) & DICH_CHAIN2_MASK)
55 #define DICH_CHAIN_LAST(x) ((x) & DICH_CHAIN_LAST_MASK)
56
57 #define OP_CLASS_OFFSET 16
58 #define OP_CLASS_OFFSET_GET(x) (((x) >> OP_CLASS_OFFSET) & 0xffff)
59 #define OP_CLASS_GET(op) ({ \
60       Eobj_Class_Id tmp = OP_CLASS_OFFSET_GET(op); \
61       (Eobj_Class *) ((tmp <= _eobj_classes_last_id) && (tmp > 0)) ? \
62       (_eobj_classes[tmp - 1]) : NULL; \
63       })
64 #define OP_SUB_ID_GET(op) ((op) & 0xffff)
65
66 /* Structure of Eobj_Op is:
67  * 16bit: class
68  * 16bit: op.
69  */
70
71 typedef struct _Dich_Chain1 Dich_Chain1;
72
73 typedef struct
74 {
75    eobj_op_func_type func;
76 } op_type_funcs;
77
78 typedef struct
79 {
80    op_type_funcs *funcs;
81 } Dich_Chain2;
82
83 struct _Dich_Chain1
84 {
85    Dich_Chain2 *chain;
86 };
87
88 typedef struct
89 {
90      EINA_INLIST;
91      const Eobj_Class *klass;
92 } Eobj_Extension_Node;
93
94 struct _Eobj_Class
95 {
96    Eobj_Class_Id class_id;
97    const Eobj_Class *parent;
98    const Eobj_Class_Description *desc;
99    Dich_Chain1 chain[DICH_CHAIN1_SIZE];
100    Eina_Inlist *extensions;
101
102    const Eobj_Class **mro;
103
104    size_t data_offset; /* < Offset of the data within object data. */
105
106    Eina_Bool constructed : 1;
107 };
108
109 static inline eobj_op_func_type
110 dich_func_get(const Eobj_Class *klass, Eobj_Op op)
111 {
112    const Dich_Chain1 *chain1 = &klass->chain[DICH_CHAIN1(op)];
113    if (!chain1) return NULL;
114    if (!chain1->chain) return NULL;
115    Dich_Chain2 *chain2 = &chain1->chain[DICH_CHAIN2(op)];
116    if (!chain2) return NULL;
117    if (!chain2->funcs) return NULL;
118
119    /* num_ops is calculated from the class. */
120    const Eobj_Class *op_klass = OP_CLASS_GET(op);
121    if (!op_klass || (DICH_CHAIN_LAST(op) >= op_klass->desc->ops.count))
122       return NULL;
123
124    return chain2->funcs[DICH_CHAIN_LAST(op)].func;
125 }
126
127 static inline void
128 dich_func_set(Eobj_Class *klass, Eobj_Op op, eobj_op_func_type func)
129 {
130    const Eobj_Class *op_klass = OP_CLASS_GET(op);
131    size_t num_ops;
132
133    /* Verify op is valid. */
134    if (op_klass)
135      {
136         /* num_ops is calculated from the class. */
137         num_ops = op_klass->desc->ops.count;
138         if (DICH_CHAIN_LAST(op) >= num_ops)
139           {
140              ERR("OP %x is too big for the domain '%s', expected value < %x.",
141                    op, op_klass->desc->name, op_klass->desc->ops.count);
142           }
143      }
144    else
145      {
146         ERR("OP %x is from an illegal class.", op);
147         return;
148      }
149
150    Dich_Chain1 *chain1 = &klass->chain[DICH_CHAIN1(op)];
151    if (!chain1->chain)
152      {
153         klass->chain[DICH_CHAIN1(op)].chain =
154            chain1->chain =
155            calloc(DICH_CHAIN2_SIZE, sizeof(*(chain1->chain)));
156      }
157
158    Dich_Chain2 *chain2 = &chain1->chain[DICH_CHAIN2(op)];
159    if (!chain2->funcs)
160      {
161         chain2->funcs = chain1->chain[DICH_CHAIN2(op)].funcs =
162            calloc(num_ops, sizeof(*(chain2->funcs)));
163      }
164
165    chain2->funcs[DICH_CHAIN_LAST(op)].func = func;
166 }
167
168 static inline void
169 dich_func_clean_all(Eobj_Class *klass)
170 {
171    int i;
172    Dich_Chain1 *chain1 = klass->chain;
173
174    for (i = 0 ; i < DICH_CHAIN1_SIZE ; i++, chain1++)
175      {
176         int j;
177         Dich_Chain2 *chain2 = chain1->chain;
178
179         if (!chain2)
180            continue;
181
182         for (j = 0 ; j < DICH_CHAIN2_SIZE ; j++, chain2++)
183           {
184              free(chain2->funcs);
185           }
186         free(chain1->chain);
187         chain1->chain = NULL;
188      }
189 }
190
191 /* END OF DICH */
192
193 static const Eobj_Op_Description noop_desc =
194         EOBJ_OP_DESCRIPTION(EOBJ_NOOP, "", "No operation.");
195
196 static const Eobj_Op_Description *
197 _eobj_op_id_desc_get(Eobj_Op op)
198 {
199    const Eobj_Class *klass = OP_CLASS_GET(op);
200    Eobj_Op sub_id = OP_SUB_ID_GET(op);
201
202    if (op == EOBJ_NOOP)
203       return &noop_desc;
204
205    if (klass && (sub_id < klass->desc->ops.count))
206       return klass->desc->ops.descs + sub_id;
207
208    return NULL;
209 }
210
211 static const char *
212 _eobj_op_id_name_get(Eobj_Op op)
213 {
214    const Eobj_Op_Description *desc = _eobj_op_id_desc_get(op);
215    return (desc) ? desc->name : NULL;
216 }
217
218 typedef struct
219 {
220    EINA_INLIST;
221    Eobj_Op op;
222    const Eobj_Class **kls_itr;
223 } Eobj_Kls_Itr_Node;
224
225 static inline Eina_Bool
226 _eobj_kls_itr_init(Eobj *obj, Eobj_Op op)
227 {
228    if (obj->kls_itr)
229      {
230         Eobj_Kls_Itr_Node *node =
231            EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
232         if (node->op == op)
233           {
234              return EINA_FALSE;
235           }
236      }
237
238
239      {
240         Eobj_Kls_Itr_Node *node = calloc(1, sizeof(*node));
241         node->op = op;
242         node->kls_itr = obj->klass->mro;
243         obj->kls_itr = eina_inlist_prepend(obj->kls_itr,
244               EINA_INLIST_GET(node));
245
246         return EINA_TRUE;
247      }
248 }
249
250 static inline void
251 _eobj_kls_itr_end(Eobj *obj, Eobj_Op op)
252 {
253    Eobj_Kls_Itr_Node *node =
254       EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
255
256    if (node->op != op)
257       return;
258
259    obj->kls_itr = eina_inlist_remove(obj->kls_itr, obj->kls_itr);
260    free(node);
261 }
262
263 static inline const Eobj_Class *
264 _eobj_kls_itr_get(Eobj *obj)
265 {
266    Eobj_Kls_Itr_Node *node =
267       EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
268
269    return (node) ? *(node->kls_itr) : NULL;
270 }
271
272 static inline const Eobj_Class *
273 _eobj_kls_itr_next(Eobj *obj, Eobj_Op op)
274 {
275    Eobj_Kls_Itr_Node *node =
276       EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
277
278    if (!node || (node->op != op))
279      {
280         Eobj_Op node_op = (node) ? node->op : EOBJ_NOOP;
281         ERR("Called with op %d ('%s') while expecting: %d ('%s'). This probaly means you called eobj_*_super functions from a wrong place.",
282               op, _eobj_op_id_name_get(op),
283               node_op, _eobj_op_id_name_get(node_op));
284         return NULL;
285      }
286
287    const Eobj_Class **kls_itr = node->kls_itr;
288    if (*kls_itr)
289      {
290         kls_itr++;
291         node->kls_itr = kls_itr;
292         return *kls_itr;
293      }
294    else
295      {
296         return NULL;
297      }
298 }
299
300 static inline Eina_Bool
301 _eobj_kls_itr_reached_end(const Eobj *obj)
302 {
303    Eobj_Kls_Itr_Node *node =
304       EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
305    const Eobj_Class **kls_itr = node->kls_itr;
306    return !(*kls_itr && *(kls_itr + 1));
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, op);
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    /* Advance the kls itr. */
400    obj_klass = _eobj_kls_itr_next(obj, op);
401
402    if (!obj_klass)
403      {
404         return EINA_FALSE;
405      }
406
407    va_start(p_list, op);
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(const 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 void
549 eobj_class_free(Eobj_Class *klass)
550 {
551    if (klass->constructed)
552      {
553         if (klass->desc->class_destructor)
554            klass->desc->class_destructor(klass);
555
556         dich_func_clean_all(klass);
557      }
558
559      {
560         Eina_Inlist *itrn;
561         Eobj_Extension_Node *extn;
562         EINA_INLIST_FOREACH_SAFE(klass->extensions, itrn, extn)
563           {
564              free(extn);
565           }
566      }
567
568    free(klass->mro);
569
570    free(klass);
571 }
572
573 /* DEVCHECK */
574 static Eina_Bool
575 _eobj_class_check_op_descs(const Eobj_Class *klass)
576 {
577    const Eobj_Class_Description *desc = klass->desc;
578    const Eobj_Op_Description *itr;
579    size_t i;
580
581    if (desc->ops.count > 0)
582      {
583         if (!desc->ops.base_op_id)
584           {
585              ERR("Class '%s' has a non-zero ops count, but base_id is NULL.",
586                    desc->name);
587              return EINA_FALSE;
588           }
589
590         if (!desc->ops.descs)
591           {
592              ERR("Class '%s' has a non-zero ops count, but there are no descs.",
593                    desc->name);
594              return EINA_FALSE;
595           }
596      }
597
598    itr = desc->ops.descs;
599    for (i = 0 ; i < desc->ops.count ; i++, itr++)
600      {
601         if (itr->sub_op != i)
602           {
603              if (itr->name)
604                {
605                   ERR("Wrong order in Ops description for class '%s'. Expected %d and got %d", desc->name, i, itr->sub_op);
606                }
607              else
608                {
609                   ERR("Found too few Ops description for class '%s'. Expected %d descriptions, but found %d.", desc->name, desc->ops.count, i);
610                }
611              return EINA_FALSE;
612           }
613      }
614
615    if (itr && itr->name)
616      {
617         ERR("Found extra Ops description for class '%s'. Expected %d descriptions, but found more.", desc->name, desc->ops.count);
618         return EINA_FALSE;
619      }
620
621    return EINA_TRUE;
622 }
623
624 EAPI const Eobj_Class *
625 eobj_class_new(const Eobj_Class_Description *desc, const Eobj_Class *parent, ...)
626 {
627    Eobj_Class *klass;
628    va_list p_list;
629
630    va_start(p_list, parent);
631
632 #define _CLS_NEW_CHECK(x) \
633    do \
634      { \
635         if (!x) \
636           { \
637              ERR("%s must not be NULL! Aborting.", #x); \
638              return NULL; \
639           } \
640      } \
641    while(0)
642
643    _CLS_NEW_CHECK(desc);
644    _CLS_NEW_CHECK(desc->name);
645
646    klass = calloc(1, sizeof(Eobj_Class));
647    klass->parent = parent;
648
649    /* Handle class extensions */
650      {
651         Eobj_Class *extn = NULL;
652
653         extn = va_arg(p_list, Eobj_Class *);
654         while (extn)
655           {
656              switch (extn->desc->type)
657                {
658                 case EOBJ_CLASS_TYPE_REGULAR:
659                 case EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT:
660                    /* Use it like an interface. */
661                 case EOBJ_CLASS_TYPE_INTERFACE:
662                    break;
663                 case EOBJ_CLASS_TYPE_MIXIN:
664                      {
665                         Eobj_Extension_Node *node = calloc(1, sizeof(*node));
666                         node->klass = extn;
667                         klass->extensions =
668                            eina_inlist_append(klass->extensions,
669                                  EINA_INLIST_GET(node));
670                      }
671                    break;
672                }
673
674              extn = va_arg(p_list, Eobj_Class *);
675           }
676      }
677
678    klass->desc = desc;
679
680    /* Handle the inheritance */
681    if (klass->parent)
682      {
683         /* Verify the inheritance is allowed. */
684         switch (klass->desc->type)
685           {
686            case EOBJ_CLASS_TYPE_REGULAR:
687            case EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT:
688               if ((klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR) &&
689                     (klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT))
690                 {
691                    ERR("Regular classes ('%s') aren't allowed to inherit from non-regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
692                    goto cleanup;
693                 }
694               break;
695            case EOBJ_CLASS_TYPE_INTERFACE:
696            case EOBJ_CLASS_TYPE_MIXIN:
697               if ((klass->parent->desc->type != EOBJ_CLASS_TYPE_INTERFACE) &&
698                     (klass->parent->desc->type != EOBJ_CLASS_TYPE_MIXIN))
699                 {
700                    ERR("Non-regular classes ('%s') aren't allowed to inherit from regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
701                    goto cleanup;
702                 }
703               break;
704           }
705
706
707         /* Update the current offset. */
708         /* FIXME: Make sure this alignment is enough. */
709         klass->data_offset = klass->parent->data_offset +
710            klass->parent->desc->data_size +
711            (sizeof(void *) -
712                   (klass->parent->desc->data_size % sizeof(void *)));
713      }
714
715    if (!_eobj_class_check_op_descs(klass))
716      {
717         goto cleanup;
718      }
719
720    klass->class_id = ++_eobj_classes_last_id;
721      {
722         /* FIXME: Handle errors. */
723         Eobj_Class **tmp;
724         tmp = realloc(_eobj_classes, _eobj_classes_last_id * sizeof(*_eobj_classes));
725         _eobj_classes = tmp;
726         _eobj_classes[klass->class_id - 1] = klass;
727      }
728
729    _eobj_class_base_op_init(klass);
730
731    /* FIXME: Shouldn't be called here - should be called from eobj_add. */
732    _eobj_class_constructor(klass);
733
734    va_end(p_list);
735
736    return klass;
737
738 cleanup:
739    eobj_class_free(klass);
740    return NULL;
741 }
742 #undef _CLS_NEW_CHECK
743
744 EAPI Eobj *
745 eobj_add(const Eobj_Class *klass, Eobj *parent)
746 {
747    if (klass->desc->type != EOBJ_CLASS_TYPE_REGULAR)
748      {
749         ERR("Class '%s' is not instantiate-able. Aborting.", klass->desc->name);
750         return NULL;
751      }
752
753    Eobj *obj = calloc(1, sizeof(*obj));
754    obj->klass = klass;
755    obj->parent = parent;
756
757    obj->refcount++;
758
759    obj->data_blob = calloc(1, klass->data_offset + klass->desc->data_size);
760
761    _eobj_kls_itr_init(obj, EOBJ_NOOP);
762    eobj_constructor_error_unset(obj);
763
764    eobj_ref(obj);
765    eobj_class_constructor(obj, klass);
766
767    if (eobj_constructor_error_get(obj))
768      {
769         ERR("Type '%s' - One of the object constructors have failed.", klass->desc->name);
770         goto fail;
771      }
772
773    if (!_eobj_kls_itr_reached_end(obj))
774      {
775         ERR("Type '%s' - Not all of the object constructors have been executed.", klass->desc->name);
776         goto fail;
777      }
778    _eobj_kls_itr_end(obj, EOBJ_NOOP);
779    eobj_unref(obj);
780
781    return obj;
782
783 fail:
784    /* Unref twice, once for the ref above, and once for the basic object ref. */
785    eobj_unref(obj);
786    eobj_unref(obj);
787    return NULL;
788 }
789
790 typedef struct
791 {
792    EINA_INLIST;
793    const Eobj *ref_obj;
794    const char *file;
795    int line;
796 } Eobj_Xref_Node;
797
798 EAPI Eobj *
799 eobj_xref_internal(Eobj *obj, const Eobj *ref_obj, const char *file, int line)
800 {
801    eobj_ref(obj);
802
803 #ifndef NDEBUG
804    Eobj_Xref_Node *xref = calloc(1, sizeof(*xref));
805    xref->ref_obj = ref_obj;
806    xref->file = file;
807    xref->line = line;
808
809    /* FIXME: Make it sorted. */
810    obj->xrefs = eina_inlist_prepend(obj->xrefs, EINA_INLIST_GET(xref));
811 #else
812    (void) ref_obj;
813    (void) file;
814    (void) line;
815 #endif
816
817    return obj;
818 }
819
820 EAPI void
821 eobj_xunref(Eobj *obj, const Eobj *ref_obj)
822 {
823 #ifndef NDEBUG
824    Eobj_Xref_Node *xref = NULL;
825    EINA_INLIST_FOREACH(obj->xrefs, xref)
826      {
827         if (xref->ref_obj == ref_obj)
828            break;
829      }
830
831    if (xref)
832      {
833         obj->xrefs = eina_inlist_remove(obj->xrefs, EINA_INLIST_GET(xref));
834         free(xref);
835      }
836    else
837      {
838         ERR("ref_obj (%p) does not reference obj (%p). Aborting unref.", ref_obj, obj);
839         return;
840      }
841 #else
842    (void) ref_obj;
843 #endif
844    eobj_unref(obj);
845 }
846
847 EAPI Eobj *
848 eobj_ref(Eobj *obj)
849 {
850    obj->refcount++;
851    return obj;
852 }
853
854 EAPI void
855 eobj_unref(Eobj *obj)
856 {
857    if (--(obj->refcount) == 0)
858      {
859         /* We need that for the event callbacks that may ref/unref. */
860         obj->refcount++;
861
862         if (!obj->delete)
863           {
864              eobj_event_callback_call(obj, EOBJ_EV_DEL, NULL);
865              obj->delete = EINA_TRUE;
866           }
867         eobj_event_callback_call(obj, EOBJ_EV_FREE, NULL);
868
869         obj->refcount--;
870
871         const Eobj_Class *klass = eobj_class_get(obj);
872         _eobj_kls_itr_init(obj, EOBJ_NOOP);
873         eobj_constructor_error_unset(obj);
874         eobj_class_destructor(obj, klass);
875         if (eobj_constructor_error_get(obj))
876           {
877              ERR("Type '%s' - One of the object destructors have failed.", klass->desc->name);
878           }
879
880         if (!_eobj_kls_itr_reached_end(obj))
881           {
882              ERR("Type '%s' - Not all of the object destructors have been executed.", klass->desc->name);
883           }
884         _eobj_kls_itr_end(obj, EOBJ_NOOP);
885         /*FIXME: add eobj_class_unref(klass) ? - just to clear the caches. */
886
887         /* If for some reason it's not empty, clear it. */
888         while (obj->kls_itr)
889           {
890              WRN("Kls_Itr is not empty, possibly a bug, please report. - An error will be reported for each kls_itr in the stack.");
891              Eina_Inlist *nitr = nitr->next;
892              free(EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node));
893              obj->kls_itr = nitr;
894           }
895
896 #ifndef NDEBUG
897         /* If for some reason it's not empty, clear it. */
898         while (obj->xrefs)
899           {
900              WRN("obj->xrefs is not empty, possibly a bug, please report. - An error will be reported for each xref in the stack.");
901              Eina_Inlist *nitr = nitr->next;
902              free(EINA_INLIST_CONTAINER_GET(obj->xrefs, Eobj_Kls_Itr_Node));
903              obj->xrefs = nitr;
904           }
905 #endif
906
907         Eina_List *itr, *itr_n;
908         Eobj *emb_obj;
909         EINA_LIST_FOREACH_SAFE(obj->composite_objects, itr, itr_n, emb_obj)
910           {
911              eobj_composite_object_detach(obj, emb_obj);
912           }
913
914         _eobj_callback_remove_all(obj);
915
916         if (obj->data_blob)
917            free(obj->data_blob);
918
919         free(obj);
920      }
921 }
922
923 EAPI int
924 eobj_ref_get(const Eobj *obj)
925 {
926    return obj->refcount;
927 }
928
929 /* Weak reference. */
930 Eina_Bool
931 _eobj_weak_ref_cb(void *data, Eobj *obj __UNUSED__, const Eobj_Event_Description *desc __UNUSED__, void *event_info __UNUSED__)
932 {
933    Eobj_Weak_Ref *wref = data;
934    wref->obj = NULL;
935
936    return EINA_TRUE;
937 }
938
939 EAPI Eobj_Weak_Ref *
940 eobj_weak_ref_new(const Eobj *_obj)
941 {
942    Eobj *obj = (Eobj *) _obj;
943    Eobj_Weak_Ref *wref = calloc(1, sizeof(*wref));
944    wref->obj = obj;
945    eobj_event_callback_add(obj, EOBJ_EV_DEL, _eobj_weak_ref_cb, wref);
946
947    return wref;
948 }
949
950 EAPI void
951 eobj_weak_ref_free(Eobj_Weak_Ref *wref)
952 {
953    if (wref->obj)
954      {
955         eobj_event_callback_del_full(wref->obj, EOBJ_EV_DEL, _eobj_weak_ref_cb,
956               wref);
957      }
958    free(wref);
959 }
960
961 /* EOF Weak reference. */
962
963 EAPI void
964 eobj_del(Eobj *obj)
965 {
966    if (!obj->delete)
967      {
968         eobj_event_callback_call(obj, EOBJ_EV_DEL, NULL);
969         obj->delete = EINA_TRUE;
970      }
971    eobj_unref(obj);
972 }
973
974 EAPI Eobj *
975 eobj_parent_get(Eobj *obj)
976 {
977    return obj->parent;
978 }
979
980 EAPI void
981 eobj_constructor_error_set(Eobj *obj)
982 {
983    obj->construct_error = EINA_TRUE;
984 }
985
986 static void
987 eobj_constructor_error_unset(Eobj *obj)
988 {
989    obj->construct_error = EINA_FALSE;
990 }
991
992 EAPI Eina_Bool
993 eobj_constructor_error_get(const Eobj *obj)
994 {
995    return obj->construct_error;
996 }
997
998 static inline void
999 _eobj_constructor_default(Eobj *obj)
1000 {
1001    eobj_constructor_super(obj);
1002 }
1003
1004 static inline void
1005 _eobj_destructor_default(Eobj *obj)
1006 {
1007    eobj_destructor_super(obj);
1008 }
1009
1010 static void
1011 eobj_class_constructor(Eobj *obj, const Eobj_Class *klass)
1012 {
1013    if (!klass)
1014       return;
1015
1016    if (klass->desc->constructor)
1017       klass->desc->constructor(obj, eobj_data_get(obj, klass));
1018    else
1019       _eobj_constructor_default(obj);
1020 }
1021
1022 static void
1023 eobj_class_destructor(Eobj *obj, const Eobj_Class *klass)
1024 {
1025    if (!klass)
1026       return;
1027
1028    if (klass->desc->destructor)
1029       klass->desc->destructor(obj, eobj_data_get(obj, klass));
1030    else
1031       _eobj_destructor_default(obj);
1032 }
1033
1034 EAPI void
1035 eobj_constructor_super(Eobj *obj)
1036 {
1037    eobj_class_constructor(obj, _eobj_kls_itr_next(obj, EOBJ_NOOP));
1038 }
1039
1040 EAPI void
1041 eobj_destructor_super(Eobj *obj)
1042 {
1043    eobj_class_destructor(obj, _eobj_kls_itr_next(obj, EOBJ_NOOP));
1044 }
1045
1046 EAPI void *
1047 eobj_data_get(Eobj *obj, const Eobj_Class *klass)
1048 {
1049    /* FIXME: Add a check that this is of the right klass and we don't seg.
1050     * Probably just return NULL. */
1051    if (klass->desc->data_size > 0)
1052       return ((char *) obj->data_blob) + klass->data_offset;
1053    else
1054       return NULL;
1055 }
1056
1057 EAPI Eina_Bool
1058 eobj_init(void)
1059 {
1060    const char *log_dom = "eobj";
1061    if (_eobj_init_count++ > 0)
1062       return EINA_TRUE;
1063
1064    eina_init();
1065
1066    _eobj_classes = NULL;
1067    _eobj_classes_last_id = 0;
1068    _eobj_log_dom = eina_log_domain_register(log_dom, EINA_COLOR_LIGHTBLUE);
1069    if (_eobj_log_dom < 0)
1070      {
1071         EINA_LOG_ERR("Could not register log domain: %s", log_dom);
1072         return EINA_FALSE;
1073      }
1074
1075    return EINA_TRUE;
1076 }
1077
1078 EAPI Eina_Bool
1079 eobj_shutdown(void)
1080 {
1081    int i;
1082    Eobj_Class **cls_itr = _eobj_classes;
1083
1084    if (--_eobj_init_count > 0)
1085       return EINA_TRUE;
1086
1087    for (i = 0 ; i < _eobj_classes_last_id ; i++, cls_itr++)
1088      {
1089         if (*cls_itr)
1090            eobj_class_free(*cls_itr);
1091      }
1092
1093    if (_eobj_classes)
1094       free(_eobj_classes);
1095
1096    eina_log_domain_unregister(_eobj_log_dom);
1097    _eobj_log_dom = -1;
1098
1099    eina_shutdown();
1100    return EINA_TRUE;
1101 }
1102
1103 EAPI void
1104 eobj_composite_object_attach(Eobj *obj, Eobj *emb_obj)
1105 {
1106    eobj_xref(emb_obj, obj);
1107    obj->composite_objects = eina_list_prepend(obj->composite_objects, emb_obj);
1108 }
1109
1110 EAPI void
1111 eobj_composite_object_detach(Eobj *obj, Eobj *emb_obj)
1112 {
1113    obj->composite_objects = eina_list_remove(obj->composite_objects, emb_obj);
1114    eobj_xunref(emb_obj, obj);
1115 }
1116
1117 EAPI Eina_Bool
1118 eobj_composite_is(Eobj *emb_obj)
1119 {
1120    Eobj *obj = eobj_parent_get(emb_obj);
1121    Eina_List *itr;
1122    Eobj *tmp;
1123
1124    if (!obj)
1125       return EINA_FALSE;
1126
1127    EINA_LIST_FOREACH(obj->composite_objects, itr, tmp)
1128      {
1129         if (tmp == emb_obj)
1130            return EINA_TRUE;
1131      }
1132
1133    return EINA_FALSE;
1134 }
1135
1136 /* Callbacks */
1137 struct _Eobj_Callback_Description
1138 {
1139    EINA_INLIST;
1140    const Eobj_Event_Description *event;
1141    Eobj_Event_Cb func;
1142    void *func_data;
1143    Eobj_Callback_Priority priority;
1144    Eina_Bool delete_me : 1;
1145 };
1146
1147 /* Actually remove, doesn't care about walking list, or delete_me */
1148 static void
1149 _eobj_callback_remove(Eobj *obj, Eobj_Callback_Description *cb)
1150 {
1151    obj->callbacks = eina_inlist_remove(obj->callbacks,
1152          EINA_INLIST_GET(cb));
1153    free(cb);
1154 }
1155
1156 /* Actually remove, doesn't care about walking list, or delete_me */
1157 static void
1158 _eobj_callback_remove_all(Eobj *obj)
1159 {
1160    Eina_Inlist *initr;
1161    Eobj_Callback_Description *cb;
1162    EINA_INLIST_FOREACH_SAFE(obj->callbacks, initr, cb)
1163      {
1164         _eobj_callback_remove(obj, cb);
1165      }
1166 }
1167
1168 static void
1169 _eobj_callbacks_clear(Eobj *obj)
1170 {
1171    Eina_Inlist *itn;
1172    Eobj_Callback_Description *cb;
1173
1174    /* Abort if we are currently walking the list. */
1175    if (obj->walking_list > 0)
1176       return;
1177
1178    EINA_INLIST_FOREACH_SAFE(obj->callbacks, itn, cb)
1179      {
1180         if (cb->delete_me)
1181           {
1182              _eobj_callback_remove(obj, cb);
1183           }
1184      }
1185 }
1186
1187 static int
1188 _callback_priority_cmp(const void *_a, const void *_b)
1189 {
1190    const Eobj_Callback_Description *a, *b;
1191    a = (const Eobj_Callback_Description *) _a;
1192    b = (const Eobj_Callback_Description *) _b;
1193    if (a->priority < b->priority)
1194       return -1;
1195    else
1196       return 1;
1197 }
1198
1199 EAPI Eina_Bool
1200 eobj_event_callback_priority_add(Eobj *obj,
1201       const Eobj_Event_Description *desc,
1202       Eobj_Callback_Priority priority,
1203       Eobj_Event_Cb func,
1204       const void *data)
1205 {
1206    Eobj_Callback_Description *cb = calloc(1, sizeof(*cb));
1207    cb->event = desc;
1208    cb->func = func;
1209    cb->func_data = (void *) data;
1210    cb->priority = priority;
1211    obj->callbacks = eina_inlist_sorted_insert(obj->callbacks,
1212          EINA_INLIST_GET(cb), _callback_priority_cmp);
1213
1214    eobj_event_callback_call(obj, EOBJ_EV_CALLBACK_ADD, desc);
1215
1216    return EINA_TRUE;
1217 }
1218
1219 EAPI void *
1220 eobj_event_callback_del(Eobj *obj, const Eobj_Event_Description *desc, Eobj_Event_Cb func)
1221 {
1222    void *ret = NULL;
1223    Eobj_Callback_Description *cb;
1224    EINA_INLIST_FOREACH(obj->callbacks, cb)
1225      {
1226         if ((cb->event == desc) && (cb->func == func))
1227           {
1228              void *data;
1229
1230              data = cb->func_data;
1231              cb->delete_me = EINA_TRUE;
1232              _eobj_callbacks_clear(obj);
1233              ret = data;
1234              goto found;
1235           }
1236      }
1237
1238    return NULL;
1239
1240 found:
1241    eobj_event_callback_call(obj, EOBJ_EV_CALLBACK_DEL, desc);
1242    return ret;
1243 }
1244
1245 EAPI void *
1246 eobj_event_callback_del_full(Eobj *obj, const Eobj_Event_Description *desc, Eobj_Event_Cb func, const void *user_data)
1247 {
1248    void *ret = NULL;
1249    Eobj_Callback_Description *cb;
1250    EINA_INLIST_FOREACH(obj->callbacks, cb)
1251      {
1252         if ((cb->event == desc) && (cb->func == func) &&
1253               (cb->func_data == user_data))
1254           {
1255              void *data;
1256
1257              data = cb->func_data;
1258              cb->delete_me = EINA_TRUE;
1259              _eobj_callbacks_clear(obj);
1260              ret = data;
1261              goto found;
1262           }
1263      }
1264
1265    return NULL;
1266
1267 found:
1268    eobj_event_callback_call(obj, EOBJ_EV_CALLBACK_DEL, desc);
1269    return ret;
1270 }
1271
1272 EAPI Eina_Bool
1273 eobj_event_callback_call(Eobj *obj, const Eobj_Event_Description *desc,
1274       const void *event_info)
1275 {
1276    Eina_Bool ret = EINA_TRUE;
1277    Eobj_Callback_Description *cb;
1278
1279    eobj_ref(obj);
1280    obj->walking_list++;
1281
1282    EINA_INLIST_FOREACH(obj->callbacks, cb)
1283      {
1284         if (!cb->delete_me  && (cb->event == desc))
1285           {
1286              /* Abort callback calling if the func says so. */
1287              if (!cb->func((void *) cb->func_data, obj, desc,
1288                       (void *) event_info))
1289                {
1290                   ret = EINA_FALSE;
1291                   break;
1292                }
1293           }
1294         if (obj->delete)
1295           break;
1296      }
1297    obj->walking_list--;
1298    _eobj_callbacks_clear(obj);
1299    eobj_unref(obj);
1300
1301    return ret;
1302 }
1303
1304 static Eina_Bool
1305 _eobj_event_forwarder_callback(void *data, Eobj *obj, const Eobj_Event_Description *desc, void *event_info)
1306 {
1307    (void) obj;
1308    Eobj *new_obj = (Eobj *) data;
1309    return eobj_event_callback_call(new_obj, desc, event_info);
1310 }
1311
1312 /* FIXME: Change default priority? Maybe call later? */
1313 EAPI Eina_Bool
1314 eobj_event_callback_forwarder_add(Eobj *obj, const Eobj_Event_Description *desc, Eobj *new_obj)
1315 {
1316    return eobj_event_callback_add(obj, desc, _eobj_event_forwarder_callback, new_obj);
1317 }
1318
1319 EAPI Eina_Bool
1320 eobj_event_callback_forwarder_del(Eobj *obj, const Eobj_Event_Description *desc, Eobj *new_obj)
1321 {
1322    eobj_event_callback_del_full(obj, desc, _eobj_event_forwarder_callback, new_obj);
1323    return EINA_TRUE;
1324 }
1325