From 390fff5bb4422fe7a6359eb62786fc0ea6453424 Mon Sep 17 00:00:00 2001 From: artemp Date: Wed, 1 Apr 2015 15:04:05 +0200 Subject: [PATCH] ensure we're returning the same object from multiple return paths from_wkb now returns geometry::geometry_empty instead of throwing update cpp_tests --- plugins/input/shape/shape_io.cpp | 14 ++++--- src/wkb.cpp | 62 ++++++++++++++++++---------- tests/cpp_tests/wkb_formats_test.cpp | 15 ++++--- 3 files changed, 59 insertions(+), 32 deletions(-) diff --git a/plugins/input/shape/shape_io.cpp b/plugins/input/shape/shape_io.cpp index 41b0917ab..4865195c7 100644 --- a/plugins/input/shape/shape_io.cpp +++ b/plugins/input/shape/shape_io.cpp @@ -90,12 +90,12 @@ void shape_io::read_bbox(shape_file::record_type & record, mapnik::box2d mapnik::geometry::geometry shape_io::read_polyline(shape_file::record_type & record) { + mapnik::geometry::geometry geom; // default empty int num_parts = record.read_ndr_integer(); int num_points = record.read_ndr_integer(); if (num_parts == 1) { - mapnik::geometry::line_string line; line.reserve(num_points); record.skip(4); @@ -105,7 +105,7 @@ mapnik::geometry::geometry shape_io::read_polyline(shape_file::record_type & rec double y = record.read_double(); line.add_coord(x, y); } - return mapnik::geometry::geometry(std::move(line)); + geom = std::move(line); } else { @@ -139,13 +139,15 @@ mapnik::geometry::geometry shape_io::read_polyline(shape_file::record_type & rec } multi_line.push_back(std::move(line)); } - return mapnik::geometry::geometry(std::move(multi_line)); + geom = std::move(multi_line); } + return geom; } mapnik::geometry::geometry shape_io::read_polygon(shape_file::record_type & record) { + mapnik::geometry::geometry geom; // default empty int num_parts = record.read_ndr_integer(); int num_points = record.read_ndr_integer(); @@ -190,7 +192,9 @@ mapnik::geometry::geometry shape_io::read_polygon(shape_file::record_type & reco if (multi_poly.size() > 0) // multi { multi_poly.emplace_back(std::move(poly)); - return mapnik::geometry::geometry(std::move(multi_poly)); + geom = std::move(multi_poly); + return geom; } - return mapnik::geometry::geometry(std::move(poly)); + geom = std::move(poly); + return geom; } diff --git a/src/wkb.cpp b/src/wkb.cpp index 345479100..a0e438f17 100644 --- a/src/wkb.cpp +++ b/src/wkb.cpp @@ -120,61 +120,81 @@ public: mapnik::geometry::geometry read() { + mapnik::geometry::geometry geom = mapnik::geometry::geometry_empty(); int type = read_integer(); switch (type) { case wkbPoint: - return std::move(mapnik::geometry::geometry(read_point())); + geom = std::move(read_point()); + break; case wkbLineString: - return std::move(mapnik::geometry::geometry(read_linestring())); + geom = std::move(read_linestring()); + break; case wkbPolygon: - return std::move(mapnik::geometry::geometry(read_polygon())); + geom = std::move(read_polygon()); + break; case wkbMultiPoint: - return std::move(mapnik::geometry::geometry(read_multipoint())); + geom = std::move(read_multipoint()); + break; case wkbMultiLineString: - return std::move(mapnik::geometry::geometry(read_multilinestring())); + geom = std::move(read_multilinestring()); + break; case wkbMultiPolygon: - return std::move(mapnik::geometry::geometry(read_multipolygon())); + geom = std::move(read_multipolygon()); + break; case wkbGeometryCollection: - return std::move(mapnik::geometry::geometry(read_collection())); + geom = std::move(read_collection()); + break; case wkbPointZ: case wkbPointM: - return std::move(mapnik::geometry::geometry(read_point())); + geom = std::move(read_point()); + break; case wkbPointZM: - return std::move(mapnik::geometry::geometry(read_point())); + geom = std::move(read_point()); + break; case wkbLineStringZ: case wkbLineStringM: - return std::move(mapnik::geometry::geometry(read_linestring())); + geom = std::move(read_linestring()); + break; case wkbLineStringZM: - return std::move(mapnik::geometry::geometry(read_linestring())); + geom = std::move(read_linestring()); + break; case wkbPolygonZ: case wkbPolygonM: - return std::move(mapnik::geometry::geometry(read_polygon())); + geom = std::move(read_polygon()); + break; case wkbPolygonZM: - return std::move(mapnik::geometry::geometry(read_polygon())); + geom = std::move(read_polygon()); case wkbMultiPointZ: case wkbMultiPointM: - return std::move(mapnik::geometry::geometry(read_multipoint())); + geom = std::move(read_multipoint()); + break; case wkbMultiPointZM: - return std::move(mapnik::geometry::geometry(read_multipoint())); + geom = std::move(read_multipoint()); + break; case wkbMultiLineStringZ: case wkbMultiLineStringM: - return std::move(mapnik::geometry::geometry(read_multilinestring())); + geom = std::move(read_multilinestring()); + break; case wkbMultiLineStringZM: - return std::move(mapnik::geometry::geometry(read_multilinestring())); + geom = std::move(read_multilinestring()); + break; case wkbMultiPolygonZ: case wkbMultiPolygonM: - return std::move(mapnik::geometry::geometry(read_multipolygon())); + geom = std::move(read_multipolygon()); + break; case wkbMultiPolygonZM: - return std::move(mapnik::geometry::geometry(read_multipolygon())); + geom = std::move(read_multipolygon()); + break; case wkbGeometryCollectionZ: case wkbGeometryCollectionM: case wkbGeometryCollectionZM: - return std::move(mapnik::geometry::geometry(read_collection())); + geom = std::move(read_collection()); + break; default: break; } - throw std::runtime_error("Uknown WKB geometry type"); + return geom; } private: diff --git a/tests/cpp_tests/wkb_formats_test.cpp b/tests/cpp_tests/wkb_formats_test.cpp index 5ba4f0667..72c49c050 100644 --- a/tests/cpp_tests/wkb_formats_test.cpp +++ b/tests/cpp_tests/wkb_formats_test.cpp @@ -80,9 +80,11 @@ int main(int argc, char** argv) - BOOST_TEST_THROWS(mapnik::geometry_utils::from_wkb((const char*)sp_invalid_blob, - sizeof(sp_invalid_blob) / sizeof(sp_invalid_blob[0]), - mapnik::wkbAuto), std::exception); + geom = mapnik::geometry_utils::from_wkb((const char*)sp_invalid_blob, + sizeof(sp_invalid_blob) / sizeof(sp_invalid_blob[0]), + mapnik::wkbAuto); + BOOST_TEST(geom.is()); // returns geometry_empty + // sqlite generic wkb blob geom = mapnik::geometry_utils::from_wkb((const char*)sq_valid_blob, @@ -96,9 +98,10 @@ int main(int argc, char** argv) BOOST_TEST(mapnik::geometry::is_valid(geom) && mapnik::geometry::is_simple(geom)); - BOOST_TEST_THROWS(mapnik::geometry_utils::from_wkb((const char*)sq_invalid_blob, - sizeof(sq_invalid_blob) / sizeof(sq_invalid_blob[0]), - mapnik::wkbGeneric), std::exception); + geom = mapnik::geometry_utils::from_wkb((const char*)sq_invalid_blob, + sizeof(sq_invalid_blob) / sizeof(sq_invalid_blob[0]), + mapnik::wkbGeneric); + BOOST_TEST(geom.is()); // returns geometry_empty } catch (std::exception const& ex) { BOOST_TEST(false);