Availability.h-like宏 [英] Availability.h -like macro

查看:190
本文介绍了Availability.h-like宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用自定义可用性宏,例如__OSX_AVAILABLE_STARTING。我需要它以相同的方式执行,我只需要更改其名称和版本和参数的数量?

解决方案

是的,当然。 Objective-C是C的一个严格的超集,所以C宏是非常在你的处置,并且该设施只是一组C宏,最终扩展为
gcc的 __ attribute __ 关键字声明函数的特殊属性。 p>

相关声明全部在





要刷新,您可以使用 __ OSX_AVAILABLE_STARTING 宏将函数声明标记为支持

  extern void mymacfunc()__OSX_AVAILABLE_STARTING(__ MAC_10_5,__ IPHONE_NA); 

那么我们需要什么来实现呢?如果你删除他们的
支持两个不同的操作系统(mac,iphone),可用性设施归结为:


  1. 一个包含 __ MY_AVAILABLE_STARTING(< version>)的版本参数的宏:

      #define __MY_AVAILABLE_STARTING(_myversion)__MY_AVAILABILITY_INTERNAL ## _ myversion 


  2. ,像 Availability.h 中那些是上述的有效参数的那些:

      #define __MYVER_2_0 20000 
    #define __MYVER_2_1 20100
    #define __MYVER_2_2 20200
    #define __MYVER_3_0 30000


  3. 另一组宏,如 AvailabilityInternal.h 中的指定每个版本应该发生什么,不可用,弱等)。同样,这是编译器的函数,请参见 gcc docs (还有很多其他有趣的选项):

      #define __MY_AVAILABILITY_INTERNAL__MYVER_2_0 __AVAILABILITY_INTERNAL_UNAVAILABLE 
    #define __MY_AVAILABILITY_INTERNAL__MYVER_2_1 __AVAILABILITY_INTERNAL_WEAK_IMPORT
    #define __MY_AVAILABILITY_INTERNAL__MYVER_2_1 __AVAILABILITY_INTERNAL_REGULAR


  4. ,其中buck结束,宏扩展到 __属性__ 设施。



    对于上面的那些,你可以继续使用苹果的宏:

      #define __AVAILABILITY_INTERNAL_DEPRECATED __attribute __((弃用,可见性(默认)))
    #define __AVAILABILITY_INTERNAL_UNAVAILABLE __attribute __((不可用,可见性(默认)))
    #define __AVAILABILITY_INTERNAL_WEAK_IMPORT __attribute __ weak_import,visibility(default)))
    #define __AVAILABILITY_INTERNAL_REGULAR __attribute __((visibility(default)))

    或者,当然,你可以定义自己的疯狂。


东西,经常被忽视。祝你好运!


Is it possible to have a custom availability macro like the __OSX_AVAILABLE_STARTING for instance. I need it to perform in the same way, I just need to change its name and the versions and number of parameters?

解决方案

Yes, certainly. Objective-C is a strict superset of C, so C macros are very much at your disposal, and that facility is simply a set of C macros that eventually expand to gcc's __attribute__ keyword to declare special attributes of a function.

The relevant declarations are all in

To refresh, you use the __OSX_AVAILABLE_STARTING macro to tag a function declaration as being supported for a particular version, like this:

extern void mymacfunc() __OSX_AVAILABLE_STARTING(__MAC_10_5,__IPHONE_NA);

So what do we need to implement this ourselves? If you strip their support for two different OS (mac, iphone), the availability facility boils down to:

  1. A macro that takes a version argument like __MY_AVAILABLE_STARTING(<version>):

    #define __MY_AVAILABLE_STARTING(_myversion) __MY_AVAILABILITY_INTERNAL##_myversion
    

  2. Set of version arguments, like those in Availability.h, that are valid arguments for the above:

    #define __MYVER_2_0     20000  
    #define __MYVER_2_1     20100  
    #define __MYVER_2_2     20200  
    #define __MYVER_3_0     30000  
    

  3. Another set of macros, like thos in AvailabilityInternal.h that specifies what should happen for each version (regular support, deprecated, unavailable, weak, etc). Again, this is a function of the compiler, see gcc docs (there are lots of other interesting options):

    #define __MY_AVAILABILITY_INTERNAL__MYVER_2_0 __AVAILABILITY_INTERNAL_UNAVAILABLE
    #define __MY_AVAILABILITY_INTERNAL__MYVER_2_1 __AVAILABILITY_INTERNAL_WEAK_IMPORT
    #define __MY_AVAILABILITY_INTERNAL__MYVER_2_1 __AVAILABILITY_INTERNAL_REGULAR
    

  4. And finally, where the buck ends, the macros that expand to the __attribute__ facility.

    For the ones I have above, you can just keep using Apple's macros:

    #define __AVAILABILITY_INTERNAL_DEPRECATED         __attribute__((deprecated,visibility("default")))
    #define __AVAILABILITY_INTERNAL_UNAVAILABLE        __attribute__((unavailable,visibility("default")))
    #define __AVAILABILITY_INTERNAL_WEAK_IMPORT        __attribute__((weak_import,visibility("default")))
    #define __AVAILABILITY_INTERNAL_REGULAR            __attribute__((visibility("default")))
    

    Or, of course, you can define your own craziness.

C Macros are powerful stuff, often overlooked. Good luck!

这篇关于Availability.h-like宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆