Skip to content

Commit

Permalink
std::ostream operator<< overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoabinader committed Nov 8, 2018
1 parent 26adc07 commit c83a2ab
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 0 deletions.
98 changes: 98 additions & 0 deletions include/mapbox/geometry_io.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#pragma once

#include <mapbox/geometry/empty.hpp>
#include <mapbox/feature.hpp>

#include <iostream>
#include <string>

namespace mapbox {
namespace geometry {

std::ostream& operator<<(std::ostream& os, const empty&)
{
return os << "[]";
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const point<T>& point)
{
return os << "[" << point.x << "," << point.y << "]";
}

template <typename T, template <class, class...> class C, class... Args>
std::ostream& operator<<(std::ostream& os, const C<T, Args...>& cont)
{
os << "[";
for (auto it = cont.cbegin();;)
{
os << *it;
if (++it == cont.cend())
{
break;
}
os << ",";
}
return os << "]";
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const line_string<T>& geom)
{
return os << static_cast<typename line_string<T>::container_type>(geom);
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const linear_ring<T>& geom)
{
return os << static_cast<typename linear_ring<T>::container_type>(geom);
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const polygon<T>& geom)
{
return os << static_cast<typename polygon<T>::container_type>(geom);
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const multi_point<T>& geom)
{
return os << static_cast<typename multi_point<T>::container_type>(geom);
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const multi_line_string<T>& geom)
{
return os << static_cast<typename multi_line_string<T>::container_type>(geom);
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const multi_polygon<T>& geom)
{
return os << static_cast<typename multi_polygon<T>::container_type>(geom);
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const geometry<T>& geom)
{
geometry<T>::visit(geom, [&](const auto& g) { os << g; });
return os;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const geometry_collection<T>& geom)
{
return os << static_cast<typename geometry_collection<T>::container_type>(geom);
}

} // namespace geometry

namespace feature {

std::ostream& operator<<(std::ostream& os, const null_value_t&)
{
return os << "[]";

This comment has been minimized.

Copy link
@springmeyer

springmeyer Nov 8, 2018

Contributor

@brunoabinader noting that this is missing code coverage. Do you think this is important to test?

Note: I can see this because I have https://github.com/codecov/browser-extension installed.

screen shot 2018-11-08 at 11 02 03 am

This comment has been minimized.

Copy link
@brunoabinader

brunoabinader Nov 9, 2018

Author Member

Good catch @springmeyer - I have added a test for it in #62.

BTW - This extension looks cool! I tried to install this browser extension, but it seems no longer supported. Do you have any other alternatives?

This comment has been minimized.

Copy link
@springmeyer

springmeyer Nov 10, 2018

Contributor

@brunoabinader looks like their is another extension now https://chrome.google.com/webstore/detail/sourcegraph/dgjhfomjieaadpoljlnidmbgkdffpack. To get it working install it and then check the box for "use extensions" in its config. However one problem with it is that it does not yet (like the previous) show coverage for commits: https://github.com/sourcegraph/sourcegraph-codecov/issues/10

}

} // namespace feature
} // namespace mapbox
57 changes: 57 additions & 0 deletions test/io.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <catch.hpp>
#include <mapbox/geometry_io.hpp>

#include <iostream>

TEST_CASE("operator<<")
{
mapbox::geometry::empty empty;
mapbox::geometry::point<double> point{10, 20};
mapbox::geometry::point<double> point2{30, 40};
mapbox::geometry::line_string<double> lineString{point, point2};
mapbox::geometry::polygon<double> polygon{mapbox::geometry::linear_ring<double>{point, point2}};
mapbox::geometry::multi_point<double> multiPoint{point, point2};
mapbox::geometry::multi_line_string<double> multiLineString{lineString, lineString};
mapbox::geometry::multi_polygon<double> multiPolygon{polygon};
mapbox::geometry::geometry_collection<double> collection{multiPolygon};

std::stringstream stream;
stream << empty << std::endl;
stream << point << std::endl;
stream << lineString << std::endl;
stream << polygon << std::endl;
stream << multiPoint << std::endl;
stream << multiLineString << std::endl;
stream << multiPolygon << std::endl;
stream << collection << std::endl;
stream << mapbox::geometry::geometry<double>{collection} << std::endl;

std::string line;

std::getline(stream, line);
CHECK(line == std::string("[]"));

std::getline(stream, line);
CHECK(line == std::string("[10,20]"));

std::getline(stream, line);
CHECK(line == std::string("[[10,20],[30,40]]"));

std::getline(stream, line);
CHECK(line == std::string("[[[10,20],[30,40]]]"));

std::getline(stream, line);
CHECK(line == std::string("[[10,20],[30,40]]"));

std::getline(stream, line);
CHECK(line == std::string("[[[10,20],[30,40]],[[10,20],[30,40]]]"));

std::getline(stream, line);
CHECK(line == std::string("[[[[10,20],[30,40]]]]"));

std::getline(stream, line);
CHECK(line == std::string("[[[[[10,20],[30,40]]]]]"));

std::getline(stream, line);
CHECK(line == std::string("[[[[[10,20],[30,40]]]]]"));
}

0 comments on commit c83a2ab

Please sign in to comment.