Skip to content

Commit

Permalink
CoordinateSequence: Add type-detecting forEach for use with auto lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaston committed Jan 20, 2023
1 parent 20f3443 commit 213f198
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ include(CMakeDependentOption)

## CMake global variables
option(BUILD_SHARED_LIBS "Build GEOS with shared libraries" ON)
set(CMAKE_CXX_STANDARD 11 CACHE STRING "C++ standard version to use (default is 11)")
set(CMAKE_CXX_STANDARD 14 CACHE STRING "C++ standard version to use (default is 11)")

## GEOS custom variables
option(BUILD_BENCHMARKS "Build GEOS benchmarks" OFF)
Expand Down
10 changes: 10 additions & 0 deletions include/geos/geom/CoordinateSequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,16 @@ class GEOS_DLL CoordinateSequence {
}
}

template<typename F>
void forEach(F&& fun) const {
switch(getCoordinateType()) {
case CoordinateType::XY: for (const auto& c : items<CoordinateXY>()) { fun(c); } break;
case CoordinateType::XYZ: for (const auto& c : items<Coordinate>()) { fun(c); } break;
case CoordinateType::XYM: for (const auto& c : items<CoordinateXYM>()) { fun(c); } break;
case CoordinateType::XYZM: for (const auto& c : items<CoordinateXYZM>()) { fun(c); } break;
}
}

template<typename T, typename F>
void forEach(F&& fun) const
{
Expand Down
17 changes: 10 additions & 7 deletions src/geom/CoordinateSequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,16 +566,19 @@ std::ostream&
operator<< (std::ostream& os, const CoordinateSequence& cs)
{
os << "(";
for(std::size_t i = 0, n = cs.size(); i < n; ++i) {
if(i) {

bool writeComma = false;
auto write = [&os, &writeComma](const auto& coord) {
if (writeComma) {
os << ", ";
}
if (cs.hasZ()) {
os << cs.getAt(i);
} else {
os << cs.getAt<CoordinateXY>(i);
writeComma = true;
}
}

os << coord;
};

cs.forEach(write);
os << ")";

return os;
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/geom/CoordinateSequenceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1392,4 +1392,29 @@ void object::test<52>
}
}

// Test type-detecting version of forEach
template<>
template<>
void object::test<53>
()
{
CoordinateSequence dst(0u, true, true);

CoordinateSequence src1{{Coordinate{1, 2, 3}, Coordinate{4, 5, 6}}};
CoordinateSequence src2{{CoordinateXYM{7, 8, 9}, {10, 11, 12}}};

auto appendToDst = [&dst](auto& coord) {
dst.add(coord);
};

src1.forEach(appendToDst);
src2.forEach(appendToDst);

ensure_equals(dst.size(), 4u);
ensure_equals_xyzm(dst.getAt<CoordinateXYZM>(0), CoordinateXYZM{1, 2, 3, DoubleNotANumber});
ensure_equals_xyzm(dst.getAt<CoordinateXYZM>(1), CoordinateXYZM{4, 5, 6, DoubleNotANumber});
ensure_equals_xyzm(dst.getAt<CoordinateXYZM>(2), CoordinateXYZM{7, 8, DoubleNotANumber, 9});
ensure_equals_xyzm(dst.getAt<CoordinateXYZM>(3), CoordinateXYZM{10, 11, DoubleNotANumber, 12});
}

} // namespace tut

0 comments on commit 213f198

Please sign in to comment.