summaryrefslogtreecommitdiff
path: root/include/mbgl/style/conversion/property_setter.hpp
blob: 52fb160fdeda1a68b0a88398de511b6dbc70cce9 (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
#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 <functional>
#include <string>

namespace mbgl {
namespace style {
namespace conversion {

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

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

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

        Result<PropertyValue> typedValue = convert<PropertyValue>(value);
        if (!typedValue) {
            return typedValue.error();
        }

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

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

    Result<VisibilityType> visibility = convert<VisibilityType>(value);
    if (!visibility) {
        return visibility.error();
    }

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

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