summaryrefslogtreecommitdiff
path: root/platform/darwin/src/NSExpression+MGLAdditions.mm
diff options
context:
space:
mode:
authorJustin R. Miller <incanus@users.noreply.github.com>2016-09-16 11:46:22 -0700
committerGitHub <noreply@github.com>2016-09-16 11:46:22 -0700
commit0bd66d40ddf9e75f860fe18e7c80de9c840f48ac (patch)
tree43e4509dca2ad447cab14454b1da8a7bdfe445be /platform/darwin/src/NSExpression+MGLAdditions.mm
parent767aedc6766d1dd277f674ae54894cfcf21a437a (diff)
downloadqtlocation-mapboxgl-0bd66d40ddf9e75f860fe18e7c80de9c840f48ac.tar.gz
[ios] fixes #6248: properly handle all NSNumber types in filters (#6290)
Diffstat (limited to 'platform/darwin/src/NSExpression+MGLAdditions.mm')
-rw-r--r--platform/darwin/src/NSExpression+MGLAdditions.mm33
1 files changed, 27 insertions, 6 deletions
diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm
index 129c9a740c..52a0b9bd88 100644
--- a/platform/darwin/src/NSExpression+MGLAdditions.mm
+++ b/platform/darwin/src/NSExpression+MGLAdditions.mm
@@ -27,12 +27,33 @@
return { std::string([(NSString *)value UTF8String]) };
} else if ([value isKindOfClass:NSNumber.class]) {
NSNumber *number = (NSNumber *)value;
- if((strcmp([number objCType], @encode(int))) == 0) {
- return { number.intValue };
- } else if ((strcmp([number objCType], @encode(double))) == 0) {
- return { number.doubleValue };
- } else if ((strcmp([number objCType], @encode(char))) == 0) {
- return { number.boolValue };
+ if ((strcmp([number objCType], @encode(char)) == 0) ||
+ (strcmp([number objCType], @encode(BOOL)) == 0)) {
+ // char: 32-bit boolean
+ // BOOL: 64-bit boolean
+ return { (bool)number.boolValue };
+ } else if (strcmp([number objCType], @encode(double)) == 0) {
+ // Double values on all platforms are interpreted precisely.
+ return { (double)number.doubleValue };
+ } else if (strcmp([number objCType], @encode(float)) == 0) {
+ // Float values when taken as double introduce precision problems,
+ // so warn the user to avoid them. This would require them to
+ // explicitly use -[NSNumber numberWithFloat:] arguments anyway.
+ // We still do this conversion in order to provide a valid value.
+ static dispatch_once_t onceToken;
+ dispatch_once(&onceToken, ^{
+ NSLog(@"One-time warning: Float values are converted to double and can introduce imprecision; please use double values explicitly in predicate arguments.");
+ });
+ return { (double)number.doubleValue };
+ } else if ([number compare:@(0)] == NSOrderedDescending ||
+ [number compare:@(0)] == NSOrderedSame) {
+ // Positive integer or zero; use uint64_t per mbgl::Value definition.
+ // We use unsigned long long here to avoid any truncation.
+ return { (uint64_t)number.unsignedLongLongValue };
+ } else if ([number compare:@(0)] == NSOrderedAscending) {
+ // Negative integer; use int64_t per mbgl::Value definition.
+ // We use long long here to avoid any truncation.
+ return { (int64_t)number.longLongValue };
}
}
[NSException raise:@"Value not handled"