Eobj: Improved tests.
[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_Class *op_klass = OP_CLASS_GET(op);
364              const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL;
365              ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.",
366                    op, _eobj_op_id_name_get(op), _dom_name,
367                    obj->klass->desc->name);
368              ret = EINA_FALSE;
369              break;
370           }
371         op = va_arg(*p_list, Eobj_Op);
372      }
373
374    return ret;
375 }
376
377 EAPI Eina_Bool
378 eobj_do_internal(Eobj *obj, ...)
379 {
380    Eina_Bool ret;
381    va_list p_list;
382    eobj_ref(obj);
383    va_start(p_list, obj);
384    ret = _eobj_ops_internal(obj, &p_list);
385    va_end(p_list);
386    eobj_unref(obj);
387    return ret;
388 }
389
390 EAPI Eina_Bool
391 eobj_do_super(Eobj *obj, Eobj_Op op, ...)
392 {
393    const Eobj_Class *obj_klass;
394    Eina_Bool ret = EINA_TRUE;
395    va_list p_list;
396
397    /* Advance the kls itr. */
398    obj_klass = _eobj_kls_itr_next(obj, op);
399
400    if (!obj_klass)
401      {
402         return EINA_FALSE;
403      }
404
405    va_start(p_list, op);
406    if (!_eobj_op_internal(obj, op, &p_list))
407      {
408         const Eobj_Class *op_klass = OP_CLASS_GET(op);
409         const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL;
410         ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.",
411               op, _eobj_op_id_name_get(op), _dom_name,
412               (obj_klass) ? obj_klass->desc->name : NULL);
413         ret = EINA_FALSE;
414      }
415    va_end(p_list);
416
417    return ret;
418 }
419
420 EAPI const Eobj_Class *
421 eobj_class_get(const Eobj *obj)
422 {
423    return obj->klass;
424 }
425
426 EAPI const char *
427 eobj_class_name_get(const Eobj_Class *klass)
428 {
429    return klass->desc->name;
430 }
431
432 static void
433 _eobj_class_base_op_init(Eobj_Class *klass)
434 {
435    const Eobj_Class_Description *desc = klass->desc;
436    if (!desc || !desc->ops.base_op_id)
437       return;
438
439    /* FIXME: Depends on values defined above! */
440    *(desc->ops.base_op_id) = klass->class_id << OP_CLASS_OFFSET;
441 }
442
443 static Eina_List *
444 _eobj_class_mro_add(Eina_List *mro, const Eobj_Class *klass)
445 {
446    Eina_List *extn_pos = NULL;
447    Eina_Bool check_consistency = !mro;
448    if (!klass)
449       return mro;
450
451    mro = eina_list_append(mro, klass);
452
453    /* Recursively add extenions. */
454      {
455         Eobj_Extension_Node *extn;
456         EINA_INLIST_FOREACH(klass->extensions, extn)
457           {
458              mro = _eobj_class_mro_add(mro, extn->klass);
459              if (!mro)
460                 return NULL;
461
462              if (check_consistency)
463                {
464                   extn_pos = eina_list_append(extn_pos, eina_list_last(mro));
465                }
466           }
467      }
468
469    /* Check if we can create a consistent mro. We only do it for the class
470     * we are working on (i.e no parents). */
471    if (check_consistency)
472      {
473         Eobj_Extension_Node *extn;
474
475         Eina_List *itr = extn_pos;
476         EINA_INLIST_FOREACH(klass->extensions, extn)
477           {
478              /* Get the first one after the extension. */
479              Eina_List *extn_list = eina_list_next(eina_list_data_get(itr));
480
481              /* If we found the extension again. */
482              if (eina_list_data_find_list(extn_list, extn->klass))
483                {
484                   eina_list_free(mro);
485                   ERR("Cannot create a consistent method resolution order for class '%s' because of '%s'.", klass->desc->name, extn->klass->desc->name);
486                   return NULL;
487                }
488
489              itr = eina_list_next(itr);
490           }
491      }
492
493
494    mro = _eobj_class_mro_add(mro, klass->parent);
495
496    return mro;
497 }
498
499 static Eina_Bool
500 _eobj_class_mro_init(Eobj_Class *klass)
501 {
502    Eina_List *mro = NULL;
503
504    DBG("Started creating MRO for class '%s'", klass->desc->name);
505    mro = _eobj_class_mro_add(mro, klass);
506
507    if (!mro)
508       return EINA_FALSE;
509
510    /* Remove duplicates and make them the right order. */
511      {
512         Eina_List *itr1, *itr2, *itr2n;
513
514         itr1 = eina_list_last(mro);
515         while (itr1)
516           {
517              itr2 = eina_list_prev(itr1);
518
519              while (itr2)
520                {
521                   itr2n = eina_list_prev(itr2);
522
523                   if (eina_list_data_get(itr1) == eina_list_data_get(itr2))
524                     {
525                        mro = eina_list_remove_list(mro, itr2);
526                     }
527
528                   itr2 = itr2n;
529                }
530
531              itr1 = eina_list_prev(itr1);
532           }
533      }
534
535    /* Copy the mro and free the list. */
536      {
537         const Eobj_Class *kls_itr;
538         const Eobj_Class **mro_itr;
539         klass->mro = calloc(sizeof(*klass->mro), eina_list_count(mro) + 1);
540
541         mro_itr = klass->mro;
542
543         EINA_LIST_FREE(mro, kls_itr)
544           {
545              *(mro_itr++) = kls_itr;
546
547              DBG("Added '%s' to MRO", kls_itr->desc->name);
548           }
549         *(mro_itr) = NULL;
550      }
551
552    DBG("Finished creating MRO for class '%s'", klass->desc->name);
553
554    return EINA_TRUE;
555 }
556
557 static void
558 _eobj_class_constructor(Eobj_Class *klass)
559 {
560    if (klass->constructed)
561       return;
562
563    klass->constructed = EINA_TRUE;
564
565    if (klass->desc->class_constructor)
566       klass->desc->class_constructor(klass);
567 }
568
569 EAPI void
570 eobj_class_funcs_set(Eobj_Class *klass, const Eobj_Op_Func_Description *func_descs)
571 {
572    const Eobj_Op_Func_Description *itr;
573    itr = func_descs;
574    if (itr)
575      {
576         for ( ; itr->op != 0 ; itr++)
577           {
578              dich_func_set(klass, itr->op, itr->func);
579           }
580      }
581 }
582
583 void
584 eobj_class_free(Eobj_Class *klass)
585 {
586    if (klass->constructed)
587      {
588         if (klass->desc->class_destructor)
589            klass->desc->class_destructor(klass);
590
591         dich_func_clean_all(klass);
592      }
593
594      {
595         Eina_Inlist *itrn;
596         Eobj_Extension_Node *extn;
597         EINA_INLIST_FOREACH_SAFE(klass->extensions, itrn, extn)
598           {
599              free(extn);
600           }
601      }
602
603    free(klass->mro);
604
605    free(klass);
606 }
607
608 /* DEVCHECK */
609 static Eina_Bool
610 _eobj_class_check_op_descs(const Eobj_Class *klass)
611 {
612    const Eobj_Class_Description *desc = klass->desc;
613    const Eobj_Op_Description *itr;
614    size_t i;
615
616    if (desc->ops.count > 0)
617      {
618         if (!desc->ops.base_op_id)
619           {
620              ERR("Class '%s' has a non-zero ops count, but base_id is NULL.",
621                    desc->name);
622              return EINA_FALSE;
623           }
624
625         if (!desc->ops.descs)
626           {
627              ERR("Class '%s' has a non-zero ops count, but there are no descs.",
628                    desc->name);
629              return EINA_FALSE;
630           }
631      }
632
633    itr = desc->ops.descs;
634    for (i = 0 ; i < desc->ops.count ; i++, itr++)
635      {
636         if (itr->sub_op != i)
637           {
638              if (itr->name)
639                {
640                   ERR("Wrong order in Ops description for class '%s'. Expected %d and got %d", desc->name, i, itr->sub_op);
641                }
642              else
643                {
644                   ERR("Found too few Ops description for class '%s'. Expected %d descriptions, but found %d.", desc->name, desc->ops.count, i);
645                }
646              return EINA_FALSE;
647           }
648      }
649
650    if (itr && itr->name)
651      {
652         ERR("Found extra Ops description for class '%s'. Expected %d descriptions, but found more.", desc->name, desc->ops.count);
653         return EINA_FALSE;
654      }
655
656    return EINA_TRUE;
657 }
658
659 EAPI const Eobj_Class *
660 eobj_class_new(const Eobj_Class_Description *desc, const Eobj_Class *parent, ...)
661 {
662    Eobj_Class *klass;
663    va_list p_list;
664
665    va_start(p_list, parent);
666
667 #define _CLS_NEW_CHECK(x) \
668    do \
669      { \
670         if (!x) \
671           { \
672              ERR("%s must not be NULL! Aborting.", #x); \
673              return NULL; \
674           } \
675      } \
676    while(0)
677
678    _CLS_NEW_CHECK(desc);
679    _CLS_NEW_CHECK(desc->name);
680
681    klass = calloc(1, sizeof(Eobj_Class));
682    klass->parent = parent;
683
684    /* Handle class extensions */
685      {
686         Eobj_Class *extn = NULL;
687
688         extn = va_arg(p_list, Eobj_Class *);
689         while (extn)
690           {
691              switch (extn->desc->type)
692                {
693                 case EOBJ_CLASS_TYPE_REGULAR:
694                 case EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT:
695                    /* Use it like an interface. */
696                 case EOBJ_CLASS_TYPE_INTERFACE:
697                    break;
698                 case EOBJ_CLASS_TYPE_MIXIN:
699                      {
700                         Eobj_Extension_Node *node = calloc(1, sizeof(*node));
701                         node->klass = extn;
702                         klass->extensions =
703                            eina_inlist_append(klass->extensions,
704                                  EINA_INLIST_GET(node));
705                      }
706                    break;
707                }
708
709              extn = va_arg(p_list, Eobj_Class *);
710           }
711      }
712
713    klass->desc = desc;
714
715    /* Handle the inheritance */
716    if (klass->parent)
717      {
718         /* Verify the inheritance is allowed. */
719         switch (klass->desc->type)
720           {
721            case EOBJ_CLASS_TYPE_REGULAR:
722            case EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT:
723               if ((klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR) &&
724                     (klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT))
725                 {
726                    ERR("Regular classes ('%s') aren't allowed to inherit from non-regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
727                    goto cleanup;
728                 }
729               break;
730            case EOBJ_CLASS_TYPE_INTERFACE:
731            case EOBJ_CLASS_TYPE_MIXIN:
732               if ((klass->parent->desc->type != EOBJ_CLASS_TYPE_INTERFACE) &&
733                     (klass->parent->desc->type != EOBJ_CLASS_TYPE_MIXIN))
734                 {
735                    ERR("Non-regular classes ('%s') aren't allowed to inherit from regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
736                    goto cleanup;
737                 }
738               break;
739           }
740
741
742         /* Update the current offset. */
743         /* FIXME: Make sure this alignment is enough. */
744         klass->data_offset = klass->parent->data_offset +
745            klass->parent->desc->data_size +
746            (sizeof(void *) -
747                   (klass->parent->desc->data_size % sizeof(void *)));
748      }
749
750    if (!_eobj_class_check_op_descs(klass))
751      {
752         goto cleanup;
753      }
754
755    if (!_eobj_class_mro_init(klass))
756      {
757         goto cleanup;
758      }
759
760    klass->class_id = ++_eobj_classes_last_id;
761      {
762         /* FIXME: Handle errors. */
763         Eobj_Class **tmp;
764         tmp = realloc(_eobj_classes, _eobj_classes_last_id * sizeof(*_eobj_classes));
765         _eobj_classes = tmp;
766         _eobj_classes[klass->class_id - 1] = klass;
767      }
768
769    _eobj_class_base_op_init(klass);
770
771    /* FIXME: Shouldn't be called here - should be called from eobj_add. */
772    _eobj_class_constructor(klass);
773
774    va_end(p_list);
775
776    return klass;
777
778 cleanup:
779    eobj_class_free(klass);
780    return NULL;
781 }
782 #undef _CLS_NEW_CHECK
783
784 EAPI Eobj *
785 eobj_add(const Eobj_Class *klass, Eobj *parent)
786 {
787    if (klass->desc->type != EOBJ_CLASS_TYPE_REGULAR)
788      {
789         ERR("Class '%s' is not instantiate-able. Aborting.", klass->desc->name);
790         return NULL;
791      }
792
793    Eobj *obj = calloc(1, sizeof(*obj));
794    obj->klass = klass;
795    obj->parent = parent;
796
797    obj->refcount++;
798
799    obj->data_blob = calloc(1, klass->data_offset + klass->desc->data_size);
800
801    _eobj_kls_itr_init(obj, EOBJ_NOOP);
802    eobj_constructor_error_unset(obj);
803
804    eobj_ref(obj);
805    eobj_class_constructor(obj, klass);
806
807    if (eobj_constructor_error_get(obj))
808      {
809         ERR("Type '%s' - One of the object constructors have failed.", klass->desc->name);
810         goto fail;
811      }
812
813    if (!_eobj_kls_itr_reached_end(obj))
814      {
815         ERR("Type '%s' - Not all of the object constructors have been executed.", klass->desc->name);
816         goto fail;
817      }
818    _eobj_kls_itr_end(obj, EOBJ_NOOP);
819    eobj_unref(obj);
820
821    return obj;
822
823 fail:
824    /* Unref twice, once for the ref above, and once for the basic object ref. */
825    eobj_unref(obj);
826    eobj_unref(obj);
827    return NULL;
828 }
829
830 typedef struct
831 {
832    EINA_INLIST;
833    const Eobj *ref_obj;
834    const char *file;
835    int line;
836 } Eobj_Xref_Node;
837
838 EAPI Eobj *
839 eobj_xref_internal(Eobj *obj, const Eobj *ref_obj, const char *file, int line)
840 {
841    eobj_ref(obj);
842
843 #ifndef NDEBUG
844    Eobj_Xref_Node *xref = calloc(1, sizeof(*xref));
845    xref->ref_obj = ref_obj;
846    xref->file = file;
847    xref->line = line;
848
849    /* FIXME: Make it sorted. */
850    obj->xrefs = eina_inlist_prepend(obj->xrefs, EINA_INLIST_GET(xref));
851 #else
852    (void) ref_obj;
853    (void) file;
854    (void) line;
855 #endif
856
857    return obj;
858 }
859
860 EAPI void
861 eobj_xunref(Eobj *obj, const Eobj *ref_obj)
862 {
863 #ifndef NDEBUG
864    Eobj_Xref_Node *xref = NULL;
865    EINA_INLIST_FOREACH(obj->xrefs, xref)
866      {
867         if (xref->ref_obj == ref_obj)
868            break;
869      }
870
871    if (xref)
872      {
873         obj->xrefs = eina_inlist_remove(obj->xrefs, EINA_INLIST_GET(xref));
874         free(xref);
875      }
876    else
877      {
878         ERR("ref_obj (%p) does not reference obj (%p). Aborting unref.", ref_obj, obj);
879         return;
880      }
881 #else
882    (void) ref_obj;
883 #endif
884    eobj_unref(obj);
885 }
886
887 EAPI Eobj *
888 eobj_ref(Eobj *obj)
889 {
890    obj->refcount++;
891    return obj;
892 }
893
894 EAPI void
895 eobj_unref(Eobj *obj)
896 {
897    if (--(obj->refcount) == 0)
898      {
899         /* We need that for the event callbacks that may ref/unref. */
900         obj->refcount++;
901
902         if (!obj->delete)
903           {
904              eobj_event_callback_call(obj, EOBJ_EV_DEL, NULL);
905              obj->delete = EINA_TRUE;
906           }
907         eobj_event_callback_call(obj, EOBJ_EV_FREE, NULL);
908
909         obj->refcount--;
910
911         const Eobj_Class *klass = eobj_class_get(obj);
912         _eobj_kls_itr_init(obj, EOBJ_NOOP);
913         eobj_constructor_error_unset(obj);
914         eobj_class_destructor(obj, klass);
915         if (eobj_constructor_error_get(obj))
916           {
917              ERR("Type '%s' - One of the object destructors have failed.", klass->desc->name);
918           }
919
920         if (!_eobj_kls_itr_reached_end(obj))
921           {
922              ERR("Type '%s' - Not all of the object destructors have been executed.", klass->desc->name);
923           }
924         _eobj_kls_itr_end(obj, EOBJ_NOOP);
925         /*FIXME: add eobj_class_unref(klass) ? - just to clear the caches. */
926
927         /* If for some reason it's not empty, clear it. */
928         while (obj->kls_itr)
929           {
930              WRN("Kls_Itr is not empty, possibly a bug, please report. - An error will be reported for each kls_itr in the stack.");
931              Eina_Inlist *nitr = nitr->next;
932              free(EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node));
933              obj->kls_itr = nitr;
934           }
935
936 #ifndef NDEBUG
937         /* If for some reason it's not empty, clear it. */
938         while (obj->xrefs)
939           {
940              WRN("obj->xrefs is not empty, possibly a bug, please report. - An error will be reported for each xref in the stack.");
941              Eina_Inlist *nitr = nitr->next;
942              free(EINA_INLIST_CONTAINER_GET(obj->xrefs, Eobj_Kls_Itr_Node));
943              obj->xrefs = nitr;
944           }
945 #endif
946
947         Eina_List *itr, *itr_n;
948         Eobj *emb_obj;
949         EINA_LIST_FOREACH_SAFE(obj->composite_objects, itr, itr_n, emb_obj)
950           {
951              eobj_composite_object_detach(obj, emb_obj);
952           }
953
954         _eobj_callback_remove_all(obj);
955
956         if (obj->data_blob)
957            free(obj->data_blob);
958
959         free(obj);
960      }
961 }
962
963 EAPI int
964 eobj_ref_get(const Eobj *obj)
965 {
966    return obj->refcount;
967 }
968
969 /* Weak reference. */
970 Eina_Bool
971 _eobj_weak_ref_cb(void *data, Eobj *obj __UNUSED__, const Eobj_Event_Description *desc __UNUSED__, void *event_info __UNUSED__)
972 {
973    Eobj_Weak_Ref *wref = data;
974    wref->obj = NULL;
975
976    return EINA_TRUE;
977 }
978
979 EAPI Eobj_Weak_Ref *
980 eobj_weak_ref_new(const Eobj *_obj)
981 {
982    Eobj *obj = (Eobj *) _obj;
983    Eobj_Weak_Ref *wref = calloc(1, sizeof(*wref));
984    wref->obj = obj;
985    eobj_event_callback_add(obj, EOBJ_EV_DEL, _eobj_weak_ref_cb, wref);
986
987    return wref;
988 }
989
990 EAPI void
991 eobj_weak_ref_free(Eobj_Weak_Ref *wref)
992 {
993    if (wref->obj)
994      {
995         eobj_event_callback_del_full(wref->obj, EOBJ_EV_DEL, _eobj_weak_ref_cb,
996               wref);
997      }
998    free(wref);
999 }
1000
1001 /* EOF Weak reference. */
1002
1003 EAPI void
1004 eobj_del(Eobj *obj)
1005 {
1006    if (!obj->delete)
1007      {
1008         eobj_event_callback_call(obj, EOBJ_EV_DEL, NULL);
1009         obj->delete = EINA_TRUE;
1010      }
1011    eobj_unref(obj);
1012 }
1013
1014 EAPI Eobj *
1015 eobj_parent_get(Eobj *obj)
1016 {
1017    return obj->parent;
1018 }
1019
1020 EAPI void
1021 eobj_constructor_error_set(Eobj *obj)
1022 {
1023    obj->construct_error = EINA_TRUE;
1024 }
1025
1026 static void
1027 eobj_constructor_error_unset(Eobj *obj)
1028 {
1029    obj->construct_error = EINA_FALSE;
1030 }
1031
1032 EAPI Eina_Bool
1033 eobj_constructor_error_get(const Eobj *obj)
1034 {
1035    return obj->construct_error;
1036 }
1037
1038 static inline void
1039 _eobj_constructor_default(Eobj *obj)
1040 {
1041    eobj_constructor_super(obj);
1042 }
1043
1044 static inline void
1045 _eobj_destructor_default(Eobj *obj)
1046 {
1047    eobj_destructor_super(obj);
1048 }
1049
1050 static void
1051 eobj_class_constructor(Eobj *obj, const Eobj_Class *klass)
1052 {
1053    if (!klass)
1054       return;
1055
1056    if (klass->desc->constructor)
1057       klass->desc->constructor(obj, eobj_data_get(obj, klass));
1058    else
1059       _eobj_constructor_default(obj);
1060 }
1061
1062 static void
1063 eobj_class_destructor(Eobj *obj, const Eobj_Class *klass)
1064 {
1065    if (!klass)
1066       return;
1067
1068    if (klass->desc->destructor)
1069       klass->desc->destructor(obj, eobj_data_get(obj, klass));
1070    else
1071       _eobj_destructor_default(obj);
1072 }
1073
1074 EAPI void
1075 eobj_constructor_super(Eobj *obj)
1076 {
1077    eobj_class_constructor(obj, _eobj_kls_itr_next(obj, EOBJ_NOOP));
1078 }
1079
1080 EAPI void
1081 eobj_destructor_super(Eobj *obj)
1082 {
1083    eobj_class_destructor(obj, _eobj_kls_itr_next(obj, EOBJ_NOOP));
1084 }
1085
1086 EAPI void *
1087 eobj_data_get(Eobj *obj, const Eobj_Class *klass)
1088 {
1089    /* FIXME: Add a check that this is of the right klass and we don't seg.
1090     * Probably just return NULL. */
1091    if (klass->desc->data_size > 0)
1092       return ((char *) obj->data_blob) + klass->data_offset;
1093    else
1094       return NULL;
1095 }
1096
1097 EAPI Eina_Bool
1098 eobj_init(void)
1099 {
1100    const char *log_dom = "eobj";
1101    if (_eobj_init_count++ > 0)
1102       return EINA_TRUE;
1103
1104    eina_init();
1105
1106    _eobj_classes = NULL;
1107    _eobj_classes_last_id = 0;
1108    _eobj_log_dom = eina_log_domain_register(log_dom, EINA_COLOR_LIGHTBLUE);
1109    if (_eobj_log_dom < 0)
1110      {
1111         EINA_LOG_ERR("Could not register log domain: %s", log_dom);
1112         return EINA_FALSE;
1113      }
1114
1115    return EINA_TRUE;
1116 }
1117
1118 EAPI Eina_Bool
1119 eobj_shutdown(void)
1120 {
1121    int i;
1122    Eobj_Class **cls_itr = _eobj_classes;
1123
1124    if (--_eobj_init_count > 0)
1125       return EINA_TRUE;
1126
1127    for (i = 0 ; i < _eobj_classes_last_id ; i++, cls_itr++)
1128      {
1129         if (*cls_itr)
1130            eobj_class_free(*cls_itr);
1131      }
1132
1133    if (_eobj_classes)
1134       free(_eobj_classes);
1135
1136    eina_log_domain_unregister(_eobj_log_dom);
1137    _eobj_log_dom = -1;
1138
1139    eina_shutdown();
1140    return EINA_TRUE;
1141 }
1142
1143 EAPI void
1144 eobj_composite_object_attach(Eobj *obj, Eobj *emb_obj)
1145 {
1146    eobj_xref(emb_obj, obj);
1147    obj->composite_objects = eina_list_prepend(obj->composite_objects, emb_obj);
1148 }
1149
1150 EAPI void
1151 eobj_composite_object_detach(Eobj *obj, Eobj *emb_obj)
1152 {
1153    obj->composite_objects = eina_list_remove(obj->composite_objects, emb_obj);
1154    eobj_xunref(emb_obj, obj);
1155 }
1156
1157 EAPI Eina_Bool
1158 eobj_composite_is(Eobj *emb_obj)
1159 {
1160    Eobj *obj = eobj_parent_get(emb_obj);
1161    Eina_List *itr;
1162    Eobj *tmp;
1163
1164    if (!obj)
1165       return EINA_FALSE;
1166
1167    EINA_LIST_FOREACH(obj->composite_objects, itr, tmp)
1168      {
1169         if (tmp == emb_obj)
1170            return EINA_TRUE;
1171      }
1172
1173    return EINA_FALSE;
1174 }
1175
1176 /* Callbacks */
1177 struct _Eobj_Callback_Description
1178 {
1179    EINA_INLIST;
1180    const Eobj_Event_Description *event;
1181    Eobj_Event_Cb func;
1182    void *func_data;
1183    Eobj_Callback_Priority priority;
1184    Eina_Bool delete_me : 1;
1185 };
1186
1187 /* Actually remove, doesn't care about walking list, or delete_me */
1188 static void
1189 _eobj_callback_remove(Eobj *obj, Eobj_Callback_Description *cb)
1190 {
1191    obj->callbacks = eina_inlist_remove(obj->callbacks,
1192          EINA_INLIST_GET(cb));
1193    free(cb);
1194 }
1195
1196 /* Actually remove, doesn't care about walking list, or delete_me */
1197 static void
1198 _eobj_callback_remove_all(Eobj *obj)
1199 {
1200    Eina_Inlist *initr;
1201    Eobj_Callback_Description *cb;
1202    EINA_INLIST_FOREACH_SAFE(obj->callbacks, initr, cb)
1203      {
1204         _eobj_callback_remove(obj, cb);
1205      }
1206 }
1207
1208 static void
1209 _eobj_callbacks_clear(Eobj *obj)
1210 {
1211    Eina_Inlist *itn;
1212    Eobj_Callback_Description *cb;
1213
1214    /* Abort if we are currently walking the list. */
1215    if (obj->walking_list > 0)
1216       return;
1217
1218    EINA_INLIST_FOREACH_SAFE(obj->callbacks, itn, cb)
1219      {
1220         if (cb->delete_me)
1221           {
1222              _eobj_callback_remove(obj, cb);
1223           }
1224      }
1225 }
1226
1227 static int
1228 _callback_priority_cmp(const void *_a, const void *_b)
1229 {
1230    const Eobj_Callback_Description *a, *b;
1231    a = (const Eobj_Callback_Description *) _a;
1232    b = (const Eobj_Callback_Description *) _b;
1233    if (a->priority < b->priority)
1234       return -1;
1235    else
1236       return 1;
1237 }
1238
1239 EAPI Eina_Bool
1240 eobj_event_callback_priority_add(Eobj *obj,
1241       const Eobj_Event_Description *desc,
1242       Eobj_Callback_Priority priority,
1243       Eobj_Event_Cb func,
1244       const void *data)
1245 {
1246    Eobj_Callback_Description *cb = calloc(1, sizeof(*cb));
1247    cb->event = desc;
1248    cb->func = func;
1249    cb->func_data = (void *) data;
1250    cb->priority = priority;
1251    obj->callbacks = eina_inlist_sorted_insert(obj->callbacks,
1252          EINA_INLIST_GET(cb), _callback_priority_cmp);
1253
1254    eobj_event_callback_call(obj, EOBJ_EV_CALLBACK_ADD, desc);
1255
1256    return EINA_TRUE;
1257 }
1258
1259 EAPI void *
1260 eobj_event_callback_del(Eobj *obj, const Eobj_Event_Description *desc, Eobj_Event_Cb func)
1261 {
1262    void *ret = NULL;
1263    Eobj_Callback_Description *cb;
1264    EINA_INLIST_FOREACH(obj->callbacks, cb)
1265      {
1266         if ((cb->event == desc) && (cb->func == func))
1267           {
1268              void *data;
1269
1270              data = cb->func_data;
1271              cb->delete_me = EINA_TRUE;
1272              _eobj_callbacks_clear(obj);
1273              ret = data;
1274              goto found;
1275           }
1276      }
1277
1278    return NULL;
1279
1280 found:
1281    eobj_event_callback_call(obj, EOBJ_EV_CALLBACK_DEL, desc);
1282    return ret;
1283 }
1284
1285 EAPI void *
1286 eobj_event_callback_del_full(Eobj *obj, const Eobj_Event_Description *desc, Eobj_Event_Cb func, const void *user_data)
1287 {
1288    void *ret = NULL;
1289    Eobj_Callback_Description *cb;
1290    EINA_INLIST_FOREACH(obj->callbacks, cb)
1291      {
1292         if ((cb->event == desc) && (cb->func == func) &&
1293               (cb->func_data == user_data))
1294           {
1295              void *data;
1296
1297              data = cb->func_data;
1298              cb->delete_me = EINA_TRUE;
1299              _eobj_callbacks_clear(obj);
1300              ret = data;
1301              goto found;
1302           }
1303      }
1304
1305    return NULL;
1306
1307 found:
1308    eobj_event_callback_call(obj, EOBJ_EV_CALLBACK_DEL, desc);
1309    return ret;
1310 }
1311
1312 EAPI Eina_Bool
1313 eobj_event_callback_call(Eobj *obj, const Eobj_Event_Description *desc,
1314       const void *event_info)
1315 {
1316    Eina_Bool ret = EINA_TRUE;
1317    Eobj_Callback_Description *cb;
1318
1319    eobj_ref(obj);
1320    obj->walking_list++;
1321
1322    EINA_INLIST_FOREACH(obj->callbacks, cb)
1323      {
1324         if (!cb->delete_me && (cb->event == desc))
1325           {
1326              /* Abort callback calling if the func says so. */
1327              if (!cb->func((void *) cb->func_data, obj, desc,
1328                       (void *) event_info))
1329                {
1330                   ret = EINA_FALSE;
1331                   break;
1332                }
1333           }
1334         if (obj->delete)
1335           break;
1336      }
1337    obj->walking_list--;
1338    _eobj_callbacks_clear(obj);
1339    eobj_unref(obj);
1340
1341    return ret;
1342 }
1343
1344 static Eina_Bool
1345 _eobj_event_forwarder_callback(void *data, Eobj *obj, const Eobj_Event_Description *desc, void *event_info)
1346 {
1347    (void) obj;
1348    Eobj *new_obj = (Eobj *) data;
1349    return eobj_event_callback_call(new_obj, desc, event_info);
1350 }
1351
1352 /* FIXME: Change default priority? Maybe call later? */
1353 EAPI Eina_Bool
1354 eobj_event_callback_forwarder_add(Eobj *obj, const Eobj_Event_Description *desc, Eobj *new_obj)
1355 {
1356    return eobj_event_callback_add(obj, desc, _eobj_event_forwarder_callback, new_obj);
1357 }
1358
1359 EAPI Eina_Bool
1360 eobj_event_callback_forwarder_del(Eobj *obj, const Eobj_Event_Description *desc, Eobj *new_obj)
1361 {
1362    eobj_event_callback_del_full(obj, desc, _eobj_event_forwarder_callback, new_obj);
1363    return EINA_TRUE;
1364 }
1365