Skip to content

Commit

Permalink
Enable for_each_point with custom point type
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed May 25, 2016
1 parent 819066f commit dba2004
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
42 changes: 16 additions & 26 deletions include/mapbox/geometry/for_each_point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,38 @@

namespace mapbox { namespace geometry {

// const

template <typename T, typename F>
void for_each_point(point<T> const& point, F&& f)
template <typename Point, typename F>
auto for_each_point(Point&& point, F&& f)
-> decltype(point.x, point.y, void())
{
f(point);
}

template <typename...Types, typename F>
void for_each_point(mapbox::util::variant<Types...> const& geom, F&& f)
{
mapbox::util::variant<Types...>::visit(geom, [&] (auto const& g) { for_each_point(g, f); });
f(std::forward<Point>(point));
}

template <typename Container, typename F>
auto for_each_point(Container const& container, F&& f)
-> decltype(container.begin(), container.end(), void())
{
for (auto const& e: container) {
for_each_point(e, f);
}
}
auto for_each_point(Container&& container, F&& f)
-> decltype(container.begin(), container.end(), void());

// mutable

template <typename T, typename F>
void for_each_point(point<T> & point, F&& f)
template <typename...Types, typename F>
void for_each_point(mapbox::util::variant<Types...> const& geom, F&& f)
{
f(point);
mapbox::util::variant<Types...>::visit(geom, [&] (auto const& g) {
for_each_point(g, f);
});
}

template <typename...Types, typename F>
void for_each_point(mapbox::util::variant<Types...> & geom, F&& f)
{
mapbox::util::variant<Types...>::visit(geom, [&] (auto & g) { for_each_point(g, f); });
mapbox::util::variant<Types...>::visit(geom, [&] (auto & g) {
for_each_point(g, f);
});
}

template <typename Container, typename F>
auto for_each_point(Container & container, F&& f)
auto for_each_point(Container&& container, F&& f)
-> decltype(container.begin(), container.end(), void())
{
for (auto & e: container) {
for (auto& e: container) {
for_each_point(e, f);
}
}
Expand Down
19 changes: 14 additions & 5 deletions tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@ static void testFeatureCollection() {
assert(fc1.size() == 0);
}

static void testForEachPoint() {
struct point_counter {
std::size_t count = 0;
void operator()(point<double> const&) { count++; };
};
struct point_counter {
std::size_t count = 0;
template <class Point>
void operator()(Point const&) { count++; };
};

static void testForEachPoint() {
auto count_points = [] (auto const& g) {
point_counter counter;
for_each_point(g, counter);
Expand Down Expand Up @@ -169,6 +170,14 @@ static void testForEachPoint() {
// Custom geometry type
using my_geometry = mapbox::util::variant<point<double>>;
assert(count_points(my_geometry(point<double>())) == 1);

// Custom point type
struct my_point {
int16_t x;
int16_t y;
};
assert(count_points(std::vector<my_point>({my_point{0, 1}})) == 1);
assert(count_points(mapbox::util::variant<my_point>(my_point{0, 1})) == 1);
}

static void testEnvelope() {
Expand Down

0 comments on commit dba2004

Please sign in to comment.