Creating a simple model

This example shows the creation of a model with five properties, named: 'a', 'b', 'c', 'd' and 'e' with values 0, 1, 2, 3 and 4 respectively. In addition to the 5 properties our model also add 5 children, and to each child we give a property named 'x' with a value of 1, 2, 3, 4 and 5.

In other words this piece of code shows how to use eina_model to store a list of elements, given that the list itself has some properties.

Now let's walk through the code and examine the interesting bits.

This is some pretty standard initialization code.

We now create our eina_model, the important detail here is the type of the model being created, for this example we use the generic type provided by eina:

Once our model has been created we can add callbacks to be notified of events that happen to our model, for this example we are just going to add a callback for the "delete" event. To get a list of events a given eina model can emit see eina_model_event_names_list_get().

Once we have a model, we need to populate it with information. There are two types of information we can store on an eina model: properties and eina models. We are going to start by looking at properties.

Properties are, simply put, named values. They have a char* identifier and an Eina_Value value. This means you can store in a property almost any type of data. For this example we are going to add some very simple numeric properties which will have single letter identifiers.

Despite being able to store almost any value properties the least flexible information unit we can put in an eina model. We can add eina models to our eina model, this allows us to represt complex information hierarchies. This example adds 5 models(with no children of their own) to our parent model m.

The code here should be pretty easy to understand, we create a model, much like we did before, and we then add a property to our model, again a task we have already done.

The important issue to note here is that we could have given each of our c child models as complex an structure as we needed, they could each be a list or a tree on their own right.

Now that we have a populated model we print a string representation of it(without forgetting to free the string):

And since we are done using our model we release our reference to it(and since no else holds references to it, it will be freed):

Note that we don't need to iterate over the children of m unrefing it, this is because we don't hold references to it, we freed our references right after we added them to their parent model, so when the parent model dies(and releases the references to it's children) they will be freed.

The only thing we are going to look at is the callback we registered for whenever a model is deleted, since our models don't do anything fancy we are just going to print the memory address of the model being freed.

Note that this means the memory address is still valid, our callback is called just before the memory is freed so we could still access its information here.

The full code can be seen in eina_model_02.c