summaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorDaniel Zaoui <daniel.zaoui@samsung.com>2012-12-20 08:29:39 +0000
committerDaniel Zaoui <daniel.zaoui@yahoo.com>2012-12-20 08:29:39 +0000
commit8122fed82e29e10813b29e6b7619fc92c7b72b0f (patch)
treecba368f2521c1b9a16ef3b2e9b67d38c5a66e819 /doc
parentaf7efb7ad0ee2f991696d8dcbeab8676a8823857 (diff)
Simple tutorial for Eo. Supposed to help you to write code with Eo and
prevent eventual bugs. Feel free to ask, comment, modify this tutorial. My last commit before the end of the world. Signed-off-by: Daniel Zaoui <daniel.zaoui@samsung.com> SVN revision: 81425
Diffstat (limited to 'doc')
-rw-r--r--doc/Eo_Tutorial280
1 files changed, 280 insertions, 0 deletions
diff --git a/doc/Eo_Tutorial b/doc/Eo_Tutorial
new file mode 100644
index 0000000000..766ed1ed6e
--- /dev/null
+++ b/doc/Eo_Tutorial
@@ -0,0 +1,280 @@
1EO Tutorial:
2===========
3
4* Purpose:
5---------
6The purpose of this document is to explain how to work with Eo, how to port your code to Eo and what are the common pitfalls. It doesn't explain how it works inside.
7
8* Description:
9-------------
10Eo is an Object oriented infrastructure for the EFL. It is a API/ABI safe library.
11
12It supports inheritance, mixins, interfaces and composite objects.
13Every class can implement functions from every other class.
14It supports event signals, function overrides, private/protected/public/etc. variables and functions.
15
16At the creation of the class, a "virtual table" is filled with the needed functions.
17The key of this table is a (class id, function id) tuple.
18
19eo_do is invoked with a list of op ids and their parameters and is in charge to dispatch the relevant functions. Finding the correct function is fast because it is just a lookup table.
20
21* How to use it?
22---------------
231) Creation of an instance of a class
24Old way:
25object = evas_object_line_add(canvas);
26
27New way:
28object = eo_add(EVAS_OBJ_LINE_CLASS, canvas);
29
302) Call to function
31Old way:
32 evas_object_move(obj, 120, 120);
33 evas_object_resize(obj, 200, 200);
34
35Eo way:
36 eo_do(obj,
37 evas_obj_move(120, 120),
38 evas_obj_resize(200, 200));
39
403) Extract specific data
41Old way:
42 Evas_Object_Line *o = (Evas_Object_Line *)(obj->object_data);
43 MAGIC_CHECK(o, Evas_Object_Line, MAGIC_OBJ_LINE);
44 return;
45 MAGIC_CHECK_END();
46
47Eo way:
48 Evas_Object_Line *o = eo_data_get(obj, EVAS_OBJ_LINE_CLASS);
49 if (!o) return;
50
514) Call function of parent
52Old way:
53 ELM_WIDGET_CLASS(_elm_button_parent_sc)->theme(obj))
54
55New way:
56 eo_do_super(obj, elm_wdg_theme(&int_ret));
57
58* Important to know:
59-------------------
601) eo_do is the function used to invoke functions of a specific class on an object.
61
622) eo_data_get receives an object and a class and returns the data of the given class for the object. The class must belong to the object class hierarchy.
63
643) eo_isa indicates if a given object is of a given type.
65
664) eo_do_super is in charge to invoke a function in the next parents that implements it. It is recommended to use eo_do_super only from a function with the same op id.
67In addition, there is no way to jump over classes who implement the function. If A inherits from B, B from C and A, B and C implement a virtual function defined in C, the function calls order will be A, then B and finally C. It is impossible to pass over B.
68
695) eo_do returns if the operation succeeded or failed (function found, object deleted...), not the result of the called function. Pay attention to this detail when you call eo_do. The return value needs to be an additional parameter which will hold a return value.
70
716) Don't do this:
72 int w, h;
73 eo_do(obj,
74 evas_obj_size_get(&w, &h),
75 evas_obj_size_set(w+10, h+20));
76 w+10 and h+20 are evaluated before the call to size_get.
77 Instead, separate it in two calls to eo_do.
78
797) When calling eo_add on an object, the reference counter of this one is incremented. You need to call eo_unref to decrement this counter.
80If it is called with a parent, two references are on the object. eo_del removes both of these references.
81When there is no more references on an object, this one is deleted and then can be freed. The deletion calls to the destructor (see eo_destructor). When done, Eo checks if the object can be freed. The free mechanism can be "disabled" through eo_manual_free_set. It is the responsibility of the developer to call eo_manual_free on the object to free it. This mechanism has been used for example in Evas on the Evas objects and in Ecore
82
838) When eo_do reaches a function of a class, it is the responsibility of the user to extract from the va_list ALL the parameters needed for this function, NO MORE, NO LESS. Otherwise, unknown behavior can occur. eo_do is called with a list of op id, params, op id, params... A bad extraction of parameters can bring to the parsing of a wrong op id and so in the best case, to an error, in the worst case, to another function not in relation with the actual use case.
84
859) Always pay attention to:
86 - the pairing between function id and the function itself. Using the same function for two ids occurs and is hard to debug.
87 - the definition of the function macros in the H file and their parameters order/type
88 - to extract all the parameters of a function from the va_list
89
9010) Enum of the op ids in H file and descriptions in C file must be synchronized, i.e same number of ids and same order. If you change the order by adding, removing or moving ids, you break ABI of your library.
91
9211) Avoid exposing your class data to prevent ABI break. Supply access functions instead.
93
94* How to create a class - H side?
95--------------------------------
96- If the object is new, establish the public APIs
97- #define $(CLASS_NAME) $(class_name)_class_get(): will be used to access data/inherit from this class...
98- const Eo_Class *$(class_name)_class_get(void) EINA_CONST: declaration of the function that will create the class (not the instance), i.e virtual table...
99- extern EAPI Eo_Op $(CLASS_NAME)_BASE_ID: class id that will be essentially used to identify functions set of this class
100- enum of the function ids of the class in the form $(CLASS_NAME)_SUB_ID: used to identify the function inside the class; function id is unique per class but (class id, function id) is unique per system..
101- #define $(CLASS_NAME)_ID(sub_id) ($(CLASS_NAME)_BASE_ID + sub_id): formula to calculate the system function id
102- define of each function consists of:
103 - the name of the function that will be used in eo_do
104 - parameters without types
105 - $(CLASS_NAME)_ID($(CLASS_NAME)_SUB_ID_FUNCTION
106 - EO_TYPECHECK for each parameter: type and variable name
107- And don't forget to document each function
108
109Evas Object Line Example:
110------------------------
111#define EVAS_OBJ_LINE_CLASS evas_object_line_class_get()
112const Eo_Class *evas_object_line_class_get(void) EINA_CONST;
113
114extern EAPI Eo_Op EVAS_OBJ_LINE_BASE_ID;
115
116enum
117{
118 EVAS_OBJ_LINE_SUB_ID_XY_SET,
119 EVAS_OBJ_LINE_SUB_ID_XY_GET,
120 EVAS_OBJ_LINE_SUB_ID_LAST
121}
122
123#define EVAS_OBJ_LINE_ID(sub_id) (EVAS_OBJ_LINE_BASE_ID + sub_id)
124
125/**
126 * @def evas_obj_line_xy_set
127 * @since 1.8
128 *
129 * Sets the coordinates of the end points of the given evas line object.
130 *
131 * @param[in] x1
132 * @param[in] y1
133 * @param[in] x2
134 * @param[in] y2
135 *
136 * @see evas_object_line_xy_set
137 */
138#define evas_obj_line_xy_set(x1, y1, x2, y2) EVAS_OBJ_LINE_ID(EVAS_OBJ_LINE_SUB_ID_XY_SET), EO_TYPECHECK(Evas_Coord, x1), EO_TYPECHECK(Evas_Coord, y1), EO_TYPECHECK(Evas_Coord, x2), EO_TYPECHECK(Evas_Coord, y2)
139
140/**
141 * @def evas_obj_line_xy_get
142 * @since 1.8
143 *
144 * Retrieves the coordinates of the end points of the given evas line object.
145 *
146 * @param[out] x1
147 * @param[out] y1
148 * @param[out] x2
149 * @param[out] y2
150 *
151 * @see evas_object_line_xy_get
152 */
153#define evas_obj_line_xy_get(x1, y1, x2, y2) EVAS_OBJ_LINE_ID(EVAS_OBJ_LINE_SUB_ID_XY_GET), EO_TYPECHECK(Evas_Coord *, x1), EO_TYPECHECK(Evas_Coord *, y1), EO_TYPECHECK(Evas_Coord *, x2), EO_TYPECHECK(Evas_Coord *, y2)
154
155* How to create a class - C side?
156--------------------------------
157Below, the object line as example.
158
159#include "Eo.h"
160EAPI Eo_Op $(CLASS_NAME)_BASE_ID = EO_NOOP; // Initialisation of the class id to 0. Will be set dynamically by Eo itself.
161#define MY_CLASS $(CLASS_NAME)
162
163...
164
165Example for a developer function called by Eo:
166This function receives the Eo object, the data corresponding to this class and the list of parameters.
167static void
168_foo(Eo *eo_obj, void *_pd, va_list *list)
169{
170 int param_1 = va_arg(*list, int);
171 Eina_Bool param_2 = va_arg(*list, int);
172 Eina_Bool ret = va_arg(*list, Eina_Bool *);
173 foo_data obj = _pd;
174
175 if (ret) *ret = EINA_FALSE;
176 ...
177}
178
179You can (not a must) implement a constructor. This constructor MUST call the parent constructor (eo_do_super). It is the same for the destructor.
180See eo_constructor and eo_destructor.
181If you don't have anything to do in constructor (like malloc, variables init...) or in destructor (free), don't implement them.
182
183At the end of the file, you need to describe the class.
184First, you need to supply the class constructor that sets the list of the functions that are implemented in this class. It includes functions overriden (constructor, destructor, member_add...) and functions specific to this class. If there is no function implemented in this class, you don't need a class constructor.
185Then, you need a list to describe the new functions. For each one, the op id and a description.
186Then, the class itself that consists in:
187 - the version of Eo, used for backward compatibility
188 - the class name
189 - the type of the class:
190 - regular: an object can be created from this type (mostly used)
191 - regular non instant-iable: Same as previous, but objects can’t be created from this type (elm_widget)
192 - interface: used to extend classes, no implementations for functions and no internal data
193 - mixin : interfaces with internal data and pre-made implementations for functions
194 - the descriptions list described above and the number of ops for this class
195 - events
196 - size of the data of the class
197 - the class constructor, if exists, else NULL
198 - the class destructor (NULL most of the time)
199
200Finally, we define the class with:
201 - the function name to give to the function that creates/returns the class
202 - the class description explained above
203 - the parent and the interfaces/mixins, finished by NULL
204
205
206Evas Object Line Example:
207------------------------
208#include "Eo.h"
209EAPI Eo_Op EVAS_OBJ_LINE_BASE_ID = EO_NOOP;
210#define MY_CLASS EVAS_OBJ_LINE_CLASS
211
212...
213
214static void
215_line_xy_get(Eo *eo_obj, void *_pd, va_list *list)
216{
217 const Evas_Object_Line *o = _pd;
218
219 Evas_Coord *x1 = va_arg(*list, Evas_Coord *);
220 Evas_Coord *y1 = va_arg(*list, Evas_Coord *);
221 Evas_Coord *x2 = va_arg(*list, Evas_Coord *);
222 Evas_Coord *y2 = va_arg(*list, Evas_Coord *);
223
224 Evas_Object_Protected_Data *obj = eo_data_get(eo_obj, EVAS_OBJ_CLASS);
225 if (x1) *x1 = obj->cur.geometry.x + o->cur.x1;
226 if (y1) *y1 = obj->cur.geometry.y + o->cur.y1;
227 if (x2) *x2 = obj->cur.geometry.x + o->cur.x2;
228 if (y2) *y2 = obj->cur.geometry.y + o->cur.y2;
229}
230
231static void
232_constructor(Eo *eo_obj, void *class_data, va_list *list EINA_UNUSED)
233{
234 eo_do_super(eo_obj, eo_constructor());
235
236 Evas_Object_Protected_Data *obj = eo_data_get(eo_obj, EVAS_OBJ_CLASS);
237 evas_object_line_init(eo_obj);
238}
239
240...
241
242/* class constructor */
243static void
244_class_constructor(Eo_Class *klass)
245{
246 const Eo_Op_Func_Description func_desc[] = {
247 /* Virtual functions of parent class implemented in this class */
248 EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_CONSTRUCTOR), _constructor),
249 EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_DESTRUCTOR), _destructor),
250 /* Specific functions to this class */
251 EO_OP_FUNC(EVAS_OBJ_LINE_ID(EVAS_OBJ_LINE_SUB_ID_XY_SET), _line_xy_set),
252 EO_OP_FUNC(EVAS_OBJ_LINE_ID(EVAS_OBJ_LINE_SUB_ID_XY_GET), _line_xy_get),
253 EO_OP_FUNC_SENTINEL
254 };
255
256 eo_class_funcs_set(klass, func_desc);
257}
258
259/* Descriptions for the functions specific to this class */
260static const Eo_Op_Description op_desc[] = {
261 EO_OP_DESCRIPTION(EVAS_OBJ_LINE_SUB_ID_XY_SET, "Sets the coordinates of the end points of the given evas line object."),
262 EO_OP_DESCRIPTION(EVAS_OBJ_LINE_SUB_ID_XY_GET, "Retrieves the coordinates of the end points of the given evas line object."),
263 EO_OP_DESCRIPTION_SENTINEL
264};
265
266/* Description of the class */
267static const Eo_Class_Description class_desc = {
268 EO_VERSION,
269 "Evas_Object_Line",
270 EO_CLASS_TYPE_REGULAR,
271 EO_CLASS_DESCRIPTION_OPS(&EVAS_OBJ_LINE_BASE_ID, op_desc, EVAS_OBJ_LINE_SUB_ID_LAST),
272 NULL,
273 sizeof(Evas_Object_Line),
274 _class_constructor,
275 NULL
276};
277
278/* Definition of the class */
279EO_DEFINE_CLASS(evas_object_line_class_get, &class_desc, EVAS_OBJ_CLASS, NULL);
280