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