Generic Value

The Eina_Value object provides generic data storage and access, allowing you to store what you want in one single type of Eina_Value. It is meant for simple data types, providing uniform access and release functions, useful to exchange data preserving their types. The Eina_Value comes with predefined types for numbers, array, list, hash, blob, and structs, and it can convert between data types, including string.

The Eina_Value can handle the following types:

  • EINA_VALUE_TYPE_UCHAR: unsigned char
  • EINA_VALUE_TYPE_USHORT: unsigned short
  • EINA_VALUE_TYPE_UINT: unsigned int
  • EINA_VALUE_TYPE_ULONG: unsigned long
  • EINA_VALUE_TYPE_TIMESTAMP: unsigned long used for timestamps
  • EINA_VALUE_TYPE_UINT64: unsigned integer of 64 bits
  • EINA_VALUE_TYPE_CHAR: char
  • EINA_VALUE_TYPE_SHORT: short
  • EINA_VALUE_TYPE_INT: int
  • EINA_VALUE_TYPE_LONG: long
  • EINA_VALUE_TYPE_INT64: integer of 64 bits
  • EINA_VALUE_TYPE_FLOAT: float
  • EINA_VALUE_TYPE_DOUBLE: double
  • EINA_VALUE_TYPE_STRINGSHARE: stringshared string
  • EINA_VALUE_TYPE_STRING: string
  • EINA_VALUE_TYPE_ARRAY: array
  • EINA_VALUE_TYPE_LIST: list
  • EINA_VALUE_TYPE_HASH: hash
  • EINA_VALUE_TYPE_TIMEVAL: 'struct timeval'
  • EINA_VALUE_TYPE_BLOB: blob of bytes
  • EINA_VALUE_TYPE_STRUCT: struct

To set up a generic value:

1. Declare the necessary variables:

// The Eina_Value itself
Eina_Value v;
// An integer
int i;
// And a char *
char *newstr;

2. To set up an Eina_Value for an integer, use the eina_value_setup() function. The first argument is the Eina_Value and the second is the type.

eina_value_setup(&v, EINA_VALUE_TYPE_INT);

To manage the generic value:

  • To set an integer, use the eina_value_set() function:
eina_value_set(&v, 123);
  • To get the value, use the eina_value_get() function. Pass the Eina_Value as the first argument, and a pointer to a variable to store the value (the target variable must have the same type as the Eina_Value).
eina_value_get(&v, &i);
printf("v=%d\n", i);

The above example prints “v=123”.

  • 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);
  • To store an Eina_List, use the Eina_Value that corresponds to the EINA_VALUE_TYPE_LIST type.
  • To create an Eina_Value_List, use the eina_value_list_setup() function. The function initializes a generic value storage of the list type. The first parameter is the “object” value, and the second one is the type (how to manage the stored list members).