summaryrefslogtreecommitdiff
path: root/platform/darwin/src/NSPredicate+MGLAdditions.mm
blob: 9c3f9c888c6b386d07d0ff32b92e590b5c5f90f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#import "NSPredicate+MGLAdditions.h"

#import "MGLValueEvaluator.h"

class FilterEvaluator {
public:
    
    NSArray* getPredicates(std::vector<mbgl::style::Filter> filters) {
        NSMutableArray *predicates = [NSMutableArray arrayWithCapacity:filters.size()];
        for (auto filter : filters) {
            [predicates addObject:mbgl::style::Filter::visit(filter, FilterEvaluator())];
        }
        return predicates;
    }
    
    NSArray* getValues(std::vector<mbgl::Value> values) {
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:values.size()];
        for (auto value : values) {
            [array addObject:mbgl::Value::visit(value, ValueEvaluator())];
        }
        return array;
    }
    
    NSPredicate* operator()(mbgl::style::NullFilter filter) {
        return nil;
    }
    
    NSPredicate* operator()(mbgl::style::EqualsFilter filter) {
        return [NSPredicate predicateWithFormat:@"%K == %@", @(filter.key.c_str()), mbgl::Value::visit(filter.value, ValueEvaluator())];
    }
    
    NSPredicate* operator()(mbgl::style::NotEqualsFilter filter) {
        return [NSPredicate predicateWithFormat:@"%K != %@", @(filter.key.c_str()), mbgl::Value::visit(filter.value, ValueEvaluator())];
    }
    
    NSPredicate* operator()(mbgl::style::GreaterThanFilter filter) {
        return [NSPredicate predicateWithFormat:@"%K > %@", @(filter.key.c_str()), mbgl::Value::visit(filter.value, ValueEvaluator())];
    }
    
    NSPredicate* operator()(mbgl::style::GreaterThanEqualsFilter filter) {
        return [NSPredicate predicateWithFormat:@"%K >= %@", @(filter.key.c_str()), mbgl::Value::visit(filter.value, ValueEvaluator())];
    }
    
    NSPredicate* operator()(mbgl::style::LessThanFilter filter) {
        return [NSPredicate predicateWithFormat:@"%K < %@", @(filter.key.c_str()), mbgl::Value::visit(filter.value, ValueEvaluator())];
    }
    
    NSPredicate* operator()(mbgl::style::LessThanEqualsFilter filter) {
        return [NSPredicate predicateWithFormat:@"%K <= %@", @(filter.key.c_str()), mbgl::Value::visit(filter.value, ValueEvaluator())];
    }
    
    NSPredicate* operator()(mbgl::style::InFilter filter) {
        return [NSPredicate predicateWithFormat:@"%K IN %@", @(filter.key.c_str()), getValues(filter.values)];
    }
    
    NSPredicate* operator()(mbgl::style::NotInFilter filter) {
        return [NSPredicate predicateWithFormat:@"NOT %K IN %@", @(filter.key.c_str()), getValues(filter.values)];
    }
    
    NSPredicate* operator()(mbgl::style::AnyFilter filter) {
        return [NSCompoundPredicate orPredicateWithSubpredicates:getPredicates(filter.filters)];
    }
    
    NSPredicate* operator()(mbgl::style::AllFilter filter) {
        return [NSCompoundPredicate andPredicateWithSubpredicates:getPredicates(filter.filters)];
    }
    
    NSPredicate* operator()(mbgl::style::NoneFilter filter) {
        NSArray *predicates = getPredicates(filter.filters);
        if (predicates.count > 1) {
            NSCompoundPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];
            return [NSCompoundPredicate notPredicateWithSubpredicate:predicate];
        } else {
            return [NSCompoundPredicate notPredicateWithSubpredicate:predicates.firstObject];
        }
    }
    
    NSPredicate* operator()(mbgl::style::HasFilter filter) {
        [NSException raise:@"Unsupported filter type"
                    format:@"Cannot convert mbgl::style::HasFilter to NSPredicate"];
        return nil;
    }
    
    NSPredicate* operator()(mbgl::style::NotHasFilter filter) {
        [NSException raise:@"Unsupported filter type"
                    format:@"Cannot convert mbgl::style::NotHasFilter to NSPredicate"];
        return nil;
    }
    
};

@implementation NSPredicate (MGLAdditions)

- (mbgl::style::Filter)mgl_filter
{
    [NSException raise:@"Not supported"
                format:@"NSPredicate doesn't implement ’-mgl_filter’. Try with NSComparisonPredicate or NSCompoundPredicate instead."];
    return {};
}

+ (instancetype)mgl_predicateWithFilter:(mbgl::style::Filter)filter
{
    FilterEvaluator evaluator;
    return mbgl::style::Filter::visit(filter, evaluator);
}

@end