Skip to content

Instantly share code, notes, and snippets.

@jspahrsummers
Last active December 18, 2015 16:29
Show Gist options
  • Save jspahrsummers/5812222 to your computer and use it in GitHub Desktop.
Save jspahrsummers/5812222 to your computer and use it in GitHub Desktop.
Make "self" cause a compilation error only within blocks.
#define self \
( \
_Pragma("clang diagnostic push") _Pragma("clang diagnostic ignored \"-Wunused-value\"") self, \
/* Depends on _cmd being a const copy, like other captured variables. */ \
_Pragma("clang diagnostic pop") (*(_cmd = _cmd, &self)) \
)
// Replace libextobjc's @weakify with one that's aware of the "self" macro.
#undef ext_weakify_
#define ext_weakify_(INDEX, CONTEXT, VAR) \
CONTEXT metamacro_if_eq(metamacro_argcount(VAR), metamacro_argcount(, VAR))(ext_weakify__(VAR))(ext_weakify_self)
#define ext_weakify__(VAR) \
__typeof__(VAR) metamacro_concat(VAR, _weak_) = (VAR);
#define ext_weakify_self \
ext_weakify__(metamacro_head self) \
/* The diagnostic pop from above got stripped out, so we add it back in manually. */ \
_Pragma("clang diagnostic pop") \
SEL _cmd_copy = _cmd;
// Replace libextobjc's @strongify with one that's aware of the "self" macro.
#undef ext_strongify_
#define ext_strongify_(INDEX, VAR) \
metamacro_if_eq(metamacro_argcount(VAR), metamacro_argcount(, VAR))(ext_strongify__(VAR))(ext_strongify_self)
#define ext_strongify__(VAR) \
__strong __typeof__(VAR) VAR = metamacro_concat(VAR, _weak_);
#define ext_strongify_self \
ext_strongify__(metamacro_head self) \
/* The diagnostic pop from above got stripped out, so we add it back in manually. */ \
_Pragma("clang diagnostic pop") \
/* Replace _cmd with a mutable variable so that the "self" macro works fine in the block. */ \
SEL _cmd = _cmd_copy;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment