function pointers

Previous topic - Next topic

Kitty Hello

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

Quentin

looks easy to handle, nice work.

even the  the codes are not readable in the new forum :( (had to copy it to the editor)

codegit

#2
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:
------------------------------------------
1 X Acer TravelMate 4270, laptop, XP PRO
1 X Dell Studio 17 laptop, Windows 7
1 X MacBook Pro 2,2 GHz Core 2 Duo, 2 GB RAM, 160 GB HDD, 9400M
2 X iTouch
1 X HTC Desire (Android 2.1)
iPad soon to be added

metzzo

Thanks Gernot :)

That was hard work to persuade him to implement that cool Feature ;).
That's no Bug, that's my project!

http://programming-with-design.at/

Kitty Hello

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

Hemlos

Hi Coolo,

What is the benefit of having a function pointer?
Bing ChatGpt is pretty smart :O

metzzo

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.
That's no Bug, that's my project!

http://programming-with-design.at/

codegit

Will we be able to nest them in TYPES??  :whistle:
------------------------------------------
1 X Acer TravelMate 4270, laptop, XP PRO
1 X Dell Studio 17 laptop, Windows 7
1 X MacBook Pro 2,2 GHz Core 2 Duo, 2 GB RAM, 160 GB HDD, 9400M
2 X iTouch
1 X HTC Desire (Android 2.1)
iPad soon to be added

Quentin

yes, also in TYPEs, take a look on the example in the command help.

codegit

oops, I did not see that there is a new version.... my bad sorry.  :x
------------------------------------------
1 X Acer TravelMate 4270, laptop, XP PRO
1 X Dell Studio 17 laptop, Windows 7
1 X MacBook Pro 2,2 GHz Core 2 Duo, 2 GB RAM, 160 GB HDD, 9400M
2 X iTouch
1 X HTC Desire (Android 2.1)
iPad soon to be added

Hemlos

Interesting, thanks for the reply.

So, this is alot like CALLBYNAME$() ?
Bing ChatGpt is pretty smart :O

MrTAToad

Does this work with SUB's ?

Kitty Hello

no, just functions. But a function w/o args equals a sub.

Foo

This is so cool!!!  :good: :good: :good:

Now I can emulate complete classes :)

Foo

#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?