Revision history for ProPgProcedurePointers


Revision [22032]

Last 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