-
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
Reimplement ASRectTable using unordered_map to avoid obscure NSMapTable exception. #719
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// | ||
// ASRectMap.mm | ||
// Texture | ||
// | ||
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
// This source code is licensed under the BSD-style license found in the | ||
// LICENSE file in the /ASDK-Licenses directory of this source tree. An additional | ||
// grant of patent rights can be found in the PATENTS file in the same directory. | ||
// | ||
// Modifications to this file made after 4/13/2017 are: Copyright (c) 2017-present, | ||
// Pinterest, Inc. Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
|
||
#import "ASRectMap.h" | ||
#import "ASObjectDescriptionHelpers.h" | ||
#import <UIKit/UIGeometry.h> | ||
#import <unordered_map> | ||
|
||
@implementation ASRectMap { | ||
std::unordered_map<void *, CGRect> _map; | ||
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. We may should add a comment that we use a 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. Could we not use 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. Unfortunately I don't think we can use
And we could use it via:
|
||
} | ||
|
||
+ (ASRectMap *)rectMapForWeakObjectPointers | ||
{ | ||
return [[self alloc] init]; | ||
} | ||
|
||
- (CGRect)rectForKey:(id)key | ||
{ | ||
auto result = _map.find((__bridge void *)key); | ||
if (result != _map.end()) { | ||
// result->first is the key; result->second is the value, a CGRect. | ||
return result->second; | ||
} else { | ||
return CGRectNull; | ||
} | ||
} | ||
|
||
- (void)setRect:(CGRect)rect forKey:(id)key | ||
{ | ||
if (key) { | ||
_map[(__bridge void *)key] = rect; | ||
} | ||
} | ||
|
||
- (void)removeRectForKey:(id)key | ||
{ | ||
if (key) { | ||
_map.erase((__bridge void *)key); | ||
} | ||
} | ||
|
||
- (id)copyWithZone:(NSZone *)zone | ||
{ | ||
ASRectMap *copy = [ASRectMap rectMapForWeakObjectPointers]; | ||
copy->_map = _map; | ||
return copy; | ||
} | ||
|
||
- (NSMutableArray<NSDictionary *> *)propertiesForDescription | ||
{ | ||
NSMutableArray *result = [NSMutableArray array]; | ||
|
||
// { ptr1->rect1 ptr2->rect2 ptr3->rect3 } | ||
NSMutableString *str = [NSMutableString string]; | ||
for (auto it = _map.begin(); it != _map.end(); it++) { | ||
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. You could also use of a bit of a smaller code:
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. Is this method dangerous, now that the keys are stored unretained? Could we be calling 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. Agree with @Adlai-Holler. Let's pay extra attention here. |
||
[str appendFormat:@" %@->%@", it->first, NSStringFromCGRect(it->second)]; | ||
} | ||
[result addObject:@{ @"ASRectMap": str }]; | ||
|
||
return result; | ||
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.
Edit: NM I see you used |
||
} | ||
|
||
- (NSString *)description | ||
{ | ||
return ASObjectDescriptionMakeWithoutObject([self propertiesForDescription]); | ||
} | ||
|
||
@end |
This file was deleted.
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.
Please update this file license per Danger's comment.