Eina_Accessor usage

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

#include <stdio.h>
#include <Eina.h>
int
main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
{
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.
EINA_API int eina_init(void)
Initializes the Eina library.
Definition: eina_main.c:291
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition: eina_types.h:339
Structure to provide random access to data structures.
Definition: eina_accessor.h:153
Type for an array of data.
Definition: eina_array.h:229
Type for a generic double linked list.
Definition: eina_list.h:318

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]);
}
static Eina_Bool eina_array_push(Eina_Array *array, const void *data)
Appends a data item to an array.
EINA_API Eina_Array * eina_array_new(unsigned int step)
Creates a new array.
Definition: eina_array.c:276
EINA_API Eina_List * eina_list_append(Eina_List *list, const void *data)
Appends the given data to the given linked list.
Definition: eina_list.c:584

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

EINA_API Eina_Accessor * eina_array_accessor_new(const Eina_Array *array)
Gets a new accessor associated with an array.
Definition: eina_array.c:419

Once we have 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_API Eina_Bool eina_accessor_data_get(Eina_Accessor *accessor, unsigned int position, void **data)
Gets the data of an accessor at the 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):

EINA_API void eina_accessor_free(Eina_Accessor *accessor)
Frees an accessor.
Definition: eina_accessor.c:96
EINA_API void eina_array_free(Eina_Array *array)
Frees an array.
Definition: eina_array.c:295

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

EINA_API Eina_Accessor * eina_list_accessor_new(const Eina_List *list)
Returns a new accessor associated with a list.
Definition: eina_list.c:1620

And now the interesting part, 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 its container and we free that:

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

Finally we shut eina down and leave:

return 0;
}
EINA_API int eina_shutdown(void)
Shuts down the Eina library.
Definition: eina_main.c:379

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