P
PowerStudent
Purpose of my Macros in short:
o PFUNC(R,N,P) typedef R (*N)(P)
I use this macro to define myself different function pointer types.
These functions look like this:
RETURN_TYPE NAME(PARAMETER_TYPE param);
RETURN_TYPE, PARAMETER_TYPE can be: void, int, char, float
For exemple:
// assume there is a function int blabla();
PFUNC(int, piFunkv,void);
piFunkv myPointer = blabla;
o FUNC(R,N,P)
This macro declares my functions I want my pointers to point to.
The resulting functions look like this:
(*) R N(T in);
only exception of the rule:
FUNC(R, N, void); produces the following function:
R N();
o NEW_VAR(R, N, V)
This macro declares new variables, if R is unequal to void.
Otherwise the preprocessor just leaves white space.
o PRINT(R, N, V)
This macro prints the return type, the name and (if there is a
parameter) the parameter type and name of the function it's standing
in.
Where possible, it also prints out the returned value and the
parameter value.
o RETURN(R, N)
This macro is either replaced by return; - RETURN(void, N); - or by
return N; - RETURN(R, N);.
o CREATE(R, N)
This macro defines function pointer types und defines & declares
functions and corresponding pointers
exemple:
CREATE(void, vFunc);
//=>
typedef void (*pvFuncv)(void);
typedef void (*pvFuncc)(char);
typedef void (*pvFunci)(int);
typedef void (*pvFuncf)(float);
void vFuncv(void){
//...
}
void vFuncc(char){
//...
}
void vFunci(int){
//...
}
void vFuncf(float){
//...
}
const pvFuncv pvFuncvVar = vFuncv;
const pvFuncc pvFunccVar = vFuncc;
const pvFunci pvFunciVar = vFunci;
const pvFuncf pvFuncfVar = vFuncf;
o CALL(P, V)
Calls every function defined in funcs.h which parameter is of type P
with the given value V.
The cause why I have so many FUNC_*, NEW_VAR_*, *_PRINT_*, RETURN_*
macros defined is that some parameters of FUNC would have created
functions that aren't allowed in C++:
// If FUNK would create all functions like defined above (*), then the
following call
FUNC(void, notWorking, void);
// would have the following result:
// multiple errors: there can't be a variable of type void
void notWorking(void in){
void result = static_cast<void>(in);
printf("The function void %s(void in) was called and returned %?.
Value of in: %?", notWorking, result, in);
return result;
}
First I tried to use MACROS inside MACROS, like
#define IS_VOID(T) IS_VOID_##T
#define IS_VOID_true true
#define IS_VOID_false false
#define RETURN(R, N) #if ( IS_VOID(R) ) \ return; \ #else \ return N
But this doesn't work.
P.S. @Francesco S. Carta : I actually tried cout first, but then my
source code became very untidy and I couldn't get it to work properly,
so I switched to printf which was easier to use.
o PFUNC(R,N,P) typedef R (*N)(P)
I use this macro to define myself different function pointer types.
These functions look like this:
RETURN_TYPE NAME(PARAMETER_TYPE param);
RETURN_TYPE, PARAMETER_TYPE can be: void, int, char, float
For exemple:
// assume there is a function int blabla();
PFUNC(int, piFunkv,void);
piFunkv myPointer = blabla;
o FUNC(R,N,P)
This macro declares my functions I want my pointers to point to.
The resulting functions look like this:
(*) R N(T in);
only exception of the rule:
FUNC(R, N, void); produces the following function:
R N();
o NEW_VAR(R, N, V)
This macro declares new variables, if R is unequal to void.
Otherwise the preprocessor just leaves white space.
o PRINT(R, N, V)
This macro prints the return type, the name and (if there is a
parameter) the parameter type and name of the function it's standing
in.
Where possible, it also prints out the returned value and the
parameter value.
o RETURN(R, N)
This macro is either replaced by return; - RETURN(void, N); - or by
return N; - RETURN(R, N);.
o CREATE(R, N)
This macro defines function pointer types und defines & declares
functions and corresponding pointers
exemple:
CREATE(void, vFunc);
//=>
typedef void (*pvFuncv)(void);
typedef void (*pvFuncc)(char);
typedef void (*pvFunci)(int);
typedef void (*pvFuncf)(float);
void vFuncv(void){
//...
}
void vFuncc(char){
//...
}
void vFunci(int){
//...
}
void vFuncf(float){
//...
}
const pvFuncv pvFuncvVar = vFuncv;
const pvFuncc pvFunccVar = vFuncc;
const pvFunci pvFunciVar = vFunci;
const pvFuncf pvFuncfVar = vFuncf;
o CALL(P, V)
Calls every function defined in funcs.h which parameter is of type P
with the given value V.
The cause why I have so many FUNC_*, NEW_VAR_*, *_PRINT_*, RETURN_*
macros defined is that some parameters of FUNC would have created
functions that aren't allowed in C++:
// If FUNK would create all functions like defined above (*), then the
following call
FUNC(void, notWorking, void);
// would have the following result:
// multiple errors: there can't be a variable of type void
void notWorking(void in){
void result = static_cast<void>(in);
printf("The function void %s(void in) was called and returned %?.
Value of in: %?", notWorking, result, in);
return result;
}
First I tried to use MACROS inside MACROS, like
#define IS_VOID(T) IS_VOID_##T
#define IS_VOID_true true
#define IS_VOID_false false
#define RETURN(R, N) #if ( IS_VOID(R) ) \ return; \ #else \ return N
But this doesn't work.
P.S. @Francesco S. Carta : I actually tried cout first, but then my
source code became very untidy and I couldn't get it to work properly,
so I switched to printf which was easier to use.