Tizen Native API
4.0
|
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:
//Compile with: //gcc eina_value_01.c -o eina_value_01 `pkg-config --cflags --libs eina` #include <Eina.h> int main(int argc, char **argv) { (void)argc; (void)argv; Eina_Value v; int i; char *newstr; eina_init();
Now we can jump into using eina value. We set a value, get this value and then print it:
eina_value_setup(&v, EINA_VALUE_TYPE_INT); eina_value_set(&v, 123); eina_value_get(&v, &i); printf("v=%d\n", i);
In the above snippet of code we printed an int
value, we can however print the value as a string:
newstr = eina_value_to_string(&v); printf("v as string: %s\n", newstr); free(newstr); // it was allocated by eina_value_to_string()
And once done with a value it's good practice to destroy it:
eina_value_flush(&v); // destroy v contents, will not use anymore
We now reuse v
to store a string, get its value and print it:
const char *s; eina_value_setup(&v, EINA_VALUE_TYPE_STRING); eina_value_set(&v, "My string"); eina_value_get(&v, &s); printf("v=%s (pointer: %p)\n", s, s);
- Note:
- Since
s
is the value and not returned byeina_value_to_string()
we don't need to free it.
Just because we stored a string doesn't mean we can't use the 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):
newstr = eina_value_to_string(&v); printf("v as string: %s (pointer: %p)\n", newstr, newstr); free(newstr); // it was allocated by eina_value_to_string() eina_value_flush(&v); // destroy v contents, string 's' is not valid anymore!
And now to explore conversions between two type we'll create another value:
Eina_Value otherv; eina_value_setup(&otherv, EINA_VALUE_TYPE_STRING);
And make sure v
and others
have different types:
We then set a value to 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:
// convert from int to string: eina_value_set(&v, 123); eina_value_convert(&v, &otherv);
And now let's check the conversion worked:
eina_value_get(&otherv, &s); printf("otherv=%s\n", s);
But converting to strings is not particularly exciting, eina_value_to_string()
already did that, so now let's make the conversion the other way around, from string to int:
// and the other way around! eina_value_set(&otherv, "33"); eina_value_convert(&otherv, &v); eina_value_get(&v, &i); printf("v=%d\n", i);
And once done, destroy the values:
eina_value_flush(&otherv); eina_value_flush(&v); }
Full source code: eina_value_01.c