Originally Posted by
JohnAnon
I'm not having much luck with understanding how to implement the GetModuleHandle()/GetProcAddress() solution.
I appreciate the suggestion of the internal table, it may very well be a compromise between what I originally wanted: a switch statement and a dynamic solution (which my boss wants).
Seems a shame that there isn't a built in mechanism to deal with data matching a process. If I'm working with a WIDGET, then I should be able to say something like DO WIDGET to get my result. Such is life.
I'll keep working with the GetModuleHandle()/GetProcAddress() solution because someday, I want to separate myself from working on or in my boss's function and, at the same time, limit my efforts to that which is within my routine.
I know that your suggestion accomplishes the first half of the goal.
Thanks.
John
Its possible, you just have to implement it. Meaning that you will have something like:
Code:
double MYROUTINE(double x){
// do stuff
// register routine
}
double MYBOSSESROUTINE(const char* z)
{
// value of z is "MYROUTINE"
Call(z)(); // or something like that
}
Think that if there was a built in system it will more or less do something like above, just maybe more efficiently.
There are a few ways to make this elegant. One is to use std::map. They key will be a string and the value will actually be a function pointer. Then you will do something like:
Code:
//define a function pointer
typedef double (*fnc_ptr)(double x);
//this will be your function registry
std::map<std::string, fnc_ptr> fncRegistry;
//maybe this is not actually needed
double MYROUTINE_A(double x);
double MYROUTINE_A(double x)
{
fncRegistry("MYROUTINE_A", MYROUTINE_A);
//define the function
}
double MYBOSSESROUTINE(const char* z)
{
....
fncRegistry(z)(10.2); //call z
....
}
So you do exactly what you want to do very nicely
Note that you can also register functions that belong to a library
Code:
#include <math.h>
//as above
int main()
{
fncRegistry.insert(std::pair<std::string, fnc_ptr>("cos", cos)); //register
MYBOSSESROUTINE("cos");
}
so the solution is indeed dynamic. You just have to register all the functions that are going to be used.
In other languages there are run-time solutions to get a name of a method. But the cases actually really needed are a few, usually a good design will solve your problems.
Note that the functions has to return double and get one input as double. You can extend this with more code. Note also that if the function is misspelled, it will crash. You can fix this by making a "Call(...)" function that checks if the name exists or not first etc
EDIT: This is sample code. Something might not compile, but you fix this with a few tweaks