summaryrefslogtreecommitdiff
path: root/include/mbgl/style/function/source_function.hpp
blob: 0ae8357d7b3da08b0f546fa9d90a1968ce63b81a (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
#pragma once

#include <mbgl/style/function/convert.hpp>

#include <mbgl/style/function/exponential_stops.hpp>
#include <mbgl/style/function/interval_stops.hpp>
#include <mbgl/style/function/categorical_stops.hpp>
#include <mbgl/style/function/identity_stops.hpp>
#include <mbgl/util/interpolate.hpp>
#include <mbgl/util/variant.hpp>

#include <string>

namespace mbgl {
namespace style {

template <class T>
class SourceFunction {
public:
    using Stops = std::conditional_t<
        util::Interpolatable<T>::value,
        variant<
            ExponentialStops<T>,
            IntervalStops<T>,
            CategoricalStops<T>,
            IdentityStops<T>>,
        variant<
            IntervalStops<T>,
            CategoricalStops<T>,
            IdentityStops<T>>>;

    SourceFunction(std::string property_, Stops stops_, optional<T> defaultValue_ = {})
        : property(std::move(property_)),
          stops(std::move(stops_)),
          defaultValue(std::move(defaultValue_)),
          expression(stops.match([&] (const auto& s) {
            return expression::Convert::toExpression(property, s, defaultValue);
          }))
    {}

    template <class Feature>
    T evaluate(const Feature& feature, T finalDefaultValue) const {
        auto result = expression->evaluate<T>(expression::EvaluationParameters { optional<float>(), &feature });
        if (!result) {
            return finalDefaultValue;
        }
        return *result;
    }

    friend bool operator==(const SourceFunction& lhs,
                           const SourceFunction& rhs) {
        return std::tie(lhs.property, lhs.stops, lhs.defaultValue)
            == std::tie(rhs.property, rhs.stops, rhs.defaultValue);
    }

    bool useIntegerZoom = false;

    // retained for compatibility with pre-expression function API
    std::string property;
    Stops stops;
    optional<T> defaultValue;

private:
    std::shared_ptr<expression::Expression> expression;
};

} // namespace style
} // namespace mbgl