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

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


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

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

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

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

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

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

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

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

struct ValueType {
    constexpr ValueType() {}
    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;

struct Array;

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

struct Array {
    Array(Type itemType_) : itemType(itemType_) {}
    Array(Type itemType_, std::size_t N_) : itemType(itemType_), N(N_) {}
    Array(Type itemType_, optional<std::size_t> N_) : itemType(itemType_), N(N_) {}
    std::string getName() const {
        if (N) {
            return "Array<" + toString(itemType) + ", " + std::to_string(*N) + ">";
        } else if (toString(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& t) { return t.match([&] (const auto& t) { return t.getName(); }); }

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