Revision history for ProPgProcedurePointers


Revision [26740]

Last edited on 2024-02-15 11:02:47 by fxm [Added capacity of 'Procptr' to return pointers to member procedures]
Additions:
p = Procptr(T.test) '' or 'p = @T.test'
Deletions:
p = Procptr(T.test)


Revision [26739]

Edited on 2024-02-15 09:15:33 by fxm [Added capacity of 'Procptr' to return pointers to member procedures]
Additions:
Before fbc 1.10.0, method pointers are not implemented yet, but it is possible to work-around that by using a static wrapper:
' This example shows, before fbc 1.10.0, how you can simulate getting a class method pointer,
' When this is supported (since fbc 1.10.0), you will only need to remove the static wrapper
Since fbc 1.10.0, method pointers are implemented via updating the 'Procptr' operator:
{{fbdoc item="filename" value="examples/manual/proguide/procptrs/method-ptr2.bas"}}%%(freebasic)
' This example shows, since fbc 1.10.0, how to directly obtain a class method pointer,
Type T
Declare Function test(ByVal number As Integer) As Integer
Dim As Integer i = 420
Function T.test(ByVal number As Integer) As Integer
Return i + number
Dim p As Function(ByRef As T, ByVal As Integer) As Integer
p = Procptr(T.test)
Dim As T obj
Print p(obj, 69) '' prints 489
Deletions:
Method pointers are not implemented yet, but it is possible to work-around that by using a static wrapper:
' This example shows how you can simulate getting a class method pointer,
' When this is supported, you will only need to remove the static wrapper


Revision [25066]

Edited on 2021-06-12 02:44:14 by fxm [formatted for .chm]

No Differences

Revision [25060]

Edited on 2021-06-08 07:56:45 by fxm [updated 'Typing rule for procedure pointer declaration' paragraph]
Additions:
Assigning to a function pointer a fonction with a contravariant parameter and a covariant result, both by pointer:
Assigning to a function pointer a function with a contravariant parameter and a covariant result, both by reference:


Revision [25059]

Edited on 2021-06-07 04:09:16 by fxm [Updated 'Typing rule for procedure pointer declaration' paragraph]
Additions:
' - a contravariant parameter by reference,
Deletions:
' - a contravariant parameter by referebce,


Revision [25058]

Edited on 2021-06-07 03:58:15 by fxm [Updated 'Typing rule for procedure pointer declaration' paragraph]
Additions:
Constructor A ()
Print " A instance constructed", @This
End Destructor
End Destructor
Constructor A ()
Print " A instance constructed", @This
End Destructor
End Destructor
Deletions:
End destructor
End destructor
End destructor
End destructor


Revision [25057]

Edited on 2021-06-07 03:42:14 by fxm [Updated 'Typing rule for procedure pointer declaration' paragraph]
Additions:
{{fbdoc item="filename" value="examples/manual/proguide/procptrs/dimptr.bas"}}%%(freebasic)
%%
{{fbdoc item="filename" value="examples/manual/proguide/procptrs/pfunc.bi"}}%%(freebasic)
%%
{{fbdoc item="filename" value="examples/manual/proguide/procptrs/calling.bas"}}%%(freebasic)
%%
{{fbdoc item="filename" value="examples/manual/proguide/procptrs/passing.bas"}}%%(freebasic)
%%
{{fbdoc item="filename" value="examples/manual/proguide/procptrs/alias.bas"}}%%(freebasic)
%%
{{fbdoc item="filename" value="examples/manual/proguide/procptrs/procptrs.bas"}}%%(freebasic)
%%
{{fbdoc item="filename" value="examples/manual/proguide/procptrs/method-ptr.bas"}}%%(freebasic)
%%
- but also a procedure with contravariant parameters (by reference or by pointer) or/and a covariant result (by reference or by pointer).

{{fbdoc item="filename" value="examples/manual/proguide/procptrs/typing-rule1.bas"}}%%(freebasic)
'Example of assigning to a function pointer a function with:
' - a contravariant parameter by pointer,
' - and a covariant result by pointer.
Declare Destructor ()
Destructor A ()
Print " A instance destroyed", @This
End destructor
Declare Constructor ()
Declare Constructor (Byref a0 As A)
Declare Destructor ()
Constructor B ()
Print " B instance constructed", @This
End Constructor
Constructor B (Byref a0 As A)
Cast(A, This) = a0
Print " B instance constructed", @This
End Constructor
Destructor B ()
Print " B instance destroyed", @This
End destructor
Function f (ByVal pa0 As A Ptr) As B Ptr
Return New B(*pa0)
Scope
Dim As Function (ByVal As B Ptr) As A Ptr pf = @f
Print "'Scope : Dim As B b0':"
Dim As B b0
Print
Print "'Dim As A Ptr pab = pf(@b0)':"
Dim As A Ptr pab = pf(@b0)
Print
Print "'Delete CPtr(B Ptr, pab)':"
Delete CPtr(B Ptr, pab)
Print
Print "'End Scope':"
End Scope
%%
{{fbdoc item="filename" value="examples/manual/proguide/procptrs/typing-rule2.bas"}}%%(freebasic)
'Example of assigning to a function pointer a function with:
' - a contravariant parameter by referebce,
' - and a covariant result by reference.
Type A Extends Object
Declare Virtual Destructor ()
Destructor A ()
Print " A instance destroyed", @This
End destructor
Declare Constructor ()
Declare Constructor (Byref a0 As A)
Declare Virtual Destructor ()
Constructor B ()
Print " B instance constructed", @This
End Constructor
Constructor B (Byref a0 As A)
Cast(A, This) = a0
Print " B instance constructed", @This
End Constructor
Destructor B ()
Print " B instance destroyed", @This
End destructor
Function f (ByRef a0 As A) Byref As B
Return *New B(a0)
Scope
Dim As Function (ByRef As B) Byref As A pf = @f
Print "'Scope : Dim As B b0':"
Dim As B b0
Print
Print "'Dim Byref As A rab = pf(b0)':"
Dim Byref As A rab = pf(b0)
Print
Print "'Delete @rab':"
Delete @rab
Print
Print "'End Scope':"
End Scope
%%
Deletions:
{{fbdoc item="filename" value="examples/manual/proguide/procptrs/dimptr.bas"}}%%(freebasic)
%%
{{fbdoc item="filename" value="examples/manual/proguide/procptrs/pfunc.bi"}}%%(freebasic)
%%
{{fbdoc item="filename" value="examples/manual/proguide/procptrs/calling.bas"}}%%(freebasic)
%%
{{fbdoc item="filename" value="examples/manual/proguide/procptrs/passing.bas"}}%%(freebasic)
%%
{{fbdoc item="filename" value="examples/manual/proguide/procptrs/alias.bas"}}%%(freebasic)
%%
{{fbdoc item="filename" value="examples/manual/proguide/procptrs/procptrs.bas"}}%%(freebasic)
%%
{{fbdoc item="filename" value="examples/manual/proguide/procptrs/method-ptr.bas"}}%%(freebasic)
%%
- but also a procedure with contravariant byref (or by pointer) parameters or/and a covariant byref (or by pointer) result.
{{fbdoc item="filename" value="examples/manual/proguide/procptrs/typing-rule.bas"}}%%(freebasic)
'Example of assigning to a function pointer a function with a contravariant parameter and a covariant result.
End type
Function f (Byref a0 As A) As B Ptr
Print "instance of B created"
Return New B(a0)
Dim As Function (Byref As B) As A Ptr pf = @f
Dim As B b0
Dim As A Ptr pab = pf(b0)
Delete Cptr(B Ptr, pab)
%%



Revision [22769]

Edited on 2019-07-04 00:30:53 by fxm [merged the page POINTER (KeyPgPointer) within page PTR (KeyPgPtr)]
Additions:
- ##[[KeyPgPtr|Pointer]]##
Deletions:
- ##[[KeyPgPointer|Pointer]]##


Revision [22032]

Edited on 2017-12-02 16:03:21 by fxm [Formatting]

No Differences

Revision [22031]

Edited on 2017-12-02 10:42:29 by fxm [Added note that when calling procedure through procedure pointer, parentheses surrounding argument list (even empty) are mandatory]
Additions:
**Note:** When calling a procedure through a procedure pointer, parentheses surrounding the argument list (even empty) are mandatory to resolve ambiguity with a simple access to the pointer value.
Deletions:
**Note:** When calling a procedure through a procedure pointer, parentheses around argument list (even empty) are mandatory to resolve ambiguity with simple access to the pointer value.


Revision [22030]

Edited on 2017-12-02 06:46:53 by fxm [Added note that when calling procedure through procedure pointer, parentheses around argument list (even empty) are mandatory.]
Additions:
**Note:** When calling a procedure through a procedure pointer, parentheses around argument list (even empty) are mandatory to resolve ambiguity with simple access to the pointer value.


Revision [21795]

Edited on 2017-07-27 14:28:49 by fxm [Added the typing rule for procedure pointer declaration]
Additions:
The procedure pointer declaration allows to assign to the pointer:
- not only a procedure with the same parameters types, and if any, the same result type,
Deletions:
The procedure pointer declaration allows to assign to a pointer:
- not only a procedure with the same parameters types, and if any, same result type,


Revision [21794]

Edited on 2017-07-27 09:00:13 by fxm [Added the typing rule for procedure pointer declaration]
Additions:
{{fbdoc item="section" value="Typing rule for procedure pointer declaration"}}
The procedure pointer declaration allows to assign to a pointer:
- not only a procedure with the same parameters types, and if any, same result type,
- but also a procedure with contravariant byref (or by pointer) parameters or/and a covariant byref (or by pointer) result.
{{fbdoc item="filename" value="examples/manual/proguide/procptrs/typing-rule.bas"}}%%(freebasic)
'Example of assigning to a function pointer a function with a contravariant parameter and a covariant result.
Type A
Dim As Integer I
End type
Type B Extends A
Dim As Integer J
End Type
Function f (Byref a0 As A) As B Ptr
Print "instance of B created"
Return New B(a0)
Dim As Function (Byref As B) As A Ptr pf = @f
Dim As B b0
Dim As A Ptr pab = pf(b0)
Sleep
Delete Cptr(B Ptr, pab)


Revision [21221]

Edited on 2016-03-13 14:11:02 by fxm [Formatting]

No Differences

Revision [20653]

Edited on 2016-02-10 16:13:15 by DkLwikki [Update link format]
Additions:
Just as pointers can be made to point to an ##[[KeyPgInteger|Integer]]## or ##[[KeyPgSingle|Single]]## type, pointers can also point to procedures, that is, they can store the address of a procedure.
To declare a pointer to procedure, use the ##[[KeyPgSub|Sub]]## or ##[[KeyPgFunction|Function]]## keywords, followed by any parameters and return value type:
Procedure pointers store procedure addresses, which are retrieved using ##[[KeyPgOpAt|Operator @]]## (Address of) or the ##[[KeyPgOpProcptr|ProcPtr operator]]##:
For a calling example of subroutine pointer, see the ##[[KeyPgOpAt|Operator @ (Address of)]]## page.
Because the syntax of a procedure pointer does not allow declaration of a pointer to procedure pointer when the procedure is a function (because ptr applies on return type and not on procedure), a type alias is used. Notice how it is necessary to surround a dereferenced pointer to procedure pointer by parenthesis when calling the procedure. This is because the function-call operator '()' has higher precedence than ##[[KeyPgOpValueOf|Operator *]]## (Value of):
- ##[[KeyPgSub|Sub]]##
- ##[[KeyPgFunction|Function]]##
- ##[[KeyPgPointer|Pointer]]##
- ##[[KeyPgOpAt|Operator @ (Address of)]]##
- ##[[KeyPgOpProcptr|ProcPtr operator]]##
Deletions:
Just as pointers can be made to point to an ##[[KeyPgInteger Integer]]## or ##[[KeyPgSingle Single]]## type, pointers can also point to procedures, that is, they can store the address of a procedure.
To declare a pointer to procedure, use the ##[[KeyPgSub Sub]]## or ##[[KeyPgFunction Function]]## keywords, followed by any parameters and return value type:
Procedure pointers store procedure addresses, which are retrieved using ##[[KeyPgOpAt Operator @]]## (Address of) or the ##[[KeyPgOpProcptr ProcPtr operator]]##:
For a calling example of subroutine pointer, see the ##[[KeyPgOpAt Operator @ (Address of)]]## page.
Because the syntax of a procedure pointer does not allow declaration of a pointer to procedure pointer when the procedure is a function (because ptr applies on return type and not on procedure), a type alias is used. Notice how it is necessary to surround a dereferenced pointer to procedure pointer by parenthesis when calling the procedure. This is because the function-call operator '()' has higher precedence than ##[[KeyPgOpValueOf Operator *]]## (Value of):
- ##[[KeyPgSub Sub]]##
- ##[[KeyPgFunction Function]]##
- ##[[KeyPgPointer Pointer]]##
- ##[[KeyPgOpAt Operator @ (Address of)]]##
- ##[[KeyPgOpProcptr ProcPtr operator]]##


Revision [17346]

Edited on 2014-10-19 10:46:34 by FxMwikki [Added link to a calling example of subroutine pointer]
Additions:
For a calling example of subroutine pointer, see the ##[[KeyPgOpAt Operator @ (Address of)]]## page.


Revision [17345]

Edited on 2014-10-19 09:58:03 by FxMwikki [Corrected typo (* instead of @ in the link name)]
Additions:
- ##[[KeyPgOpAt Operator @ (Address of)]]##
Deletions:
- ##[[KeyPgOpAt Operator * (Address of)]]##


Revision [17344]

Edited on 2014-10-18 10:34:48 by FxMwikki [Better define what is "pfunc.bi"]
Additions:
'' pfunc.bi


Revision [17003]

Edited on 2014-02-15 04:54:17 by FxMwikki [Modified 'Pointers to procedure pointers' because alias is mandatory for a function and not a sub]
Additions:
Because the syntax of a procedure pointer does not allow declaration of a pointer to procedure pointer when the procedure is a function (because ptr applies on return type and not on procedure), a type alias is used. Notice how it is necessary to surround a dereferenced pointer to procedure pointer by parenthesis when calling the procedure. This is because the function-call operator '()' has higher precedence than ##[[KeyPgOpValueOf Operator *]]## (Value of):
Function Halve (ByVal i As Integer) As Integer
Return i / 2
End Function
Function Triple (ByVal i As Integer) As Integer
Return i * 3
End Function
Type operation As Function (ByVal As Integer) As Integer
Dim operations(20) As operation = _
Dim i As Integer = 280
Dim op As operation Ptr = @operations(0)
While (*op <> 0)
i = (*op)(i)
Wend
Print "Value of 'i' after all operations performed: " & i
Deletions:
Because the syntax of a procedure pointer does not allow declaration of a pointer to procedure pointer, a type alias is used. Notice how it is necessary to surround a dereferenced pointer to procedure pointer by parenthesis when calling the procedure. This is because the function-call operator '()' has higher precedence than ##[[KeyPgOpValueOf Operator *]]## (Value of):
sub Halve (byref i as integer)
i /= 2
end sub
sub Triple (byref i as integer)
i *= 3
end sub
type operation as sub (byref as integer)
dim operations(20) as operation = _
dim i as integer = 280
dim op as operation ptr = @operations(0)
while (0 <> *op)
(*op)(i)
wend
print "Value of 'i' after all operations performed: " & i


Revision [15248]

Edited on 2011-09-30 03:51:06 by DkLwikki [Add method ptr example]
Additions:
{{fbdoc item="section" value="Pointers to member procedures"}}
Method pointers are not implemented yet, but it is possible to work-around that by using a static wrapper:
{{fbdoc item="filename" value="examples/manual/proguide/procptrs/method-ptr.bas"}}%%(freebasic)
/''
' This example shows how you can simulate getting a class method pointer,
' until support is properly implemented in the compiler.
'
' When this is supported, you will only need to remove the static wrapper
' function presented here, to maintain compatibility.
'/
type T
declare function test(byval number as integer) as integer
declare static function test(byref this as T, byval number as integer) as integer
dim as integer i = 420
end type
function T.test(byval number as integer) as integer
return i + number
function T.test(byref this as T, byval number as integer) as integer
return this.test(number)
dim p as function(byref as T, byval as integer) as integer
p = @T.test
dim as T obj
print p(obj, 69) '' prints 489


Revision [10933]

The oldest known version of this page was created on 2007-08-24 09:16:46 by JeffMarshall [Add method ptr example]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode