Hi folks. I need to create and use numerous function pointers and I want to keep them in some kind of list for fast access, but of course it must allow the functions to still be called - and it needs to be quick.
I have three ideas I thought I'd run by you. The first would be to create an array of function pointers somehow and then call each function by accessing the array? The second would be to create a new Type with a single function pointer in it and make an array of that Type, then access the array again to call the functions? Thirdly to use a Bank and store the function pointers as groups of 4 bytes using PokeLong, and read them back and call them using PeekLong? I don't know if any of these methods are possible in BlitzMax. If anyone can shed some light on this it'd be much appreciated.
Here is what I am trying to do, using an array of function pointers .. I don't know if this will even work...
' Trying to use an array of function pointers
Local myfunctions[50](param:int) ' ???
Function doingsomething(p1:int)
Return p1*10
End Function
Function doingsomething2(p1:int)
Return p1*20
End Function
For Local f:int=0 to 49
If (f mod 2)=0
myfunctions[f]=doingsomething
Else
myfuncitons[f]=doingsomething2
EndIf
Next
For Local f:int=0 to 49
Print myfunctions[f](f) ' ???
Next
Or using an array of types containing function pointers?...
' Trying to use an array of types containing function pointers
Type myfunct
field myfunction(param:int)
End Type
Local myfunctions:myfunct[50]
Function doingsomething(p1:int)
Return p1*10
End Function
Function doingsomething2(p1:int)
Return p1*20
End Function
For Local f:int=0 to 49
If (f mod 2)=0
myfunctions[f].myfunction=doingsomething
Else
myfuncitons[f].myfunction=doingsomething2
EndIf
Next
For Local f:int=0 to 49
Print myfunctions[f].myfunction(f)
Next
Or using a Bank of function pointers?...
' Trying to use a bank of function pointers
' Maybe I need to be using VarPtr in here somewhere?
Local myfunctions=CreateBank(50*4) ' Not sure on the syntax
Function doingsomething(p1:int)
Return p1*10
End Function
Function doingsomething2(p1:int)
Return p1*20
End Function
For Local f:int=0 to 49*4 Step 4
If (f mod 8)=0
PokeLong myfunctions+f,doingsomething
Else
PokeLong myfuncitons+f,doingsomething2
EndIf
Next
Local fptr(param:int)
For Local f:int=0 to 49*4 Step 4
Print PeekLong(myfunctions+f(f)) ' ???
' Or should it be: fptr=PeekLong(myfunctions+f)
' Print fptr(f) ' ????
Next
For my purposes I figure the bank method is preferable since I can access it faster and handle my own offsets? I'm guessing the compiler might not like using PeekLong and PokeLong with function pointers?
I've not idea if any of these will work or even compile. Can someone please advise how I can best do this? Please note it needs to be very efficient. Thanks.
|