Next Generation Emulation banner
1 - 8 of 8 Posts

· Knowledge is the solution
Joined
·
7,484 Posts
Discussion Starter · #1 · (Edited)
I have the following method

template <int number> methodA(int *x){
blablabla

}

That is called by a number of different methods B,C,D. Each method B,C,D managed a different number of parameters itself

template<int number> methodB(int *x, int* y, int*z){

methodA<number>(x);
}
template<int number> methodC(int *x, int* y){

methodA<number>(x);
}
template<int number> methodD(int *x){

methodA<number>(x);
}

What I want to do is to create a generic caller, such that ideally I could do

genericCaller(methodName, parameterList){
methodName<512>(parameterList);

}

What is the proper procedure for doing this? BTW default parameters are not an option, since these functions are just an example, in reality each method signature is vastly different.

Now that I read about it this is not really a template usage question, but whatever :p More like how to pass method names and parameter lists as parameters themselves.

Ps: if possible I'd prefer the solution to be in pure C rather than C++
 

· You're already dead...
Joined
·
10,293 Posts
well if using c++ you can basically do this:

Code:
template <int number>
void templateMethod(int *x){
    // blablabla
}

void genericCaller(int *x, int* y, int*z){
    templateMethod<512>(x);
}
void genericCaller(int *x, int* y){
    templateMethod<512>(x);
}
void genericCaller(int *x){
    templateMethod<512>(x);
}
if you do it your way by passing arguments and the function pointer, it'll get messy (and slower)

C doesn't support function overloading, whereas C++ does.
so the code i posted won't work in C.
 

· Knowledge is the solution
Joined
·
7,484 Posts
Discussion Starter · #3 · (Edited)
Well, i know that I can do that, but in fact the complete generic caller method would be something like
Code:
callermethodA(int x, int n){
 switch(n){
   case 1: methodA< 1>(x);
   case 2: methodA< 2>(x);
   case 4: methodA< 4>(x);
   case 8: methodA< 8>(x);
   case 16: methodA< 16>(x);
   ...
 }
}
And all the way up to a bunch of power of 2's. If I separate it among all the functions that need this, it will result in A LOT of code redundancy once i do callerMethodB, callerMethodC and all the caller methods I'll need, so I wanted to use a single caller function to avoid this. I know that this way of doing things is a little twisted and non conventional, but I really need to do it this way, since this is some sort of code preprocesing of a set of functions that I will be running on a specialized device, so I don't really mind doing a little more work on the CPU side in order to improve performance on the specialized device, but at the same time I don't want to bloat my code with 20 lines that repeat themselves a lot.
 

· You're already dead...
Joined
·
10,293 Posts
well why are you using templates, if you're going to put them in a switch statement like that?

you should just pass int number as a function argument, instead of using templates. it'll be faster than using the switch statement like you're doing.
(using templates creates completely new functions for every different template parameter, and it'll cause cpu cache clutter. furthermore, the switch statement would eliminate the benefit from using templates.)

and you might want to take a look at this:
The Function Pointer Tutorials - Syntax
 

· Knowledge is the solution
Joined
·
7,484 Posts
Discussion Starter · #5 · (Edited)
you should just pass int number as a function argument, instead of using templates. it'll be faster than using the switch statement like you're doing.
(using templates creates completely new functions for every different template parameter, and it'll cause cpu cache clutter. furthermore, the switch statement would eliminate the benefit from using templates.)
I know. I'm doing it like that because I'm using a GPU/CUDA, and in a GPU branching (eg. if, else, switch and friends) are quite expensive operands when predication is not possible, so using templates that way, and letting the compiler create new functions for each case I will have to handle is actually more optimal (the wonders of stream programming). So what I want to do is to leave the branching in the CPU so the GPU can just go ahead and use a single function per execution. Geez, I KNOW what I'm doing :p (well, not really, I still need to know how to pass method names and parameter lists as arguments, but that seems to be what your link is about :p).
 

· Knowledge is the solution
Joined
·
7,484 Posts
Discussion Starter · #6 ·
Another question, is there possible to define argument lists in C, I think in Python something similar was possible

something like
void method1(unknown type and number of arguments){
method2(just forward the arguments here);

}

I thought about defining an array of void pointers, passing that as a reference and within method1 do the casting of the elements, and then pass them to method2, but I don't know whether there's an easier way.

Something like

void method1(void** args){
method2((int*)args[0], (char*)args[1]);

}
 

· Emu Author
Joined
·
616 Posts
i know its pretty much possible in c# as i do a similar method, i create a string array of names that i pass to a function that creates a tab on my menu in my emulator than with the array it creates all the items for that particular menu tab, like i can pass it the name File to create the file tab than i can have say: Open Rom, Close Rom, Close. and in another function it goes through each item in the array to create it under the tab File.

but i am unsure how you could do it in c or c++ but i am sure it can be done just not as easily i am sure. maybe even possible to do it that same way if the parameters are all of the same type maybe?
 

· Knowledge is the solution
Joined
·
7,484 Posts
Discussion Starter · #8 · (Edited)
Well, in that case it is a different application since it receives a one type kind of array, and the only thing it does is being a button factory method, which receives button names as input and outputs buttons. My example is more similar to preprocessor and a forwarding function. Like a callback function, but not quite :p

The way I'm doing it with a a void** array actually works, but I'm sure that I'm breaking more than one design pattern with that, and making a hell-to maintain kind of code by tying all the call forwards to a set of particular method signatures. Hence why I'd prefer some kind of: just forward whatever parameters you receive to the next method kind of solution. Aw well, maybe encapsulating them al in one structure could work. It will be one hell of a structure though.
 
1 - 8 of 8 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top