c++ - WINAPI_FAMILY_PARTITION 有什么作用?

标签 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)

最佳答案

假设一个文件包含可以在地铁应用程序中使用的代码
所以标题将定义

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 是 3,意味着两个 api 都可以使用 #define WINAPI_FAMILY_PARTITION(Partition),然后您可以指定 Partition = 2, 3 并做 (3 & 1) 或 (3& 2) == 1 这意味着如果 WINAPI_FAMILY = 3,您可以同时使用(桌面或地铁 api) 这就是为什么需要安定

关于c++ - WINAPI_FAMILY_PARTITION 有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32011303/

相关文章:

windows - WinAPI Shell API 与基本 API 和 CRT 函数有何不同?

c++ - 无法在 Eclipse 中运行 SDL 程序,但可以在 Windows 资源管理器中运行

c++ - Qt 文件调整大小在 Linux 中表现怪异

windows - VS 代码首选项的位置

c++ - 在C++中使用 vector 实现堆栈

c++ - 安全派生指针值的示例

c++ - 启用多线程 Eclipse C++

c++ - 在 C++ 中放置新的 VS 显式构造函数调用

c++ - 检查子类是否执行了方法重写

c - 如何在 C win32 控制台应用程序 EXE 文件中保存包含的库