\chapter{Eina\+\_\+\+Accessor usage}
\hypertarget{eina_accessor_example_01_page}{}\label{eina_accessor_example_01_page}\index{Eina\_Accessor usage@{Eina\_Accessor usage}}


We start by including necessary headers, declaring variables, and initializing eina\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <stdio.h>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <\mbox{\hyperlink{Eina_8h}{Eina.h}}>}}
\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{\ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *strings[]\ =\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \textcolor{stringliteral}{"{}even"{}},\ \textcolor{stringliteral}{"{}odd"{}},\ \textcolor{stringliteral}{"{}even"{}},\ \textcolor{stringliteral}{"{}odd"{}},\ \textcolor{stringliteral}{"{}even"{}},\ \textcolor{stringliteral}{"{}odd"{}},\ \textcolor{stringliteral}{"{}even"{}},\ \textcolor{stringliteral}{"{}odd"{}},\ \textcolor{stringliteral}{"{}even"{}},\ \textcolor{stringliteral}{"{}odd"{}}}
\DoxyCodeLine{\ \ \ \};}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *more\_strings[]\ =\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \textcolor{stringliteral}{"{}0"{}},\ \textcolor{stringliteral}{"{}1"{}},\ \textcolor{stringliteral}{"{}2"{}},\ \textcolor{stringliteral}{"{}3"{}},\ \textcolor{stringliteral}{"{}4"{}},\ \textcolor{stringliteral}{"{}5"{}},\ \textcolor{stringliteral}{"{}6"{}},\ \textcolor{stringliteral}{"{}7"{}},\ \textcolor{stringliteral}{"{}8"{}},\ \textcolor{stringliteral}{"{}9"{}}}
\DoxyCodeLine{\ \ \ \};}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{struct__Eina__Array}{Eina\_Array}}\ *array;}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{struct__Eina__List}{Eina\_List}}\ *list\ =\ NULL;}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{struct__Eina__Accessor}{Eina\_Accessor}}\ *acc;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{unsigned}\ \textcolor{keywordtype}{short}\ \textcolor{keywordtype}{int}\ i;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{void}\ *data;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Main__Group_ga81b8137665b8a4d2cb38ca50017d4c74}{eina\_init}}();}

\end{DoxyCodeInclude}
 Next we populate our array and list\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ array\ =\ \mbox{\hyperlink{group__Eina__Array__Group_gaf4b5844225b5359a617744210a5ed6ff}{eina\_array\_new}}(10);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ 10;\ i++)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Eina__Array__Group_ga97ac1fe353f4cd7a9762af4d4db7b9c4}{eina\_array\_push}}(array,\ strings[i]);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ list\ =\ \mbox{\hyperlink{group__Eina__List__Group_ga625698a383dda8fc0db245a1c7608c49}{eina\_list\_append}}(list,\ more\_strings[i]);}
\DoxyCodeLine{\ \ \ \ \ \}}

\end{DoxyCodeInclude}
 Now that we have two containers populated we can actually start the example and create an accessor\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ acc\ =\ \mbox{\hyperlink{group__Eina__Array__Group_ga01a0d9dba5a1f9f39f0ec7d0bab99bfb}{eina\_array\_accessor\_new}}(array);}

\end{DoxyCodeInclude}
 Once we have the accessor we can use it to access certain elements in the container\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{for}(i\ =\ 1;\ i\ <\ 10;\ i\ +=\ 2)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Eina__Accessor__Group_ga44dccec20fabc2242e88bbb8ae281c31}{eina\_accessor\_data\_get}}(acc,\ i,\ \&data);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}\%s\(\backslash\)n"{}},\ (\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *)data);}
\DoxyCodeLine{\ \ \ \ \ \}}

\end{DoxyCodeInclude}
\begin{DoxyNote}{Note}
Unlike iterators accessors allow us non-\/linear access, which allows us to print only the odd elements in the container.
\end{DoxyNote}
As with every other resource we allocate we need to free the accessor(and the array)\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Accessor__Group_ga5a4baf96fea2d4150e03b6abcc43be13}{eina\_accessor\_free}}(acc);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Array__Group_ga7fa7e2cdcfb739140d5c5a5044086ce1}{eina\_array\_free}}(array);}

\end{DoxyCodeInclude}
 Now we create another accessor, this time for the list\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ acc\ =\ \mbox{\hyperlink{group__Eina__List__Group_ga335519c1e915268f4d96233d317ce06d}{eina\_list\_accessor\_new}}(list);}

\end{DoxyCodeInclude}
 And now the interesting part, we use the same code we used above, to print parts of the array, to print parts of the list\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{for}(i\ =\ 1;\ i\ <\ 10;\ i\ +=\ 2)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Eina__Accessor__Group_ga44dccec20fabc2242e88bbb8ae281c31}{eina\_accessor\_data\_get}}(acc,\ i,\ \&data);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}\%s\(\backslash\)n"{}},\ (\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *)data);}
\DoxyCodeLine{\ \ \ \ \ \}}

\end{DoxyCodeInclude}
 And to free the list we use a gimmick, instead of freeing {\itshape list}, we ask the accessor for its container and we free that\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__List__Group_ga82a1bfa543e21853f9ea3ff88dd1250b}{eina\_list\_free}}(\mbox{\hyperlink{group__Eina__Accessor__Group_gaec9fcc05b1b693ea473f667ca6238565}{eina\_accessor\_container\_get}}(acc));}

\end{DoxyCodeInclude}
 Finally we shut eina down and leave\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Accessor__Group_ga5a4baf96fea2d4150e03b6abcc43be13}{eina\_accessor\_free}}(acc);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Main__Group_ga5bf35513199d5220341437c05696285d}{eina\_shutdown}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 The full source code can be found in the examples folder in the \doxysectlink{eina_accessor_01_c}{eina\+\_\+accessor\+\_\+01.c}{0} file. 