

This very simple example shows how to use some of the basic features of eina value\+: setting and getting values, converting between types and printing a value as a string.

Our main function starts out with the basic, declaring some variables and initializing eina\+: 
\begin{DoxyCodeInclude}
\textcolor{comment}{//Compile with:}
\textcolor{comment}{//gcc eina\_value\_01.c -o eina\_value\_01 `pkg-config --cflags --libs eina`}

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

\textcolor{keywordtype}{int} main(\textcolor{keywordtype}{int} argc, \textcolor{keywordtype}{char} **argv)
\{
   (void)argc;
   (void)argv;
   \hyperlink{struct__Eina__Value}{Eina\_Value} v;
   \textcolor{keywordtype}{int} i;
   \textcolor{keywordtype}{char} *newstr;

   \hyperlink{group__Eina__Main__Group_gabf448ada5bc81a07342b66c6f818ee5e}{eina\_init}();

\end{DoxyCodeInclude}


Now we can jump into using eina value. We set a value, get this value and then print it\+: 
\begin{DoxyCodeInclude}

   \hyperlink{group__Eina__Value__Value__Group_gab5c6558b1b8be2ad597d71a765cbf5f6}{eina\_value\_setup}(&v, \hyperlink{group__Eina__Value__Group_ga17974fd9ab773a3efefd591ffde58f77}{EINA\_VALUE\_TYPE\_INT});
   \hyperlink{group__Eina__Value__Value__Group_ga9a1407c953694c3d6f40ab01ebe9a294}{eina\_value\_set}(&v, 123);
   \hyperlink{group__Eina__Value__Value__Group_ga6f088fe6c6127d808cdf214b355cbdeb}{eina\_value\_get}(&v, &i);
   printf(\textcolor{stringliteral}{"v=%d\(\backslash\)n"}, i);

\end{DoxyCodeInclude}


In the above snippet of code we printed an {\ttfamily int} value, we can however print the value as a string\+: 
\begin{DoxyCodeInclude}

   newstr = \hyperlink{group__Eina__Value__Value__Group_ga8f34b6d6639654dd88cd197c9def78a3}{eina\_value\_to\_string}(&v);
   printf(\textcolor{stringliteral}{"v as string: %s\(\backslash\)n"}, newstr);
   free(newstr); \textcolor{comment}{// it was allocated by eina\_value\_to\_string()}

\end{DoxyCodeInclude}


And once done with a value it's good practice to destroy it\+: 
\begin{DoxyCodeInclude}
   \hyperlink{group__Eina__Value__Value__Group_ga940660709a2862bafac92f47bb721a68}{eina\_value\_flush}(&v); \textcolor{comment}{// destroy v contents, will not use anymore}

\end{DoxyCodeInclude}


We now reuse {\ttfamily v} to store a string, get its value and print it\+: 
\begin{DoxyCodeInclude}

   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *s;
   \hyperlink{group__Eina__Value__Value__Group_gab5c6558b1b8be2ad597d71a765cbf5f6}{eina\_value\_setup}(&v, \hyperlink{group__Eina__Value__Group_ga916f76c8d5ffd4c3ee3c322a54efd0b7}{EINA\_VALUE\_TYPE\_STRING});
   \hyperlink{group__Eina__Value__Value__Group_ga9a1407c953694c3d6f40ab01ebe9a294}{eina\_value\_set}(&v, \textcolor{stringliteral}{"My string"});
   \hyperlink{group__Eina__Value__Value__Group_ga6f088fe6c6127d808cdf214b355cbdeb}{eina\_value\_get}(&v, &s);
   printf(\textcolor{stringliteral}{"v=%s (pointer: %p)\(\backslash\)n"}, s, s);

\end{DoxyCodeInclude}
 \begin{DoxyNote}{Note}
Since {\ttfamily s} is the value and not returned by {\ttfamily \hyperlink{group__Eina__Value__Value__Group_ga8f34b6d6639654dd88cd197c9def78a3}{eina\+\_\+value\+\_\+to\+\_\+string()}} we don't need to free it.
\end{DoxyNote}
Just because we stored a string doesn't mean we can't use the {\ttfamily \hyperlink{group__Eina__Value__Value__Group_ga8f34b6d6639654dd88cd197c9def78a3}{eina\+\_\+value\+\_\+to\+\_\+string()}} function, we can and it's important to note that it will return not the stored string but rather a copy of it(one we have to free)\+: 
\begin{DoxyCodeInclude}

   newstr = \hyperlink{group__Eina__Value__Value__Group_ga8f34b6d6639654dd88cd197c9def78a3}{eina\_value\_to\_string}(&v);
   printf(\textcolor{stringliteral}{"v as string: %s (pointer: %p)\(\backslash\)n"}, newstr, newstr);
   free(newstr); \textcolor{comment}{// it was allocated by eina\_value\_to\_string()}
   \hyperlink{group__Eina__Value__Value__Group_ga940660709a2862bafac92f47bb721a68}{eina\_value\_flush}(&v); \textcolor{comment}{// destroy v contents, string 's' is not valid anymore!}

\end{DoxyCodeInclude}


And now to explore conversions between two type we'll create another value\+: 
\begin{DoxyCodeInclude}

   \hyperlink{struct__Eina__Value}{Eina\_Value} otherv;
   \hyperlink{group__Eina__Value__Value__Group_gab5c6558b1b8be2ad597d71a765cbf5f6}{eina\_value\_setup}(&otherv, \hyperlink{group__Eina__Value__Group_ga916f76c8d5ffd4c3ee3c322a54efd0b7}{EINA\_VALUE\_TYPE\_STRING});

\end{DoxyCodeInclude}


And make sure {\ttfamily v} and {\ttfamily otherv} have different types\+: 
\begin{DoxyCodeInclude}
   \hyperlink{group__Eina__Value__Value__Group_gab5c6558b1b8be2ad597d71a765cbf5f6}{eina\_value\_setup}(&v, \hyperlink{group__Eina__Value__Group_ga17974fd9ab773a3efefd591ffde58f77}{EINA\_VALUE\_TYPE\_INT});

\end{DoxyCodeInclude}


We then set a value to {\ttfamily v} and have it converted, to do this we don't need to tell to which type we want to convert, we just say were we want to store the converted value and eina value will figure out what to convert to, and how\+: 
\begin{DoxyCodeInclude}

   \textcolor{comment}{// convert from int to string:}
   \hyperlink{group__Eina__Value__Value__Group_ga9a1407c953694c3d6f40ab01ebe9a294}{eina\_value\_set}(&v, 123);
   \hyperlink{group__Eina__Value__Value__Group_ga86f91748c90652df59e3ef38bf54949d}{eina\_value\_convert}(&v, &otherv);

\end{DoxyCodeInclude}


And now let's check the conversion worked\+: 
\begin{DoxyCodeInclude}
   \hyperlink{group__Eina__Value__Value__Group_ga6f088fe6c6127d808cdf214b355cbdeb}{eina\_value\_get}(&otherv, &s);
   printf(\textcolor{stringliteral}{"otherv=%s\(\backslash\)n"}, s);

\end{DoxyCodeInclude}


But converting to strings is not particularly exciting, {\ttfamily \hyperlink{group__Eina__Value__Value__Group_ga8f34b6d6639654dd88cd197c9def78a3}{eina\+\_\+value\+\_\+to\+\_\+string()}} already did that, so now let's make the conversion the other way around, from string to {\ttfamily int\+:} 
\begin{DoxyCodeInclude}

   \textcolor{comment}{// and the other way around!}
   \hyperlink{group__Eina__Value__Value__Group_ga9a1407c953694c3d6f40ab01ebe9a294}{eina\_value\_set}(&otherv, \textcolor{stringliteral}{"33"});
   \hyperlink{group__Eina__Value__Value__Group_ga86f91748c90652df59e3ef38bf54949d}{eina\_value\_convert}(&otherv, &v);
   \hyperlink{group__Eina__Value__Value__Group_ga6f088fe6c6127d808cdf214b355cbdeb}{eina\_value\_get}(&v, &i);
   printf(\textcolor{stringliteral}{"v=%d\(\backslash\)n"}, i);

\end{DoxyCodeInclude}


And once done, destroy the values\+: 
\begin{DoxyCodeInclude}

   \hyperlink{group__Eina__Value__Value__Group_ga940660709a2862bafac92f47bb721a68}{eina\_value\_flush}(&otherv);
   \hyperlink{group__Eina__Value__Value__Group_ga940660709a2862bafac92f47bb721a68}{eina\_value\_flush}(&v);
\}

\end{DoxyCodeInclude}


Full source code\+: \hyperlink{eina_value_01_c}{eina\+\_\+value\+\_\+01.\+c} 