ant2009
ant2009

Reputation: 22486

using #define #ifdef for conditions

gcc 4.7.2
c89

Hello,

I have the following code that I am testing that will not modify the rtp stream.

#define DO_MODIFY_RTP
#ifdef DO_NOT_MODIFY_RTP
    if(modify_rtp_media_stream(channel->ipm) == FALSE) {
        status = FALSE;
    }
#endif

When I want to test the modify rtp stream I will set the #define to this:

#define DO_MODIFY_RTP
#ifdef DO_MODIFY_RTP
    if(modify_rtp_media_stream(channel->ipm) == FALSE) {
        status = FALSE;
    }
#endif

Eventfully this will be controlled by a property in a configuration file. But I am just testing both cases before I do that.

Does it seem strange to use #define like this?

Many thanks for any suggestions,

Upvotes: 0

Views: 846

Answers (4)

Jonathan Leffler
Jonathan Leffler

Reputation: 753475

Usually, you don't want to have to modify the source code, so you'd probably do something like this:

#undef MODIFY_RTP
#if defined(DO_NOT_MODIFY_RTP)
#define MODIFY_RTP 0
#else
#define MODIFY_RTP 1
#endif

if (MODIFY_RTP)
{
    if (modify_rtp_media_stream(channel->ipm) == FALSE)
        status = FALSE;
}

On the compiler command line you can have -DDO_NOT_MODIFY_RTP, or in a configuration header you can write #define DO_NOT_MODIFY_RTP, and the RTP stream will not be changed. By omitting the definition of the DO_NOT_MODIFY_RTP macro, the function will be called and the RTP stream could be modified.

Or you can invert the condition so that the default is not to modify the stream:

#if defined(DO_MODIFY_RTP)
#define MODIFY_RTP 1
#else
#define MODIFY_RTP 0
#endif

The big advantage of the code shown is that it is always compiled — but the optimizer will eliminate the call to modify_rtp_media_stream() if MODIFY_RTP is 0. This means that the compiler checks the syntax of the statement, so it doesn't get out of date as the code around it changes. Debug code tends to decay if it is not compiled as the code around it changes but the compiler doesn't notify you of the problems because the preprocessor removed the code so that the compiler proper doesn't see it.

Upvotes: 2

UncleO
UncleO

Reputation: 8449

The method you propose is not standard at all. I had to look closely to notice the macros were different, because in that configuration they are usually the same.

This is more common:

#define DO_MODIFY_RTP
#ifdef DO_MODIFY_RTP
    if(modify_rtp_media_stream(channel->ipm) == FALSE) {
        status = FALSE;
    }
#endif

or

#undef DO_MODIFY_RTP
#ifdef DO_MODIFY_RTP
    if(modify_rtp_media_stream(channel->ipm) == FALSE) {
        status = FALSE;
    }
#endif

Upvotes: 1

Manoj Awasthi
Manoj Awasthi

Reputation: 3520

Test positive

#define DO_MODIFY_RTP
#ifdef DO_MODIFY_RTP // 
if(modify_rtp_media_stream(channel->ipm) == FALSE) {
        status = FALSE;
    }
#endif 

Test negative

#define DO_MODIFY_RTP
#ifndef DO_MODIFY_RTP // 
if(modify_rtp_media_stream(channel->ipm) == FALSE) {
        status = FALSE;
    }
#endif 

Upvotes: 1

Alexander Matusiak
Alexander Matusiak

Reputation: 1828

If the macro is going to used in other files besides that single one, I would advise against doing this. If someone else is going to read the code - they'll have to navigate to another file to find what the macro is defined as.

#define is usually meant to replace text literals or constants that you will be using everywhere like 'true' or 'false'. In this case, if someone uses this and "status" is not being defined or instantiated, it'll throw an error.

Personally, I would just call it a function and name it appropriately as to what it's doing. Just have a function that returns 0 if false, or 1 if true would make it's usage clearer, and if someone else modifies the code less prone to ill-use.

If the project is large enough, you might forget what the #define is in six months, so think ahead and save yourself some pain.

Upvotes: 1

Related Questions