473,321 Members | 1,622 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,321 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 3867
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
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.