For this example we'll be creating our own custom type of eina value. Eina value can already store struct timeval(man gettimeofday for more information) but it has no type to store struct timezone, so that's what this example will do.
To create our own custom eina value type we need to define functions to do the following operations on it:
Most of this functions are very simple, so let's look at them, starting with setup which only clear the memory so that we can be certain we won't be using stale data:
Now the flush function, which is even simpler, it does nothing, that's because there is nothing we need to do, all the necessary steps are taken by eina value itself:
Our next function, copy, is a bit more interesting, but not much, it just casts our void pointers to struct timezone pointers and does the copy:
Next we have the comparison function, which compares the tz_minuteswest
field of struct timezone, we don't compare tz_dsttime
because that field is not used in linux:
Next we have setting, this however requires not one but rather two functions, the reason for this is because to be able to receive arguments of any type eina value uses variadic functions, so we need a function to get the argument from a va_list and another to actually to the setting.
Lets first look at the pset function which sets the received value to a pointer:
Next we have the vset function which get the argument from the va_list and passes it to the pset function:
And now the function to get the value, a very simple copying of the value to the given pointer:
And finally our conversion function, this is our longest and most interesting one. For numeric type we simply assign the value of tz_minuteswest
to the new type and call a set function using it:
For string types we use snprintf()
to format our tz_minuteswest
field and put it in a string(again tz_dsttime
is ignored because it's not used):
Finally we handle any other types by returning an error in that case:
Now that we have all the functions, we can populate an Eina_Value_Type
to later use it with eina_value_setup()
:
We can now finally use our new TZ_TYPE with eina value, so lets conclude our example by practicing that by setting its value and printing it:
For the full source code see eina_value_03.c.