Code: Select all
'a macro that takes as entry:
' * a array processing function name, say F( A() as datatype)
' * the datatype of the array (1D array)
' * a variadic list of items (of the datatype) in curled braces notation
'then,
'sends the variadic list as an array to the function
'
'syntax:
'MACRONAME(F, single, { 1, 2, 3 } )
'
'note that this seems not possible to write F( {1, 2, 3} ) in FB otherwise
sub ArrayProcess( ArrayParameter() as single )
print "ProcessArray function call"
print "Array received length: "; uBound( ArrayParameter ) + 1
print "Array content:"
for index as integer = 0 to uBound(ArrayParameter)
? ArrayParameter(index)
next index
end sub
#define _CARSTR(a, b...) #a
#define _CDRSTR(a, b...) #b
#macro _SENDDATATOARRAYPROCESSOR(arrayProcessorName, arrayDataType, item0, items...)
scope
'prepare a void array of the required datatype
dim as arrayDataType array(any)
'make the initial list content a string
dim as string listContent => _
trim(rtrim( " "& rtrim(ltrim(_CARSTR(item0, items), "{"), "}") &", "& _
rtrim( _CDRSTR(item0, items), "}" ) _
, any " " ), _
"," )
do
'parse the list and feed the array
dim as string tempItem
if instr(listContent, ",")>0 then
tempItem = left(listContent, instr(listContent, ",") - 1)
else
tempItem = listContent
end if
'the following if-block should be treated depending on the datatype
'here a void char in the list will be ignored as an item for the array
if trim(tempItem, any space(1))<>"" then
redim preserve array(uBound(array) + 1)
'the following instruction should offer one case by datatype
select case trim(lCase(#arrayDataType), any space(1))
case "single"
array(uBound(array)) = cSng(tempItem)
end select
end if
listContent = right(listContent, len(listContent) - len(tempItem) - 1)
loop until listContent=""
'send data list as array to processor function
arrayProcessorName( array() )
end scope
#endMacro
_SENDDATATOARRAYPROCESSOR(ArrayProcess, single, { 1, 2, 3 } )
getKey()
'(eof)