OK, I've been messing with function pointers a bit. It was easier than I thought
// this is not important to you - just my stuff
template<class prototype> struct GLB_PROTO
{
prototype ptr;
GLB_PROTO() : ptr(NULL){}
inline prototype operator=( prototype p) {ptr = p; return ptr;}
inline operator prototype()
{
if(!ptr) exit(1);
return ptr;
}
};
#define PROTODECL(proto, retval, args) typedef retval(* __glb_ ##proto) args; typedef GLB_PROTO< __glb_ ##proto> proto;
// OK, this is a real function, taking 2 integers, returning the sum
int real(int a, int b)
{
return a+b;
}
// here I declare a function pointer type, returning an integer, taking 2 integers as argument and name it "ptr_2_foo"
PROTODECL(ptr_2_foo, int, (int, int));
int main()
{
// Make 2 variables of type "pointer to function"
ptr_2_foo foo, bar;
// really assign one
foo = real;
// call the good one
printf("%d\n", foo(1,1) );
// call the bad one (quits program)
printf("%d\n", bar(1,1) );
return 0;
}
What does that mean to you?
Next version will allow to do something like this:
PROTOTYPE foo% : bar%
LOCAL goo AS foo
goo = realone
goo(5) // call "realone" with argument "5"
// now watch this:
sophisticated(realone)
sophisticated(realtwo)
// a real function, matching the prototype so you can call the variable pointing to it
FUNCTION realone%: arg1%
STDOUT "one: "+arg1 + "\n"
ENDFUNCTION
FUNCTION realtwo%: arg1%
STDOUT "two: "+arg1+"\n"
ENDFUNCTION
FUNCTION sophisticated: call AS foo
call(5)
ENDFUNCTION
Beta material, subject to change
looks easy to handle, nice work.
even the the codes are not readable in the new forum :( (had to copy it to the editor)
Function pointers.....YES, YES, YES. Thank you, I will have one of these. Jokes aside, this will be a really cooooool add on and something I wish we had earlier when I was designing my sprite engine for the iTouch :good:
Thanks Gernot :)
That was hard work to persuade him to implement that cool Feature ;).
Quote from: coolo on 2009-Oct-09
That was hard work to persuade him to implement that cool Feature ;).
You have no idea. Ask Trucidare how long it took for the MAc compiler. THAT was hard work to get me persuaded. :P
Hi Coolo,
What is the benefit of having a function pointer?
There are a lot!
If you use a Script Engine, you can use the Function Pointers, and you don't need that ugly "Select Case" Monsters.
There are also many Libs and DLLs that need Function Pointers.
For a GUI System it is really usefull for the Event System.
And much more.
Will we be able to nest them in TYPES?? :whistle:
yes, also in TYPEs, take a look on the example in the command help.
oops, I did not see that there is a new version.... my bad sorry. :x
Interesting, thanks for the reply.
So, this is alot like CALLBYNAME$() ?
Does this work with SUB's ?
no, just functions. But a function w/o args equals a sub.
This is so cool!!! :good: :good: :good:
Now I can emulate complete classes :)
I tried prototypes now, but I got an error:
// --------------------------------- //
// Project: test-function-pointer
// Start: Wednesday, October 14, 2009
// IDE Version: 7.142
// SETCURRENTDIR("Media") // seperate media and binaries?
LOCAL f AS Foo
f.echoNameMethod = echoName
// Main
f.name$ = "Foo"
f.echoNameMethod(f) // Line 16
SHOWSCREEN
KEYWAIT
TYPE Foo
name$ = ""
echoNameMethod AS protoEchoName
ENDTYPE
PROTOTYPE protoEchoName: self AS Foo
FUNCTION echoName: self AS Foo
PRINT self.name$, 10, 10
ENDFUNCTION
Quote_______________________________________
*** Configuration: WIN32 ***
precompiling:
GPC - GLBasic Precompiler V.7.098 SN:46716f63 - 3D, NET
"test-function-pointer.gbas"(16) error : call to undefined function : Foo
What's wrong with my code?
Hmm i put proto and the struct at top, but all the lines are the same, and it will print 'Foo'.
// --------------------------------- //
// Project: test-function-pointer
// Start: Wednesday, October 14, 2009
// IDE Version: 7.142
// SETCURRENTDIR("Media") // seperate media and binaries?
PROTOTYPE protoEchoName: self AS Foo
TYPE Foo
name$ = ""
echoNameMethod AS protoEchoName
ENDTYPE
LOCAL f AS Foo
f.echoNameMethod = echoName
// Main
f.name$ = "Foo"
f.echoNameMethod(f) // Line 16
SHOWSCREEN
KEYWAIT
FUNCTION echoName: self AS Foo
PRINT self.name$, 10, 10
ENDFUNCTION
Got it - thanks :D
Now I can rember - in C++, whats behind GLBasic, protoype functions must be first defined to use them.
Another Problem - Pointers do not work in functions:
// --------------------------------- //
// Project: test-function-pointer
// Start: Wednesday, October 14, 2009
// IDE Version: 7.142
LIMITFPS 10
// SETCURRENTDIR("Media") // seperate media and binaries?
PROTOTYPE protoEchoName: self AS Foo
TYPE Foo
x = 0
y = 0
name$ = ""
echoNameMethod AS protoEchoName
ENDTYPE
LOCAL f AS Foo
LOCAL f2 AS Foo
LOCAL list[] AS Foo
f.x = 10
f.y = 10
f2.x = 10
f2.y = 20
f2.name$ = "Foo 2"
f.echoNameMethod = echoName
f2.echoNameMethod = echoName
DIMPUSH list[], f
DIMPUSH list[], f2
ALIAS ff AS list[0]
ALIAS ff2 AS list[1]
// Main
WHILE TRUE
PRINT "Test...", 10, 30
ff.name$ = "Foo " + RND(100)
echoAll(list[])
// Works
// FOREACH e IN list[]
// e.echoNameMethod(e) // Line 16
// NEXT
SHOWSCREEN
WEND
SHOWSCREEN
KEYWAIT
FUNCTION echoName: self AS Foo
//
self.echoName(self) // LINE 59 error : call to undefined function : Foo
///PRINT self.name$, self.x, self.y
ENDFUNCTION
FUNCTION echoAll: list[] AS Foo
FOREACH e IN list[]
e.echoNameMethod(e)
NEXT
ENDFUNCTION
Quote_______________________________________
*** Configuration: WIN32 ***
precompiling:
GPC - GLBasic Precompiler V.7.098 SN:46716f63 - 3D, NET
"test-function-pointer.gbas"(59) error : call to undefined function : Foo
ohoh Gernot :/
My hat goes off to Gernot for giving us function pointers. I think one of the problems is that everybody is going to want to use them in a different way. I would like to implement them similar to methods in objects. (Basic with a bit of OOP) :whistle: Actually, if I remember correctly Schranz0r asked for exactly this way of using function pointers as well. :good:
Like this...............
PROTOTYPE Init: x, y, graphic
TYPE TPlayer
x; y
graphic
ptr_init AS Init = InitPlayer
ENDTYPE
GLOBAL player AS TPlayer
// ------------------------------------------------------------- //
// --- INITPLAYER ---
// ------------------------------------------------------------- //
FUNCTION InitPlayer: px, py, pgraphic
PRINT "x:" + px + "y:" + py + "Graphic:" + pgraphic, 0, 0
ENDFUNCTION // INITPLAYER
LOL..... Mr DRAWSPRITE =D =D
Fix 1 - you're not calling the pointer!
FUNCTION echoName: self AS Foo
// self.echoName(self) // LINE 59 error : call to undefined function : Foo
self.echoNameMethod(self)
ENDFUNCTION
Fix 2 - not possible. That's a fantasy of Schranz0r ;)
ptr_init AS Init // = InitPlayer
Fix 2 - not possible. That's a fantasy of Schranz0r ;)
ptr_init AS Init // = InitPlayer
[/quote]
:'( :'( :'( :'( :'( :'( :'( :'(
Thats soooooooooo true :'( :'( :'( :'( :'( :'( :'( :'(