summaryrefslogtreecommitdiff
path: root/include/mbgl/style/expression/type.hpp
blob: da59eb001c06183cbdec579c7e1adfc74374a51d (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
#pragma once

#include <mbgl/util/optional.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/util/variant.hpp>
#include <vector>

namespace mbgl {
namespace style {
namespace expression {
namespace type {

template <class T>
std::string toString(const T& t);

struct NullType {
    constexpr NullType() = default;
    std::string getName() const { return "null"; }
    bool operator==(const NullType&) const { return true; }
};

struct NumberType {
    constexpr NumberType() = default;
    std::string getName() const { return "number"; }
    bool operator==(const NumberType&) const { return true; }
};

struct BooleanType {
    constexpr BooleanType() = default;
    std::string getName() const { return "boolean"; }
    bool operator==(const BooleanType&) const { return true; }
};

struct StringType {
    constexpr StringType() = default;
    std::string getName() const { return "string"; }
    bool operator==(const StringType&) const { return true; }
};

struct ColorType {
    constexpr ColorType() = default;
    std::string getName() const { return "color"; }
    bool operator==(const ColorType&) const { return true; }
};

struct ObjectType {
    constexpr ObjectType() = default;
    std::string getName() const { return "object"; }
    bool operator==(const ObjectType&) const { return true; }
};

struct ErrorType {
    constexpr ErrorType() = default;
    std::string getName() const { return "error"; }
    bool operator==(const ErrorType&) const { return true; }
};

struct ValueType {
    constexpr ValueType() = default;
    std::string getName() const { return "value"; }
    bool operator==(const ValueType&) const { return true; }
};

constexpr NullType Null;
constexpr NumberType Number;
constexpr StringType String;
constexpr BooleanType Boolean;
constexpr ColorType Color;
constexpr ValueType Value;
constexpr ObjectType Object;
constexpr ErrorType Error;

struct Array;

using Type = variant<
    NullType,
    NumberType,
    BooleanType,
    StringType,
    ColorType,
    ObjectType,
    ValueType,
    mapbox::util::recursive_wrapper<Array>,
    ErrorType>;

struct Array {
    explicit Array(Type itemType_) : itemType(std::move(itemType_)) {}
    Array(Type itemType_, std::size_t N_) : itemType(std::move(itemType_)), N(N_) {}
    Array(Type itemType_, optional<std::size_t> N_) : itemType(std::move(itemType_)), N(std::move(N_)) {}
    std::string getName() const {
        if (N) {
            return "array<" + toString(itemType) + ", " + util::toString(*N) + ">";
        } else if (itemType == Value) {
            return "array";
        } else {
            return "array<" + toString(itemType) + ">";
        }
    }

    bool operator==(const Array& rhs) const { return itemType == rhs.itemType && N == rhs.N; }
    
    Type itemType;
    optional<std::size_t> N;
};
    
template <class T>
std::string toString(const T& type) { return type.match([&] (const auto& t) { return t.getName(); }); }

} // namespace type
} // namespace expression
} // namespace style
} // namespace mbgl