Xcode - 宏的行为是否有所不同,具体取决于它是包含在 .h 文件还是 .m 文件中?

发布于 2024-11-17 12:24:29 字数 655 浏览 3 评论 0原文

我想在一个文件中包含一个宏 SomeMacro(city, Country),该文件将位于文件 MacroFile.h 中,我将从 .h 文件或 .m 文件中 #include 该文件。我希望 SomeMacro 变得不同,具体取决于包含树中 MacroFile.h 上方的文件是 .h 文件还是 .m 文件。我想在 .m 文件中不定义特殊常量的情况下执行此操作。这可能吗?

在伪代码中,我希望 MacroFile.h 做的是这样的:

#if (file from which I was directly included is a .h)
#define SomeMacro(city, country) SomeStuffInvolvingCityAndCountry
#else
#define SomeMacro(city, country) SomeDifferentStuffInvolvingCityAndCountry
#endif

SomeMacro(washington, USA)
SomeMacro(tokyo, Japan)
SomeMacro(berlin, Germany)

如果您还可以让 MacroFile.h 检查包含树中它上面两层的内容,那就加分了。

编辑:如果有一种方法可以让宏判断它是否是从 @implementation 块内部调用的,那就足够了。

I want to have a macro SomeMacro(city, country) in a file that will be in a file MacroFile.h that I will #include from either a .h file or a .m file. And I want SomeMacro to become something different depending on whether the file immediately above MacroFile.h in the include tree is a .h file or a .m file. I want to do this without defining a special constant in the .m file. Is that possible?

In pseudo-code, what I want MacroFile.h to do is this:

#if (file from which I was directly included is a .h)
#define SomeMacro(city, country) SomeStuffInvolvingCityAndCountry
#else
#define SomeMacro(city, country) SomeDifferentStuffInvolvingCityAndCountry
#endif

SomeMacro(washington, USA)
SomeMacro(tokyo, Japan)
SomeMacro(berlin, Germany)

Bonus points if you can also get MacroFile.h to examine what is two levels above it in the include tree.

EDIT: If there is a way for the macro to tell whether or not it is being called from inside an @implementation block, that would be good enough.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

壹場煙雨 2024-11-24 12:24:29

没有预处理器测试来确定宏是从哪里扩展的。 .h 是头文件的约定,但没有语义价值。

您可以创建两个文件 MacroFile-header.hMacroFile-code.h,如下所示:

MacroFile-header.h:

// definitions
#define CityCountryMacro(city, country)  SomeStuffInvolvingCityAndCountry
#define StateZipMacro(state, zip)        SomeStuffInvolvingStateAndZip

MacroFile -code.h

// undefine MacroFile-header.h macros
#if defined(CityCountryMacro)
    #undef CityCountryMacro
#endif
#if defined(StateZipMacro)
    #undef StateZipMacro
#endif

// definitions
#define CityCountryMacro(city, country)  OtherStuffInvolvingCityAndCountry
#define StateZipMacro(state, zip)        OtherStuffInvolvingStateAndZip

始终在头文件中导入MacroFile-header.h,如下所示:

SomeObject.h

#import "MacroFile-header.h"

// use macros

MacroFile-code。 h 所有其他导入之后,在您的实现中:

SomeObject.m

#import "SomeObject.h"
#import ...
#import ...

// always include last
#import "MacroFile-code.h"

// use macros

There is no preprocessor test to determine where the macro is being expanded from. .h is a convention for a header file, but has no semantic value.

You could create two files MacroFile-header.h and MacroFile-code.h as follows:

MacroFile-header.h:

// definitions
#define CityCountryMacro(city, country)  SomeStuffInvolvingCityAndCountry
#define StateZipMacro(state, zip)        SomeStuffInvolvingStateAndZip

MacroFile-code.h:

// undefine MacroFile-header.h macros
#if defined(CityCountryMacro)
    #undef CityCountryMacro
#endif
#if defined(StateZipMacro)
    #undef StateZipMacro
#endif

// definitions
#define CityCountryMacro(city, country)  OtherStuffInvolvingCityAndCountry
#define StateZipMacro(state, zip)        OtherStuffInvolvingStateAndZip

Always import MacroFile-header.h in your header files, as follows:

SomeObject.h:

#import "MacroFile-header.h"

// use macros

and MacroFile-code.h after all other imports, in your implementation:

SomeObject.m:

#import "SomeObject.h"
#import ...
#import ...

// always include last
#import "MacroFile-code.h"

// use macros
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文