Whenever using Eina we must include it:
For this example we are going to define two classes, person and pilot, and since every pilot is a person we use inheritance. To be type safe we are going to add EINA_MAGIC to our classes:
- Note:
- The values of BASETYPE_MAGIC and SUBTYPE_MAGIC have no meaning, the only important thing about them is that they be unique.
And now the counterpart, a function the free a person.
- Note:
- EINA_MAGIC_FAIL is a macro that make's it easy to print an appropriate (and consistent) error message. Now knowing that ptr is indeed of type person we prooced to set EINA_MAGIC to EINA_MAGIC_NONE and free alocated memory:
Setting EINA_MAGIC to EINA_MAGIC_NONE is important to prevent the struct from being used after freed.
The function to free a pilot is not too different from the one that frees a person:
We also create functions to print a person or a pilot that check the type of the pointers they receive:
And on to our main function where we declare some variables and initialize Eina:
For Eina to be able to provide more informative error messages we are going to give names to our EINA_MAGIC types:
Since our types won't live longer than the scope of the current function we can set the name without eina making a copy of the string:
Now we create a person, a pilot and print both as persons:
Now we try to print both as pilots, which will obvisouly not work since base is not a pilot:
That's all folks:
See full source here.