Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinhnx/f68cd0954513aa1f353c to your computer and use it in GitHub Desktop.
Save vinhnx/f68cd0954513aa1f353c to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDECodeSnippetCompletionPrefix</key>
<string>@weakselfnotnil</string>
<key>IDECodeSnippetCompletionScopes</key>
<array>
<string>CodeExpression</string>
</array>
<key>IDECodeSnippetContents</key>
<string>@weakselfnotnil(^(&lt;#arguments#&gt;)) {
&lt;#body#&gt;
} @weakselfend</string>
<key>IDECodeSnippetIdentifier</key>
<string>3D5F0063-0266-40FE-AA0B-C3AB67CE491D</string>
<key>IDECodeSnippetLanguage</key>
<string>Xcode.SourceCodeLanguage.Objective-C</string>
<key>IDECodeSnippetSummary</key>
<string>Creates block wrapped to @weakselfnotnil macro</string>
<key>IDECodeSnippetTitle</key>
<string>Weakselfnotnil block</string>
<key>IDECodeSnippetUserSnippet</key>
<true/>
<key>IDECodeSnippetVersion</key>
<integer>2</integer>
</dict>
</plist>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDECodeSnippetCompletionPrefix</key>
<string>@weakself</string>
<key>IDECodeSnippetCompletionScopes</key>
<array>
<string>CodeExpression</string>
</array>
<key>IDECodeSnippetContents</key>
<string>@weakself(^(&lt;#arguments#&gt;)) {
&lt;#body#&gt;
} @weakselfend</string>
<key>IDECodeSnippetIdentifier</key>
<string>FFE129E4-54E5-46D0-8454-B37CC620BB2E</string>
<key>IDECodeSnippetLanguage</key>
<string>Xcode.SourceCodeLanguage.Objective-C</string>
<key>IDECodeSnippetSummary</key>
<string>Creates block wrapped to @weakself macro</string>
<key>IDECodeSnippetTitle</key>
<string>Weakself block</string>
<key>IDECodeSnippetUserSnippet</key>
<true/>
<key>IDECodeSnippetVersion</key>
<integer>2</integer>
</dict>
</plist>
#ifndef DEBUG
#define weakself(ARGS) \
"weakself should be called as @weakself" @"" ? \
({ __weak typeof(self) _private_weakSelf = self; \
ARGS { \
__strong typeof(_private_weakSelf) self __attribute__((unused)) = _private_weakSelf; \
return ^ (void) {
#define weakselfnotnil(ARGS) \
"weakself should be called as @weakself" @"" ? \
({ __weak typeof(self) _private_weakSelf = self; \
ARGS { \
__strong typeof(_private_weakSelf) self __attribute__((unused)) = _private_weakSelf; \
return ^ (void) { if (self)
#else // DEBUG
struct RefCountCheckerData {
CFTypeRef weakSelf;
NSUInteger refCountBefore;
};
static inline void vbr_CheckRefCountForWeakSelf(struct RefCountCheckerData *data) {
const NSUInteger refCountAfter = CFGetRetainCount(data->weakSelf);
const NSUInteger countOfSelfRefInBlock = refCountAfter - data->refCountBefore;
assert(countOfSelfRefInBlock == 0);
}
#define weakself(ARGS) \
"weakself should be called as @weakself" @"" ? \
({ __weak typeof(self) _private_weakSelf = self; \
__attribute__((cleanup(vbr_CheckRefCountForWeakSelf), unused)) \
struct RefCountCheckerData _private_refCountCheckerData = { \
.weakSelf = (__bridge CFTypeRef)self, \
.refCountBefore = CFGetRetainCount((__bridge CFTypeRef)self), \
};\
ARGS { \
__strong typeof(_private_weakSelf) self __attribute__((unused)) = _private_weakSelf; \
return ^ (void) {
#define weakselfnotnil(ARGS) \
"weakself should be called as @weakself" @"" ? \
({ __weak typeof(self) _private_weakSelf = self; \
__attribute__((cleanup(vbr_CheckRefCountForWeakSelf), unused)) \
struct RefCountCheckerData _private_refCountCheckerData = { \
.weakSelf = (__bridge CFTypeRef)self, \
.refCountBefore = CFGetRetainCount((__bridge CFTypeRef)self), \
};\
ARGS { \
__strong typeof(_private_weakSelf) self __attribute__((unused)) = _private_weakSelf; \
return ^ (void) { if (self)
#endif // DEBUG
#define weakselfend \
try {} @finally {} } (); }; \
}) : nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment