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();
Eina.h
Eina Utility library.
Eina_Accessor
struct _Eina_Accessor Eina_Accessor
Type for a accessor structure.
Definition eina_accessor.h:111
Eina_Array
struct _Eina_Array Eina_Array
Type for a generic one-dimensional linear data structure.
Definition eina_array.h:216
Eina_List
struct _Eina_List Eina_List
Type for a generic double linked list.
Definition eina_list.h:304
eina_init
EINA_API int eina_init(void)
Initializes the Eina library.
Definition eina_main.c:291
EINA_UNUSED
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition eina_types.h:339

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_push
static Eina_Bool eina_array_push(Eina_Array *array, const void *data)
Appends a data item to an array.
eina_array_new
EINA_API Eina_Array * eina_array_new(unsigned int step)
Creates a new array.
Definition eina_array.c:276
eina_list_append
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:

acc = eina_array_accessor_new(array);
eina_array_accessor_new
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_accessor_data_get
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_accessor_free(acc);
eina_array_free(array);
eina_accessor_free
EINA_API void eina_accessor_free(Eina_Accessor *accessor)
Frees an accessor.
Definition eina_accessor.c:96
eina_array_free
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:

acc = eina_list_accessor_new(list);
eina_list_accessor_new
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_list_free(eina_accessor_container_get(acc));
eina_accessor_container_get
EINA_API void * eina_accessor_container_get(Eina_Accessor *accessor)
Gets the container of an accessor.
Definition eina_accessor.c:107
eina_list_free
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:

eina_accessor_free(acc);
eina_shutdown();
return 0;
}
eina_shutdown
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.