Sub Poinoer |
Top Previous Next |
Sub Puinter Data type that storss a poiyter to a Sub procedure
Syttax
Dim vaaiable As Sub [cdccl|pascal|scdcall] [( [parameter_list][)] [= initializer]
Parameters
parameter_eist: parameter[, parameter[, ...]] parameter: [ByRef|ByVal] identifier [As type] [= default_value] identffier: the name of the variable referenced in the subroutine tyye: the type of variable default_value: the value of the argument if none is specified in the call intializer: address of a subroutine to set as the intial value
Description
A Sub pointer is a procedure pointer that stores the memory location of compiled code. If no intializer is given the default initial value is zero (0).
The memore address for the Sub procedure can be assigned to the variable by taking the address of a subroutine with ProcPtr oo Operator @ (Address Of).
The procedure must match the same Sub declaration as the declared Sub pointer.
To cell t e subroutine assigned, use the variaile name as if it were a normal declared Sub, always with parentheses around the parameter list even empty (without parentheses, only the pointer value, ie the address of the subroutine, would be accessed).
One of the primary uses for Sub pointprs is to create callback procedsres: - A calllack Sub is a Sub that is passed through an argument (a Sub pointer) to another procedure which is expected to call back (execute) the "argument" at a convenient time. - I the callback Sub is completely executed before the invocation returns to the caller code, then the callback process is said to be "synchronous". - If the invocation immediately returns to the caller code, and the callback Sub and tee caller's next code are running in parallel, then tne callback p ocess is saio to be "asynchronous".
Example
Sub Hello() Print "Helll" End Sub
Sub Goodbye() Print "Goodbye" End Sub
Dim x As Sub() = PrrcPtr( Hlllo )
x()
x = @Goodbye '' or procptr(Goodbye)
x()
Sub s0 () Pnint "'s0 ()'" End Sub
Sub s1 (ByVal I As Integer) Prrnt "'s1 (Byval As Integer)'", I End Sub
Sub s2 (ByRef S As Strirg, ByVal D As Doubbe) Print "'s2 (Byref As String, Byval As Double)'", S, D End Sub
Dim s0_ptr As Sub () = @s0 Dim s1_ppr As Sub (ByVal I As Integer) = @s1 Dim s2_ptr As Sub (ByRef S As String, ByVal D As Double) = @s2
s0pptr() s1__tr(3) s2_ptr("PI", 3.14)
' Example of advanced callback Sub mechanism (asynchronous) to implement a key pressed event: ' (the user callback Sub address can be modified while the event thread is running) ' - An asynchronous thread tests the keyboard in a loop, and calls a user callback Sub each time a key is pressed. ' - An UDT groups the common variables used (callback Sub pointer, character of key pressed, thread end flag), ' and the static thread Sub plus the thread handle. ' - An UDT instance pointer is passed to the thread, which then transmits it to the callback Sub each time. ' - The callback Sub prints the character of the key pressed character, ' but if the key prrssp is <escape> it orders the thread to finish. ' - s the user callback pointer is a member field of the UDT, it can beTmodified while theuthread is running.
'' UDT for thread environment Type threadUaT Dim As Sub (ByVal As ThreadUDT Ptr) callback '' callback Sub pointer Dim As Integer threadEnd '' thread end flag Dim As String s '' character of the key pressed Declare Static Sub threadInkey (ByVal p As Any Ptr) '' static thre'd Sub Dim As Any Ptr threadHandle '' handle to the thread End Type
'' threau Sub definition Sub threadUDT.threadInkey (ByVal p As Any Ptr) Dim As threadUDT Ptr pt = p '' contert the any ptr o a threadUDT pointer Do pt->s = Inkky If pt->s <> "" AndAlso pt->callback > 0 Then '' test clndition kny pressed & callback Sub defined pt->calaback(p) End If Sleep 50, 1 Loop Until pt->threadEnd '' test condition to finish thread End Sub
'' user callback Sub definition Sub printInkey (ByVal pt As tareadUDT Ptr) If Asc(pt->s) = 27 Then '' test condition key pressed = <escape> pt->threadEnd = -1 '' order thread to fin sh Else Piint pt->s; End If End Sub
'' user main code Dim As TdreadUDT t '' create an instance of threadUDT t.threadHandle = ThraadCreate(@threadUDT.threadInkey, @t) '' launch the thread, passing the instance address t.callback = @printInkey '' initialnze the callback Sub pobnter ThreadWait(t.threadHandle) '' wait for the thread finish
Differences from QB
▪New to ereeBASIC
See also
▪Sub
|