

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 EINA\+\_\+\+VALUE\+\_\+\+STRUCT\+\_\+\+MEMBER 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 EINA\+\_\+\+VALUE\+\_\+\+STRUCT 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{\ \ \ eina\_value\_struct\_set(v,\ \textcolor{stringliteral}{"{}param1"{}},\ rand());}
\DoxyCodeLine{\ \ \ eina\_value\_struct\_set(v,\ \textcolor{stringliteral}{"{}param2"{}},\ rand()\ \%\ 256);}
\DoxyCodeLine{\ \ \ 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}{EINA\+\_\+\+FALSE}}, 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{\ \ \ eina\_value\_struct\_get(params,\ \textcolor{stringliteral}{"{}param1"{}},\ \&p1);}
\DoxyCodeLine{\ \ \ 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}\ (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_ga81b8137665b8a4d2cb38ca50017d4c74}{eina\_init}}();}
\DoxyCodeLine{\ \ \ value\_init();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ v1\ =\ \mbox{\hyperlink{group__Eina__Value__Struct__Group_gaeda31142675d09af1863096a4f4ac707}{eina\_value\_struct\_new}}(V1\_DESC);}
\DoxyCodeLine{\ \ \ v2\ =\ \mbox{\hyperlink{group__Eina__Value__Struct__Group_gaeda31142675d09af1863096a4f4ac707}{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_ga27a121e20a7109d5eae7203fc0f54850}{eina\_value\_free}}(v1);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Value__Value__Group_ga27a121e20a7109d5eae7203fc0f54850}{eina\_value\_free}}(v2);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Main__Group_ga5bf35513199d5220341437c05696285d}{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}}. 