__attribute__

2017-04-26  本文已影响12人  Bearger

懒得找出处了~!

    In fact, when __attribute__ was first introduced to GCC, it was faced with some resistance by some who suggested that #pragma be used exclusively for the same purposes.

    There were, however, two very good reasons why __attribute__ was added:

    It was impossible to generate #pragma commands from a macro (before the C99 _Pragma operator).
    There is no telling what the same #pragma might mean in another compiler.
    Quoth the GCC Documentation for Function Attributes:

    These two reasons applied to almost any application that might have been proposed for #pragma. It was basically a mistake to use #pragma for anything.

    Indeed, if you look at modern Objective-C–in the headers of Apple frameworks and well-engineered open-source projects–__attribute__ is used for myriad purposes. (By contrast, #pragma’s main claim to fame these days is decoration: #pragma mark)
__attribute__((objc_requires_super))
//eg:略
__attribute__((objc_boxable))

typedef struct __attribute__((objc_boxable)) {
    CGFloat x, y, width, height;
} YYGRect;

YYGRect rect2 = {1, 2, 3, 4};
NSValue *value2 = @(rect2);      //编译通过

#include<stdio.h> 
__attribute__((constructor)) void before_main() { 
   printf("before main\n"); 
} 

__attribute__((destructor)) void after_main() { 
   printf("after main\n"); 
} 

int main(int argc, char **argv) { 
   printf("in main\n"); 
   return 0; 
}
// 指定一个cleanup方法,注意入参是所修饰变量的地址,类型要一样
// 对于指向objc对象的指针(id *),如果不强制声明__strong默认是
// __autoreleasing,造成类型不匹配
__strong NSString *string __attribute__((cleanup(Show))) = @"Yin Yu 思密达";

...//这里写N多代码,都是最后执行下面的函数

static void Show(__strong NSString **string)
{   
    NSLog(@"代码以为我为标志结束!%@",*string);
}

//eg:

// 函数里面:
// 加了个`unused`的attribute用来消除`unused variable`的warning
__strong void(^block)(void) __attribute__((cleanup(blockCleanUp), unused)) = ^{
        NSLog(@"代码以为我标志结束!");
    };

//所调用的函数
// void(^block)(void)的指针是void(^*block)(void)
static void blockCleanUp(__strong void(^*block)(void)) 
{
    (*block)();
}
//定义宏
#define onExit __strong void(^block)(void) __attribute__((cleanup(blockCleanUp), unused)) = ^

//使用的地方
onExit {
        NSLog(@"代码以为为结束");
    };
//下面~~~~~~~~N多代码



# 所调用的函数
// void(^block)(void)的指针是void(^*block)(void)
static void blockCleanUp(__strong void(^*block)(void)) 
{
    (*block)();
}
    typedef void (^mtl_cleanupBlock_t)();

    #define metamacro_concat_(A, B) A ## B

    #define metamacro_concat(A, B) \
            metamacro_concat_(A, B)

        #define onExit \
        try {} @finally {} \
        __strong mtl_cleanupBlock_t metamacro_concat(mtl_exitBlock_, __LINE__) __attribute__((cleanup(mtl_executeCleanupBlock), unused)) = ^


        + (void)enumeratePropertiesUsingBlock:(void (^)(objc_property_t property, BOOL *stop))block {
        Class cls = self;
        BOOL stop = NO;

        while (!stop && ![cls isEqual:MTLModel.class]) {
            unsigned count = 0;
            objc_property_t *properties = class_copyPropertyList(cls, &count);

            cls = cls.superclass;
            if (properties == NULL) continue;
    //注意这里的用法
            @onExit {
                free(properties);
            };

            for (unsigned i = 0; i < count; i++) {
                block(properties[i], &stop);
                if (stop) break;
            }
        }
    }
    //@onExit 宏展开之后
    @try {} @finally {}
            __strong mtl_cleanupBlock_t mtl_exitBlock___LINE__ __attribute__((cleanup(mtl_executeCleanupBlock), unused)) = ^{
                free(properties);
            };
//可以保证 程序在即将运行出 propertties的作用时释放  properties
__attribute__((overloadable)) void logAnything(id arr) 
{
    NSLog(@"%@", arr);
}
__attribute__((overloadable)) void logAnything(int num) 
{
    NSLog(@"%@", @(num));
}
__attribute__((overloadable)) void logAnything(CGRect rect)
{
    NSLog(@"%@", NSStringFromCGRect(rect));
}
#调用
//传递数值
    logAnything(@[@"hello",@"world"]);  
//int 数值 
    logAnything(66);                               
//Rect
    logAnything(CGRectMake(0, 0, 0, 0));
_attribute__((objc_runtime_name("FalseEunuch")))    
//相当于给Eunuch起个学名
@interface Eunuch : NSObject

+(void)show;

@end
//示例:
 NSLog(@"%@",[Eunuch class]);
//得到的打印消息
//2016-08-03 14:16:46.882 0803-黑魔法[24329:1921649] FalseEunuch
- (CGSize)sizeWithFont:(UIFont *)font NS_DEPRECATED_IOS(2_0, 7_0, "Use -sizeWithAttributes:") __TVOS_PROHIBITED;
//来看一下 后边的宏
#define NS_DEPRECATED_IOS(_iosIntro, _iosDep, ...) CF_DEPRECATED_IOS(_iosIntro, _iosDep, __VA_ARGS__)

define CF_DEPRECATED_IOS(_iosIntro, _iosDep, ...) __attribute__((availability(ios,introduced=_iosIntro,deprecated=_iosDep,message="" __VA_ARGS__)))
//宏展开以后如下
__attribute__((availability(ios,introduced=2_0,deprecated=7_0,message=""__VA_ARGS__)));
//iOS即是iOS平台
//introduced 从哪个版本开始使用
//deprecated 从哪个版本开始弃用
//警告的消息
//其实还可以再加一个参数例如
void f(void) __attribute__((availability(macosx,introduced=10.4,deprecated=10.6,obsoleted=10.7)));
//obsoleted完全禁止使用的版本
上一篇下一篇

猜你喜欢

热点阅读