call short integer (Procedures) using an array of pointers

Just starting out? Need help? Post your questions and find answers here.
hgcoon
New User
New User
Posts: 9
Joined: Wed Feb 19, 2014 11:09 pm

call short integer (Procedures) using an array of pointers

Post by hgcoon »

PureBasic is a very good and remarkable achievement - producing very fast compiled code. However, the documentation has proven very difficult for me to use... 1) I want to make about 300 small, usually simple, functions or procedures which operate on a small list of Global variables. 2) I want to make pointers to these procedures and store them in an array of pointers and then call the functions using the pointer array index to select the functions in arbitrary order. I have tried prototypes but find the the documentation a hopeless muddle that does not appear to deal with a problem like this. In other languages (including 3 Basics) where one simply gets a code pointer and stores it in the array and then calls the function using the array index to the appropriate pointer. Alas, I have not been able to find out how to do this in Pure Basic. Can it even be done in PureBasic? Which commands can take the name of the Procedure, make the code pointer and load the pointer array and then call the functions using the array subscript? I wish there were an "indirect function calling" article in the PB docs...
Any help with this problem would be very much appreciated. Thank you, hgc
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: call short integer (Procedures) using an array of pointe

Post by Danilo »

What about using CallFunctionFast? It is not type safe, considered un-safe coding style. PB has not much type checking anyway... ;)

Code: Select all

Procedure Proc_1() : Debug "Proc 1" : EndProcedure
Procedure Proc_2() : Debug "Proc 2" : EndProcedure
Procedure Proc_3() : Debug "Proc 3" : EndProcedure
Procedure Proc_4() : Debug "Proc 4" : EndProcedure

Dim func(10)
func(0)=@Proc_1()
func(1)=@Proc_2()
func(2)=@Proc_3()
func(3)=@Proc_4()

For i = 0 To 3
    CallFunctionFast( func(i) )
Next i

;----------------------------------

Procedure Proc_10(arg1.i,arg2.s) : Debug "Proc 10: "+arg1+" - "+arg2 : EndProcedure
Procedure Proc_20(arg1.i,arg2.s) : Debug "Proc 20: "+arg1+" - "+arg2 : EndProcedure
Procedure Proc_30(arg1.i,arg2.s) : Debug "Proc 30: "+arg1+" - "+arg2 : EndProcedure
Procedure Proc_40(arg1.i,arg2.s) : Debug "Proc 40: "+arg1+" - "+arg2 : EndProcedure

Prototype theFunc(arg1.i,arg2.s)

Dim f(10)
f(0)=@Proc_10()
f(1)=@Proc_20()
f(2)=@Proc_30()
f(3)=@Proc_40()

For i = 0 To 3
    p.theFunc = f(i)
    p( i, "Str("+Str(i)+")" )
Next i
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: call short integer (Procedures) using an array of pointe

Post by Demivec »

Code: Select all

;PureBasic is a very good and remarkable achievement - producing very fast compiled code.

; 1) I want to make about 300 small, usually simple, functions or procedures which operate on a small list of global variables.

Global a = 1
Procedure Proc_1() : a + 1: Debug "Proc 1(+1): " + a : EndProcedure
Procedure Proc_2() : a + 2: Debug "Proc 2(+2): " + a : EndProcedure
Procedure Proc_3() : a - 1: Debug "Proc 3(-1): " + a : EndProcedure
Procedure Proc_4() : a - 2: Debug "Proc 4(-2): " + a : EndProcedure

Prototype NoArgs_proto()
Structure NoArgs_protoStruc
  f.NoArgs_proto
EndStructure

; 2) I want to make pointers to these procedures and store them in an array of pointers and then call the functions using the
;  pointer array index to select the functions in arbitrary order.

Dim func.NoArgs_protoStruc(3)
func(0)\f=@Proc_1()
func(1)\f=@Proc_2()
func(2)\f=@Proc_3()
func(3)\f=@Proc_4()

Debug "---- Sequential order ----"
Debug "Starting value: " + a
For i = 0 To 3
    func(i)\f()
Next

Debug "---- Arbitrary order ----"
Debug "Starting value: " + a
For i = 0 To 9
    m = Random(ArraySize(func()))
    func(m)\f()
Next
hgcoon
New User
New User
Posts: 9
Joined: Wed Feb 19, 2014 11:09 pm

Re: call short integer (Procedures) using an array of pointe

Post by hgcoon »

Many thanks to Danilo and Demivec; I only wish that I still had the wit left to have extracted these syntaxes from the PBdocs -- especially the use of the back slash -- I had also gotten the impression from the docs that CallFunctionFast was used only for the Libraries and not for my own stuff. (I should have tried it.) A few more examples in the docs would have been helpful. You have solved my problems -- thank you again, you have made my month! hgc
Post Reply