GLBasic forum

Main forum => Announcements => Topic started by: Kitty Hello on 2009-Oct-08

Title: function pointers
Post by: Kitty Hello on 2009-Oct-08
OK, I've been messing with function pointers a bit. It was easier than I thought

Code (glbasic) Select

// 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:
Code (glbasic) Select

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
Title: Re: function pointers
Post by: Quentin on 2009-Oct-08
looks easy to handle, nice work.

even the  the codes are not readable in the new forum :( (had to copy it to the editor)
Title: Re: function pointers
Post by: codegit on 2009-Oct-09
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:
Title: Re: function pointers
Post by: metzzo on 2009-Oct-09
Thanks Gernot :)

That was hard work to persuade him to implement that cool Feature ;).
Title: Re: function pointers
Post by: Kitty Hello on 2009-Oct-09
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
Title: Re: function pointers
Post by: Hemlos on 2009-Oct-10
Hi Coolo,

What is the benefit of having a function pointer?
Title: Re: function pointers
Post by: metzzo on 2009-Oct-10
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.
Title: Re: function pointers
Post by: codegit on 2009-Oct-10
Will we be able to nest them in TYPES??  :whistle:
Title: Re: function pointers
Post by: Quentin on 2009-Oct-10
yes, also in TYPEs, take a look on the example in the command help.
Title: Re: function pointers
Post by: codegit on 2009-Oct-10
oops, I did not see that there is a new version.... my bad sorry.  :x
Title: Re: function pointers
Post by: Hemlos on 2009-Oct-10
Interesting, thanks for the reply.

So, this is alot like CALLBYNAME$() ?
Title: Re: function pointers
Post by: MrTAToad on 2009-Oct-11
Does this work with SUB's ?
Title: Re: function pointers
Post by: Kitty Hello on 2009-Oct-12
no, just functions. But a function w/o args equals a sub.
Title: Re: function pointers
Post by: Foo on 2009-Oct-13
This is so cool!!!  :good: :good: :good:

Now I can emulate complete classes :)
Title: Re: function pointers
Post by: Foo on 2009-Oct-14
I tried prototypes now, but I got an error:

Code (glbasic) Select
// --------------------------------- //
// 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?
Title: Re: function pointers
Post by: Hemlos on 2009-Oct-14
Hmm i put proto and the struct at top, but all the lines are the same, and it will print 'Foo'.


Code (glbasic) Select
// --------------------------------- //
// 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
Title: Re: function pointers
Post by: Foo on 2009-Oct-14
Got it - thanks :D
Now I can rember - in C++, whats behind GLBasic, protoype functions must be first defined to use them.
Title: Re: function pointers
Post by: Foo on 2009-Oct-14
Another Problem - Pointers do not work in functions:
Code (glbasic) Select

// --------------------------------- //
// 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
Title: Re: function pointers
Post by: Schranz0r on 2009-Oct-15
ohoh Gernot :/
Title: Re: function pointers
Post by: codegit on 2009-Oct-15
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...............
Code (glbasic) Select

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
Title: Re: function pointers
Post by: Kitty Hello on 2009-Oct-15
Fix 1 - you're not calling the pointer!
Code (glbasic) Select

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 ;)
Code (glbasic) Select

   ptr_init AS Init // = InitPlayer

Title: Re: function pointers
Post by: codegit on 2009-Oct-15

Fix 2 - not possible. That's a fantasy of Schranz0r ;)
Code (glbasic) Select

   ptr_init AS Init // = InitPlayer

[/quote]

:'( :'( :'( :'( :'( :'( :'( :'(
Title: Re: function pointers
Post by: Schranz0r on 2009-Oct-16
Thats soooooooooo true  :'( :'( :'( :'( :'( :'( :'( :'(