Revision history for KeyPgSubPtr


Revision [22458]

Last edited on 2018-09-02 23:40:22 by JeffMarshall [name case fix-up]
Additions:
The memory address for the ##[[KeyPgSub|Sub]]## procedure can be assigned to the variable by taking the address of a subroutine with ##[[KeyPgOpProcptr|ProcPtr]]## or ##[[KeyPgOpAt|Operator @ (Address of)]]##.
- ##[[KeyPgOpProcptr|ProcPtr]]##
Deletions:
The memory address for the ##[[KeyPgSub|Sub]]## procedure can be assigned to the variable by taking the address of a subroutine with ##[[KeyPgOpProcPtr|ProcPtr]]## or ##[[KeyPgOpAt|Operator @ (Address of)]]##.
- ##[[KeyPgOpProcPtr|ProcPtr]]##


Revision [22349]

Edited on 2018-07-21 15:36:53 by fxm [Added an intermediate example]
Additions:
Sub s0 ()
Print "'s0 ()'"
End Sub
Sub s1 (Byval I As Integer)
Print "'s1 (Byval As Integer)'", I
End Sub
Sub s2 (Byref S As String, Byval D As Double)
Print "'s2 (Byref As String, Byval As Double)'", S, D
End Sub
Dim s0_ptr As Sub () = @s0
Dim s1_ptr As Sub (Byval I As Integer) = @s1
Dim s2_ptr As Sub (Byref S As String, Byval D As Double) = @s2
s0_ptr()
s1_ptr(3)
s2_ptr("PI", 3.14)
{{fbdoc item="filename" value="examples/manual/datatype/subptr3.bas"}}%%(freebasic)


Revision [22348]

Edited on 2018-07-21 13:20:06 by fxm [Added an example of callback mechanism using a Sub pointer]
Additions:
{{fbdoc item="filename" value="examples/manual/datatype/subptr.bas"}}%%(freebasic)
{{fbdoc item="filename" value="examples/manual/datatype/subptr2.bas"}}%%(freebasic)
' Example of advanced callback Sub mechanism 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 pressed is <escape> it orders the thread to finish.
' - As the user callback pointer is a member field of the UDT, it can be modified while the thread is running.
'' UDT for thread environment
Type threadUDT
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 thread Sub
Dim As Any Ptr threadHandle '' handle to the thread
End Type
'' thread Sub definition
Sub threadUDT.threadInkey (Byval p As Any Ptr)
Dim As threadUDT Ptr pt = p '' convert the any ptr to a threadUDT pointer
Do
pt->s = Inkey
If pt->s <> "" Andalso pt->callback > 0 Then '' test condition key pressed & callback Sub defined
pt->callback(p)
End If
Sleep 50
Loop Until pt->threadEnd '' test condition to finish thread
End Sub
'' user callback Sub definition
Sub printInkey (Byval pt As threadUDT Ptr)
If Asc(pt->s) = 27 Then '' test condition key pressed = <escape>
pt->threadEnd = -1 '' order thread to finish
Print
Else
Print pt->s;
End If
End Sub
'' user main code
Dim As ThreadUDT t '' create an instance of threadUDT
t.threadHandle = Threadcreate(@threadUDT.threadInkey, @t) '' launch the thread, passing the instance address
t.callback = @printInkey '' initialize the callback Sub pointer
Threadwait(t.threadHandle) '' wait for the thread finish
Deletions:
{{fbdoc item="filename" value="examples/manual/datatype/funcptr.bas"}}%%(freebasic)


Revision [22345]

Edited on 2018-07-19 01:26:20 by fxm [Added sentence about use of procedure pointers to create callback procedures]
Additions:
One of the primary uses for procedure pointers is to create callback procedures. A callback procedure is a procedure created in the user program that is called by another procedure either from the user own code space or from an external library.


Revision [22339]

The oldest known version of this page was created on 2018-06-28 10:46:14 by JeffMarshall [Added sentence about use of procedure pointers to create callback procedures]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode