Skip to content

Commit

Permalink
Added missing include header, added identifier stream
Browse files Browse the repository at this point in the history
  • Loading branch information
flippmoke committed Jul 28, 2020
1 parent 1314d18 commit 62eb926
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 39 deletions.
63 changes: 28 additions & 35 deletions include/mapbox/geometry_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <mapbox/geometry/empty.hpp>
#include <mapbox/feature.hpp>

#include <algorithm>
#include <iostream>
#include <string>

Expand Down Expand Up @@ -116,10 +117,6 @@ struct value_to_stream_visitor
{

std::ostream& out;
bool in;

value_to_stream_visitor(std::ostream& out_)
: out(out_), in(false) {}

template <typename T>
void operator()(T val)
Expand All @@ -129,14 +126,7 @@ struct value_to_stream_visitor

void operator()(std::string const& val)
{
if (in)
{
quote_string(val, out);
}
else
{
out << val;
}
quote_string(val, out);
}

void operator()(bool val)
Expand All @@ -148,12 +138,6 @@ struct value_to_stream_visitor
{
out << '[';
bool first = true;
bool set_in = false;
if (!in)
{
in = true;
set_in = true;
}
for (auto const& item : vec)
{
if (first)
Expand All @@ -166,10 +150,6 @@ struct value_to_stream_visitor
}
mapbox::util::apply_visitor(*this, item);
}
if (set_in)
{
in = false;
}
out << ']';
}

Expand All @@ -182,12 +162,6 @@ struct value_to_stream_visitor
{
out << '{';
std::vector<std::string> keys;
bool set_in = false;
if (!in)
{
in = true;
set_in = true;
}
for (auto const& p : map)
{
keys.push_back(p.first);
Expand All @@ -209,10 +183,6 @@ struct value_to_stream_visitor
out << ':';
mapbox::util::apply_visitor(*this, val->second);
}
if (set_in)
{
in = false;
}
out << '}';
}

Expand All @@ -224,21 +194,44 @@ struct value_to_stream_visitor

inline std::ostream& operator<<(std::ostream& os, std::unordered_map<std::string, mapbox::feature::value> const& map)
{
value_to_stream_visitor vis(os);
value_to_stream_visitor vis{os};
vis(map);
return os;
}

inline std::ostream& operator<<(std::ostream& os, std::vector<mapbox::feature::value> const& vec)
{
value_to_stream_visitor vis(os);
value_to_stream_visitor vis{os};
vis(vec);
return os;
}

inline std::ostream& operator<<(std::ostream& os, mapbox::feature::value const& val)
{
mapbox::util::apply_visitor(value_to_stream_visitor(os), val);
mapbox::util::apply_visitor(value_to_stream_visitor{os}, val);
return os;
}

struct identifier_to_stream_visitor {

std::ostream& out;

template <typename T>
void operator()(T val)
{
out << val;
}

void operator()(std::string const& val)
{
quote_string(val, out);
}

};

inline std::ostream& operator<<(std::ostream& os, mapbox::feature::identifier const& val)
{
mapbox::util::apply_visitor(identifier_to_stream_visitor{os}, val);
return os;
}

Expand Down
51 changes: 47 additions & 4 deletions test/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ TEST_CASE("operator<<")
CHECK(line == std::string("[[[[[10,20],[30,40]]]]]"));
}

TEST_CASE("operator<< feature")
TEST_CASE("operator<< feature value")
{
mapbox::feature::null_value_t null;
mapbox::feature::value val_null{};
Expand Down Expand Up @@ -107,13 +107,13 @@ TEST_CASE("operator<< feature")
CHECK(line == std::string("1.2"));

std::getline(stream, line);
CHECK(line == std::string("foo"));
CHECK(line == std::string("\"foo\""));

std::getline(stream, line);
CHECK(line == std::string("\"foo\""));
CHECK(line == std::string("\"\\\"foo\\\"\""));

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

std::getline(stream, line);
CHECK(line == std::string("true"));
Expand All @@ -133,3 +133,46 @@ TEST_CASE("operator<< feature")
std::getline(stream, line);
CHECK(line == std::string("{\"blah\\\"\":12,\"fee\":\"foo\"}"));
}

TEST_CASE("operator<< feature identifier")
{
mapbox::feature::identifier id_null{};
mapbox::feature::identifier id_int{static_cast<std::int64_t>(1)};
mapbox::feature::identifier id_uint{static_cast<std::uint64_t>(1U)};
mapbox::feature::identifier id_double{static_cast<double>(1.2)};
mapbox::feature::identifier id_str{"foo"};
mapbox::feature::identifier id_str_quote{"\"foo\""};
mapbox::feature::identifier id_str_backslash{"\\"};

std::stringstream stream;
stream << id_null << std::endl;
stream << id_int << std::endl;
stream << id_uint << std::endl;
stream << id_double << std::endl;
stream << id_str << std::endl;
stream << id_str_quote << std::endl;
stream << id_str_backslash << std::endl;

std::string line;

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

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

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

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

std::getline(stream, line);
CHECK(line == std::string("\"foo\""));

std::getline(stream, line);
CHECK(line == std::string("\"\\\"foo\\\"\""));

std::getline(stream, line);
CHECK(line == std::string("\"\\\\\""));
}

0 comments on commit 62eb926

Please sign in to comment.