

Whenever using Eina we must include it\+: 
\begin{DoxyCodeInclude}
\textcolor{preprocessor}{#include <\hyperlink{Eina_8h}{Eina.h}>}

\end{DoxyCodeInclude}


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 E\+I\+N\+A\+\_\+\+M\+A\+G\+IC to our classes\+: 
\begin{DoxyCodeInclude}

\textcolor{preprocessor}{#define BASETYPE\_MAGIC 0x12345}
\textcolor{keyword}{struct }\_person \{
   \hyperlink{group__Eina__Magic__Group_ga4085afe8dc73c98ae626cc952c69c668}{EINA\_MAGIC};
   \textcolor{keywordtype}{char} *name;
\};
\textcolor{keyword}{typedef} \textcolor{keyword}{struct }\_person person;

\textcolor{preprocessor}{#define SUBTYPE\_MAGIC 0x3333}
\textcolor{keyword}{struct }\_pilot \{
   person base;
   \hyperlink{group__Eina__Magic__Group_ga4085afe8dc73c98ae626cc952c69c668}{EINA\_MAGIC};
   \textcolor{keywordtype}{char} *callsign;
\};
\textcolor{keyword}{typedef} \textcolor{keyword}{struct }\_pilot pilot;

\end{DoxyCodeInclude}
 \begin{DoxyNote}{Note}
The values of B\+A\+S\+E\+T\+Y\+P\+E\+\_\+\+M\+A\+G\+IC and S\+U\+B\+T\+Y\+P\+E\+\_\+\+M\+A\+G\+IC have no meaning, the only important thing about them is that they are unique.
\end{DoxyNote}
Here we have a function to create a person given a name, nothing too fancy\+: 
\begin{DoxyCodeInclude}

person *
person\_new(\textcolor{keyword}{const} \textcolor{keywordtype}{char} *name)
\{
   person *ptr = malloc(\textcolor{keyword}{sizeof}(person));
   \hyperlink{group__Eina__Magic__Group_ga470f5ce28eaaf59ce80523be8471c5e0}{EINA\_MAGIC\_SET}(ptr, BASETYPE\_MAGIC);
   ptr->name = strdup(name);

   \textcolor{keywordflow}{return} ptr;
\}

\end{DoxyCodeInclude}


And now the counterpart, a function to free a person. 
\begin{DoxyCodeInclude}

\textcolor{keywordtype}{void}
person\_free(person *ptr) \{

\end{DoxyCodeInclude}
 Before we start releasing resources we check that the pointer we are given actually points to a person, and if not we print an error message and quit\+: 
\begin{DoxyCodeInclude}
   \textcolor{keywordflow}{if} (!\hyperlink{group__Eina__Magic__Group_ga13317919a91b35412b6f40ca1166b373}{EINA\_MAGIC\_CHECK}(ptr, BASETYPE\_MAGIC))
   \{
      \hyperlink{group__Eina__Magic__Group_gad7411adf9ff7121ad2004046f1b08408}{EINA\_MAGIC\_FAIL}(ptr, BASETYPE\_MAGIC);
      \textcolor{keywordflow}{return};
   \}

\end{DoxyCodeInclude}
 \begin{DoxyNote}{Note}
E\+I\+N\+A\+\_\+\+M\+A\+G\+I\+C\+\_\+\+F\+A\+IL is a macro that makes it easy to print an appropriate (and consistent) error message. Now knowing that ptr is indeed of type person we proceed to set E\+I\+N\+A\+\_\+\+M\+A\+G\+IC to E\+I\+N\+A\+\_\+\+M\+A\+G\+I\+C\+\_\+\+N\+O\+NE and free the allocated memory\+: 
\begin{DoxyCodeInclude}
   \hyperlink{group__Eina__Magic__Group_ga470f5ce28eaaf59ce80523be8471c5e0}{EINA\_MAGIC\_SET}(ptr, \hyperlink{group__Eina__Magic__Group_ga087a0d5a1e482da550b33fe4fcb0435f}{EINA\_MAGIC\_NONE});
   free(ptr->name);
   free(ptr);
\}

\end{DoxyCodeInclude}
 

Setting E\+I\+N\+A\+\_\+\+M\+A\+G\+IC to E\+I\+N\+A\+\_\+\+M\+A\+G\+I\+C\+\_\+\+N\+O\+NE is important to prevent the struct from being used after it is freed.
\end{DoxyNote}
Now we have our function to create a pilot, this one is a little more complex because we need to set E\+I\+N\+A\+\_\+\+M\+A\+G\+IC for the pilot and pilot-\/$>$base, this is very important so that checking the E\+I\+N\+A\+\_\+\+M\+A\+G\+IC of (person$\ast$)my\+\_\+pilot works\+: 
\begin{DoxyCodeInclude}

pilot *
pilot\_new(\textcolor{keyword}{const} \textcolor{keywordtype}{char} *name, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *callsign)
\{
   pilot *ptr = malloc(\textcolor{keyword}{sizeof}(pilot));
   \hyperlink{group__Eina__Magic__Group_ga470f5ce28eaaf59ce80523be8471c5e0}{EINA\_MAGIC\_SET}(ptr, SUBTYPE\_MAGIC);
   \hyperlink{group__Eina__Magic__Group_ga470f5ce28eaaf59ce80523be8471c5e0}{EINA\_MAGIC\_SET}(&ptr->base, BASETYPE\_MAGIC);
   ptr->base.name = strdup(name);
   ptr->callsign = strdup(callsign);

   \textcolor{keywordflow}{return} ptr;
\}

\end{DoxyCodeInclude}


The function to free a pilot is not too different from the one that frees a person\+: 
\begin{DoxyCodeInclude}

\textcolor{keywordtype}{void}
pilot\_free(pilot *ptr) \{
   \textcolor{keywordflow}{if} (!\hyperlink{group__Eina__Magic__Group_ga13317919a91b35412b6f40ca1166b373}{EINA\_MAGIC\_CHECK}(ptr, SUBTYPE\_MAGIC))
   \{
      \hyperlink{group__Eina__Magic__Group_gad7411adf9ff7121ad2004046f1b08408}{EINA\_MAGIC\_FAIL}(ptr, SUBTYPE\_MAGIC);
      \textcolor{keywordflow}{return};
   \}
   \hyperlink{group__Eina__Magic__Group_ga470f5ce28eaaf59ce80523be8471c5e0}{EINA\_MAGIC\_SET}(ptr, \hyperlink{group__Eina__Magic__Group_ga087a0d5a1e482da550b33fe4fcb0435f}{EINA\_MAGIC\_NONE});
   \hyperlink{group__Eina__Magic__Group_ga470f5ce28eaaf59ce80523be8471c5e0}{EINA\_MAGIC\_SET}(&ptr->base, \hyperlink{group__Eina__Magic__Group_ga087a0d5a1e482da550b33fe4fcb0435f}{EINA\_MAGIC\_NONE});
   free(ptr->base.name);
   free(ptr->callsign);
   free(ptr);
\}

\end{DoxyCodeInclude}


We also create functions to print a person or a pilot that check the type of the pointers they receive\+: 
\begin{DoxyCodeInclude}

\textcolor{keywordtype}{void}
print\_person(person *ptr)
\{
   \textcolor{keywordflow}{if} (!\hyperlink{group__Eina__Magic__Group_ga13317919a91b35412b6f40ca1166b373}{EINA\_MAGIC\_CHECK}(ptr, BASETYPE\_MAGIC))\{
      \hyperlink{group__Eina__Magic__Group_gad7411adf9ff7121ad2004046f1b08408}{EINA\_MAGIC\_FAIL}(ptr, BASETYPE\_MAGIC);
      \textcolor{keywordflow}{return};
   \}
   printf(\textcolor{stringliteral}{"name: %s\(\backslash\)n"}, ptr->name);
\}

\end{DoxyCodeInclude}


And for our main function where we declare some variables and initialize Eina\+: 
\begin{DoxyCodeInclude}

\textcolor{keywordtype}{void}
print\_pilot(pilot *ptr)
\{
   \textcolor{keywordflow}{if} (!\hyperlink{group__Eina__Magic__Group_ga13317919a91b35412b6f40ca1166b373}{EINA\_MAGIC\_CHECK}(ptr, SUBTYPE\_MAGIC)) \{
      \hyperlink{group__Eina__Magic__Group_gad7411adf9ff7121ad2004046f1b08408}{EINA\_MAGIC\_FAIL}(ptr, SUBTYPE\_MAGIC);
      \textcolor{keywordflow}{return};
   \}
   print\_person(&ptr->base);
   printf(\textcolor{stringliteral}{"callsign: %s\(\backslash\)n"}, ptr->callsign);
\}

\textcolor{keywordtype}{int}
main(\textcolor{keywordtype}{int} argc EINA\_UNUSED, \textcolor{keywordtype}{char} **argv EINA\_UNUSED)
\{
   person *base;
   pilot *sub;

   \hyperlink{group__Eina__Main__Group_gabf448ada5bc81a07342b66c6f818ee5e}{eina\_init}();

\end{DoxyCodeInclude}


For Eina to be able to provide more informative error messages we are going to give names to our E\+I\+N\+A\+\_\+\+M\+A\+G\+IC types\+: 
\begin{DoxyCodeInclude}
   \hyperlink{group__Eina__Magic__Group_ga0d77e61edf5b7b6c276a382d6bf191e7}{eina\_magic\_string\_set}(BASETYPE\_MAGIC, \textcolor{stringliteral}{"person"});

\end{DoxyCodeInclude}


Since our types won\textquotesingle{}t live longer than the scope of the current function we can set the name without eina making a copy of the string\+: 
\begin{DoxyCodeInclude}
   \hyperlink{group__Eina__Magic__Group_ga5d202c69f78f9cb078efd3e798cb5a53}{eina\_magic\_string\_static\_set}(SUBTYPE\_MAGIC, \textcolor{stringliteral}{"pilot"});

\end{DoxyCodeInclude}


Now we create a person, a pilot, and print both as persons\+: 
\begin{DoxyCodeInclude}

   base = person\_new(\textcolor{stringliteral}{"Tyrol"});
   sub = pilot\_new(\textcolor{stringliteral}{"thrace"}, \textcolor{stringliteral}{"starbuck"});

   print\_person(base);
   print\_person((person *)sub);

\end{DoxyCodeInclude}


Now we try to print both as pilots, which obviously does not work since base is not a pilot\+: 
\begin{DoxyCodeInclude}

   print\_pilot(base); \textcolor{comment}{//BAD}
   print\_pilot(sub);

\end{DoxyCodeInclude}


That\textquotesingle{}s all folks\+: 
\begin{DoxyCodeInclude}

   \hyperlink{group__Eina__Main__Group_ga6741fcaba116e31e96ce14dcfa03f568}{eina\_shutdown}();
\}

\end{DoxyCodeInclude}


See full source \hyperlink{eina_magic_example_01_c}{here}. 