C宏启用和禁用代码功能

时间:2013-08-21 03:57:49

标签: c++ c macros c-preprocessor preprocessor-directive

我之前使用过代码库,它有一个用于启用和禁用代码段的宏系统。它看起来像下面这样:

#define IN_USE      X
#define NOT_IN_USE  _

#if defined( WIN32 )
    #define FEATURE_A       IN_USE
    #define FEATURE_B       IN_USE
    #define FEATURE_C       NOT_IN_USE
#elif defined( OSX )
    #define FEATURE_A       NOT_IN_USE
    #define FEATURE_B       NOT_IN_USE
    #define FEATURE_C       IN_USE
#else
    #define FEATURE_A       NOT_IN_USE
    #define FEATURE_B       NOT_IN_USE
    #define FEATURE_C       NOT_IN_USE
#endif

然后功能的代码如下:

void DoFeatures()
{
#if USING( FEATURE_A )
    // Feature A code...
#endif

#if USING( FEATURE_B )
    // Feature B code...
#endif

#if USING( FEATURE_C )
    // Feature C code...
#endif

#if USING( FEATURE_D ) // Compile error since FEATURE_D was never defined
    // Feature D code...
#endif
}

我的问题(我不记得的部分)是如何定义'USING'宏,以便在功能尚未定义为'IN_USE'或'NOT_IN_USE'时出错?如果您忘记包含正确的头文件,可能就是这种情况。

#define USING( feature ) ((feature == IN_USE) ? 1 : ((feature == NOT_IN_USE) ? 0 : COMPILE_ERROR?))

6 个答案:

答案 0 :(得分:3)

您的示例已经达到了您想要的效果,因为如果未定义#if USING(x)USING将生成错误消息。您在头文件中需要的只是

#define IN_USE 1
#define NOT_IN_USE 0
#define USING(feature) feature

如果你想确保你只是因为做

之类的事情而得到错误
#if FEATURE

#if USING(UNDEFINED_MISPELED_FEETURE)

那么你可以做,比方说,

#define IN_USE == 1
#define NOT_IN_USE == 0
#define USING(feature) 1 feature

但你无法阻止这种误用

#ifdef FEATURE

答案 1 :(得分:0)

如果它没有被使用,就不要定义它

#if defined( WIN32 )
    #define FEATURE_A       
    #define FEATURE_B       
#else if defined( OSX )
    #define FEATURE_C      
#else

#endif

然后在你的代码中:

void DoFeatures()
{
#ifdef FEATURE_A
    // Feature A code...
#endif

#ifdef FEATURE_B
    // Feature B code...
#endif

#ifdef FEATURE_C
    // Feature C code...
#endif

#ifdef FEATURE_D // Compile error since FEATURE_D was never defined
    // Feature D code...
#endif
}

答案 2 :(得分:0)

你不能只使用另一组#ifdef s?

#if defined(WIN32)
    #define FEATURE_A
    #define FEATURE_B
#elif defined (OSX)
    #define FEATURE_C
#endif

// ...

#if defined(FEATURE_A)
    do_a();
#endif

答案 3 :(得分:0)

#define IN_USE      1
//#define NOT_IN_USE  _  //Not required


#if defined( WIN32 )
    #define FEATURE_A       IN_USE
    #define FEATURE_B       IN_USE
    #define FEATURE_C       NOT_IN_USE
#elif defined( OSX )
    #define FEATURE_A       NOT_IN_USE
    #define FEATURE_B       NOT_IN_USE
    #define FEATURE_C       IN_USE
#else
    #define FEATURE_A       NOT_IN_USE
    #define FEATURE_B       NOT_IN_USE
    #define FEATURE_C       NOT_IN_USE
#endif

#define USING( feature ) feature

然后你的代码

答案 4 :(得分:0)

这样的事情怎么样?

#define IN_USE      1
#define NOT_IN_USE  0

#if defined( WIN32 )
    #define FEATURE_A       IN_USE
    #define FEATURE_B       IN_USE
    #define FEATURE_C       NOT_IN_USE
#elif defined( OSX )
    #define FEATURE_A       NOT_IN_USE
    #define FEATURE_B       NOT_IN_USE
    #define FEATURE_C       IN_USE
#else
    #define FEATURE_A       NOT_IN_USE
    #define FEATURE_B       NOT_IN_USE
    #define FEATURE_C       NOT_IN_USE
#endif

#define USING(f) ((f == IN_USE) ? 1 : (f == NOT_IN_USE) ? 0 : (f))

#include <stdio.h>

void DoFeatures()
{
#if USING( FEATURE_A )
    // Feature A code...
    printf("Feature A\n");
#endif

#if USING( FEATURE_B )
    // Feature B code...
    printf("Feature B\n");
#endif

#if USING( FEATURE_C )
    // Feature C code...
    printf("Feature C\n");
#endif

#if defined( FEATURE_D ) // Compile error since FEATURE_D was never defined
    // Feature D code...
    printf("Feature D\n");
#else
#error FEATURE_D not defined.
#endif
}

int main(int argc, char **argv)
{
    DoFeatures();
    return 0;
}

对于编译错误,我不知道如何将它集成到宏中。如果有人能为我们提供一些启示,将不胜感激。 :P

答案 5 :(得分:0)

以下作品。它将为FEATURE_D提供编译错误。如果您注释掉FEATURE_D的代码,那么它将执行FEATURE_A和FEATURE_B的代码。代码几乎是不言自明的。您可以将它们放在if块中,而不是检查是否在DoFeatures函数中定义了FEATURE_D或其他内容。这样,编译器将尝试执行代码块。如果是1,则if块中的代码将被执行;在0的情况下,它将不会被执行。如果它从未被定义,那么将得到编译错误。

#include <stdio.h>

#define IN_USE      1
#define NOT_IN_USE  0

#define FEATURE_A       IN_USE
#define FEATURE_B       IN_USE
#define FEATURE_C       NOT_IN_USE


void DoFeatures()
{
    if(FEATURE_A){
        // Feature A code...
        printf("Feature A\n");
    }

    if(FEATURE_B){
        // Feature B code...
        printf("Feature B\n");
    }

    if(FEATURE_C){
        // Feature C code...
        printf("Feature C\n");
    }

    if(FEATURE_D) {// Compile error since FEATURE_D was never defined
        // Feature D code...
        printf("Feature D\n");
    }
}

int main(int argc, char **argv)
{
    DoFeatures();
    return 0;
}