macro trick?

P

P L

I have codes with blocks like this:

void h() {
..
..
..
a;
b1;
b2;
..
..
..
bn;
c;
..
..
..
}

and I need to change it to something like:

static void g()
{
b1;
b2;
..
..
..
bn;
}

void h() {
..
..
..
a;
#ifdef ABC
b1;
b2;
..
..
..
bn;
#else
f(g); /* f is some function taking g as arg */
#endif
c;
..
..
..

}

I don't like to copy the blocks because of two reasons: there are lot
of such changed need to be made; and any future changes in b1; ...; bn
will need changes in two places. I am reluctant to add a lot of
backslashes and put b1; ...; bn in a macro; and would prefer some
other elegant way. Can someone shed some light? Thanks. - PL
 
K

Kris Wempa

P L said:
I have codes with blocks like this:

void h() {
.
.
.
a;
b1;
b2;
.
.
.
bn;
c;
.
.
.
}

and I need to change it to something like:

static void g()
{
b1;
b2;
.
.
.
bn;
}

void h() {
.
.
.
a;
#ifdef ABC
b1;
b2;
.
.
.
bn;
#else
f(g); /* f is some function taking g as arg */
#endif
c;
.
.
.

}

I don't like to copy the blocks because of two reasons: there are lot
of such changed need to be made; and any future changes in b1; ...; bn
will need changes in two places. I am reluctant to add a lot of
backslashes and put b1; ...; bn in a macro; and would prefer some
other elegant way. Can someone shed some light? Thanks. - PL


Your explanation of what you need to do is extremely confusing. Can you
explain it a little better ?
 
P

P L

My apology for the confusion. The problem arose from a bug in a large
software to be fixed. I determined that the bug could be fixed by
modifying existing code like this:

void h() {
.
.
.
a;
b1; /* this block will be duplicated in the fix */
b2;
.
.
.
bn;
c;
.
.
.
}


to something like this:

static void g()
{
b1; /* 1, the first copy of the block */
b2;
.
.
.
bn;
}

void h() {
.
.
.
a;
#ifdef ABC
b1; /* 2, the second copy of the block */
b2;
.
.
.
bn;
#else
f(g); /* f is some function taking g as arg */
#endif
c;
.
.
.
}

As you can see, the change included two duplicated copies of the same
block b1; ...; bn. I don't like it because there are lot of such
changed need to be made and any future changes in b1; ...; bn will
need changes in two places. One way I have to avoid duplicating the
blocks is

static void g();

void h() {
.
.
.
a;
#define BLOCK
b1; \
b2; \
. \
. \
. \
bn;
#ifdef ABC
BLOCK
#else
f(g); /* f is some function taking g as arg */
#endif
c;
.
.
.
}


static void g()
{
BLOCK /* 1, the first copy of the block */
}
#undef BLOCK



Yet I am reluctant to do this and add a lot of backslashes and would
prefer some elegant alternative. Thanks. -PL
 
P

P L

Another alternative without those back slashes is:

static void g()
{
b1;
b2;
...
bn;
}

void h()
{
/* block A of code */

#ifdef ABC
g();
#else
f(g); /* f is some function taking g as arg */
#endif

/* block C of code */
}

Yet that would move the block b1, b2, ..., bn away from where it
currently is. Can someone suggets a better alternative that keeps b1,
b2, ..., bn where it is? Thanks. -PL
 

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,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top