summaryrefslogtreecommitdiff
path: root/include/mbgl/style/conversion/property_setter.hpp
blob: 8658fd60b0393bc2831594dafd0d0b8e77259f09 (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
#pragma once

#include <mbgl/style/layer.hpp>
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/constant.hpp>
#include <mbgl/style/conversion/property_value.hpp>
#include <mbgl/style/conversion/data_driven_property_value.hpp>
#include <mbgl/style/conversion/transition_options.hpp>

#include <string>

namespace mbgl {
namespace style {
namespace conversion {

template <class V>
using LayoutPropertySetter = optional<Error> (*) (Layer&, const V&);

template <class V>
using PaintPropertySetter = optional<Error> (*) (Layer&, const V&, const optional<std::string>&);

template <class V, class L, class PropertyValue, void (L::*setter)(PropertyValue)>
optional<Error> setLayoutProperty(Layer& layer, const V& value) {
    auto* typedLayer = layer.as<L>();
    if (!typedLayer) {
        return Error { "layer doesn't support this property" };
    }

    Error error;
    optional<PropertyValue> typedValue = convert<PropertyValue>(value, error);
    if (!typedValue) {
        return error;
    }

    (typedLayer->*setter)(*typedValue);
    return {};
}

template <class V, class L, class PropertyValue, void (L::*setter)(PropertyValue, const optional<std::string>&)>
optional<Error> setPaintProperty(Layer& layer, const V& value, const optional<std::string>& klass) {
    auto* typedLayer = layer.as<L>();
    if (!typedLayer) {
        return Error { "layer doesn't support this property" };
    }

    Error error;
    optional<PropertyValue> typedValue = convert<PropertyValue>(value, error);
    if (!typedValue) {
        return error;
    }

    (typedLayer->*setter)(*typedValue, klass);
    return {};
}

template <class V, class L, void (L::*setter)(const TransitionOptions&, const optional<std::string>&)>
optional<Error> setTransition(Layer& layer, const V& value, const optional<std::string>& klass) {
    auto* typedLayer = layer.as<L>();
    if (!typedLayer) {
        return Error { "layer doesn't support this property" };
    }

    Error error;
    optional<TransitionOptions> transition = convert<TransitionOptions>(value, error);
    if (!transition) {
        return error;
    }

    (typedLayer->*setter)(*transition, klass);
    return {};
}

template <class V>
optional<Error> setVisibility(Layer& layer, const V& value) {
    if (isUndefined(value)) {
        layer.setVisibility(VisibilityType::Visible);
        return {};
    }

    Error error;
    optional<VisibilityType> visibility = convert<VisibilityType>(value, error);
    if (!visibility) {
        return error;
    }

    layer.setVisibility(*visibility);
    return {};
}

} // namespace conversion
} // namespace style
} // namespace mbgl