diff --git a/include/mapbox/geometry/for_each_point.hpp b/include/mapbox/geometry/for_each_point.hpp index b68d4b3..06ee261 100644 --- a/include/mapbox/geometry/for_each_point.hpp +++ b/include/mapbox/geometry/for_each_point.hpp @@ -4,48 +4,38 @@ namespace mapbox { namespace geometry { -// const - -template -void for_each_point(point const& point, F&& f) +template +auto for_each_point(Point&& point, F&& f) + -> decltype(point.x, point.y, void()) { - f(point); -} - -template -void for_each_point(mapbox::util::variant const& geom, F&& f) -{ - mapbox::util::variant::visit(geom, [&] (auto const& g) { for_each_point(g, f); }); + f(std::forward(point)); } template -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 -void for_each_point(point & point, F&& f) +template +void for_each_point(mapbox::util::variant const& geom, F&& f) { - f(point); + mapbox::util::variant::visit(geom, [&] (auto const& g) { + for_each_point(g, f); + }); } template void for_each_point(mapbox::util::variant & geom, F&& f) { - mapbox::util::variant::visit(geom, [&] (auto & g) { for_each_point(g, f); }); + mapbox::util::variant::visit(geom, [&] (auto & g) { + for_each_point(g, f); + }); } template -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); } } diff --git a/tests/test.cpp b/tests/test.cpp index a577dc2..2dba1c5 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -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 const&) { count++; }; - }; +struct point_counter { + std::size_t count = 0; + template + void operator()(Point const&) { count++; }; +}; +static void testForEachPoint() { auto count_points = [] (auto const& g) { point_counter counter; for_each_point(g, counter); @@ -169,6 +170,14 @@ static void testForEachPoint() { // Custom geometry type using my_geometry = mapbox::util::variant>; assert(count_points(my_geometry(point())) == 1); + + // Custom point type + struct my_point { + int16_t x; + int16_t y; + }; + assert(count_points(std::vector({my_point{0, 1}})) == 1); + assert(count_points(mapbox::util::variant(my_point{0, 1})) == 1); } static void testEnvelope() {