Eobj: Made eobj_class_new return a const class.
[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
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    itr = desc->ops.descs;
577    for (i = 0 ; i < desc->ops.count ; i++, itr++)
578      {
579         if (itr->sub_op != i)
580           {
581              ERR("Wrong order in Ops description for class '%s'. Expected %d and got %d", desc->name, i, itr->sub_op);
582              return EINA_FALSE;
583           }
584      }
585
586    if (itr && itr->name)
587      {
588         ERR("Found extra Ops description for class '%s'. Expected %d descriptions, but found more.", desc->name, desc->ops.count);
589         return EINA_FALSE;
590      }
591
592    return EINA_TRUE;
593 }
594
595 EAPI const Eobj_Class *
596 eobj_class_new(const Eobj_Class_Description *desc, const Eobj_Class *parent, ...)
597 {
598    Eobj_Class *klass;
599    va_list p_list;
600
601    va_start(p_list, parent);
602
603 #define _CLS_NEW_CHECK(x) \
604    do \
605      { \
606         if (!x) \
607           { \
608              ERR("%s can't be NULL! Aborting.", #x); \
609              return NULL; \
610           } \
611      } \
612    while(0)
613
614    _CLS_NEW_CHECK(desc);
615    _CLS_NEW_CHECK(desc->name);
616
617    klass = calloc(1, sizeof(Eobj_Class));
618    klass->parent = parent;
619
620    /* Handle class extensions */
621      {
622         Eobj_Class *extn = NULL;
623
624         extn = va_arg(p_list, Eobj_Class *);
625         while (extn)
626           {
627              switch (extn->desc->type)
628                {
629                 case EOBJ_CLASS_TYPE_REGULAR:
630                 case EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT:
631                    /* Use it like an interface. */
632                 case EOBJ_CLASS_TYPE_INTERFACE:
633                    break;
634                 case EOBJ_CLASS_TYPE_MIXIN:
635                      {
636                         Eobj_Extension_Node *node = calloc(1, sizeof(*node));
637                         node->klass = extn;
638                         klass->extensions =
639                            eina_inlist_append(klass->extensions,
640                                  EINA_INLIST_GET(node));
641                      }
642                    break;
643                }
644
645              extn = va_arg(p_list, Eobj_Class *);
646           }
647      }
648
649    klass->desc = desc;
650
651    /* Handle the inheritance */
652    if (klass->parent)
653      {
654         /* Verify the inheritance is allowed. */
655         switch (klass->desc->type)
656           {
657            case EOBJ_CLASS_TYPE_REGULAR:
658            case EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT:
659               if ((klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR) &&
660                     (klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT))
661                 {
662                    /* FIXME: Actually handle it. */
663                    ERR("Regular classes ('%s') aren't allowed to inherit from non-regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
664                    goto cleanup;
665                 }
666               break;
667            case EOBJ_CLASS_TYPE_INTERFACE:
668            case EOBJ_CLASS_TYPE_MIXIN:
669               if ((klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR) &&
670                     (klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT))
671                 {
672                    /* FIXME: Actually handle it. */
673                    ERR("Non-regular classes ('%s') aren't allowed to inherit from regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
674                    goto cleanup;
675                 }
676               break;
677           }
678
679
680         /* Update the current offset. */
681         /* FIXME: Make sure this alignment is enough. */
682         klass->data_offset = klass->parent->data_offset +
683            klass->parent->desc->data_size +
684            (sizeof(void *) -
685                   (klass->parent->desc->data_size % sizeof(void *)));
686      }
687
688    if (!_eobj_class_check_op_descs(klass))
689      {
690         ERR("Class '%s' has a bad op description.", klass->desc->name);
691         goto cleanup;
692      }
693
694    klass->class_id = ++_eobj_classes_last_id;
695      {
696         /* FIXME: Handle errors. */
697         Eobj_Class **tmp;
698         tmp = realloc(_eobj_classes, _eobj_classes_last_id * sizeof(*_eobj_classes));
699         _eobj_classes = tmp;
700         _eobj_classes[klass->class_id - 1] = klass;
701      }
702
703    _eobj_class_base_op_init(klass);
704
705    /* FIXME: Shouldn't be called here - should be called from eobj_add. */
706    _eobj_class_constructor(klass);
707
708    va_end(p_list);
709
710    return klass;
711
712 cleanup:
713    eobj_class_free(klass);
714    return NULL;
715 }
716 #undef _CLS_NEW_CHECK
717
718 EAPI Eobj *
719 eobj_add(const Eobj_Class *klass, Eobj *parent)
720 {
721    if (klass->desc->type != EOBJ_CLASS_TYPE_REGULAR)
722      {
723         ERR("Class '%s' is not instantiate-able. Aborting.", klass->desc->name);
724         return NULL;
725      }
726
727    Eobj *obj = calloc(1, sizeof(*obj));
728    obj->klass = klass;
729    obj->parent = parent;
730
731    obj->refcount++;
732
733    obj->data_blob = calloc(1, klass->data_offset + klass->desc->data_size);
734
735    _eobj_kls_itr_init(obj, EOBJ_NOOP);
736    eobj_constructor_error_unset(obj);
737
738    eobj_ref(obj);
739    eobj_class_constructor(obj, klass);
740
741    if (eobj_constructor_error_get(obj))
742      {
743         ERR("Type '%s' - One of the object constructors have failed.", klass->desc->name);
744         goto fail;
745      }
746
747    if (!_eobj_kls_itr_reached_end(obj))
748      {
749         ERR("Type '%s' - Not all of the object constructors have been executed.", klass->desc->name);
750         goto fail;
751      }
752    _eobj_kls_itr_end(obj, EOBJ_NOOP);
753    eobj_unref(obj);
754
755    return obj;
756
757 fail:
758    /* Unref twice, once for the ref above, and once for the basic object ref. */
759    eobj_unref(obj);
760    eobj_unref(obj);
761    return NULL;
762 }
763
764 EAPI Eobj *
765 eobj_ref(Eobj *obj)
766 {
767    obj->refcount++;
768    return obj;
769 }
770
771 EAPI void
772 eobj_unref(Eobj *obj)
773 {
774    if (--(obj->refcount) == 0)
775      {
776         /* We need that for the event callbacks that may ref/unref. */
777         obj->refcount++;
778
779         if (!obj->delete)
780           {
781              eobj_event_callback_call(obj, EOBJ_EV_DEL, NULL);
782              obj->delete = EINA_TRUE;
783           }
784         eobj_event_callback_call(obj, EOBJ_EV_FREE, NULL);
785
786         obj->refcount--;
787
788         const Eobj_Class *klass = eobj_class_get(obj);
789         _eobj_kls_itr_init(obj, EOBJ_NOOP);
790         eobj_constructor_error_unset(obj);
791         eobj_class_destructor(obj, klass);
792         if (eobj_constructor_error_get(obj))
793           {
794              ERR("Type '%s' - One of the object destructors have failed.", klass->desc->name);
795           }
796
797         if (!_eobj_kls_itr_reached_end(obj))
798           {
799              ERR("Type '%s' - Not all of the object destructors have been executed.", klass->desc->name);
800           }
801         _eobj_kls_itr_end(obj, EOBJ_NOOP);
802         /*FIXME: add eobj_class_unref(klass) ? - just to clear the caches. */
803
804         /* If for some reason it's not empty, clear it. */
805         while (obj->kls_itr)
806           {
807              WRN("Kls_Itr is not empty, possibly a bug, please report. - An error will be reported for each kls_itr in the stack.");
808              Eina_Inlist *nitr = nitr->next;
809              free(EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node));
810              obj->kls_itr = nitr;
811           }
812
813         Eina_List *itr, *itr_n;
814         Eobj *emb_obj;
815         EINA_LIST_FOREACH_SAFE(obj->composite_objects, itr, itr_n, emb_obj)
816           {
817              /* FIXME: Should probably be unref. */
818              eobj_del(emb_obj);
819              obj->composite_objects =
820                 eina_list_remove_list(obj->composite_objects, itr);
821           }
822
823         _eobj_callback_remove_all(obj);
824
825         if (obj->data_blob)
826            free(obj->data_blob);
827
828         _eobj_generic_data_del_all(obj);
829
830         free(obj);
831      }
832 }
833
834 EAPI int
835 eobj_ref_get(const Eobj *obj)
836 {
837    return obj->refcount;
838 }
839
840 EAPI void
841 eobj_del(Eobj *obj)
842 {
843    if (!obj->delete)
844      {
845         eobj_event_callback_call(obj, EOBJ_EV_DEL, NULL);
846         obj->delete = EINA_TRUE;
847      }
848    eobj_unref(obj);
849 }
850
851 EAPI Eobj *
852 eobj_parent_get(Eobj *obj)
853 {
854    return obj->parent;
855 }
856
857 EAPI void
858 eobj_constructor_error_set(Eobj *obj)
859 {
860    obj->construct_error = EINA_TRUE;
861 }
862
863 static void
864 eobj_constructor_error_unset(Eobj *obj)
865 {
866    obj->construct_error = EINA_FALSE;
867 }
868
869 EAPI Eina_Bool
870 eobj_constructor_error_get(const Eobj *obj)
871 {
872    return obj->construct_error;
873 }
874
875 static inline void
876 _eobj_constructor_default(Eobj *obj)
877 {
878    eobj_constructor_super(obj);
879 }
880
881 static inline void
882 _eobj_destructor_default(Eobj *obj)
883 {
884    eobj_destructor_super(obj);
885 }
886
887 static void
888 eobj_class_constructor(Eobj *obj, const Eobj_Class *klass)
889 {
890    if (!klass)
891       return;
892
893    if (klass->desc->constructor)
894       klass->desc->constructor(obj, eobj_data_get(obj, klass));
895    else
896       _eobj_constructor_default(obj);
897 }
898
899 static void
900 eobj_class_destructor(Eobj *obj, const Eobj_Class *klass)
901 {
902    if (!klass)
903       return;
904
905    if (klass->desc->destructor)
906       klass->desc->destructor(obj, eobj_data_get(obj, klass));
907    else
908       _eobj_destructor_default(obj);
909 }
910
911 EAPI void
912 eobj_constructor_super(Eobj *obj)
913 {
914    eobj_class_constructor(obj, _eobj_kls_itr_next(obj));
915 }
916
917 EAPI void
918 eobj_destructor_super(Eobj *obj)
919 {
920    eobj_class_destructor(obj, _eobj_kls_itr_next(obj));
921 }
922
923 EAPI void *
924 eobj_data_get(Eobj *obj, const Eobj_Class *klass)
925 {
926    /* FIXME: Add a check that this is of the right klass and we don't seg.
927     * Probably just return NULL. */
928    if (klass->desc->data_size > 0)
929       return ((char *) obj->data_blob) + klass->data_offset;
930    else
931       return NULL;
932 }
933
934 typedef struct
935 {
936    EINA_INLIST;
937    Eina_Stringshare *key;
938    void *data;
939 } Eobj_Generic_Data_Node;
940
941 static void
942 _eobj_generic_data_node_free(Eobj_Generic_Data_Node *node)
943 {
944    eina_stringshare_del(node->key);
945    free(node);
946 }
947
948 static void
949 _eobj_generic_data_del_all(Eobj *obj)
950 {
951    Eina_Inlist *nnode;
952    Eobj_Generic_Data_Node *node;
953
954    EINA_INLIST_FOREACH_SAFE(obj->generic_data, nnode, node)
955      {
956         obj->generic_data = eina_inlist_remove(obj->generic_data,
957               EINA_INLIST_GET(node));
958
959         _eobj_generic_data_node_free(node);
960      }
961 }
962
963 EAPI void *
964 eobj_generic_data_set(Eobj *obj, const char *key, const void *data)
965 {
966    void *prev_data;
967    Eobj_Generic_Data_Node *node;
968
969    if (!key) return NULL;
970    if (!data) return NULL;
971
972    prev_data = eobj_generic_data_del(obj, key);
973
974    node = malloc(sizeof(Eobj_Generic_Data_Node));
975    node->key = eina_stringshare_add(key);
976    node->data = (void *) data;
977    obj->generic_data = eina_inlist_prepend(obj->generic_data,
978          EINA_INLIST_GET(node));
979
980    return prev_data;
981 }
982
983 EAPI void *
984 eobj_generic_data_get(const Eobj *obj, const char *key)
985 {
986    Eobj_Generic_Data_Node *node;
987
988    if (!key) return NULL;
989
990    EINA_INLIST_FOREACH(obj->generic_data, node)
991      {
992         if (!strcmp(node->key, key))
993           {
994              ((Eobj *) obj)->generic_data =
995                 eina_inlist_promote(obj->generic_data, EINA_INLIST_GET(node));
996              return node->data;
997           }
998      }
999    return NULL;
1000 }
1001
1002 EAPI void *
1003 eobj_generic_data_del(Eobj *obj, const char *key)
1004 {
1005    Eobj_Generic_Data_Node *node;
1006
1007    if (!key) return NULL;
1008
1009    EINA_INLIST_FOREACH(obj->generic_data, node)
1010      {
1011         if (!strcmp(node->key, key))
1012           {
1013              void *data;
1014
1015              data = node->data;
1016              obj->generic_data = eina_inlist_remove(obj->generic_data,
1017                    EINA_INLIST_GET(node));
1018              _eobj_generic_data_node_free(node);
1019              return data;
1020           }
1021      }
1022    return NULL;
1023 }
1024
1025 EAPI Eina_Bool
1026 eobj_init(void)
1027 {
1028    const char *log_dom = "eobj";
1029    if (_eobj_init_count++ > 0)
1030       return EINA_TRUE;
1031
1032    eina_init();
1033
1034    _eobj_classes = NULL;
1035    _eobj_classes_last_id = 0;
1036    _eobj_log_dom = eina_log_domain_register(log_dom, EINA_COLOR_LIGHTBLUE);
1037    if (_eobj_log_dom < 0)
1038      {
1039         EINA_LOG_ERR("Could not register log domain: %s", log_dom);
1040         return EINA_FALSE;
1041      }
1042
1043    return EINA_TRUE;
1044 }
1045
1046 EAPI Eina_Bool
1047 eobj_shutdown(void)
1048 {
1049    int i;
1050    Eobj_Class **cls_itr = _eobj_classes;
1051
1052    if (--_eobj_init_count > 0)
1053       return EINA_TRUE;
1054
1055    for (i = 0 ; i < _eobj_classes_last_id ; i++, cls_itr++)
1056      {
1057         if (*cls_itr)
1058            eobj_class_free(*cls_itr);
1059      }
1060
1061    if (_eobj_classes)
1062       free(_eobj_classes);
1063
1064    eina_log_domain_unregister(_eobj_log_dom);
1065    _eobj_log_dom = -1;
1066
1067    eina_shutdown();
1068    return EINA_TRUE;
1069 }
1070
1071 EAPI void
1072 eobj_composite_object_attach(Eobj *obj, Eobj *emb_obj)
1073 {
1074    eobj_ref(emb_obj);
1075    obj->composite_objects = eina_list_prepend(obj->composite_objects, emb_obj);
1076 }
1077
1078 EAPI void
1079 eobj_composite_object_detach(Eobj *obj, Eobj *emb_obj)
1080 {
1081    obj->composite_objects = eina_list_remove(obj->composite_objects, emb_obj);
1082    eobj_unref(emb_obj);
1083 }
1084
1085 EAPI Eina_Bool
1086 eobj_composite_is(Eobj *emb_obj)
1087 {
1088    Eobj *obj = eobj_parent_get(emb_obj);
1089    Eina_List *itr;
1090    Eobj *tmp;
1091
1092    if (!obj)
1093       return EINA_FALSE;
1094
1095    EINA_LIST_FOREACH(obj->composite_objects, itr, tmp)
1096      {
1097         if (tmp == emb_obj)
1098            return EINA_TRUE;
1099      }
1100
1101    return EINA_FALSE;
1102 }
1103
1104 /* Callbacks */
1105 struct _Eobj_Callback_Description
1106 {
1107    EINA_INLIST;
1108    const Eobj_Event_Description *event;
1109    Eobj_Event_Cb func;
1110    void *func_data;
1111    Eobj_Callback_Priority priority;
1112    Eina_Bool delete_me : 1;
1113 };
1114
1115 /* Actually remove, doesn't care about walking list, or delete_me */
1116 static void
1117 _eobj_callback_remove(Eobj *obj, Eobj_Callback_Description *cb)
1118 {
1119    obj->callbacks = eina_inlist_remove(obj->callbacks,
1120          EINA_INLIST_GET(cb));
1121    free(cb);
1122 }
1123
1124 /* Actually remove, doesn't care about walking list, or delete_me */
1125 static void
1126 _eobj_callback_remove_all(Eobj *obj)
1127 {
1128    Eina_Inlist *initr;
1129    Eobj_Callback_Description *cb;
1130    EINA_INLIST_FOREACH_SAFE(obj->callbacks, initr, cb)
1131      {
1132         _eobj_callback_remove(obj, cb);
1133      }
1134 }
1135
1136 static void
1137 _eobj_callbacks_clear(Eobj *obj)
1138 {
1139    Eina_Inlist *itn;
1140    Eobj_Callback_Description *cb;
1141
1142    /* Abort if we are currently walking the list. */
1143    if (obj->walking_list > 0)
1144       return;
1145
1146    EINA_INLIST_FOREACH_SAFE(obj->callbacks, itn, cb)
1147      {
1148         if (cb->delete_me)
1149           {
1150              _eobj_callback_remove(obj, cb);
1151           }
1152      }
1153 }
1154
1155 static int
1156 _callback_priority_cmp(const void *_a, const void *_b)
1157 {
1158    const Eobj_Callback_Description *a, *b;
1159    a = (const Eobj_Callback_Description *) _a;
1160    b = (const Eobj_Callback_Description *) _b;
1161    if (a->priority < b->priority)
1162       return -1;
1163    else
1164       return 1;
1165 }
1166
1167 EAPI Eina_Bool
1168 eobj_event_callback_priority_add(Eobj *obj,
1169       const Eobj_Event_Description *desc,
1170       Eobj_Callback_Priority priority,
1171       Eobj_Event_Cb func,
1172       const void *data)
1173 {
1174    Eobj_Callback_Description *cb = calloc(1, sizeof(*cb));
1175    cb->event = desc;
1176    cb->func = func;
1177    cb->func_data = (void *) data;
1178    cb->priority = priority;
1179    obj->callbacks = eina_inlist_sorted_insert(obj->callbacks,
1180          EINA_INLIST_GET(cb), _callback_priority_cmp);
1181
1182    eobj_event_callback_call(obj, EOBJ_EV_CALLBACK_ADD, desc);
1183
1184    return EINA_TRUE;
1185 }
1186
1187 EAPI void *
1188 eobj_event_callback_del(Eobj *obj, const Eobj_Event_Description *desc, Eobj_Event_Cb func)
1189 {
1190    void *ret = NULL;
1191    Eobj_Callback_Description *cb;
1192    EINA_INLIST_FOREACH(obj->callbacks, cb)
1193      {
1194         if ((cb->event == desc) && (cb->func == func))
1195           {
1196              void *data;
1197
1198              data = cb->func_data;
1199              cb->delete_me = EINA_TRUE;
1200              _eobj_callbacks_clear(obj);
1201              ret = data;
1202              goto found;
1203           }
1204      }
1205
1206    return NULL;
1207
1208 found:
1209    eobj_event_callback_call(obj, EOBJ_EV_CALLBACK_DEL, desc);
1210    return ret;
1211 }
1212
1213 EAPI void *
1214 eobj_event_callback_del_full(Eobj *obj, const Eobj_Event_Description *desc, Eobj_Event_Cb func, const void *user_data)
1215 {
1216    void *ret = NULL;
1217    Eobj_Callback_Description *cb;
1218    EINA_INLIST_FOREACH(obj->callbacks, cb)
1219      {
1220         if ((cb->event == desc) && (cb->func == func) &&
1221               (cb->func_data == user_data))
1222           {
1223              void *data;
1224
1225              data = cb->func_data;
1226              cb->delete_me = EINA_TRUE;
1227              _eobj_callbacks_clear(obj);
1228              ret = data;
1229              goto found;
1230           }
1231      }
1232
1233    return NULL;
1234
1235 found:
1236    eobj_event_callback_call(obj, EOBJ_EV_CALLBACK_DEL, desc);
1237    return ret;
1238 }
1239
1240 EAPI Eina_Bool
1241 eobj_event_callback_call(Eobj *obj, const Eobj_Event_Description *desc,
1242       const void *event_info)
1243 {
1244    Eina_Bool ret = EINA_TRUE;
1245    Eobj_Callback_Description *cb;
1246
1247    eobj_ref(obj);
1248    obj->walking_list++;
1249
1250    EINA_INLIST_FOREACH(obj->callbacks, cb)
1251      {
1252         if (!cb->delete_me  && (cb->event == desc))
1253           {
1254              /* Abort callback calling if the func says so. */
1255              if (!cb->func((void *) cb->func_data, obj, desc,
1256                       (void *) event_info))
1257                {
1258                   ret = EINA_FALSE;
1259                   break;
1260                }
1261           }
1262         if (obj->delete)
1263           break;
1264      }
1265    obj->walking_list--;
1266    _eobj_callbacks_clear(obj);
1267    eobj_unref(obj);
1268
1269    return ret;
1270 }
1271
1272 static Eina_Bool
1273 _eobj_event_forwarder_callback(void *data, Eobj *obj, const Eobj_Event_Description *desc, void *event_info)
1274 {
1275    (void) obj;
1276    Eobj *new_obj = (Eobj *) data;
1277    return eobj_event_callback_call(new_obj, desc, event_info);
1278 }
1279
1280 /* FIXME: Change default priority? Maybe call later? */
1281 EAPI Eina_Bool
1282 eobj_event_callback_forwarder_add(Eobj *obj, const Eobj_Event_Description *desc, Eobj *new_obj)
1283 {
1284    return eobj_event_callback_add(obj, desc, _eobj_event_forwarder_callback, new_obj);
1285 }
1286
1287 EAPI Eina_Bool
1288 eobj_event_callback_forwarder_del(Eobj *obj, const Eobj_Event_Description *desc, Eobj *new_obj)
1289 {
1290    eobj_event_callback_del_full(obj, desc, _eobj_event_forwarder_callback, new_obj);
1291    return EINA_TRUE;
1292 }
1293
1294 /* EOBJ_CLASS_BASE stuff */
1295 static const Eobj_Class *_my_class = NULL;
1296
1297 /* FIXME: Set proper type descriptions. */
1298 EAPI const Eobj_Event_Description _EOBJ_EV_CALLBACK_ADD =
1299    EOBJ_EVENT_DESCRIPTION("callback,add", "?", "A callback was added.");
1300 EAPI const Eobj_Event_Description _EOBJ_EV_CALLBACK_DEL =
1301    EOBJ_EVENT_DESCRIPTION("callback,del", "?", "A callback was deleted.");
1302 EAPI const Eobj_Event_Description _EOBJ_EV_FREE =
1303    EOBJ_EVENT_DESCRIPTION("free", "", "Obj is being freed.");
1304 EAPI const Eobj_Event_Description _EOBJ_EV_DEL =
1305    EOBJ_EVENT_DESCRIPTION("del", "", "Obj is being deleted.");
1306
1307 static void
1308 _constructor(Eobj *obj, void *class_data __UNUSED__)
1309 {
1310    DBG("%p - %s.", obj, _my_class->desc->name);
1311 }
1312
1313 static void
1314 _destructor(Eobj *obj, void *class_data __UNUSED__)
1315 {
1316    DBG("%p - %s.", obj, _my_class->desc->name);
1317 }
1318
1319 EAPI const Eobj_Class *
1320 eobj_base_class_get(void)
1321 {
1322    if (_my_class) return _my_class;
1323
1324    static const Eobj_Event_Description *event_desc[] = {
1325         EOBJ_EV_CALLBACK_ADD,
1326         EOBJ_EV_CALLBACK_DEL,
1327         EOBJ_EV_FREE,
1328         EOBJ_EV_DEL,
1329         NULL
1330    };
1331
1332    static const Eobj_Class_Description class_desc = {
1333         "Eobj Base",
1334         EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT,
1335         EOBJ_CLASS_DESCRIPTION_OPS(NULL, NULL, 0),
1336         event_desc,
1337         0,
1338         _constructor,
1339         _destructor,
1340         NULL,
1341         NULL
1342    };
1343
1344    return _my_class = eobj_class_new(&class_desc, NULL, NULL);
1345 }
1346