- Main Page
- Related Pages
- Modules
- Data Structures
- Files
- Examples

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;
Eina_Accessor *acc;
unsigned short int i;
void *data;
eina_init();

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:

acc = eina_array_accessor_new(array);

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

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_accessor_free(acc);
eina_array_free(array);

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

acc = eina_list_accessor_new(list);

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:

eina_list_free(eina_accessor_container_get(acc));

Finally we shut eina down and leave:

eina_accessor_free(acc);
eina_shutdown();
return 0;
}

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