iOS 宏定义~ 安全指针

//联系人:石虎  QQ: 1224614774 昵称:嗡嘛呢叭咪哄


一、概念

/**

 * \@onExit defines some code to be executed when the current scope exits. The

 * code must be enclosed in braces and terminated with a semicolon, and will be

 * executed regardless of how the scope is exited, including from exceptions,

 * \c goto, \c return, \c break, and \c continue.

 *

 * Provided code will go into a block to be executed later. Keep this in mind as

 * it pertains to memory management, restrictions on assignment, etc. Because

 * the code is used within a block, \c return is a legal (though perhaps

 * confusing) way to exit the cleanup block early.

 *

 * Multiple \@onExit statements in the same scope are executed in reverse

 * lexical order. This helps when pairing resource acquisition with \@onExit

 * statements, as it guarantees teardown in the opposite order of acquisition.

 *

 * @note This statement cannot be used within scopes defined without braces

 * (like a one line \c if). In practice, this is not an issue, since \@onExit is

 * a useless construct in such a case anyways.

 */

#define onExit \

rac_keywordify \

__strong rac_cleanupBlock_t metamacro_concat(rac_exitBlock_, __LINE__) __attribute__((cleanup(rac_executeCleanupBlock), unused)) = ^


/**

 * Creates \c __weak shadow variables for each of the variables provided as

 * arguments, which can later be made strong again with #strongify.

 *

 * This is typically used to weakly reference variables in a block, but then

 * ensure that the variables stay alive during the actual execution of the block

 * (if they were live upon entry).

 *

 * See #strongify for an example of usage.

 */

#define weakify(...) \

rac_keywordify \

metamacro_foreach_cxt(rac_weakify_,, __weak, __VA_ARGS__)


/**

 * Like #weakify, but uses \c __unsafe_unretained instead, for targets or

 * classes that do not support weak references.

 */

#define unsafeify(...) \

rac_keywordify \

metamacro_foreach_cxt(rac_weakify_,, __unsafe_unretained, __VA_ARGS__)


/**

 * Strongly references each of the variables provided as arguments, which must

 * have previously been passed to #weakify.

 *

 * The strong references created will shadow the original variable names, such

 * that the original names can be used without issue (and a significantly

 * reduced risk of retain cycles) in the current scope.

 *

 * @code

 

 id foo = [[NSObject alloc] init];

 id bar = [[NSObject alloc] init];

 

 @weakify(foo, bar);

 

 // this block will not keep 'foo' or 'bar' alive

 BOOL (^matchesFooOrBar)(id) = ^ BOOL (id obj){

 // but now, upon entry, 'foo' and 'bar' will stay alive until the block has

 // finished executing

 @strongify(foo, bar);

 

 return [foo isEqual:obj] || [bar isEqual:obj];

 };

 

 * @endcode

 */

#define strongify(...) \

rac_keywordify \

_Pragma("clang diagnostic push") \

_Pragma("clang diagnostic ignored \"-Wshadow\"") \

metamacro_foreach(rac_strongify_,, __VA_ARGS__) \

_Pragma("clang diagnostic pop")


谢谢!!!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值