digitalmars.D - Delegate or function pointer overloading?
- Chad J (40/40) Jan 11 2006 So I have a bunch of function pointers, all with the same name:
- Jarrett Billingsley (31/34) Jan 11 2006 How about something like:
- Chad J (8/56) Jan 11 2006 Alright I tried it out and it ran well. I tested this against direct
So I have a bunch of function pointers, all with the same name: public void function( ubyte[], ubyte[] ) padds; public void function( ubyte[], ubyte ) padds; public void function( ushort[], ushort[] ) padds; public void function( ushort[], ushort ) padds; public void function( byte[], byte[] ) padds; public void function( byte[], byte ) padds; public void function( short[], short[] ) padds; public void function( short[], short ) padds; Yeah, yeah... compiler errors, padds conflicts with padds. Not what I want to see. :( What I want to do is be able to do something like this: ubyte[] ArrayOne = new ubyte[32]; ubyte[] ArrayTwo = new ubyte[32]; padds( ArrayOne, ArrayTwo ); and have it use the correct function pointer, which will be pointing to one of two (or more) overloaded functions. And I'd like to use function pointers because I don't want to do something like: public void padds( ubyte[] lvalue, ubyte[] rvalue ) { if ( mmxSupported ) padds_MMX( lvalue, rvalue ); else padds_NoMMX( lvalue, rvalue ); } Which seems like it would slow the code down unnecessarily, especially if I add in other function paths like padds_whatever and have a switch-case or a string of else-ifs. I'd rather line up the various padds overloads to the correct padds_MMX and padds_NoMMX as the program starts to run, if I can help it. I tried renaming all of the padds function pointers to padds1, padds2, padds3, padds4, etc. That way I could reference the individual pointers and point them to the correct functions at runtime. Problem is, it is rather inconvenient to have to remember which one padds1 is (even if it was named better), so I tried aliasing them all onto padds. Still gave errors. I even tried giving each function pointer its own module, that way I could make a reference like paddusb.padds = &padds_MMX; while still being able to overload padds, but that still gave problems. So I was wondering if there is a way around this so that I can still overload padds while taking advantage of function pointers, or if this is impossible, why it is so?
Jan 11 2006
"Chad J" <gamerchad2.no-ip.org/email.txt LeftOfAtSignForEmail.meh> wrote in message news:dq2o02$1598$1 digitaldaemon.com...So I was wondering if there is a way around this so that I can still overload padds while taking advantage of function pointers, or if this is impossible, why it is so?How about something like: static this() { // determine CPU type with CPUID // padds_ubauba means padds(ubyte[], ubyte[]) if(mmxSupported) padds_ubauba = &padds_ubaubaMMX; else padds_ubauba = &padds_ubaubaNoMMX; // ... and so on, for all the other ones } void padds_ubaubaMMX(ubyte[] l, ubyte[] r) { // MMX implementation } void padds_ubabuaNoMMX(ubyte[] l, ubyte[] r) { // non-MMX implementation } void function(ubyte[], ubyte[]) padds_ubauba; void padds(ubyte[] l, ubyte[] r) { padds_ubauba(l, r); } This way, (1) The correct function is selected at module initialization, by setting padds_ubauba to point to the correct version of the padds_ubaubaXXX function. No determining which function to use every loop. (2) padds() can now be overloaded to take other param types. (3). padds() is so simple that the compiler will probably inline it, making it even faster.
Jan 11 2006
Jarrett Billingsley wrote:"Chad J" <gamerchad2.no-ip.org/email.txt LeftOfAtSignForEmail.meh> wrote in message news:dq2o02$1598$1 digitaldaemon.com...Alright I tried it out and it ran well. I tested this against direct function calls and there was little if any performance difference. Direct calls seemed about 3% faster, but that could have easily been noise in my test data. As an added bonus, I didn't have to do anything like padds_ubaubaMMX; because the compiler seems to match the function pointer to the correct overload of padds_MMX when I do padds_ubauba = &padds_MMX. Cool. Thanks for the help Jarrett.So I was wondering if there is a way around this so that I can still overload padds while taking advantage of function pointers, or if this is impossible, why it is so?How about something like: static this() { // determine CPU type with CPUID // padds_ubauba means padds(ubyte[], ubyte[]) if(mmxSupported) padds_ubauba = &padds_ubaubaMMX; else padds_ubauba = &padds_ubaubaNoMMX; // ... and so on, for all the other ones } void padds_ubaubaMMX(ubyte[] l, ubyte[] r) { // MMX implementation } void padds_ubabuaNoMMX(ubyte[] l, ubyte[] r) { // non-MMX implementation } void function(ubyte[], ubyte[]) padds_ubauba; void padds(ubyte[] l, ubyte[] r) { padds_ubauba(l, r); } This way, (1) The correct function is selected at module initialization, by setting padds_ubauba to point to the correct version of the padds_ubaubaXXX function. No determining which function to use every loop. (2) padds() can now be overloaded to take other param types. (3). padds() is so simple that the compiler will probably inline it, making it even faster.
Jan 11 2006