473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

macro magic: include a while loop and return result?

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
2 3870
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Tobin Fricke | last post by:
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)...
8
by: lasek | last post by:
Hi...in some posts i've read...something about using macro rather then function...but difference ??. Best regards....
4
by: winnerpl | last post by:
Hey guys I'm trying to get a magic square but I'm stuck with it printing out only 1's and 0's in random places. Hope you experts can can provide some input on what I'm doing wrong. #include...
44
by: Simon Morgan | last post by:
Hi, Can somebody please help me grok the offsetof() macro? I've found an explanation on http://www.embedded.com/shared/printableArticle.jhtml?articleID=18312031 but I'm afraid it still...
5
by: Niklas Norrthon | last post by:
I've been banging my head in the wall for some time now over a little problem having to do with partial specialization of function templates. The real problem is more complex than this, but...
1
by: williamvarah | last post by:
I want to be able to link a macro to an icon in excel so that I can run a function that I have in excel visual basic. I'm trying to use runcode to do this but it's not working. The code for the...
34
by: Umesh | last post by:
how to convert a program to a function/macro and put it in a header file? is there any shortcut method for this? thanks.
12
by: Martin Wells | last post by:
I'm trying to come up with a fully-portable macro for supplying memset with an unsigned char rather than an int. I'm going to think out loud as I go along. . . I'll take a sample system before I...
10
by: Bin Chen | last post by:
switch(i) { case 1: break; case 2: break; case N: break; }
5
by: wizdom | last post by:
I have a simple message board I created a while back. Recently they've asked me to add threading support for messages. so I created a seperate thread page, which gives me each threaded message...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.