Eina_Value usage

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)
{
int i;
char *newstr;
Eina Utility library.
int eina_init(void)
Initialize the Eina library.
Definition eina_main.c:244
defines the contents of a value
Definition eina_value.h:611

Now we can jump into using eina value. We set a value, get this value and then print it:

eina_value_set(&v, 123);
eina_value_get(&v, &i);
printf("v=%d\n", i);
const Eina_Value_Type * EINA_VALUE_TYPE_INT
manages int type.
Definition eina_value.c:5087
static Eina_Bool eina_value_get(const Eina_Value *value,...)
Get the generic value.
static Eina_Bool eina_value_set(Eina_Value *value,...)
Set the generic value.
static Eina_Bool eina_value_setup(Eina_Value *value, const Eina_Value_Type *type)
Initialize generic value storage.

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()
char * eina_value_to_string(const Eina_Value *value)
Convert value to string.
Definition eina_value.c:5203

And once done with a value it's good practice to destroy it:

eina_value_flush(&v); // destroy v contents, will not use anymore
static void eina_value_flush(Eina_Value *value)
Create generic value storage.

We now reuse v to store a string, get its value and print it:

const char *s;
eina_value_set(&v, "My string");
eina_value_get(&v, &s);
printf("v=%s (pointer: %p)\n", s, s);
const Eina_Value_Type * EINA_VALUE_TYPE_STRING
manages string type.
Definition eina_value.c:5093
Note
Since s is the value and not returned by eina_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:

And make sure v and otherv 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);
Eina_Bool eina_value_convert(const Eina_Value *value, Eina_Value *convert)
Convert one value to another type.
Definition eina_value.c:5172

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:

Full source code: eina_value_01.c