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
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