File descriptor data example, with Eet unions and variants
This is an example much like the one shown in Advanced use of Eet Data Descriptors.The difference is that here we're attaining ourselves to two new data types to store in an Eet file -- unions and variants. We don't try to come with data mapping to real world use cases, here. Instead, we're defining 3 different simple structures to be used throughout the example:
To identify, for both union and variant data cases, the type of each chunk of data, we're defining types to point to each of those structs:
We have also a mapping from those types to name strings, to be used in the Eet unions and variants type_get()
and type_set()
type identifying callbacks:
In this example, we have no fancy hash to store our data into profiles/accounts, but just two lists for union and variant data nodes:
Let's begin with our unions, then, which look like:
The first interesting part of the code is where we define our data descriptors for the main lists, the unions and all of structures upon which those two depend.
Example_Struct1
, Example_Struct2
and Example_Struct3
is straightforward, a matter already covered on Advanced use of Eet Data Descriptors. What is new, here, are the two type matching functions for our unions. There, we must set the data
pointer to its matching type, on _union_type_set
and return the correct matching type, on _union_type_get:
With the EET_DATA_DESCRIPTOR_ADD_MAPPING calls, which follow, we make the the link between our type names and their respective structs. The code handling actual data is pretty much the same as in Advanced use of Eet Data Descriptors -- one uses command line arguments to enter new data chunks (or just to visualize the contents of an Eet file), signalling if they are unions or variants. One must also pass the type of the data chuck to enter, with integers 1, 2 or 3. Then, come the fields for each type:
Variants are very similar to unions, except that data chunks need not contain previously allocated space for each of the possible types of data going in them:
The code declaring the data descriptors and handling the data is very similar to the unions part, and is left for the reader to check for him/herself. The complete code of the example follows.