Eobj: Only allocate kls_itr when really needed.
[profile/ivi/eobj.git] / lib / Eobj.h
1 #ifndef EOBJ_H
2 #define EOBJ_H
3
4 #include <stdarg.h>
5 #include <Eina.h>
6
7 /* Check that the types are castable and cast them. */
8 #define EOBJ_TYPECHECK(type, x) \
9    ({ \
10     type __x; \
11     __x = x; \
12     (void) __x; \
13     (type) x; \
14     })
15
16 #define EOBJ_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(*arr))
17
18 typedef struct _Eobj Eobj;
19 typedef uintptr_t Eobj_Op;
20
21 #define EOBJ_NOOP ((Eobj_Op) 0)
22
23 typedef struct _Eobj_Class Eobj_Class;
24 typedef int Eobj_Class_Id;
25
26 typedef enum
27 {
28    EOBJ_CLASS_TYPE_REGULAR = 0,
29    EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT,
30    EOBJ_CLASS_TYPE_INTERFACE,
31    EOBJ_CLASS_TYPE_MIXIN
32 } Eobj_Class_Type;
33
34 typedef void (*eobj_op_func_type)(Eobj *, Eobj_Op, va_list *list);
35
36 typedef struct
37 {
38    Eobj_Op op;
39    eobj_op_func_type func;
40 } Eobj_Op_Func_Description;
41
42 #define EOBJ_OP_FUNC_DESCRIPTION(op, func) { op, func }
43 #define EOBJ_OP_FUNC_DESCRIPTION_SENTINEL { 0, NULL }
44
45 typedef struct
46 {
47    const char *name; /**< name used for lookups */
48    const char *type; /**< used for introspection purposes, documents what goes as callback event information (@c event_info) */
49    const char *doc; /**< documentation for introspection purposes */
50 } Eobj_Event_Description;
51
52
53 typedef struct
54 {
55    Eobj_Op sub_op;
56    const char *name;
57    /* FIXME: properly define the type so it'll support get/set and etc.
58     * Do I even need/want type? If so, do I need/want it here? docs aren't
59     * enough? */
60    const char *type;
61    const char *doc;
62 } Eobj_Op_Description;
63
64 typedef struct
65 {
66    const char *name;
67    Eobj_Class_Type type;
68    struct {
69         Eobj_Op *base_op_id;
70         const Eobj_Op_Description *descs;
71         size_t count;
72    } ops;
73    const Eobj_Event_Description **events;
74    size_t private_size;
75    void (*constructor)(Eobj *obj);
76    void (*destructor)(Eobj *obj);
77    void (*class_constructor)(Eobj_Class *klass);
78    void (*class_destructor)(Eobj_Class *klass);
79 } Eobj_Class_Description;
80
81 #define EOBJ_CLASS_DESCRIPTION_OPS(base_op_id, op_descs, count) { base_op_id, op_descs, count }
82
83 #define EOBJ_OP_DESCRIPTION(op, type, doc) { op, #op, type, doc }
84 #define EOBJ_OP_DESCRIPTION_SENTINEL { 0, NULL, NULL, NULL }
85
86 #define EOBJ_EVENT_DESCRIPTION(name, type, doc) { name, type, doc }
87
88 EAPI Eina_Bool eobj_init(void);
89 EAPI Eina_Bool eobj_shutdown(void);
90
91 #define eobj_do(object, ...) eobj_do_internal(object, __VA_ARGS__, NULL)
92
93 EAPI Eina_Bool eobj_do_internal(Eobj *obj, ...);
94
95 EAPI Eina_Bool eobj_super_do(Eobj *obj, Eobj_Op op, ...);
96
97 EAPI const Eobj_Class *eobj_class_get(Eobj *obj);
98 EAPI const Eobj_Class *eobj_class_parent_get(const Eobj_Class *klass);
99 EAPI const char *eobj_class_name_get(const Eobj_Class *klass);
100
101 EAPI void eobj_constructor_super(Eobj *obj);
102 EAPI void eobj_destructor_super(Eobj *obj);
103 EAPI void eobj_constructor_error_set(Eobj *obj);
104 EAPI Eina_Bool eobj_constructor_error_get(const Eobj *obj);
105
106 EAPI Eobj_Class *eobj_class_new(const Eobj_Class_Description *desc, const Eobj_Class *parent, ...);
107 EAPI void eobj_class_free(Eobj_Class *klass);
108 EAPI void eobj_class_funcs_set(Eobj_Class *klass, const Eobj_Op_Func_Description *func_descs);
109
110 EAPI Eobj *eobj_add(const Eobj_Class *klass, Eobj *parent);
111 EAPI Eobj *eobj_parent_get(Eobj *obj);
112 EAPI void *eobj_data_get(Eobj *obj, const Eobj_Class *klass);
113 EAPI Eobj *eobj_ref(Eobj *obj);
114 EAPI void eobj_unref(Eobj *obj);
115 EAPI int eobj_ref_get(const Eobj *obj);
116 EAPI void eobj_del(Eobj *obj);
117 EAPI void *eobj_generic_data_set(Eobj *obj, const char *key, const void *data);
118 EAPI void *eobj_generic_data_get(const Eobj *obj, const char *key);
119 EAPI void *eobj_generic_data_del(Eobj *obj, const char *key);
120
121 #define EOBJ_CLASS_BASE eobj_base_class_get()
122 EAPI const Eobj_Class *eobj_base_class_get(void) EINA_CONST;
123
124 EAPI void eobj_composite_object_attach(Eobj *obj, Eobj *emb_obj);
125 EAPI void eobj_composite_object_detach(Eobj *obj, Eobj *emb_obj);
126 EAPI Eina_Bool eobj_composite_is(Eobj *emb_obj);
127
128 /* Events */
129 /**
130  * @def EOBJ_CALLBACK_PRIORITY_BEFORE
131  * Slightly more prioritized than default.
132  */
133 #define EOBJ_CALLBACK_PRIORITY_BEFORE -100
134 /**
135  * @def EOBJ_CALLBACK_PRIORITY_DEFAULT
136  * Default callback priority level
137  */
138 #define EOBJ_CALLBACK_PRIORITY_DEFAULT 0
139 /**
140  * @def EOBJ_CALLBACK_PRIORITY_AFTER
141  * Slightly less prioritized than default.
142  */
143 #define EOBJ_CALLBACK_PRIORITY_AFTER 100
144 typedef short Eobj_Callback_Priority;
145
146 /* True meaning continue, False meaning stop callbacks. - Make it an enum? */
147 typedef Eina_Bool (*Eobj_Event_Cb)(void *data, Eobj *obj, const Eobj_Event_Description *desc, void *event_info);
148
149 EAPI Eina_Bool eobj_event_callback_forwarder_add(Eobj *obj, const Eobj_Event_Description *desc, Eobj *new_obj);
150 EAPI Eina_Bool eobj_event_callback_forwarder_del(Eobj *obj, const Eobj_Event_Description *desc, Eobj *new_obj);
151
152 /* callbacks of the same priority are called in reverse order of creation. */
153 EAPI Eina_Bool eobj_event_callback_add(Eobj *obj, const Eobj_Event_Description *desc, Eobj_Event_Cb cb, const void *data);
154 EAPI Eina_Bool eobj_event_callback_priority_add(Eobj *obj, const Eobj_Event_Description *desc, Eobj_Callback_Priority priority, Eobj_Event_Cb cb, const void *data);
155 EAPI void *eobj_event_callback_del(Eobj *obj, const Eobj_Event_Description *desc, Eobj_Event_Cb func);
156 EAPI void *eobj_event_callback_del_full(Eobj *obj, const Eobj_Event_Description *desc, Eobj_Event_Cb func, const void *user_data);
157 EAPI Eina_Bool eobj_event_callback_call(Eobj *obj, const Eobj_Event_Description *desc, const void *event_info);
158
159 EAPI extern const Eobj_Event_Description _EOBJ_SIG_CALLBACK_ADD;
160 #define EOBJ_SIG_CALLBACK_ADD (&(_EOBJ_SIG_CALLBACK_ADD))
161 EAPI extern const Eobj_Event_Description _EOBJ_SIG_CALLBACK_DEL;
162 #define EOBJ_SIG_CALLBACK_DEL (&(_EOBJ_SIG_CALLBACK_DEL))
163
164 #endif