[futurebasic] Re: Variadic functions

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : May 2013 : Group Archive : Group : All Groups

From: Bernie <fblist.bw@...>
Date: Mon, 6 May 2013 22:03:00 +0100
This is simpler (I think).

'---------------
/*
   VAList [variadic functions]

   Bernie 20130504

   http://en.wikipedia.org/wiki/Variadic_function
   http://en.wikipedia.org/wiki/Stdarg.h
*/

include "ConsoleWindow"

#define VAList as ^char

BeginCDeclaration
#define VAStart( ap, lastArg ) va_start( ap, lastArg )
void *VAArg( va_list *ap );
double VAArgDouble( va_list *ap );
void VACopy( va_list *ap2, va_list *ap1 );
void VAEnd( va_list *ap );
EndC

BeginCFunction
void *VAArg( va_list *ap )
{ return va_arg( *ap, void* ); }

double VAArgDouble( va_list *ap )
{ return va_arg( *ap, double ); }

void VACopy( va_list *ap2, va_list *ap1 )
{ va_copy( *ap2, *ap1 ); }

void VAEnd( va_list *ap )
{ va_end( *ap ); }
EndC

toolbox VAStart( ptr ap, ptr lastArg )
toolbox fn VAArg( char *ap ) = ptr
toolbox fn VAArgDouble( char *ap ) = double
toolbox VACopy( char *ap2, char *ap1 )
toolbox VAEnd( char *ap )


local fn FunctionOne( lastArg as short, ... )
'~'1
dim as VAList   ap
dim as short    i

VAStart( ap, lastArg )

for i = 1 to lastArg
print fn VAArg( ap )
next i

VAEnd( ap )
end fn


local fn FunctionTwo( lastArg as CFStringRef, ... )
'~'1
dim as VAList      ap
dim as CFTypeRef   cfVal
dim as CFIndex     length, index
dim as UniChar     uc
dim as double      doubleVal
dim as long        longVal

VAStart( ap, lastArg )

length = fn CFStringGetLength( lastArg )
for index = 0 to length - 1
uc = fn CFStringGetCharacterAtIndex( lastArg, index )
select ( uc )
case _"i", _"d"// integer = long
longVal = fn VAArg( ap )
print longVal

case _"f"// float = double
doubleVal = fn VAArgDouble( ap )
print doubleVal

case _"@"// CFType
cfVal = fn VAArg( ap )
fn ConsolePrintCFType( cfVal )

end select
next index

VAEnd( ap )
end fn


fn FunctionOne( 3, 123, 456, 789 )

print

fn FunctionTwo( @"d@f@", 345, @"alpha", 67.89, @"bravo" )
'---------------