summaryrefslogtreecommitdiff
path: root/include/mbgl/style/conversion/property_setter.hpp
blob: 759c4512ccac7efc13a544f9bb8ad5a5386acfd0 (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
#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 PropertySetter = optional<Error> (*) (Layer&, const V&);

template <class V, class L, class PropertyValue, void (L::*setter)(PropertyValue)>
optional<Error> setProperty(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, void (L::*setter)(const TransitionOptions&)>
optional<Error> setTransition(Layer& layer, const V& value) {
    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);
    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