Skip to content

Commit dba2004

Browse files
committed
Enable for_each_point with custom point type
1 parent 819066f commit dba2004

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

include/mapbox/geometry/for_each_point.hpp

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,38 @@
44

55
namespace mapbox { namespace geometry {
66

7-
// const
8-
9-
template <typename T, typename F>
10-
void for_each_point(point<T> const& point, F&& f)
7+
template <typename Point, typename F>
8+
auto for_each_point(Point&& point, F&& f)
9+
-> decltype(point.x, point.y, void())
1110
{
12-
f(point);
13-
}
14-
15-
template <typename...Types, typename F>
16-
void for_each_point(mapbox::util::variant<Types...> const& geom, F&& f)
17-
{
18-
mapbox::util::variant<Types...>::visit(geom, [&] (auto const& g) { for_each_point(g, f); });
11+
f(std::forward<Point>(point));
1912
}
2013

2114
template <typename Container, typename F>
22-
auto for_each_point(Container const& container, F&& f)
23-
-> decltype(container.begin(), container.end(), void())
24-
{
25-
for (auto const& e: container) {
26-
for_each_point(e, f);
27-
}
28-
}
15+
auto for_each_point(Container&& container, F&& f)
16+
-> decltype(container.begin(), container.end(), void());
2917

30-
// mutable
31-
32-
template <typename T, typename F>
33-
void for_each_point(point<T> & point, F&& f)
18+
template <typename...Types, typename F>
19+
void for_each_point(mapbox::util::variant<Types...> const& geom, F&& f)
3420
{
35-
f(point);
21+
mapbox::util::variant<Types...>::visit(geom, [&] (auto const& g) {
22+
for_each_point(g, f);
23+
});
3624
}
3725

3826
template <typename...Types, typename F>
3927
void for_each_point(mapbox::util::variant<Types...> & geom, F&& f)
4028
{
41-
mapbox::util::variant<Types...>::visit(geom, [&] (auto & g) { for_each_point(g, f); });
29+
mapbox::util::variant<Types...>::visit(geom, [&] (auto & g) {
30+
for_each_point(g, f);
31+
});
4232
}
4333

4434
template <typename Container, typename F>
45-
auto for_each_point(Container & container, F&& f)
35+
auto for_each_point(Container&& container, F&& f)
4636
-> decltype(container.begin(), container.end(), void())
4737
{
48-
for (auto & e: container) {
38+
for (auto& e: container) {
4939
for_each_point(e, f);
5040
}
5141
}

tests/test.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,13 @@ static void testFeatureCollection() {
136136
assert(fc1.size() == 0);
137137
}
138138

139-
static void testForEachPoint() {
140-
struct point_counter {
141-
std::size_t count = 0;
142-
void operator()(point<double> const&) { count++; };
143-
};
139+
struct point_counter {
140+
std::size_t count = 0;
141+
template <class Point>
142+
void operator()(Point const&) { count++; };
143+
};
144144

145+
static void testForEachPoint() {
145146
auto count_points = [] (auto const& g) {
146147
point_counter counter;
147148
for_each_point(g, counter);
@@ -169,6 +170,14 @@ static void testForEachPoint() {
169170
// Custom geometry type
170171
using my_geometry = mapbox::util::variant<point<double>>;
171172
assert(count_points(my_geometry(point<double>())) == 1);
173+
174+
// Custom point type
175+
struct my_point {
176+
int16_t x;
177+
int16_t y;
178+
};
179+
assert(count_points(std::vector<my_point>({my_point{0, 1}})) == 1);
180+
assert(count_points(mapbox::util::variant<my_point>(my_point{0, 1})) == 1);
172181
}
173182

174183
static void testEnvelope() {

0 commit comments

Comments
 (0)