By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,670 Members | 1,526 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,670 IT Pros & Developers. It's quick & easy.

Can use macros to solve this problem?

P: n/a
I have a list of functions (all with suffix T). For each one, I need
to implement the following:

FunctionT()
{
if (some_condition)
{
// do some conversion first
FunctionW();
// then do some processing
}
else
{
// do some conversion first
FunctionA();
// then do some processing
}
}

The condition check, conversion and processing is the same for all
functions. Any way to use macros to save all the hassle? Or perhaps
with function pointers as well?
Nov 14 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi there,

hierro wrote:
I have a list of functions (all with suffix T). For each one, I need
to implement the following:

FunctionT()
{
if (some_condition)
{
// do some conversion first
FunctionW();
// then do some processing
}
else
{
// do some conversion first
FunctionA();
// then do some processing
}
}

The condition check, conversion and processing is the same for all
functions. Any way to use macros to save all the hassle? Or perhaps
with function pointers as well?


You can do this with macros:

--- File Tfunc.ct ---
#if defined FUNCNAME && defined RET_TYPE
#define CONCAT(A,B) A ## B
#define FUNCNAME_T(f) CONCAT(f, T)
#define FUNCNAME_W(f) CONCAT(f, W)
#define FUNCNAME_A(f) CONCAT(f, A)

#ifdef PARAM_LIST
RET_TYPE FUNCNAME_T(FUNCNAME) (PARAM_LIST)
#else
RET_TYPE FUNCNAME_T(FUNCNAME) (void)
#endif
{
if (some_condition)
{
// do some conversion first
#ifdef PARAM_LIST
FUNCNAME_W(FUNCNAME) (ARG_LIST);
#else
FUNCNAME_W(FUNCNAME) ();
#endif
// then do some processing
}
else
{
// do some conversion first
#ifdef PARAM_LIST
FUNCNAME_A(FUNCNAME) (ARG_LIST);
#else
FUNCNAME_A(FUNCNAME) ();
#endif
// then do some processing
}
}
#undef FUNCNAME_T
#undef FUNCNAME_W
#undef FUNCNAME_A
#else
#error FUNCNAME or RET_TYPE not defined
#endif
#undef FUNCNAME
#undef RET_TYPE
#ifdef PARAM_LIST
# undef PARAM_LIST
# undef ARG_LIST
#endif
---------------------

--- funcT.c ---------
#include "prototypes.h" /* prototypes for the A and W functions */

.....
#define FUNCNAME firstfun
#define RET_TYPE int
#include "Tfunc.ct"

#define FUNCNAME secondfun
#define RET_TYPE void
#define PARAM_LIST int a
#define ARG_LIST a
#include "Tfunc.ct"
.....
---------------------

However, this gets nasty when creating the corresponding headerfile
--- funcT.h ---------
.....
int firstfunT (void);

void secondfunT (int a);
.....
---------------------

I would not do this.
I'd rather consider a function pointer solution

typedef int (*AWfunptr)(int);
typedef struct {
AWfunptr Afunc;
AWfunptr Wfunc;
} AWpairing;

int TCall (AWpairing *aw, int arg)
{
if (some_condition)
{
// do some conversion first
(*aw->Wfunc)(arg);
// then do some processing
}
else
{
// do some conversion first
(*aw->Afunc)(arg);
// then do some processing
}
}

.....

Note: As the above snippets are not complete programs, I have
not tested them. Especially for the template solution (the one
with the macros), look at the preprocessor output to see whether
everything is as intended.
Apart from that: // are C++ or C99 style comments
Do not use them for C89 code.
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #2

P: n/a
hi******@hotmail.com (hierro) writes:
I have a list of functions (all with suffix T). For each one, I need
to implement the following:

FunctionT()
{
if (some_condition)
{
// do some conversion first
FunctionW();
// then do some processing
}
else
{
// do some conversion first
FunctionA();
// then do some processing
}
}

The condition check, conversion and processing is the same for all
functions. Any way to use macros to save all the hassle? Or perhaps
with function pointers as well?


Assuming the T's, W's and A's are defined/called just as shown above,
it's easy:

#define T_FUNCTION(n) \
void n ## T( void ){ common_body( n ## W, n ## A ); }

static void
common_body( void (*w)( void ), void (*a)( void ) ){
if( some_condition ){
/* conversion_w here */
w();
/* processing_w here */
} else {
/* conversion_a here */
a();
/* processing_a here */
}
}

T_FUNCTION(foo)
T_FUNCTION(bas)
T_FUNCTION(etc)

If conversion_w is the same as conversion_a, and processing_w is the
same as processing_a, then the common_body function can be simplified
further:

static void
common_body( void (*w)( void ), void (*a)( void ) ){
/* conversion */
(* (some_condition ? w : a))();
/* processing */
}

Obvious changes if the functions take arguments and/or return non-void
results.
Nov 14 '05 #3

P: n/a
hi******@hotmail.com (hierro) wrote in message news:<9f**************************@posting.google. com>...
I have a list of functions (all with suffix T). For each one, I need
to implement the following:

FunctionT()
{
if (some_condition)
{
// do some conversion first
FunctionW();
// then do some processing
}
else
{
// do some conversion first
FunctionA();
// then do some processing
}
}

The condition check, conversion and processing is the same for all
functions. Any way to use macros to save all the hassle? Or perhaps
with function pointers as well?


Can't you find a way to do:-

FunctionT()
{
// do some conversion first
if (some_condition)
{
FunctionW();
}
else
{
FunctionA();
}
// then do some processing
}

If the conversion steps are the same for all the functions?
It would make things a lot simpler.
Nov 14 '05 #4

P: n/a
Thanks everyone for the help. Still trying to digest your solutions.

I've simplified the problem actually. Actually the different functions
all have different argument lists and return types.

Eg:
int firstFuncT(int, int) --> int firstFuncA(int, int) or int
firstFuncW(int, int)
void secondFuncT(float) --> void secondFuncA(float) or void
secondFuncW(float)

Is it still possible to solve using your suggested methods?

One more thing, to answer Tim and Rob, yes, the conversion and
processing for the A functions are different from those for the W
functions. But across functions (eg: firstFuncT and secondFuncT
above), conversion and processing for A functions are identical.
Similarly for the W functions.

Actually what i'm doing here is to provide generic functions that'll
call the respective unicode or ansi functions in the windows API. For
example, DrawTextT will call DrawTextW or DrawTextA after checking for
OS versions during runtime. The T-functions will take in UTF-8 strings
(in addition to other arguments) and convert to UTF-16(unicode) or
codepage(ansi) before passing those arguments in the respective W or A
function. The processing is just some clearing up of memory.

And no, I can't use the MSLU due to some restrictions.
Nov 14 '05 #5

P: n/a
I've tried your macro method and it seems to work fine. Only problem
is my function is DrawText and in the windows API it's already defined
as follows:

#ifdef _UNICODE
#define DrawText DrawTextW
#else
#define DrawText DrawTextA
#endif

So I end up getting DrawTextAX, DrawTextAW and DrawTextAA from the
preprocessor output.

I've #undef DrawText to make it work. Is it a good idea?

Michael Mair <Mi**********@invalid.invalid> wrote in message news:<2u*************@uni-berlin.de>...
Hi there,

hierro wrote:
I have a list of functions (all with suffix T). For each one, I need
to implement the following:

FunctionT()
{
if (some_condition)
{
// do some conversion first
FunctionW();
// then do some processing
}
else
{
// do some conversion first
FunctionA();
// then do some processing
}
}

The condition check, conversion and processing is the same for all
functions. Any way to use macros to save all the hassle? Or perhaps
with function pointers as well?


You can do this with macros:

--- File Tfunc.ct ---
#if defined FUNCNAME && defined RET_TYPE
#define CONCAT(A,B) A ## B
#define FUNCNAME_T(f) CONCAT(f, T)
#define FUNCNAME_W(f) CONCAT(f, W)
#define FUNCNAME_A(f) CONCAT(f, A)

#ifdef PARAM_LIST
RET_TYPE FUNCNAME_T(FUNCNAME) (PARAM_LIST)
#else
RET_TYPE FUNCNAME_T(FUNCNAME) (void)
#endif
{
if (some_condition)
{
// do some conversion first
#ifdef PARAM_LIST
FUNCNAME_W(FUNCNAME) (ARG_LIST);
#else
FUNCNAME_W(FUNCNAME) ();
#endif
// then do some processing
}
else
{
// do some conversion first
#ifdef PARAM_LIST
FUNCNAME_A(FUNCNAME) (ARG_LIST);
#else
FUNCNAME_A(FUNCNAME) ();
#endif
// then do some processing
}
}
#undef FUNCNAME_T
#undef FUNCNAME_W
#undef FUNCNAME_A
#else
#error FUNCNAME or RET_TYPE not defined
#endif
#undef FUNCNAME
#undef RET_TYPE
#ifdef PARAM_LIST
# undef PARAM_LIST
# undef ARG_LIST
#endif
---------------------

--- funcT.c ---------
#include "prototypes.h" /* prototypes for the A and W functions */

....
#define FUNCNAME firstfun
#define RET_TYPE int
#include "Tfunc.ct"

#define FUNCNAME secondfun
#define RET_TYPE void
#define PARAM_LIST int a
#define ARG_LIST a
#include "Tfunc.ct"
....
---------------------

However, this gets nasty when creating the corresponding headerfile
--- funcT.h ---------
....
int firstfunT (void);

void secondfunT (int a);
....
---------------------

I would not do this.
I'd rather consider a function pointer solution

typedef int (*AWfunptr)(int);
typedef struct {
AWfunptr Afunc;
AWfunptr Wfunc;
} AWpairing;

int TCall (AWpairing *aw, int arg)
{
if (some_condition)
{
// do some conversion first
(*aw->Wfunc)(arg);
// then do some processing
}
else
{
// do some conversion first
(*aw->Afunc)(arg);
// then do some processing
}
}

....

Note: As the above snippets are not complete programs, I have
not tested them. Especially for the template solution (the one
with the macros), look at the preprocessor output to see whether
everything is as intended.
Apart from that: // are C++ or C99 style comments
Do not use them for C89 code.
Cheers
Michael

Nov 14 '05 #6

P: n/a
hi******@hotmail.com (hierro) writes:
I've simplified the problem actually. Actually the different functions
all have different argument lists and return types.

Eg:
int firstFuncT(int, int) --> int firstFuncA(int, int) or int
firstFuncW(int, int)
void secondFuncT(float) --> void secondFuncA(float) or void
secondFuncW(float)

Is it still possible to solve using your suggested methods?

One more thing, to answer Tim and Rob, yes, the conversion and
processing for the A functions are different from those for the W
functions. But across functions (eg: firstFuncT and secondFuncT
above), conversion and processing for A functions are identical.
Similarly for the W functions.


Something like this should work:

#define T_FUNCTION(n,parameters,arguments) \
void n ## T parameters { \
if(some_condition){ \
PRE_W_STUFF \
n ## W arguments ; \
POST_W_STUFF \
} else { \
PRE_A_STUFF \
n ## A arguments ; \
POST_A_STUFF \
} \
}

#define PRE_W_STUFF /*whatever*/
#define POST_W_STUFF /*whatever*/
#define PRE_A_STUFF /*whatever*/
#define POST_A_STUFF /*whatever*/

T_FUNCTION( firstFunc, ( int a, int b ), ( a, b ) )
T_FUNCTION( secondFunc, ( float f ), ( f ) )
Add another parameter to vary the return type. ok?
Nov 14 '05 #7

P: n/a


hierro wrote:
I've tried your macro method and it seems to work fine. Only problem
is my function is DrawText and in the windows API it's already defined
as follows:

#ifdef _UNICODE
#define DrawText DrawTextW
#else
#define DrawText DrawTextA
#endif

So I end up getting DrawTextAX, DrawTextAW and DrawTextAA from the
preprocessor output.

I've #undef DrawText to make it work. Is it a good idea?


Umh, if it already is in the API, I would not #undef it.
Or, if you cannot do without: #include the respective header
_after_ your own headers/function definitions. Only if
there is no choice, #undef it and afterwards #define it
again. As you only are getting DrawTextT, DrawTextA, and
DrawTextW, this should not interfere with anything else.

As I said: Using function pointers may be safer.
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.