summaryrefslogtreecommitdiff
path: root/test/src/mbgl/test/conversion_stubs.hpp
blob: 30395ddb9737b9916ea061b72990c79dc6d1f244 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#pragma once

#include <mbgl/style/conversion.hpp>
#include <mbgl/util/feature.hpp>
#include <mbgl/util/optional.hpp>
#include <mbgl/util/variant.hpp>

#include <string>
#include <unordered_map>

namespace mbgl {
namespace style {
namespace conversion {

class Value;
using ValueMap = std::unordered_map<std::string, Value>;
using ValueVector = std::vector<Value>;
class Value : public mbgl::variant<std::string,
                                   float,
                                   double,
                                   bool,
                                   mapbox::util::recursive_wrapper<ValueMap>,
                                   mapbox::util::recursive_wrapper<ValueVector>> {
    using variant::variant;
};

inline bool isUndefined(const Value&) {
    // Variant is always intialized
    return false;
}

inline bool isArray(const Value& value) {
    return value.is<mapbox::util::recursive_wrapper<ValueVector>>();
}

inline std::size_t arrayLength(const Value& value) {
    return value.get<mapbox::util::recursive_wrapper<ValueVector>>().get().size();
}

inline Value arrayMember(const Value& value, std::size_t i) {
    return value.get<mapbox::util::recursive_wrapper<ValueVector>>().get()[i];
}

inline bool isObject(const Value& value) {
    return value.is<mapbox::util::recursive_wrapper<ValueMap>>();
}

inline optional<Value> objectMember(const Value& value, const char* key) {
    auto map = value.get<ValueMap>();
    auto iter = map.find(key);

    if (iter != map.end()) {
        return iter->second;
    } else {
        return {};
    }
}

using EachMemberFn = std::function<optional<Error>(const std::string&, const Value&)>;

optional<Error> eachMember(const Value& value, EachMemberFn&& fn) {
    auto map = value.get<ValueMap>();
    auto iter = map.begin();

    while (iter != map.end()) {
        optional<Error> result = fn(iter->first, iter->second);
        if (result) {
            return result;
        }

        ++iter;
    }

    return {};
}

inline optional<bool> toBool(const Value& value) {
    if (value.is<bool>()) {
        return value.get<bool>();
    } else {
        return {};
    }
}

inline optional<float> toNumber(const Value& value) {
    if (value.is<float>()) {
        return value.get<float>();
    } else {
        return {};
    }
    return {};
}


inline optional<double> toDouble(const Value& value) {
    if (value.is<double>()) {
        return value.get<double>();
    }
    return {};
}

inline optional<std::string> toString(const Value& value) {
    if (value.is<std::string>()) {
        return value.get<std::string>();
    } else {
        return {};
    }
}

inline optional<mbgl::Value> toValue(const Value& value) {
    if (value.is<bool>()) {
        return { value.get<bool>() };
    } else if (value.is<std::string>()) {
        return { value.get<std::string>() };
    } else if (value.is<float>()) {
        return { double(value.get<float>()) };
    } else {
        return {};
    }
}

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