View Single Post
Re: Storing an array of function calls, and their params.....
Old
  (#16)
Pierre-Marie Baty
Roi de France
 
Pierre-Marie Baty's Avatar
 
Status: Offline
Posts: 5,049
Join Date: Nov 2003
Location: 46°43'60N 0°43'0W 0.187A
Default Re: Storing an array of function calls, and their params..... - 17-05-2005

What Maleficus is doing right now IS portable. As long as all the functions have identical arguments, storing them in the struct is perfectly doable.
Code:
typedef struct
{
	int foo; 
	int (*fooFunc) (int *param1, int *param2);
	int param1;
	int param2;
} my_func_t;
Code:
// instancing
my_func_t *callerFunc;
callerFunc = new (my_func_t);
 
 
// filling in
callerFunc->fooFunc = myCoolFunc;
callerFunc->param1 = 14;
callerFunc->param2 = 22;
 
 
// calling
callerFunc->fooFunc (callerFunc->param1, callerFunc->param2);
This is 100% portable.

If you want to call funcs that have variable types of arguments instead, you can do something like this:
Code:
typedef struct
{
	union
	{
		bool bparam;
		char cparam;
		long iparam;
		float fparam;
		long long lparam;
		double dparam;
	};
	char param_type;
} param_t;
 
 
typedef struct
{
	int foo; 
	int (*fooFunc) (int *param1, int *param2);
	param_t params[256];
	int param_count;
} my_func_t;
Code:
// instancing
my_func_t *callerFunc;
callerFunc = new (my_func_t);
 
 
// filling in
callerFunc->fooFunc = myCoolFunc;
callerFunc->params[0].fparam = 12.3456789f;
callerFunc->params[0].type = PARAM_FLOAT;
callerFunc->params[1].lparam = 24;
callerFunc->params[1].type = PARAM_LONG;
callerFunc->param_count = 2;
 
 
// calling
// HERE GOES DIRTY ASM CODE TO STACK UP THE ARGUMENTS AND CALL THE FUNCTION
of course THIS, will not be portable.



RACC home - Bots-United: beer, babies & bots (especially the latter)
"Learn to think by yourself, else others will do it for you."
  
Reply With Quote