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)
{
(void)argc;
(void)argv;
Eina_Value v;
int i;
char *newstr;
eina_init();
Eina.h
Eina Utility library.
eina_init
EINA_API int eina_init(void)
Initializes the Eina library.
Definition eina_main.c:291
Eina_Value
struct _Eina_Value Eina_Value
Store generic values.
Definition eina_value.h:307

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);
EINA_VALUE_TYPE_INT
EINA_API const Eina_Value_Type * EINA_VALUE_TYPE_INT
manages int type.
Definition eina_value.c:5602

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

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);
EINA_VALUE_TYPE_STRING
EINA_API const Eina_Value_Type * EINA_VALUE_TYPE_STRING
manages string type.
Definition eina_value.c:5608

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 types 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:

eina_value_setup(&v, EINA_VALUE_TYPE_INT);

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_value_convert
EINA_API Eina_Bool eina_value_convert(const Eina_Value *value, Eina_Value *convert)
Converts one value to another type.
Definition eina_value.c:5688

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);
eina_shutdown();
}
eina_shutdown
EINA_API int eina_shutdown(void)
Shuts down the Eina library.
Definition eina_main.c:379

Full source code: eina_value_01.c