Allow pointer-defined variables to be callable

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Allow pointer-defined variables to be callable

Post by Mistrel »

Currently we have CallFunctionFast() for calling a pointer to a function. But is this really necessary? I think it would be great if we could just shorthand call any variable defined as a pointer:

As it is currently:

Code: Select all

Procedure PrintTest()
  Debug "Test"
EndProcedure

*a=@PrintTest()

CallFunctionFast(*a)
How it could be:

Code: Select all

Procedure PrintTest()
  Debug "Test"
EndProcedure

*a=@PrintTest()

*a()
User avatar
NicTheQuick
Addict
Addict
Posts: 1227
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Allow pointer-defined variables to be callable

Post by NicTheQuick »

You can use Prototypes.

Code: Select all

Prototype.i testProc()

Procedure PrintTest()
  Debug "Test"
EndProcedure

*a.testProc =@PrintTest()

*a()
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Allow pointer-defined variables to be callable

Post by mk-soft »

:wink:

Code: Select all

-TOP

Procedure fc0()
  Debug "Func 0"
EndProcedure

Procedure fc1()
  Debug "Func 1"
EndProcedure

Procedure fc2()
  Debug "Func 2"
EndProcedure

Procedure fc3()
  Debug "Func 3"
EndProcedure

Prototype protoInvoke()

Structure udtInvoke
  Invoke.protoInvoke
EndStructure

Dim fc.udtInvoke(3)
fc(0)\Invoke = @fc0()
fc(1)\Invoke = @fc1()
fc(2)\Invoke = @fc2()
fc(3)\Invoke = @fc3()

For n = 1 To 4
  For i = 0 To 3
    fc(i)\Invoke()
  Next
  Delay(500)
Next
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Allow pointer-defined variables to be callable

Post by Mistrel »

I know how to use Prototypes. The entire premise of this request is to provide a shorthand for CallFunctionFast() where I don't have to declare ahead of time what arguments are to be passed.
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: Allow pointer-defined variables to be callable

Post by nco2k »

i have a better idea, lets remove that outdated CallFunction(Fast) garbage. 8)

c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Allow pointer-defined variables to be callable

Post by kenmo »

Mistrel wrote:a shorthand for CallFunctionFast() where I don't have to declare ahead of time what arguments are to be passed.
I understand your request.

The problem I see is:
CallFunctionFast() and Prototypes tell the compiler how many parameters to pass to the function.
But *a is just an integer that points to a memory location.
Calling *a(...), the compiler doesn't know how many parameters or their types.
If you call *a(5000), does it pass 5000 as a Word, Integer, Quad?
Sounds like a source of stack corruption problems.


Unless you are saying, only allow this shorthand for calls with 0 parameters?
User avatar
NicTheQuick
Addict
Addict
Posts: 1227
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Allow pointer-defined variables to be callable

Post by NicTheQuick »

@kenmo
Correct. The Compiler doesn't know about the parameters. So he can not compile the correct code for a function call to a function pointer which value will be set at runtime.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Post Reply