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