Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   macro help (http://www.velocityreviews.com/forums/t457784-macro-help.html)

 ankitks@yahoo.com 10-22-2006 01:45 AM

macro help

guys,
I have to do some testing, and usally code flows like this. If
something is wrong, flag error. And if it is correct, see if something
else is wrong, flag error! And recursive like that.

if (a != b) {
flag_error();
}
else {
if ( c != d) {
flag_error();
}
else {
if ( e != f) {
flag_error();
}
} //end of else c != d
} //end of else a !=b

it is really annoying to read code later for debugging. Using a !=b ?
flag_error() : c != d ? ...doesn't seem that practical either.
how can I convert this into macro so that I can use it more readable
something like this!

VERIFY a == b OTHERWISE flag_error();
GO_NEXT;
VERIFY c == d OTHERWISE flag_error();
GO_NEXT;
VERIFY e == f OTHERWISE flag_error();
is this recursive macro possible.

Sorry, for asking dumb quesitons. Trying to make life easy, if possible.

 Clark S. Cox III 10-22-2006 01:52 AM

Re: macro help

ankitks@yahoo.com wrote:
> guys,
> I have to do some testing, and usally code flows like this. If
> something is wrong, flag error. And if it is correct, see if something
> else is wrong, flag error! And recursive like that.
>
> if (a != b) {
> flag_error();
> }
> else {
> if ( c != d) {
> flag_error();
> }
> else {
> if ( e != f) {
> flag_error();
> }
> } //end of else c != d
> } //end of else a !=b
>

What's wrong with (it seems at least as readable as your proposed solution):
if (a != b) {
flag_error();
}
else if ( c != d) {
flag_error();
}
else if ( e != f) {
flag_error();
}
else
{
//If we get here, nothing is wrong
}

Or, using exceptions:
if (a != b) {
throw some_error;
}
if ( c != d) {
throw some_error;
}
if ( e != f) {
throw some_error;
}
//If we get here, nothing is wrong

--
Clark S. Cox III
clarkcox3@gmail.com

 David Harmon 10-22-2006 02:04 AM

Re: macro help

On 21 Oct 2006 18:45:13 -0700 in comp.lang.c++, ankitks@yahoo.com
wrote,
>I have to do some testing, and usally code flows like this. If
>something is wrong, flag error. And if it is correct, see if something
>else is wrong, flag error! And recursive like that.

It's actually not recursive; rather, it's alternative.

My version:

if ((a != b)
|| (c != d)
|| (e != f)) flag_error();

 Fox 10-22-2006 10:51 AM

Re: macro help

> if ((a != b)
> || (c != d)
> || (e != f)) flag_error();

That's good if there is one error to all cases.

 David Harmon 10-22-2006 03:27 PM

Re: macro help

On Sun, 22 Oct 2006 12:51:11 +0200 in comp.lang.c++, "Fox"
<roxyfox@o2.pl> wrote,
>
>> if ((a != b)
>> || (c != d)
>> || (e != f)) flag_error();

>
>That's good if there is one error to all cases.

That seemed to be the example given.
If not, then my next suggestion is:

if (a != b) return flag_error();
if (c != d) return flag_error();
if (e != f) return flag_error();

 All times are GMT. The time now is 10:31 PM.