C宏来启用和禁用代码功能
C macro to enable and disable code features
我以前使用过一个代码库,它有一个用于启用和禁用代码段的宏系统。它看起来像以下内容:
#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?))
您的示例已经达到了您想要的效果,因为如果未定义USING
,#if USING(x)
将产生错误消息。你所需要的头文件是类似的东西
#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
以下工作原理。它将为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 An");
}
if(FEATURE_B){
// Feature B code...
printf("Feature Bn");
}
if(FEATURE_C){
// Feature C code...
printf("Feature Cn");
}
if(FEATURE_D) {// Compile error since FEATURE_D was never defined
// Feature D code...
printf("Feature Dn");
}
}
int main(int argc, char **argv)
{
DoFeatures();
return 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
}
您不能使用另一组#ifdef
吗?
#if defined(WIN32)
#define FEATURE_A
#define FEATURE_B
#elif defined (OSX)
#define FEATURE_C
#endif
// ...
#if defined(FEATURE_A)
do_a();
#endif
等等。
#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
然后您的代码
这样的东西怎么样?
#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 An");
#endif
#if USING( FEATURE_B )
// Feature B code...
printf("Feature Bn");
#endif
#if USING( FEATURE_C )
// Feature C code...
printf("Feature Cn");
#endif
#if defined( FEATURE_D ) // Compile error since FEATURE_D was never defined
// Feature D code...
printf("Feature Dn");
#else
#error FEATURE_D not defined.
#endif
}
int main(int argc, char **argv)
{
DoFeatures();
return 0;
}
对于编译错误,我不知道如何将其集成到宏中。如果有人能告诉我们一些情况,我们将不胜感激P
相关文章:
- 我可以做些什么来消除或最小化这种将提供相同功能和行为的代码重复
- 如何使用 GDB 从功能C++逐步调试到 std::function 用户代码?
- 在大括号内添加语句会更改代码功能?
- c++ Visual Studio 2015 快捷方式,用于从选择代码中生成功能
- 下面的代码是如何工作的?它输出分解的数字并且功能齐全,我只是不明白它是如何做到的
- 如何避免大多数成员功能相同的代码重复
- 如何简化代码并将开关组合成一个功能?
- 维护/维持两个代码集的风险,一个用于 CPU,一个用于 GPU,需要执行非常相似的功能
- 所以我想出了他的代码,但我不确定如何继续删除功能?
- 为什么GCC违反了调用ABS功能的代码,并用简短的参数进行功能
- 如何构建包含CUDA功能和C 模板功能的代码
- 是否有一种方法可以避免在RVALUE和LVALUE参考中创建功能时避免重复的代码
- C //尝试重写一个工作代码以包括我的功能
- 代码仅在2个功能实现中的1个中溢出
- 如何在现有 c++ 代码中以功能方式实现遥测
- 在C/C 中使用检查功能的Windows NT命令行代码循环
- C Linux代码破坏Windows构建,因为缺少标头意味着功能不确定
- 代码由编译器代替,用于成员功能的功能指针
- 此代码中没有匹配功能
- C宏来启用和禁用代码功能