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;

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

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

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

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

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:

Finally we shut eina down and leave:

return 0;
}

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

_Eina_Accessor
Structure to provide random access to data structures.
Definition: eina_accessor.h:152
eina_accessor_data_get
EAPI 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
eina_list_append
EAPI 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
EINA_UNUSED
#define EINA_UNUSED
Definition: eina_types.h:339
eina_accessor_free
EAPI void eina_accessor_free(Eina_Accessor *accessor)
Frees an accessor.
Definition: eina_accessor.c:96
Eina.h
Eina Utility library.
eina_init
int eina_init(void)
Initializes the Eina library.
Definition: eina_main.c:279
_Eina_Array
Definition: eina_array.h:228
eina_array_free
EAPI void eina_array_free(Eina_Array *array)
Frees an array.
Definition: eina_array.c:295
eina_array_push
EAPI Eina_Bool static Eina_Bool eina_array_push(Eina_Array *array, const void *data) EINA_ARG_NONNULL(1
Appends a data item to an array.
eina_shutdown
int eina_shutdown(void)
Shuts down the Eina library.
Definition: eina_main.c:350
eina_array_accessor_new
EAPI Eina_Accessor * eina_array_accessor_new(const Eina_Array *array)
Gets a new accessor associated with an array.
Definition: eina_array.c:419
eina_list_accessor_new
EAPI Eina_Accessor * eina_list_accessor_new(const Eina_List *list)
Returns a new accessor associated with a list.
Definition: eina_list.c:1620
eina_array_new
EAPI Eina_Array * eina_array_new(unsigned int step)
Creates a new array.
Definition: eina_array.c:276
eina_list_free
EAPI 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
_Eina_List
Definition: eina_list.h:317
eina_accessor_container_get
EAPI void * eina_accessor_container_get(Eina_Accessor *accessor)
Gets the container of an accessor.
Definition: eina_accessor.c:107