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;
int i;
char *newstr;

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);

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

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:

Full source code: eina_value_01.c

eina_value_setup
static Eina_Bool eina_value_setup(Eina_Value *value, const Eina_Value_Type *type)
Initializes generic value storage.
EINA_VALUE_TYPE_INT
const Eina_Value_Type * EINA_VALUE_TYPE_INT
Definition: eina_value.c:5599
Eina.h
Eina Utility library.
eina_init
int eina_init(void)
Initializes the Eina library.
Definition: eina_main.c:277
eina_value_convert
Eina_Bool eina_value_convert(const Eina_Value *value, Eina_Value *convert)
Converts one value to another type.
Definition: eina_value.c:5692
eina_value_set
static Eina_Bool eina_value_set(Eina_Value *value,...)
Sets the generic value.
eina_value_get
static Eina_Bool eina_value_get(const Eina_Value *value,...)
Gets the generic value.
eina_value_flush
static void eina_value_flush(Eina_Value *value)
Empties a generic value storage.
eina_shutdown
int eina_shutdown(void)
Shuts down the Eina library.
Definition: eina_main.c:348
EINA_VALUE_TYPE_STRING
const Eina_Value_Type * EINA_VALUE_TYPE_STRING
Definition: eina_value.c:5605
_Eina_Value
Definition: eina_value.h:661
eina_value_to_string
char * eina_value_to_string(const Eina_Value *value)
Converts value to string.
Definition: eina_value.c:5723