WINAPI_FAMILY_PARTITION做什么?

时间:2015-08-14 13:29:14

标签: c++ windows

我正在阅读头文件 winapifamily.h 的定义,并注意WINAPI_FAMILY_PARTITION的以下定义:

#define WINAPI_FAMILY_PARTITION(Partitions)     (Partitions)

宏的一般用法(作为例子)如下:

#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)

现在我很困惑,似乎相当于

#if WINAPI_PARTITION_APP

拥有#if WINAPI_FAMILY_PARTITION(...)的重点是什么?

这是 winapifamily.h 头文件的相关部分:

/*
 * Header files use the WINAPI_FAMILY_PARTITION macro to assign one or
 * more declarations to some group of partitions.  The macro chooses
 * whether the preprocessor will emit or omit a sequence of declarations
 * bracketed by an #if/#endif pair.  All header file references to the
 * WINAPI_PARTITION_* values should be in the form of occurrences of
 * WINAPI_FAMILY_PARTITION(...).
 *
 * For example, the following usage of WINAPI_FAMILY_PARTITION identifies
 * a sequence of declarations that are part of both the Windows Desktop
 * Partition and the Windows-Phone-Specific Store Partition:
 *
 *     #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_PHONE_APP)
 *     ...
 *     #endif // WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_PHONE_APP)
 *
 * The comment on the closing #endif allow tools as well as people to find the
 * matching #ifdef properly.
 *
 * Usages of WINAPI_FAMILY_PARTITION may be combined, when the partitition definitions are
 * related.  In particular one might use declarations like
 * 
 *     #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
 *
 * or
 *
 *     #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_PHONE_APP)
 *
 * Direct references to WINAPI_PARTITION_ values (eg #if !WINAPI_FAMILY_PARTITION_...)
 * should not be used.
 */ 
#define WINAPI_FAMILY_PARTITION(Partitions)     (Partitions)

2 个答案:

答案 0 :(得分:1)

假设一个文件包含可以在metro app中使用的代码 所以标题将定义

WINAPI_FAMILY = WINAPI_PARTITION_APP

=>WINAPI_FAMILY_DEKSTOP_APP has value of 2

#define WINAPI_FAMILY_PARTITION(Partition)  ((WINAPI_FAMILY & Partition) == Partition)

宏将检查什么是API系列的类型

现在假设您想要只在城域环境下工作的代码,所以

#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
    your code for metro application
#endif

代替值

WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) => ((2 & 2) == 2

现在,如果用户想要使用地铁代码,他们必须定义

为什么需要间接 WINAPI_FAMILY = WINAPI_PARTITION_APP

3 = >  00 11     (both desktop & metro)
2 =>   00 10     (metro apis)
1  =>  00 01     (desktop)

使用此AND函数宏,因为如果WINAPI_FAMILY is 3,则表示可以使用两个apis #define WINAPI_FAMILY_PARTITION(Partition),然后你可以指定Partition = 2,3 做(3& 1)或(3& 2)== 1 这意味着如果WINAPI_FAMILY = 3,你可以同时使用(桌面或地铁apis) 这就是为什么需要anding

答案 1 :(得分:1)

他们现在正在定义一个可能在未来支持进一步智能的API。例如。他们将来可能会将WINAPI_PARTITION_DESKTOP定义为(2 | WINAPI_PARTITION_DEPRECATED),并WINAPI_FAMILY_PARTITION(x)检查WINAPI_PARTITION_DEPRECATED中的x位。如果允许开发人员假设WINAPI_PARTITION_DESKTOP将扩展为单个整数常量表达式,那么这种未来的灵活性(对于Microsoft)是不允许的。