P
PowerStudent
I tried to solve this problem the following way:
funcs.h:
#ifndef __FUNCS_H_
#define __FUNCS_H_
#include <iostream>
// function pointer type definition
#define PFUNC(R,N,P) typedef R (*N)(P)
// if R!=void: to define a variable of type R named N and to assign
the value V to it
#define NEW_VAR(R, N, V) NEW_VAR_##R(N, V)
#define NEW_VAR_void(N, V)
#define NEW_VAR_int(N, V) int N=static_cast<int>(V)
#define NEW_VAR_char(N, V) char N=static_cast<char>(V)
#define NEW_VAR_float(N, V) float N=static_cast<float>(V)
// printing makros
#define PRINT(R,N,P) R##_PRINT_##P(R,N,P)
#define void_PRINT_void(R,N,P) printf("The function void %s() was
called.\n", #N)
#define void_PRINT_int(R,N,P) printf("The function void %s(int in)
was called. Value of in: %d\n", #N, in)
#define void_PRINT_char(R,N,P) printf("The function void %s(char in)
was called. Value of in: %c\n", #N, in)
#define void_PRINT_float(R,N,P) printf("The function void %s(float
in) was called. Value of in: %f\n", #N, in)
#define int_PRINT_void(R,N,P) printf("The function int %s() was
called and returned %d.\n", #N, result)
#define int_PRINT_int(R,N,P) printf("The function int %s(int in)
was called and returned %d. Value of in: %d\n", #N, result, in)
#define int_PRINT_char(R,N,P) printf("The function int %s(char in)
was called and returned %d. Value of in: %c\n", #N, result, in)
#define int_PRINT_float(R,N,P) printf("The function int %s(float in)
was called and returned %d. Value of in: %f\n", #N, result, in)
#define char_PRINT_void(R,N,P) printf("The function char %s() was
called and returned %c.\n", #N, result)
#define char_PRINT_int(R,N,P) printf("The function char %s(int in)
was called and returned %c. Value of in: %d\n", #N, result, in)
#define char_PRINT_char(R,N,P) printf("The function char %s(char in)
was called and returned %c. Value of in: %c\n", #N, result, in)
#define char_PRINT_float(R,N,P) printf("The function char %s(float
in) was called and returned %c. Value of in: %f\n", #N, result, in)
#define float_PRINT_void(R,N,P) printf("The function float %s() was
called and returned %f.\n", #N, result)
#define float_PRINT_int(R,N,P) printf("The function float %s(int in)
was called and returned %f. Value of in: %d\n", #N, result, in)
#define float_PRINT_char(R,N,P) printf("The function float %s(char
in) was called and returned %f. Value of in: %c\n", #N, result, in)
#define float_PRINT_float(R,N,P) printf("The function float %s(float
in) was called and returned %f. Value of in: %f\n", #N, result, in)
#define RETURN(R,N) RETURN_##R(N)
#define RETURN_void(N) return
#define RETURN_int(N) return N
#define RETURN_char(N) RETURN_int(N)
#define RETURN_float(N) RETURN_int(N)
static unsigned int CALLS=0;
#define FUNC(R,N,P) FUNC_##P(R,N,P)
#define FUNC_void(R,N,P) R N(){\
++CALLS;\
NEW_VAR(R, result, 123.5f);\
PRINT(R,N,P);\
RETURN(R, result);\
}
#define FUNC_int(R,N,P) R N(P in){\
++CALLS;\
NEW_VAR(R, result, in);\
PRINT(R,N,P);\
RETURN(R, result);\
}
#define FUNC_char(R,N,P) FUNC_int(R,N,P)
#define FUNC_float(R,N,P) FUNC_int(R,N,P)
// create functions and pointers
#define CREATE(R,N) \
PFUNC(R, p##N##v,void); \
PFUNC(R, p##N##c,char); \
PFUNC(R, p##N##i,int); \
PFUNC(R, p##N##f,float);\
FUNC(R, N##v,void); \
FUNC(R, N##c,char); \
FUNC(R, N##i,int); \
FUNC(R, N##f,float); \
const p##N##v p##N##vVar = N##v; \
const p##N##c p##N##cVar = N##c; \
const p##N##i p##N##iVar = N##i; \
const p##N##f p##N##fVar = N##f
CREATE(void, vFunc);
CREATE(char, cFunc);
CREATE(int, iFunc);
CREATE(float, fFunc);
#define CALL_void(V) \
(*pvFuncvVar)();\
(*piFuncvVar)();\
(*pcFuncvVar)();\
(*pfFuncvVar)()
#define CALL_int(V) \
(*pvFunciVar)(V);\
(*piFunciVar)(V);\
(*pcFunciVar)(V);\
(*pfFunciVar)(V)
#define CALL_char(V) \
(*pvFunccVar)(V);\
(*piFunccVar)(V);\
(*pcFunccVar)(V);\
(*pfFunccVar)(V)
#define CALL_float(V) \
(*pvFuncfVar)(V);\
(*piFuncfVar)(V);\
(*pcFuncfVar)(V);\
(*pfFuncfVar)(V)
#define CALL(P, V) CALL_##P(V)
#undef PFUNC
#undef FUNC
#undef CREATE
#endif
main.cpp:
#include <iostream>
#include "funcs.h"
using namespace std;
int main(int argc, char *argv[])
{
CALL(void,0);
CALL(int,87);
CALL(char, 'a');
CALL(float, 7595445887654555.345f);
printf("Number of function calls: %d\n", CALLS);
return 0;
}
funcs.h:
#ifndef __FUNCS_H_
#define __FUNCS_H_
#include <iostream>
// function pointer type definition
#define PFUNC(R,N,P) typedef R (*N)(P)
// if R!=void: to define a variable of type R named N and to assign
the value V to it
#define NEW_VAR(R, N, V) NEW_VAR_##R(N, V)
#define NEW_VAR_void(N, V)
#define NEW_VAR_int(N, V) int N=static_cast<int>(V)
#define NEW_VAR_char(N, V) char N=static_cast<char>(V)
#define NEW_VAR_float(N, V) float N=static_cast<float>(V)
// printing makros
#define PRINT(R,N,P) R##_PRINT_##P(R,N,P)
#define void_PRINT_void(R,N,P) printf("The function void %s() was
called.\n", #N)
#define void_PRINT_int(R,N,P) printf("The function void %s(int in)
was called. Value of in: %d\n", #N, in)
#define void_PRINT_char(R,N,P) printf("The function void %s(char in)
was called. Value of in: %c\n", #N, in)
#define void_PRINT_float(R,N,P) printf("The function void %s(float
in) was called. Value of in: %f\n", #N, in)
#define int_PRINT_void(R,N,P) printf("The function int %s() was
called and returned %d.\n", #N, result)
#define int_PRINT_int(R,N,P) printf("The function int %s(int in)
was called and returned %d. Value of in: %d\n", #N, result, in)
#define int_PRINT_char(R,N,P) printf("The function int %s(char in)
was called and returned %d. Value of in: %c\n", #N, result, in)
#define int_PRINT_float(R,N,P) printf("The function int %s(float in)
was called and returned %d. Value of in: %f\n", #N, result, in)
#define char_PRINT_void(R,N,P) printf("The function char %s() was
called and returned %c.\n", #N, result)
#define char_PRINT_int(R,N,P) printf("The function char %s(int in)
was called and returned %c. Value of in: %d\n", #N, result, in)
#define char_PRINT_char(R,N,P) printf("The function char %s(char in)
was called and returned %c. Value of in: %c\n", #N, result, in)
#define char_PRINT_float(R,N,P) printf("The function char %s(float
in) was called and returned %c. Value of in: %f\n", #N, result, in)
#define float_PRINT_void(R,N,P) printf("The function float %s() was
called and returned %f.\n", #N, result)
#define float_PRINT_int(R,N,P) printf("The function float %s(int in)
was called and returned %f. Value of in: %d\n", #N, result, in)
#define float_PRINT_char(R,N,P) printf("The function float %s(char
in) was called and returned %f. Value of in: %c\n", #N, result, in)
#define float_PRINT_float(R,N,P) printf("The function float %s(float
in) was called and returned %f. Value of in: %f\n", #N, result, in)
#define RETURN(R,N) RETURN_##R(N)
#define RETURN_void(N) return
#define RETURN_int(N) return N
#define RETURN_char(N) RETURN_int(N)
#define RETURN_float(N) RETURN_int(N)
static unsigned int CALLS=0;
#define FUNC(R,N,P) FUNC_##P(R,N,P)
#define FUNC_void(R,N,P) R N(){\
++CALLS;\
NEW_VAR(R, result, 123.5f);\
PRINT(R,N,P);\
RETURN(R, result);\
}
#define FUNC_int(R,N,P) R N(P in){\
++CALLS;\
NEW_VAR(R, result, in);\
PRINT(R,N,P);\
RETURN(R, result);\
}
#define FUNC_char(R,N,P) FUNC_int(R,N,P)
#define FUNC_float(R,N,P) FUNC_int(R,N,P)
// create functions and pointers
#define CREATE(R,N) \
PFUNC(R, p##N##v,void); \
PFUNC(R, p##N##c,char); \
PFUNC(R, p##N##i,int); \
PFUNC(R, p##N##f,float);\
FUNC(R, N##v,void); \
FUNC(R, N##c,char); \
FUNC(R, N##i,int); \
FUNC(R, N##f,float); \
const p##N##v p##N##vVar = N##v; \
const p##N##c p##N##cVar = N##c; \
const p##N##i p##N##iVar = N##i; \
const p##N##f p##N##fVar = N##f
CREATE(void, vFunc);
CREATE(char, cFunc);
CREATE(int, iFunc);
CREATE(float, fFunc);
#define CALL_void(V) \
(*pvFuncvVar)();\
(*piFuncvVar)();\
(*pcFuncvVar)();\
(*pfFuncvVar)()
#define CALL_int(V) \
(*pvFunciVar)(V);\
(*piFunciVar)(V);\
(*pcFunciVar)(V);\
(*pfFunciVar)(V)
#define CALL_char(V) \
(*pvFunccVar)(V);\
(*piFunccVar)(V);\
(*pcFunccVar)(V);\
(*pfFunccVar)(V)
#define CALL_float(V) \
(*pvFuncfVar)(V);\
(*piFuncfVar)(V);\
(*pcFuncfVar)(V);\
(*pfFuncfVar)(V)
#define CALL(P, V) CALL_##P(V)
#undef PFUNC
#undef FUNC
#undef CREATE
#endif
main.cpp:
#include <iostream>
#include "funcs.h"
using namespace std;
int main(int argc, char *argv[])
{
CALL(void,0);
CALL(int,87);
CALL(char, 'a');
CALL(float, 7595445887654555.345f);
printf("Number of function calls: %d\n", CALLS);
return 0;
}