How can I efficienty define and/or declare hundreds of functions?

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;
}
 
F

Francesco S. Carta

I tried to solve this problem the following way:

(the problem being: "How can I efficienty define and/or declare hundreds
of functions?" as the subject reports, please repeat the point of your
message in the message's body)

<snip lots of macros>

I'm sorry I'm no PP expert, though since you're posting code like this:
#include<iostream>
^^^^^^^^
AND
printf("Number of function calls: %d\n", CALLS);
^^^^^^

....in a C++ group, I wonder if you know that C++ does things somewhat
differently from C - meaning that you should drop the printf() and use
"cout << whatever;" as your include directive would imply.

More to the point, I wonder if what you're trying to achieve couldn't be
more easily done with templates - eventually resorting to far fewer
macros in case you really need to create new identifiers composing them
with the ## command (or instruction or directive or whatever that's
called in the PP context).

I'll try to understand what exactly your macros do, although a small
briefing from you would make things easier for me.
 
F

Francesco S. Carta

(the problem being: "How can I efficienty define and/or declare hundreds
of functions?" as the subject reports, please repeat the point of your
message in the message's body)

<snip lots of macros>

I'm sorry I'm no PP expert, though since you're posting code like this:

^^^^^^

...in a C++ group, I wonder if you know that C++ does things somewhat
differently from C - meaning that you should drop the printf() and use
"cout << whatever;" as your include directive would imply.

More to the point, I wonder if what you're trying to achieve couldn't be
more easily done with templates - eventually resorting to far fewer
macros in case you really need to create new identifiers composing them
with the ## command (or instruction or directive or whatever that's
called in the PP context).

I'll try to understand what exactly your macros do, although a small
briefing from you would make things easier for me.

I've just tried to pass the code you posted to the preprocessor, I've
had to manually edit it because there are comments and macros which are
broken due to the line wrapping.

Of course, my compiler refused to compile also because of the absence of
"#include <cstdio>"... no, wait, it didn't compile because of the
/presence/ of printf() ;-)

Anyway, I've seen what your macros do (I've seen the macros expanded by
the PP), and I think you can achieve all of that with templates -
actually you can achieve something better than that, using templates.

For your convenience, please read the C++ FAQ:

http://www.parashift.com/c++-faq-lite/

All of that should help you get a better picture about what C++ can do
along with a lot of very good advices about how to "tell C++" to do
something.

Once you have read && tried something from that FAQ, if you have any
question post them here, they will be welcome - as long as you post them
following the guidelines presented by FAQ 5.8.
 
A

Alf P. Steinbach /Usenet

* PowerStudent, on 06.08.2010 19:32:
I tried to solve this problem the following way:

What problem?

int main(int argc, char *argv[])
{

Preferably don't declare unused arguments.

CALL(void,0);
CALL(int,87);
CALL(char, 'a');
CALL(float, 7595445887654555.345f);
printf("Number of function calls: %d\n", CALLS);

And preferably don't use all uppercase for non-macro names (or don't use macros
for accessing simple data or functions).

return 0;
}

It looks as what you're looking for is overloaded functions, but you failed to
state your question.

I suggest look at the FAQ's item about how to post.

Then repost, not forgetting to ask whatever it was you meant to ask.


Cheers & hth.,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top