

Whenever using Eina we must include it\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <\mbox{\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 EINA\+\_\+\+MAGIC to our classes\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#define\ BASETYPE\_MAGIC\ 0x12345}}
\DoxyCodeLine{\textcolor{keyword}{struct\ }\_person\ \{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Magic__Group_ga4085afe8dc73c98ae626cc952c69c668}{EINA\_MAGIC}};}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{char}\ *name;}
\DoxyCodeLine{\};}
\DoxyCodeLine{\textcolor{keyword}{typedef}\ \textcolor{keyword}{struct\ }\_person\ person;}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#define\ SUBTYPE\_MAGIC\ 0x3333}}
\DoxyCodeLine{\textcolor{keyword}{struct\ }\_pilot\ \{}
\DoxyCodeLine{\ \ \ person\ base;}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Magic__Group_ga4085afe8dc73c98ae626cc952c69c668}{EINA\_MAGIC}};}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{char}\ *callsign;}
\DoxyCodeLine{\};}
\DoxyCodeLine{\textcolor{keyword}{typedef}\ \textcolor{keyword}{struct\ }\_pilot\ pilot;}

\end{DoxyCodeInclude}
\begin{DoxyNote}{Note}
The values of BASETYPE\+\_\+\+MAGIC and SUBTYPE\+\_\+\+MAGIC 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}{0}
\DoxyCodeLine{}
\DoxyCodeLine{person\ *}
\DoxyCodeLine{person\_new(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *name)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ person\ *ptr\ =\ malloc(\textcolor{keyword}{sizeof}(person));}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Magic__Group_ga470f5ce28eaaf59ce80523be8471c5e0}{EINA\_MAGIC\_SET}}(ptr,\ BASETYPE\_MAGIC);}
\DoxyCodeLine{\ \ \ ptr-\/>name\ =\ strdup(name);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ ptr;}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 And now the counterpart, a function to free a person. 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{void}}
\DoxyCodeLine{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}{0}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!\mbox{\hyperlink{group__Eina__Magic__Group_ga13317919a91b35412b6f40ca1166b373}{EINA\_MAGIC\_CHECK}}(ptr,\ BASETYPE\_MAGIC))}
\DoxyCodeLine{\ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \mbox{\hyperlink{group__Eina__Magic__Group_gad7411adf9ff7121ad2004046f1b08408}{EINA\_MAGIC\_FAIL}}(ptr,\ BASETYPE\_MAGIC);}
\DoxyCodeLine{\ \ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{\ \ \ \}}

\end{DoxyCodeInclude}
\begin{DoxyNote}{Note}
EINA\+\_\+\+MAGIC\+\_\+\+FAIL 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 EINA\+\_\+\+MAGIC to EINA\+\_\+\+MAGIC\+\_\+\+NONE and free the allocated memory\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Magic__Group_ga470f5ce28eaaf59ce80523be8471c5e0}{EINA\_MAGIC\_SET}}(ptr,\ \mbox{\hyperlink{group__Eina__Magic__Group_ga087a0d5a1e482da550b33fe4fcb0435f}{EINA\_MAGIC\_NONE}});}
\DoxyCodeLine{\ \ \ free(ptr-\/>name);}
\DoxyCodeLine{\ \ \ free(ptr);}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}


Setting EINA\+\_\+\+MAGIC to EINA\+\_\+\+MAGIC\+\_\+\+NONE 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 EINA\+\_\+\+MAGIC for the pilot and pilot-\/\texorpdfstring{$>$}{>}base, this is very important so that checking the EINA\+\_\+\+MAGIC of (person$\ast$)my\+\_\+pilot works\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{pilot\ *}
\DoxyCodeLine{pilot\_new(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *name,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *callsign)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ pilot\ *ptr\ =\ malloc(\textcolor{keyword}{sizeof}(pilot));}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Magic__Group_ga470f5ce28eaaf59ce80523be8471c5e0}{EINA\_MAGIC\_SET}}(ptr,\ SUBTYPE\_MAGIC);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Magic__Group_ga470f5ce28eaaf59ce80523be8471c5e0}{EINA\_MAGIC\_SET}}(\&ptr-\/>base,\ BASETYPE\_MAGIC);}
\DoxyCodeLine{\ \ \ ptr-\/>base.name\ =\ strdup(name);}
\DoxyCodeLine{\ \ \ ptr-\/>callsign\ =\ strdup(callsign);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ ptr;}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 The function to free a pilot is not too different from the one that frees a person\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{void}}
\DoxyCodeLine{pilot\_free(pilot\ *ptr)\ \{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!\mbox{\hyperlink{group__Eina__Magic__Group_ga13317919a91b35412b6f40ca1166b373}{EINA\_MAGIC\_CHECK}}(ptr,\ SUBTYPE\_MAGIC))}
\DoxyCodeLine{\ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \mbox{\hyperlink{group__Eina__Magic__Group_gad7411adf9ff7121ad2004046f1b08408}{EINA\_MAGIC\_FAIL}}(ptr,\ SUBTYPE\_MAGIC);}
\DoxyCodeLine{\ \ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{\ \ \ \}}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Magic__Group_ga470f5ce28eaaf59ce80523be8471c5e0}{EINA\_MAGIC\_SET}}(ptr,\ \mbox{\hyperlink{group__Eina__Magic__Group_ga087a0d5a1e482da550b33fe4fcb0435f}{EINA\_MAGIC\_NONE}});}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Magic__Group_ga470f5ce28eaaf59ce80523be8471c5e0}{EINA\_MAGIC\_SET}}(\&ptr-\/>base,\ \mbox{\hyperlink{group__Eina__Magic__Group_ga087a0d5a1e482da550b33fe4fcb0435f}{EINA\_MAGIC\_NONE}});}
\DoxyCodeLine{\ \ \ free(ptr-\/>base.name);}
\DoxyCodeLine{\ \ \ free(ptr-\/>callsign);}
\DoxyCodeLine{\ \ \ free(ptr);}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 We also create functions to print a person or a pilot that check the type of the pointers they receive\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{void}}
\DoxyCodeLine{print\_person(person\ *ptr)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!\mbox{\hyperlink{group__Eina__Magic__Group_ga13317919a91b35412b6f40ca1166b373}{EINA\_MAGIC\_CHECK}}(ptr,\ BASETYPE\_MAGIC))\{}
\DoxyCodeLine{\ \ \ \ \ \ \mbox{\hyperlink{group__Eina__Magic__Group_gad7411adf9ff7121ad2004046f1b08408}{EINA\_MAGIC\_FAIL}}(ptr,\ BASETYPE\_MAGIC);}
\DoxyCodeLine{\ \ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{\ \ \ \}}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}name:\ \%s\(\backslash\)n"{}},\ ptr-\/>name);}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 And for our main function where we declare some variables and initialize Eina\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{void}}
\DoxyCodeLine{print\_pilot(pilot\ *ptr)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!\mbox{\hyperlink{group__Eina__Magic__Group_ga13317919a91b35412b6f40ca1166b373}{EINA\_MAGIC\_CHECK}}(ptr,\ SUBTYPE\_MAGIC))\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \mbox{\hyperlink{group__Eina__Magic__Group_gad7411adf9ff7121ad2004046f1b08408}{EINA\_MAGIC\_FAIL}}(ptr,\ SUBTYPE\_MAGIC);}
\DoxyCodeLine{\ \ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{\ \ \ \}}
\DoxyCodeLine{\ \ \ print\_person(\&ptr-\/>base);}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}callsign:\ \%s\(\backslash\)n"{}},\ ptr-\/>callsign);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}}
\DoxyCodeLine{main(\textcolor{keywordtype}{int}\ argc\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{char}\ **argv\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ person\ *base;}
\DoxyCodeLine{\ \ \ pilot\ *sub;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Main__Group_ga81b8137665b8a4d2cb38ca50017d4c74}{eina\_init}}();}

\end{DoxyCodeInclude}
 For Eina to be able to provide more informative error messages we are going to give names to our EINA\+\_\+\+MAGIC types\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Magic__Group_ga977e1491bee34a57366e263e7dbab6df}{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}{0}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Magic__Group_ga045b40787a82c640980bf331cecc4205}{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}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ base\ =\ person\_new(\textcolor{stringliteral}{"{}Tyrol"{}});}
\DoxyCodeLine{\ \ \ sub\ =\ pilot\_new(\textcolor{stringliteral}{"{}thrace"{}},\ \textcolor{stringliteral}{"{}starbuck"{}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ print\_person(base);}
\DoxyCodeLine{\ \ \ 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}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ print\_pilot((pilot\ *)base);\ \textcolor{comment}{//BAD:\ fails\ (C\ cast\ prevents\ GCC\ warning)}}
\DoxyCodeLine{\ \ \ print\_pilot(sub);}

\end{DoxyCodeInclude}
 That\textquotesingle{}s all folks\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Main__Group_ga5bf35513199d5220341437c05696285d}{eina\_shutdown}}();}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 See full source \mbox{\hyperlink{eina_magic_example_01_c}{here}}. 