Eina_Accessor usage

We start by including necessary headers, declaring variables and initializing eina:

#include <stdio.h>
#include <Eina.h>
int
main(int argc, char **argv)
{
const char *strings[] = {
"even", "odd", "even", "odd", "even", "odd", "even", "odd", "even", "odd"
};
const char *more_strings[] = {
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
};
Eina_Array *array;
Eina_List *list = NULL;
unsigned short int i;
void *data;
Eina Utility library.
struct _Eina_Accessor Eina_Accessor
Abstract type for accessors.
Definition eina_accessor.h:120
struct _Eina_Array Eina_Array
Type for a generic vector.
Definition eina_array.h:220
struct _Eina_List Eina_List
Type for a generic double linked list.
Definition eina_list.h:307
int eina_init(void)
Initialize the Eina library.
Definition eina_main.c:244

Next we populate our array and list:

array = eina_array_new(10);
for (i = 0; i < 10; i++)
{
eina_array_push(array, strings[i]);
list = eina_list_append(list, more_strings[i]);
}
Eina_Array * eina_array_new(unsigned int step)
Create a new array.
Definition eina_array.c:265
Eina_List * eina_list_append(Eina_List *list, const void *data)
Append the given data to the given linked list.
Definition eina_list.c:530

Now that we have two containers populated we can actually start the example and create an accessor:

Eina_Accessor * eina_array_accessor_new(const Eina_Array *array)
Returned a new accessor associated to an array.
Definition eina_array.c:464

Once having the accessor we can use it to access certain elements in the container:

for(i = 1; i < 10; i += 2)
{
eina_accessor_data_get(acc, i, &data);
printf("%s\n", (const char *)data);
}
Eina_Bool eina_accessor_data_get(Eina_Accessor *accessor, unsigned int position, void **data)
Retrieve the data of an accessor at a given position.
Definition eina_accessor.c:116
Note
Unlike iterators accessors allow us non-linear access, which allows us to print only the odd elements in the container.

As with every other resource we allocate we need to free the accessor(and the array):

void eina_accessor_free(Eina_Accessor *accessor)
Free an accessor.
Definition eina_accessor.c:96
void eina_array_free(Eina_Array *array)
Free an array.
Definition eina_array.c:289

Now we create another accessor, this time for the list:

Eina_Accessor * eina_list_accessor_new(const Eina_List *list)
Returned a new accessor associated to a list.
Definition eina_list.c:1465

And now the interesting bit, we use the same code we used above to print parts of the array to print parts of the list:

for(i = 1; i < 10; i += 2)
{
eina_accessor_data_get(acc, i, &data);
printf("%s\n", (const char *)data);
}

And to free the list we use a gimmick, instead of freeing list, we ask the accessor for it's container and free that:

void * eina_accessor_container_get(Eina_Accessor *accessor)
Return the container of an accessor.
Definition eina_accessor.c:107
Eina_List * eina_list_free(Eina_List *list)
Free an entire list and all the nodes, ignoring the data contained.
Definition eina_list.c:754

Finally we shut eina down and leave:

return 0;
}
int eina_shutdown(void)
Shut down the Eina library.
Definition eina_main.c:315

The full source code can be found on the examples folder on the eina_accessor_01.c file.