

This example will examine a hypothetical situation in which we had a structure(which represented parameters) with two fields, and then need to add a third field to our structure. If using structs directly we\textquotesingle{}d need to rewrite every piece of code that touches the struct, by using eina value, and thus having the compiler not even know the struct, we can reduce the amount of changes needed and retain interoperability between the old and new format.

Our example will start with a function that creates descriptions of both of our structs for eina value usage. The first step is to create a struct and describe its members\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{comment}{//Compile with:}}
\DoxyCodeLine{\textcolor{comment}{//gcc eina\_value\_02.c -\/o eina\_value\_02 `pkg-\/config -\/-\/cflags -\/-\/libs eina`}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <\mbox{\hyperlink{Eina_8h}{Eina.h}}>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static} \mbox{\hyperlink{struct__Eina__Value__Struct__Desc}{Eina\_Value\_Struct\_Desc}} *V1\_DESC = NULL;}
\DoxyCodeLine{\textcolor{keyword}{static} \mbox{\hyperlink{struct__Eina__Value__Struct__Desc}{Eina\_Value\_Struct\_Desc}} *V2\_DESC = NULL;}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{void} value\_init(\textcolor{keywordtype}{void})}
\DoxyCodeLine{\{}
\DoxyCodeLine{   \textcolor{keyword}{typedef} \textcolor{keyword}{struct }\_My\_Struct\_V1 \{}
\DoxyCodeLine{     \textcolor{keywordtype}{int} param1;}
\DoxyCodeLine{     \textcolor{keywordtype}{char} param2;}
\DoxyCodeLine{   \} My\_Struct\_V1;}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keyword}{static} \mbox{\hyperlink{struct__Eina__Value__Struct__Member}{Eina\_Value\_Struct\_Member}} v1\_members[] = \{}
\DoxyCodeLine{     \textcolor{comment}{// no eina\_value\_type as they are not constant initializers, see below.}}
\DoxyCodeLine{     \mbox{\hyperlink{group__Eina__Value__Struct__Group_ga139206ef35b0b76e87eef56a202c0ae9}{EINA\_VALUE\_STRUCT\_MEMBER}}(NULL, My\_Struct\_V1, param1),}
\DoxyCodeLine{     \mbox{\hyperlink{group__Eina__Value__Struct__Group_ga139206ef35b0b76e87eef56a202c0ae9}{EINA\_VALUE\_STRUCT\_MEMBER}}(NULL, My\_Struct\_V1, param2)}
\DoxyCodeLine{   \};}
\DoxyCodeLine{   v1\_members[0].\mbox{\hyperlink{struct__Eina__Value__Struct__Member_a615cdf08597bd18623c8c7daec7b4641}{type}} = \mbox{\hyperlink{group__Eina__Value__Group_ga17974fd9ab773a3efefd591ffde58f77}{EINA\_VALUE\_TYPE\_INT}};}
\DoxyCodeLine{   v1\_members[1].\mbox{\hyperlink{struct__Eina__Value__Struct__Member_a615cdf08597bd18623c8c7daec7b4641}{type}} = \mbox{\hyperlink{group__Eina__Value__Group_ga0d54e0b952b44b7ac4f2fd573f7839c0}{EINA\_VALUE\_TYPE\_CHAR}};}

\end{DoxyCodeInclude}
 \begin{DoxyNote}{Note}
We can\textquotesingle{}t pass the types of the members to E\+I\+N\+A\+\_\+\+V\+A\+L\+U\+E\+\_\+\+S\+T\+R\+U\+C\+T\+\_\+\+M\+E\+M\+B\+ER macro because they are not constant initializers.
\end{DoxyNote}
So far it should be pretty easy to understand, we said {\ttfamily My\+\_\+\+Struct\+\_\+\+V1} has two members, one of type {\ttfamily int} and another of type {\ttfamily char}. We now create the description of the actual struct, again nothing overly complex, we signal which version of E\+I\+N\+A\+\_\+\+V\+A\+L\+U\+E\+\_\+\+S\+T\+R\+U\+CT we\textquotesingle{}re using, we declare no special operations, our members and our size\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{   \textcolor{keyword}{static} \mbox{\hyperlink{struct__Eina__Value__Struct__Desc}{Eina\_Value\_Struct\_Desc}} v1\_desc = \{}
\DoxyCodeLine{     \mbox{\hyperlink{group__Eina__Value__Struct__Group_gae76aada4965672ce991f1f41baf71975}{EINA\_VALUE\_STRUCT\_DESC\_VERSION}},}
\DoxyCodeLine{     NULL, \textcolor{comment}{// no special operations}}
\DoxyCodeLine{     v1\_members,}
\DoxyCodeLine{     \mbox{\hyperlink{group__Eina__Types__Group_ga7c1b6f81660b4e30516169e9e7d3261c}{EINA\_C\_ARRAY\_LENGTH}}(v1\_members),}
\DoxyCodeLine{     \textcolor{keyword}{sizeof}(My\_Struct\_V1)}
\DoxyCodeLine{   \};}
\DoxyCodeLine{   V1\_DESC = \&v1\_desc;}

\end{DoxyCodeInclude}


We now repeat the process for the second version of our struct, the only difference is the addition of a third parameter of type {\ttfamily int} \+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keyword}{typedef} \textcolor{keyword}{struct }\_My\_Struct\_V2 \{}
\DoxyCodeLine{     \textcolor{keywordtype}{int} param1;}
\DoxyCodeLine{     \textcolor{keywordtype}{char} param2;}
\DoxyCodeLine{     \textcolor{keywordtype}{int} param3;}
\DoxyCodeLine{   \} My\_Struct\_V2;}
\DoxyCodeLine{   \textcolor{keyword}{static} \mbox{\hyperlink{struct__Eina__Value__Struct__Member}{Eina\_Value\_Struct\_Member}} v2\_members[] = \{}
\DoxyCodeLine{     \textcolor{comment}{// no eina\_value\_type as they are not constant initializers, see below.}}
\DoxyCodeLine{     \mbox{\hyperlink{group__Eina__Value__Struct__Group_ga139206ef35b0b76e87eef56a202c0ae9}{EINA\_VALUE\_STRUCT\_MEMBER}}(NULL, My\_Struct\_V2, param1),}
\DoxyCodeLine{     \mbox{\hyperlink{group__Eina__Value__Struct__Group_ga139206ef35b0b76e87eef56a202c0ae9}{EINA\_VALUE\_STRUCT\_MEMBER}}(NULL, My\_Struct\_V2, param2),}
\DoxyCodeLine{     \mbox{\hyperlink{group__Eina__Value__Struct__Group_ga139206ef35b0b76e87eef56a202c0ae9}{EINA\_VALUE\_STRUCT\_MEMBER}}(NULL, My\_Struct\_V2, param3)}
\DoxyCodeLine{   \};}
\DoxyCodeLine{   v2\_members[0].\mbox{\hyperlink{struct__Eina__Value__Struct__Member_a615cdf08597bd18623c8c7daec7b4641}{type}} = \mbox{\hyperlink{group__Eina__Value__Group_ga17974fd9ab773a3efefd591ffde58f77}{EINA\_VALUE\_TYPE\_INT}};}
\DoxyCodeLine{   v2\_members[1].\mbox{\hyperlink{struct__Eina__Value__Struct__Member_a615cdf08597bd18623c8c7daec7b4641}{type}} = \mbox{\hyperlink{group__Eina__Value__Group_ga0d54e0b952b44b7ac4f2fd573f7839c0}{EINA\_VALUE\_TYPE\_CHAR}};}
\DoxyCodeLine{   v2\_members[2].\mbox{\hyperlink{struct__Eina__Value__Struct__Member_a615cdf08597bd18623c8c7daec7b4641}{type}} = \mbox{\hyperlink{group__Eina__Value__Group_ga17974fd9ab773a3efefd591ffde58f77}{EINA\_VALUE\_TYPE\_INT}};}
\DoxyCodeLine{   \textcolor{keyword}{static} \mbox{\hyperlink{struct__Eina__Value__Struct__Desc}{Eina\_Value\_Struct\_Desc}} v2\_desc = \{}
\DoxyCodeLine{     \mbox{\hyperlink{group__Eina__Value__Struct__Group_gae76aada4965672ce991f1f41baf71975}{EINA\_VALUE\_STRUCT\_DESC\_VERSION}},}
\DoxyCodeLine{     NULL, \textcolor{comment}{// no special operations}}
\DoxyCodeLine{     v2\_members,}
\DoxyCodeLine{     \mbox{\hyperlink{group__Eina__Types__Group_ga7c1b6f81660b4e30516169e9e7d3261c}{EINA\_C\_ARRAY\_LENGTH}}(v2\_members),}
\DoxyCodeLine{     \textcolor{keyword}{sizeof}(My\_Struct\_V2)}
\DoxyCodeLine{   \};}
\DoxyCodeLine{   V2\_DESC = \&v2\_desc;}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}


We\textquotesingle{}ll now look at a function that sets the values of our structs. For simplicity\textquotesingle{}s sake we initialize it we random values, a real world case would read these values from a file, a database or even from the network. The fundamental detail here is that this function works for both V1 and V2 structs, this is because setting a parameter that a struct that doesn\textquotesingle{}t have does nothing without throwing any errors\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{void} rand\_init(\mbox{\hyperlink{struct__Eina__Value}{Eina\_Value}} *v)}
\DoxyCodeLine{\{}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (v-\/>type != \mbox{\hyperlink{group__Eina__Value__Group_ga8147143699c5f5a686415090080d3f9e}{EINA\_VALUE\_TYPE\_STRUCT}})}
\DoxyCodeLine{     \textcolor{keywordflow}{return};}
\DoxyCodeLine{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Value__Struct__Group_ga476aa64a399aba776fa14f2fb065b268}{eina\_value\_struct\_set}}(v, \textcolor{stringliteral}{"param1"}, rand());}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Value__Struct__Group_ga476aa64a399aba776fa14f2fb065b268}{eina\_value\_struct\_set}}(v, \textcolor{stringliteral}{"param2"}, rand() \% 256);}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Value__Struct__Group_ga476aa64a399aba776fa14f2fb065b268}{eina\_value\_struct\_set}}(v, \textcolor{stringliteral}{"param3"}, rand());}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 \begin{DoxyNote}{Note}
While using \mbox{\hyperlink{group__Eina__Value__Struct__Group_ga476aa64a399aba776fa14f2fb065b268}{eina\+\_\+value\+\_\+struct\+\_\+set()}} with an in-\/existing parameter causes no error, it does return \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{E\+I\+N\+A\+\_\+\+F\+A\+L\+SE}}, to notify it was not possible to set the value. This could be used to determine that we\textquotesingle{}re handling a V1 struct and take some action based on that.
\end{DoxyNote}
The next thing is to do is see what a function that uses the values of the struct looks like. We\textquotesingle{}ll again be very simplistic in our usage, we\textquotesingle{}ll just print the values, but a real world case, might send these values to another process use them to open a network/database connection or anything else. Since all versions of the struct have {\ttfamily param1} and {\ttfamily param2} we\textquotesingle{}ll unconditionally use them\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{void} my\_struct\_use(\mbox{\hyperlink{struct__Eina__Value}{Eina\_Value}} *params)}
\DoxyCodeLine{\{}
\DoxyCodeLine{   \textcolor{keywordtype}{int} p1, p3;}
\DoxyCodeLine{   \textcolor{keywordtype}{char} p2;}
\DoxyCodeLine{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Value__Struct__Group_ga07a7cc420f03a247dffe4d13ce1ad6c5}{eina\_value\_struct\_get}}(params, \textcolor{stringliteral}{"param1"}, \&p1);}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Value__Struct__Group_ga07a7cc420f03a247dffe4d13ce1ad6c5}{eina\_value\_struct\_get}}(params, \textcolor{stringliteral}{"param2"}, \&p2);}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"param1: \%d\(\backslash\)nparam2: \%c\(\backslash\)n"}, p1, p2);}

\end{DoxyCodeInclude}


The next step is to conditionally use {\ttfamily param3}, which can fortunately be done in the same step in which we get it\textquotesingle{}s value\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (\mbox{\hyperlink{group__Eina__Value__Struct__Group_ga07a7cc420f03a247dffe4d13ce1ad6c5}{eina\_value\_struct\_get}}(params, \textcolor{stringliteral}{"param3"}, \&p3))}
\DoxyCodeLine{     printf(\textcolor{stringliteral}{"param3: \%d\(\backslash\)n"}, p3);}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}


There we\textquotesingle{}ve now got functions that can both populate and use values from both our structs, so now let\textquotesingle{}s actually use them in our main function by creating a struct of each type, initializing them and them using them\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int} main(\textcolor{keywordtype}{int} argc, \textcolor{keywordtype}{char} **argv)}
\DoxyCodeLine{\{}
\DoxyCodeLine{   (void)argc;}
\DoxyCodeLine{   (void)argv;}
\DoxyCodeLine{   \mbox{\hyperlink{struct__Eina__Value}{Eina\_Value}} *v1, *v2;}
\DoxyCodeLine{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Main__Group_gafee4bfc0e509b6ffed6598edf6f4b3de}{eina\_init}}();}
\DoxyCodeLine{   value\_init();}
\DoxyCodeLine{   srand(time(NULL));}
\DoxyCodeLine{}
\DoxyCodeLine{   v1 = \mbox{\hyperlink{group__Eina__Value__Struct__Group_gaa4f22ce61a055b572fc2a0ec9051479f}{eina\_value\_struct\_new}}(V1\_DESC);}
\DoxyCodeLine{   v2 = \mbox{\hyperlink{group__Eina__Value__Struct__Group_gaa4f22ce61a055b572fc2a0ec9051479f}{eina\_value\_struct\_new}}(V2\_DESC);}
\DoxyCodeLine{}
\DoxyCodeLine{   rand\_init(v1);}
\DoxyCodeLine{   my\_struct\_use(v1);}
\DoxyCodeLine{}
\DoxyCodeLine{   rand\_init(v2);}
\DoxyCodeLine{   my\_struct\_use(v2);}
\DoxyCodeLine{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Value__Value__Group_ga86a0a82f9154937f9d0050958c72abe8}{eina\_value\_free}}(v1);}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Value__Value__Group_ga86a0a82f9154937f9d0050958c72abe8}{eina\_value\_free}}(v2);}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Main__Group_ga2c8b4020427108a30c44707af22c99d3}{eina\_shutdown}}();}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}


This concludes our example. For the full source code see \mbox{\hyperlink{eina_value_02_c}{eina\+\_\+value\+\_\+02.\+c}}. 