C代码宏启用和禁用code功能 [英] C macro to enable and disable code features
问题描述
我已经使用了code基在此之前,有启用和禁用code的部分的宏系统。它看起来像下面这样:
的#define IN_USE点¯x
#定义NOT_IN_USE _#如果定义(WIN32)
#定义FEATURE_A IN_USE
#定义FEATURE_B IN_USE
#定义FEATURE_C NOT_IN_USE
#elif指令定义(OSX)
#定义FEATURE_A NOT_IN_USE
#定义FEATURE_B NOT_IN_USE
#定义FEATURE_C IN_USE
#其他
#定义FEATURE_A NOT_IN_USE
#定义FEATURE_B NOT_IN_USE
#定义FEATURE_C NOT_IN_USE
#万一
然后code为特色,看起来像:
无效DoFeatures()
{
#如果使用(FEATURE_A)
//功能的code ...
#万一#如果使用(FEATURE_B)
//特征B code ...
#万一#如果使用(FEATURE_C)
//功能C $ C $ ...Ç
#万一#如果使用(FEATURE_D)//因为FEATURE_D从未定义编译错误
//特征量D code ...
#万一
}
我的问题(我不记得的部分)是如何定义,使其错误,如果功能尚未定义为IN_USE'或'NOT_IN_USE的使用宏?这可能是情况下,如果你忘记包含正确的头文件。
的#define使用(功能)((功能== IN_USE)?1:((功能== NOT_IN_USE)?0:?COMPILE_ERROR))
您已经例如达到你想要什么,因为#如果使用(X)
将产生一个错误信息如果使用
没有定义。所有你在你的头文件需要的是一样的东西。
的#define IN_USE 1
#定义NOT_IN_USE 0
使用的#define(功能)功能
如果你想确保你还得到一个错误只是做这样的事情。
#如果FEATURE
或
#如果使用(UNDEFINED_MISPELED_FEETURE)
那么你可以做,比如说,
的#define IN_USE == 1
#定义NOT_IN_USE == 0
使用的#define(功能)1功能
但你不能prevent这种滥用为
#IFDEF FEATURE
I've used a code base before that had a macro system for enabling and disabling sections of code. It looked something like the following:
#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
Then the code for the features would look like:
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
}
My question (the part I don't remember) is how to define the 'USING' macro so that it errors if the feature hasn't been defined as 'IN_USE' or 'NOT_IN_USE'? Which could be the case if you forget to include the correct header file.
#define USING( feature ) ((feature == IN_USE) ? 1 : ((feature == NOT_IN_USE) ? 0 : COMPILE_ERROR?))
Your example already achieves what you want, since #if USING(x)
will produce an error message if USING
isn't defined. All you need in your header file is something like
#define IN_USE 1
#define NOT_IN_USE 0
#define USING(feature) feature
If you want to be sure that you also get an error just for doing something like
#if FEATURE
or
#if USING(UNDEFINED_MISPELED_FEETURE)
then you could do, say,
#define IN_USE == 1
#define NOT_IN_USE == 0
#define USING(feature) 1 feature
but you won't be able to prevent such misuse as
#ifdef FEATURE
这篇关于C代码宏启用和禁用code功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!