WINAPI_FAMILY_PARTITION 有什么作用?

Yuc*_*ong 5 c++ windows

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

#define WINAPI_FAMILY_PARTITION(Partitions)     (Partitions)
Run Code Online (Sandbox Code Playgroud)

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

#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
Run Code Online (Sandbox Code Playgroud)

现在好迷茫,好像就相当于

#if WINAPI_PARTITION_APP
Run Code Online (Sandbox Code Playgroud)

拥有它有什么意义#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)
Run Code Online (Sandbox Code Playgroud)

vit*_*ito 1

假设一个文件包含可在 Metro 应用程序中使用的代码
,那么标头将定义

WINAPI_FAMILY = WINAPI_PARTITION_APP

=>WINAPI_FAMILY_DEKSTOP_APP has value of 2

#define WINAPI_FAMILY_PARTITION(Partition)  ((WINAPI_FAMILY & Partition) == Partition)
Run Code Online (Sandbox Code Playgroud)

该宏将检查 API 系列的类型

现在假设你想编写仅在 Metro 环境下运行的代码,所以

#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
    your code for metro application
#endif
Run Code Online (Sandbox Code Playgroud)

替换值

WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) => ((2 & 2) == 2
Run Code Online (Sandbox Code Playgroud)

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

为什么需要间接 WINAPI_FAMILY = WINAPI_PARTITION_APP

3 = >  00 11     (both desktop & metro)
2 =>   00 10     (metro apis)
1  =>  00 01     (desktop)
Run Code Online (Sandbox Code Playgroud)

使用这个 AND 函数宏,因为 if WINAPI_FAMILY is 3,意味着两个 api 都可以使用 #define WINAPI_FAMILY_PARTITION(Partition),那么你可以指定 Partition = 2, 3 并执行 (3 & 1) 或 (3& 2) == 1 ,这意味着 if WINAPI_FAMILY = 3,你可以同时使用 (桌面或地铁 api)这就是为什么需要 anding