summaryrefslogtreecommitdiff
path: root/pages/develop/tutorials/c/eo-classes.md.txt
blob: 97cbdd796cae340c0d1bfe3d3d9c268717a42578 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
---
~~Title: Creating New Classes with Eolian~~
---

# Creating New Classes with Eolian #

The [Introduction to Eo](eo-intro.md) tutorial showed you how to instantiate Eo objects of a given class using ``efl_new()``. This tutorial demonstrates how to create new classes so new kinds of objects can be instantiated.

You'll learn how to describe classes using the Eolian language and then further customize them with class-specific code. You will also master the basics of class inheritance with Eolian.

## Prerequisites ##

* The [Introduction to Eo](eo-intro.md) tutorial explains the basis of Eo object creation and reference counting.
* The [Hello World](hello-world.md) tutorial details how to write a simple application using EFL.

## Eolian Files and eolian_gen ##

Each class in EFL is described in an *Eolian file*, with the same name as the class and extension *.eo*. Eolian files are plain text files that contain, among other things, the name of the described class and the list of its properties and methods, along with their parameters and return values.

Most importantly, this class description is independent of any programming language and can be used to generate automatic bindings and boiler plate code for other programming languages. With this automatically generated code you only need to write the details of your implementation in the language of your choice.

The code generator for the C language is ``eolian_gen``, which is included in your EFL installation. So far, it is the only language available but there are plans to support more in the near future.

You can invoke ``eolian_gen`` like so:

```bash
eolian_gen -gchi my_new_class.eo
```

This generates three files:

* ``my_new_class.eo.h``: **Header file** including all the method signatures related to your class. In particular, it contains the class symbol you need to pass to ``efl_new()``, so this is the header file you need to include if you want to use your class.
* ``my_new_class.eo.c``: Boilerplate code you don't usually need to worry about. It is automatically included from the implementation file (next one).
* ``my_new_class.c``: The **implementation file**. It initially contains the empty bodies for all the methods you need to implement in your class. This is the only file you need to modify and include in your builds, as you will see in this tutorial.

The ``-gchi`` parameter tells ``eolian_gen`` to generate the Header file, the Boilerplate file and the Implementation file.

In summary, for each new class you create you must:

1. Describe the class with an ``.eo`` file and call ``eolian_gen``.
2. Add your code to the implementation file (``my_new_class.c``).
3. Add the implementation file to your build.
4. Include the header file (``my_new_class.eo.h``) from your application to use the new class.

The rest of the tutorial shows a practical example which will illustrate this procedure.

> **NOTE:**
> Eolian files can also contain **documentation** which will be transferred to the generated headers files in [Doxygen](http://doxygen.org) format. For an in-depth description of the Eolian documentation process, see the [Eolian Documentation Guide](/contrib/docs/eo-guide.md). In summary, documentation is enclosed in double square brackets ``[[ ... ]]`` but it will not be used in these tutorials.

## Step One: Creating a Simple Class Description ##

You will now create an Eolian file for a class named ``Example.Rectangle``. The file **must** be called ``example_rectangle.eo``.

This class will represent a rectangle shape, so it will have two properties, the ``width`` and ``height`` of the rectangle, which can be both read and written.

Start with the class name and the name of its parent class using the ``extends`` keyword:

```
class Example.Rectangle extends Efl.Object {
}
```

The parent class is ``Efl.Object``, which is mandatory for regular classes (exceptions like *Interfaces* will be dealt with later).

Next add a method declaration block, wherein you'll list the methods and properties of your class:

```
class Example.Rectangle extends Efl.Object {
   methods {
   }
}
```

Now add a property named ``width`` inside the ``methods`` block, which you can ``set``, ``get`` and whose only value is an ``int``:

```
      @property width {
         set {
         }
         get {
         }
         values {
            width: int;
         }
      }
```

Next do the same for the ``height`` property, right after the ``width`` block:

```
      @property height {
         set {
         }
         get {
         }
         values {
            height: int;
         }
      }
```

Finally add a method to calculate the ``area`` of the rectangle after the ``height`` property:

```
      area {
         params {
         }
         return: int;
      }
```

This method will take no parameters and return an integer.

The Eolian description for the ``Example.Rectangle`` class is now complete. The ``example_rectangle.eo`` file should look like this:

```
class Example.Rectangle extends Efl.Object {
   methods {
      @property width {
         set {
         }
         get {
         }
         values {
            width: int;
         }
      }
      @property height {
         set {
         }
         get {
         }
         values {
            height: int;
         }
      }
      area {
         params {
         }
         return: int;
      }
   }
}
```

Now turn it into C code with ``eolian_gen``:

```bash
eolian_gen -gchi example_rectangle.eo
```

This will create three files in your current folder: ``example_rectangle.c``, ``example_rectangle.eo.c`` and ``example_rectangle.eo.h``.

## Step Two: Adding the Implementation ##

As you previously found out, ``eolian_gen`` has generated the boilerplate code for you in the implementation file ``example_rectangle.c``. If you open it you will see an empty structure at the top:

* ``typedef struct {} Example_Rectangle_Data;``

This structure will hold the private data for your class. You can place anything you want here. The usual approach is to put the backing storage for your properties, in this case ``width`` and ``height``.

You will also find the following methods with empty bodies in the generated file:

* ``_example_rectangle_width_set()``
* ``_example_rectangle_width_get()``
* ``_example_rectangle_height_set()``
* ``_example_rectangle_height_get()``
* ``_example_rectangle_area()``

These are the setters and getters for your properties and method. Examine one of the getters closely:

```c
EOLIAN static int
_example_rectangle_width_get(const Eo *obj, Example_Rectangle_Data *pd)
```

This getter receives the ``Eo *`` object whose property is being retrieved and an ``Example_Rectangle_Data *`` pointer to its private data. It returns an integer, as that's what you specified as the value for this property in the Eolian file.

Everything is now in place to start coding. To get started, add some class variables to the ``Example_Rectangle_Data`` structure. This stores the actual width and height of the rectangle:

```c
typedef struct
{
   int width, height;
} Example_Rectangle_Data;
```

Now fill-in the setters and getters for ``width`` and ``height``. These simply provide access to the private variables so the code is very simple. Also none of the ``Eo *obj`` is going to be used, so mark them with ``EINA_UNUSED`` to avoid compiler warnings:

```c
EOLIAN static void
_example_rectangle_width_set(Eo *obj EINA_UNUSED, Example_Rectangle_Data *pd, int width)
{
   pd->width = width;
}

EOLIAN static int
_example_rectangle_width_get(const Eo *obj EINA_UNUSED, Example_Rectangle_Data *pd)
{
   return pd->width;
}

EOLIAN static void
_example_rectangle_height_set(Eo *obj EINA_UNUSED, Example_Rectangle_Data *pd, int height)
{
   pd->height = height;
}

EOLIAN static int
_example_rectangle_height_get(const Eo *obj EINA_UNUSED, Example_Rectangle_Data *pd)
{
   return pd->height;
}
```

Finally, calculate the area of the rectangle in the ``_example_rectangle_area()`` method:

```c
EOLIAN static int
_example_rectangle_area(Eo *obj EINA_UNUSED, Example_Rectangle_Data *pd)
{
   return pd->width * pd->height;
}
```

Your class is now complete, but you can't test it until you write some code which uses it. 

## Step Three: Using the New Class ##

Create a new file (for example ``eo_classes_main.c``) and add the basic EFL application from the [Hello World tutorial](hello-world.md):

```c
#define EFL_EO_API_SUPPORT 1
#define EFL_BETA_API_SUPPORT 1

#include <Eina.h>
#include <Efl_Core.h>

EAPI_MAIN void
efl_main(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED)
{
   efl_exit(0);
}
EFL_MAIN()
```

The first step is to include the auto-generated header file for your new class, so that it is available to the application. Add this after the other includes:

```c
#include "example_rectangle.eo.h"
```

Next add an empty method that will instantiate the class:

```c
Example_Rectangle *
_rect_create()
{
}
```

Now call this method from ``efl_main()``, right before calling ``efl_exit()``:

```c
   Eo *rectangle;

   rectangle = _rect_create();
   
   efl_unref(rectangle);
```

Notice how the object is discarded using ``efl_unref()`` before quitting. It is a good idea to write the code to remove objects at the same time you write the code that creates them, so you don't forget. You will be doing more with this ``rectangle`` later.

Finally, instantiate a new object of your shiny new class from within ``_rect_create()``:

```c
   Example_Rectangle *rectangle;

   rectangle = efl_new(EXAMPLE_RECTANGLE_CLASS,
                       efl_name_set(efl_added, "Rectangle"),
                       example_rectangle_width_set(efl_added, 5),
                       example_rectangle_height_set(efl_added, 10));

   return rectangle;
```

There are several items of interest inside the call to ``efl_add()``:

* The symbol you need to use for your class is ``EXAMPLE_RECTANGLE_CLASS``. It is defined in ``example_rectangle.eo.h``. Its name comes from the class name you defined in your Eolian file.
* The new object is configured with the two setters you defined for your class and as such starts with a ``width`` of 5 and a ``height`` of 10.
* The new object is stored in an ``Example_Rectangle *`` pointer so the kind of object it points to is clear. A generic ``Eo *`` would work too.

In theory you could now compile and run your program. The new object would be instantiated however nothing would show on screen. You can solve this issue by adding a simple print statement to prove everything is working as expected. 

In ``efl_main()``, right after calling ``_rect_create()`` add:

```c
   printf("Rectangle is %dx%d, area is %d\n",
          example_rectangle_width_get(rectangle),
          example_rectangle_height_get(rectangle),
          example_rectangle_area(rectangle));
```

Your ``eo_classes_main.c`` file should now look something like this:

```c
#define EFL_EO_API_SUPPORT 1
#define EFL_BETA_API_SUPPORT 1

#include <Eina.h>
#include <Efl_Core.h>
#include "example_rectangle.eo.h"

Example_Rectangle *
_rect_create()
{
   Example_Rectangle *rectangle;

   rectangle = efl_new(EXAMPLE_RECTANGLE_CLASS,
                       efl_name_set(efl_added, "Rectangle"),
                       example_rectangle_width_set(efl_added, 5),
                       example_rectangle_height_set(efl_added, 10));

   return rectangle;
}

EAPI_MAIN void
efl_main(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED)
{
   Eo *rectangle;

   rectangle = _rect_create();

   printf("Rectangle is %dx%d, area is %d\n",
          example_rectangle_width_get(rectangle),
          example_rectangle_height_get(rectangle),
          example_rectangle_area(rectangle));
          
   efl_unref(rectangle);

   efl_exit(0);
}
EFL_MAIN()
```

You can now build you program as usual. Remember to include the implementation file for your new class in the build:

```bash
gcc -o eo-classes eo_classes_main.c example_rectangle.c `pkg-config --cflags --libs eina efl elementary`
```

When run your program should print the following via terminal:

```
Rectangle is 5x10, area is 50
```

## Summary ##

After going through this tutorial you've learned:

* New classes are described in **Eolian files** which have the same name as your class with a ``.eo`` extension.
* Eolian files are turned into **boilerplate C code** using ``eolian_gen``.
* The code for your class is added to the **implementation file** (``name_of_your_class.c``).
* Code using your class must **include the class header** (``name_of_your_class.eo.h``).
* Applications using your class have to **build the implementation file**.

In the following tutorials you'll discover how to inherit from other classes and define interfaces.

## Further Reading ##
[Introduction to Eo](eo-intro.md)
:   How to instantiate Eo objects.

[Eolian File Format Specification](/contrib/docs/eo.md)
:    In-depth description of the Eolian file format.