\hypertarget{eina_magic_01_8c-example}{}\section{eina\+\_\+magic\+\_\+01.\+c}

\begin{DoxyCodeInclude}
\textcolor{comment}{//Compile with:}
\textcolor{comment}{//gcc -g eina\_magic\_01.c -o eina\_magic\_01 `pkg-config --cflags --libs eina`}

\textcolor{preprocessor}{#include <\mbox{\hyperlink{Eina_8h}{Eina.h}}>}

\textcolor{preprocessor}{#define BASETYPE\_MAGIC 0x12345}
\textcolor{keyword}{struct }\_person \{
   \mbox{\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;
   \mbox{\hyperlink{group__Eina__Magic__Group_ga4085afe8dc73c98ae626cc952c69c668}{EINA\_MAGIC}};
   \textcolor{keywordtype}{char} *callsign;
\};
\textcolor{keyword}{typedef} \textcolor{keyword}{struct }\_pilot pilot;

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

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

\textcolor{keywordtype}{void}
person\_free(person *ptr) \{
   \textcolor{keywordflow}{if} (!\mbox{\hyperlink{group__Eina__Magic__Group_ga13317919a91b35412b6f40ca1166b373}{EINA\_MAGIC\_CHECK}}(ptr, BASETYPE\_MAGIC))
   \{
      \mbox{\hyperlink{group__Eina__Magic__Group_gad7411adf9ff7121ad2004046f1b08408}{EINA\_MAGIC\_FAIL}}(ptr, BASETYPE\_MAGIC);
      \textcolor{keywordflow}{return};
   \}
   \mbox{\hyperlink{group__Eina__Magic__Group_ga470f5ce28eaaf59ce80523be8471c5e0}{EINA\_MAGIC\_SET}}(ptr, \mbox{\hyperlink{group__Eina__Magic__Group_ga087a0d5a1e482da550b33fe4fcb0435f}{EINA\_MAGIC\_NONE}});
   free(ptr->name);
   free(ptr);
\}

pilot *
pilot\_new(\textcolor{keyword}{const} \textcolor{keywordtype}{char} *name, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *callsign)
\{
   pilot *ptr = malloc(\textcolor{keyword}{sizeof}(pilot));
   \mbox{\hyperlink{group__Eina__Magic__Group_ga470f5ce28eaaf59ce80523be8471c5e0}{EINA\_MAGIC\_SET}}(ptr, SUBTYPE\_MAGIC);
   \mbox{\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;
\}

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

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

\textcolor{keywordtype}{void}
print\_pilot(pilot *ptr)
\{
   \textcolor{keywordflow}{if} (!\mbox{\hyperlink{group__Eina__Magic__Group_ga13317919a91b35412b6f40ca1166b373}{EINA\_MAGIC\_CHECK}}(ptr, SUBTYPE\_MAGIC)) \{
      \mbox{\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 \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}}, \textcolor{keywordtype}{char} **argv \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})
\{
   person *base;
   pilot *sub;

   \mbox{\hyperlink{group__Eina__Main__Group_gafee4bfc0e509b6ffed6598edf6f4b3de}{eina\_init}}();
   \mbox{\hyperlink{group__Eina__Magic__Group_ga0d77e61edf5b7b6c276a382d6bf191e7}{eina\_magic\_string\_set}}(BASETYPE\_MAGIC, \textcolor{stringliteral}{"person"});
   \mbox{\hyperlink{group__Eina__Magic__Group_ga5d202c69f78f9cb078efd3e798cb5a53}{eina\_magic\_string\_static\_set}}(SUBTYPE\_MAGIC, \textcolor{stringliteral}{"pilot"});

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

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

   print\_pilot((pilot *)base); \textcolor{comment}{//BAD: fails (C cast prevents GCC warning)}
   print\_pilot(sub);

   \mbox{\hyperlink{group__Eina__Main__Group_ga2c8b4020427108a30c44707af22c99d3}{eina\_shutdown}}();
\}
\end{DoxyCodeInclude}
 