File: feature.hpp

package info (click to toggle)
mapbox-geometry 2.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 304 kB
  • sloc: cpp: 1,563; sh: 150; makefile: 37
file content (224 lines) | stat: -rw-r--r-- 7,548 bytes parent folder | download
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#pragma once

#include <mapbox/geometry.hpp>

#include <mapbox/variant.hpp>

#include <cstdint>
#include <string>
#include <vector>
#include <memory>
#include <unordered_map>

namespace mapbox {
namespace feature {

// comparator functors
struct equal_comp_shared_ptr
{

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
    template <typename T>
    bool operator()(T const& lhs, T const& rhs) const
    {
        return lhs == rhs;
    }
#pragma GCC diagnostic pop

    template <typename T>
    bool operator()(std::shared_ptr<T> const& lhs, std::shared_ptr<T> const& rhs) const
    {
        if (lhs == rhs)
        {
            return true;
        }
        return *lhs == *rhs;
    }
};

struct value;

struct null_value_t
{
};

constexpr bool operator==(const null_value_t&, const null_value_t&) { return true; }
constexpr bool operator!=(const null_value_t&, const null_value_t&) { return false; }
constexpr bool operator<(const null_value_t&, const null_value_t&) { return false; }

constexpr null_value_t null_value = null_value_t();

#define DECLARE_VALUE_TYPE_ACCESOR(NAME, TYPE)        \
    TYPE* get##NAME() noexcept                        \
    {                                                 \
        return match(                                 \
            [](TYPE& val) -> TYPE* { return &val; },  \
            [](auto&) -> TYPE* { return nullptr; });  \
    }                                                 \
    const TYPE* get##NAME() const noexcept            \
    {                                                 \
        return const_cast<value*>(this)->get##NAME(); \
    }
// Multiple numeric types (uint64_t, int64_t, double) are present in order to support
// the widest possible range of JSON numbers, which do not have a maximum range.
// Implementations that produce `value`s should use that order for type preference,
// using uint64_t for positive integers, int64_t for negative integers, and double
// for non-integers and integers outside the range of 64 bits.
using value_base = mapbox::util::variant<null_value_t, bool, uint64_t, int64_t, double, std::string,
                                         std::shared_ptr<std::vector<value>>,
                                         std::shared_ptr<std::unordered_map<std::string, value>>>;

struct value : public value_base
{
    using array_type = std::vector<value>;
    using array_ptr_type = std::shared_ptr<std::vector<value>>;
    using const_array_ptr_type = std::shared_ptr<const std::vector<value>>;
    using object_type = std::unordered_map<std::string, value>;
    using object_ptr_type = std::shared_ptr<std::unordered_map<std::string, value>>;
    using const_object_ptr_type = std::shared_ptr<const std::unordered_map<std::string, value>>;

    value() : value_base(null_value) {}
    value(null_value_t) : value_base(null_value) {}
    value(bool v) : value_base(v) {}
    value(const char* c) : value_base(std::string(c)) {}
    value(std::string str) : value_base(std::move(str)) {}

    template <typename T, typename std::enable_if_t<std::is_integral<T>::value, int> = 0,
              typename std::enable_if_t<std::is_signed<T>::value, int> = 0>
    value(T t) : value_base(int64_t(t))
    {
    }

    template <typename T, typename std::enable_if_t<std::is_integral<T>::value, int> = 0,
              typename std::enable_if_t<!std::is_signed<T>::value, int> = 0>
    value(T t) : value_base(uint64_t(t))
    {
    }

    template <typename T, typename std::enable_if_t<std::is_floating_point<T>::value, int> = 0>
    value(T t) : value_base(double(t))
    {
    }
    value(array_type array) : value_base(std::make_shared<array_type>(std::forward<array_type>(array))) {}
    value(array_ptr_type array) : value_base(array) {}
    value(object_type object) : value_base(std::make_shared<object_type>(std::forward<object_type>(object))) {}
    value(object_ptr_type object) : value_base(object) {}

    bool operator==(value const& rhs) const
    {
        assert(valid() && rhs.valid());
        if (this->which() != rhs.which())
        {
            return false;
        }
        mapbox::util::detail::comparer<value, equal_comp_shared_ptr> visitor(*this);
        return visit(rhs, visitor);
    }

    explicit operator bool() const { return !is<null_value_t>(); }

    DECLARE_VALUE_TYPE_ACCESOR(Int, int64_t)
    DECLARE_VALUE_TYPE_ACCESOR(Uint, uint64_t)
    DECLARE_VALUE_TYPE_ACCESOR(Bool, bool)
    DECLARE_VALUE_TYPE_ACCESOR(Double, double)
    DECLARE_VALUE_TYPE_ACCESOR(String, std::string)

    array_ptr_type getArray() noexcept
    {
        return match(
            [](array_ptr_type& val) -> array_ptr_type { return val; },
            [](auto&) -> array_ptr_type { return nullptr; });
    }
    const_array_ptr_type getArray() const noexcept
    {
        return const_cast<value*>(this)->getArray();
    }

    object_ptr_type getObject() noexcept
    {
        return match(
            [](object_ptr_type& val) -> object_ptr_type { return val; },
            [](auto&) -> object_ptr_type { return nullptr; });
    }
    const_object_ptr_type getObject() const noexcept
    {
        return const_cast<value*>(this)->getObject();
    }
};

#undef DECLARE_VALUE_TYPE_ACCESOR

using property_map = value::object_type;

// The same considerations and requirement for numeric types apply as for `value_base`.
using identifier = mapbox::util::variant<null_value_t, uint64_t, int64_t, double, std::string>;

template <class T>
struct feature
{
    using coordinate_type = T;
    using geometry_type = mapbox::geometry::geometry<T>; // Fully qualified to avoid GCC -fpermissive error.

    geometry_type geometry;
    property_map properties;
    identifier id;

    feature()
        : geometry(),
          properties(),
          id() {}
    feature(geometry_type const& geom_)
        : geometry(geom_),
          properties(),
          id() {}
    feature(geometry_type&& geom_)
        : geometry(std::move(geom_)),
          properties(),
          id() {}
    feature(geometry_type const& geom_, property_map const& prop_)
        : geometry(geom_), properties(prop_), id() {}
    feature(geometry_type&& geom_, property_map&& prop_)
        : geometry(std::move(geom_)),
          properties(std::move(prop_)),
          id() {}
    feature(geometry_type const& geom_, property_map const& prop_, identifier const& id_)
        : geometry(geom_),
          properties(prop_),
          id(id_) {}
    feature(geometry_type&& geom_, property_map&& prop_, identifier&& id_)
        : geometry(std::move(geom_)),
          properties(std::move(prop_)),
          id(std::move(id_)) {}
};

template <class T>
constexpr bool operator==(feature<T> const& lhs, feature<T> const& rhs)
{
    return lhs.id == rhs.id && lhs.geometry == rhs.geometry && lhs.properties == rhs.properties;
}

template <class T>
constexpr bool operator!=(feature<T> const& lhs, feature<T> const& rhs)
{
    return !(lhs == rhs);
}

template <class T, template <typename...> class Cont = std::vector>
struct feature_collection : Cont<feature<T>>
{
    using coordinate_type = T;
    using feature_type = feature<T>;
    using container_type = Cont<feature_type>;
    using size_type = typename container_type::size_type;

    template <class... Args>
    feature_collection(Args&&... args) : container_type(std::forward<Args>(args)...)
    {
    }
    feature_collection(std::initializer_list<feature_type> args)
        : container_type(std::move(args)) {}
};

} // namespace feature
} // namespace mapbox