-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgrade ASLayoutElementContext to an Object #trivial #344
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,69 +30,63 @@ | |
|
||
#pragma mark - ASLayoutElementContext | ||
|
||
CGFloat const ASLayoutElementParentDimensionUndefined = NAN; | ||
CGSize const ASLayoutElementParentSizeUndefined = {ASLayoutElementParentDimensionUndefined, ASLayoutElementParentDimensionUndefined}; | ||
|
||
int32_t const ASLayoutElementContextInvalidTransitionID = 0; | ||
int32_t const ASLayoutElementContextDefaultTransitionID = ASLayoutElementContextInvalidTransitionID + 1; | ||
|
||
static inline ASLayoutElementContext _ASLayoutElementContextMake(int32_t transitionID) | ||
{ | ||
struct ASLayoutElementContext context; | ||
context.transitionID = transitionID; | ||
return context; | ||
} | ||
@implementation ASLayoutElementContext | ||
|
||
static inline BOOL _IsValidTransitionID(int32_t transitionID) | ||
- (instancetype)init | ||
{ | ||
return transitionID > ASLayoutElementContextInvalidTransitionID; | ||
if (self = [super init]) { | ||
_transitionID = ASLayoutElementContextDefaultTransitionID; | ||
} | ||
return self; | ||
} | ||
|
||
struct ASLayoutElementContext const ASLayoutElementContextNull = _ASLayoutElementContextMake(ASLayoutElementContextInvalidTransitionID); | ||
@end | ||
|
||
BOOL ASLayoutElementContextIsNull(struct ASLayoutElementContext context) | ||
{ | ||
return !_IsValidTransitionID(context.transitionID); | ||
} | ||
CGFloat const ASLayoutElementParentDimensionUndefined = NAN; | ||
CGSize const ASLayoutElementParentSizeUndefined = {ASLayoutElementParentDimensionUndefined, ASLayoutElementParentDimensionUndefined}; | ||
|
||
ASLayoutElementContext ASLayoutElementContextMake(int32_t transitionID) | ||
{ | ||
NSCAssert(_IsValidTransitionID(transitionID), @"Invalid transition ID"); | ||
return _ASLayoutElementContextMake(transitionID); | ||
} | ||
int32_t const ASLayoutElementContextInvalidTransitionID = 0; | ||
int32_t const ASLayoutElementContextDefaultTransitionID = ASLayoutElementContextInvalidTransitionID + 1; | ||
|
||
pthread_key_t ASLayoutElementContextKey; | ||
|
||
static void ASLayoutElementDestructor(void *p) { | ||
if (p != NULL) { | ||
ASDisplayNodeCFailAssert(@"Thread exited without clearing layout element context!"); | ||
CFBridgingRelease(p); | ||
} | ||
}; | ||
|
||
// pthread_key_create must be called before the key can be used. This function does that. | ||
void ASLayoutElementContextEnsureKey() | ||
{ | ||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
pthread_key_create(&ASLayoutElementContextKey, free); | ||
// NOTE: NULL destructorm means that if a thread | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's up? |
||
pthread_key_create(&ASLayoutElementContextKey, ASLayoutElementDestructor); | ||
}); | ||
} | ||
|
||
void ASLayoutElementSetCurrentContext(struct ASLayoutElementContext context) | ||
void ASLayoutElementPushContext(ASLayoutElementContext *context) | ||
{ | ||
ASLayoutElementContextEnsureKey(); | ||
ASDisplayNodeCAssert(pthread_getspecific(ASLayoutElementContextKey) == NULL, @"Nested ASLayoutElementContexts aren't supported."); | ||
pthread_setspecific(ASLayoutElementContextKey, new ASLayoutElementContext(context)); | ||
// NOTE: It would be easy to support nested contexts – just use an NSMutableArray here. | ||
ASDisplayNodeCAssertNil(ASLayoutElementGetCurrentContext(), @"Nested ASLayoutElementContexts aren't supported."); | ||
pthread_setspecific(ASLayoutElementContextKey, CFBridgingRetain(context)); | ||
} | ||
|
||
struct ASLayoutElementContext ASLayoutElementGetCurrentContext() | ||
ASLayoutElementContext *ASLayoutElementGetCurrentContext() | ||
{ | ||
ASLayoutElementContextEnsureKey(); | ||
auto heapCtx = (ASLayoutElementContext *)pthread_getspecific(ASLayoutElementContextKey); | ||
return (heapCtx ? *heapCtx : ASLayoutElementContextNull); | ||
// Don't retain here. Caller will retain if it wants to! | ||
return (__bridge __unsafe_unretained ASLayoutElementContext *)pthread_getspecific(ASLayoutElementContextKey); | ||
} | ||
|
||
void ASLayoutElementClearCurrentContext() | ||
void ASLayoutElementPopContext() | ||
{ | ||
ASLayoutElementContextEnsureKey(); | ||
auto heapCtx = (ASLayoutElementContext *)pthread_getspecific(ASLayoutElementContextKey); | ||
if (heapCtx != NULL) { | ||
delete heapCtx; | ||
} | ||
ASDisplayNodeCAssertNotNil(ASLayoutElementGetCurrentContext(), @"Attempt to pop context when there wasn't a context!"); | ||
CFBridgingRelease(pthread_getspecific(ASLayoutElementContextKey)); | ||
pthread_setspecific(ASLayoutElementContextKey, NULL); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Indentation looks off.