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

macro magic: include a while loop and return result?

P: n/a
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

Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

"Tobin Fricke" <fr****@ocf.berkeley.edu> wrote in message
news:Pi*******************************@apocalypse. OCF.Berkeley.EDU...
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

Jul 22 '05 #2

P: n/a
On Thu, 27 May 2004, Sten Westerback wrote:
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


Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.