`do` and `for` blocks
There are two ongoing trends in the mulle libraries. One is to wrap short lived data structures into a “do” block. The other is to hide the enumerators in “for” blocks.
As an example, lets write a function, that accepts some variable arguments,
puts them into a mulle_pointerarray
. Then the array is reverse enumerated,
and the contents printed line by line:
static void reverse_print( FILE *fp, ...)
{
va_list args;
char *s;
char *item;
va_start( args, fp);
mulle_pointerarray_do( array)
{
// fill array with arguments
while( (s = va_arg( args, char *)))
{
mulle_pointerarray_add( array, s);
}
// now go from back to front and print
mulle_pointerarray_for_reverse( array, item)
{
fprintf( fp, "%s\n", item);
}
}
// the array contents are freed and variable "array" is undefined outside
// of the block
va_end( args);
}
The opportunities for bugs aren’t eliminated, but things look tidier and tighter.
All data structures of mulle-container now can be used in this “do/for” manner.
And many other data structures in other parts of mulle-core and even the
MulleFoundation (like NSLockingDo
) partake in the scheme.
Post a comment
All comments are held for moderation; basic HTML formatting accepted.