macro magic: include a while loop and return result?

T

Tobin Fricke

I have a wrapper function I use to check the error conditions of various
functions:

wrap(foo(1,2,3)); (1)
while (1 == wrap(bar("fluffy"))) { ... } (2)

The wrapper function looks like this:

int wrap(int code) {
if (code) print_message(code);
return code;
}

One of the error conditions of foo and bar is PLEASE_TRY_AGAIN, indicating
that I should wait a little while and try the function again. I want to
redefine wrap as a macro that's able to handle this. (Or a C++
alternative of some kind.) A first approach is this:

#define wrap(x) { while (x == PLEASE_TRY_AGAIN); }

That works for calls like (1), but not like (2). Is there any way to make
a macro that does that?

I know (thanks mconst) there is nonstandard GCC extension that allows the
following syntax:

static int _result;
#define wrap(x) ({while ((_result=x)==PLEASE_TRY_AGAIN)Sleep(200);x;})

However, I am using MSVC so GCC extensions aren't available to me. In the
meantime I have resorted to calls like this:

static int _result;
#define wrap(x) {while ((_result=x)==PLEASE_TRY_AGAIN) Sleep(200);}

wrap(foo("fsdf"));
if (_result) (...) // in cases where extended result handling is necessary

Anyone have any suggestions?

I suppose there might be something in C++ that would solve this problem
too, so I'm interested in hearing (1) whether it's possible in standard C,
(2) whether there is some nonstandard microsoft MSVC 6.0 extension that
will let me do it, or (3) whether there is some way in C++ to accomplish
this.

Thanks!

Tobin Fricke
 
S

Sten Westerback

Well, as busy looping is not good way of programming
and your question also seems more targeted at comp.lang.c
or comp.lang.c++ .. ask there.. :)
But it would seem you need to stick with a function... or two.

- Sten
 
T

Tobin Fricke

Well, as busy looping is not good way of programming
and your question also seems more targeted at comp.lang.c
or comp.lang.c++ .. ask there.. :)
But it would seem you need to stick with a function... or two.

Busy looping IN GENERAL is not usually the best way of programming, and
here it is the only way to accomplish what I need to accomplish:

while (foo() == BUSY) Sleep(100);

The function foo() will rarely be busy, so most often it will succeed the
first time. Moreoever, there is no way to be signalled when foo() is not
busy. Thus, the loop is not only the only way to do this, it is an
appropriate way to do it.
and your question also seems more targeted at comp.lang.c
or comp.lang.c++ .. ask there.. :)

It is admittedly not related to Win32, but it is related to Microsoft
Visual C++ 6.0 (MSVC), because that compiler defines various extensions to
the C and C++ languages and has various quirks of its own. I searched
Google Groups for MSVC and the c.os.m-w.p.win32 newsgroup turned up the
most hits.
But it would seem you need to stick with a function... or two.

No, a function won't work, because C and C++ evaluate the arguments to a
function before calling the function.

int wrap(int X) {
int retval;
while ((retval = X) == BUSY) { Sleep(100); }
return retval;
}

It should be obvious why that would work as a macro but not as a function.
If you're tricky, you'll suggest passing a function pointer as the
argument X, but that's not ideal, because the functions X that are wrapped
take all sorts of different types and numbers of arguments.

Tobin
 

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

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top