Changed template parameter type from stream to iterator and added a specialization for std::ostream_iterator.

This commit is contained in:
Carlos López 2010-07-22 04:48:11 +00:00
parent 26e4fedfde
commit b5b94a868c
16 changed files with 72 additions and 109 deletions

View file

@ -33,15 +33,16 @@
namespace mapnik
{
// parameterized with the type of ostream it will use for output.
// parameterized with the type of output iterator it will use for output.
// output iterators add more flexibility than streams, because iterators
// can target many other output destinations besides streams.
template <typename T>
class MAPNIK_DECL svg_renderer : public feature_style_processor<svg_renderer<T> >,
private boost::noncopyable
{
public:
// the only parameter I'm sure of is the map.
svg_renderer(Map const& m, T& output_stream);
svg_renderer(Map const& m, T& output_iterator);
~svg_renderer();
void start_map_processing(Map const& map);
@ -83,15 +84,15 @@ namespace mapnik
proj_transform const& prj_trans);
// should this function be inline?
inline T& get_output_stream()
inline T& get_output_iterator()
{
return output_stream_;
return output_iterator_;
}
// should this function be inline?
inline const T& get_output_stream() const
inline const T& get_output_iterator() const
{
return output_stream_;
return output_iterator_;
}
// XML declaration.
@ -104,7 +105,7 @@ namespace mapnik
static const std::string SVG_NAMESPACE_URL;
private:
T& output_stream_;
T& output_iterator_;
const int width_;
const int height_;
};

View file

@ -28,17 +28,13 @@ namespace mapnik
{
template <typename T>
void svg_renderer<T>::process(building_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans)
Feature const& feature,
proj_transform const& prj_trans)
{
// nothing yet.
}
template void svg_renderer<std::ostringstream>::process(building_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
template void svg_renderer<std::ofstream>::process(building_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
template void svg_renderer<std::ostream_iterator<char> >::process(building_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
}

View file

@ -28,17 +28,13 @@ namespace mapnik
{
template <typename T>
void svg_renderer<T>::process(glyph_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans)
Feature const& feature,
proj_transform const& prj_trans)
{
// nothing yet.
}
template void svg_renderer<std::ostringstream>::process(glyph_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
template void svg_renderer<std::ofstream>::process(glyph_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
template void svg_renderer<std::ostream_iterator<char> >::process(glyph_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
}

View file

@ -34,11 +34,7 @@ namespace mapnik
// nothing yet.
}
template void svg_renderer<std::ostringstream>::process(line_pattern_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
template void svg_renderer<std::ofstream>::process(line_pattern_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
template void svg_renderer<std::ostream_iterator<char> >::process(line_pattern_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
}

View file

@ -34,11 +34,7 @@ namespace mapnik
// nothing yet.
}
template void svg_renderer<std::ostringstream>::process(line_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
template void svg_renderer<std::ofstream>::process(line_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
template void svg_renderer<std::ostream_iterator<char> >::process(line_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
}

View file

@ -34,11 +34,7 @@ namespace mapnik
// nothing yet.
}
template void svg_renderer<std::ostringstream>::process(markers_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
template void svg_renderer<std::ofstream>::process(markers_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
template void svg_renderer<std::ostream_iterator<char> >::process(markers_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
}

View file

@ -34,11 +34,7 @@ namespace mapnik
// nothing yet.
}
template void svg_renderer<std::ostringstream>::process(point_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
template void svg_renderer<std::ofstream>::process(point_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
template void svg_renderer<std::ostream_iterator<char> >::process(point_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
}

View file

@ -34,11 +34,7 @@ namespace mapnik
// nothing yet.
}
template void svg_renderer<std::ostringstream>::process(polygon_pattern_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
template void svg_renderer<std::ofstream>::process(polygon_pattern_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
template void svg_renderer<std::ostream_iterator<char> >::process(polygon_pattern_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
}

View file

@ -34,11 +34,7 @@ namespace mapnik
// nothing yet.
}
template void svg_renderer<std::ostringstream>::process(polygon_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
template void svg_renderer<std::ofstream>::process(polygon_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
template void svg_renderer<std::ostream_iterator<char> >::process(polygon_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
}

View file

@ -34,11 +34,7 @@ namespace mapnik
// nothing yet.
}
template void svg_renderer<std::ostringstream>::process(raster_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
template void svg_renderer<std::ofstream>::process(raster_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
template void svg_renderer<std::ostream_iterator<char> >::process(raster_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
}

View file

@ -34,11 +34,7 @@ namespace mapnik
// nothing yet.
}
template void svg_renderer<std::ostringstream>::process(shield_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
template void svg_renderer<std::ofstream>::process(shield_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
template void svg_renderer<std::ostream_iterator<char> >::process(shield_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
}

View file

@ -34,11 +34,7 @@ namespace mapnik
// nothing yet.
}
template void svg_renderer<std::ostringstream>::process(text_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
template void svg_renderer<std::ofstream>::process(text_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
template void svg_renderer<std::ostream_iterator<char> >::process(text_symbolizer const& sym,
Feature const& feature,
proj_transform const& prj_trans);
}

View file

@ -28,8 +28,7 @@
#ifdef MAPNIK_DEBUG
#include <iostream>
#endif
#include <sstream>
#include <fstream>
#include <ostream>
// boost
#include <boost/fusion/tuple.hpp>
@ -64,9 +63,9 @@ namespace mapnik
const std::string svg_renderer<T>::SVG_NAMESPACE_URL = "http://www.w3.org/2000/svg";
template <typename T>
svg_renderer<T>::svg_renderer(Map const& m, T & output_stream) :
svg_renderer<T>::svg_renderer(Map const& m, T & output_iterator) :
feature_style_processor<svg_renderer>(m),
output_stream_(output_stream),
output_iterator_(output_iterator),
width_(m.width()),
height_(m.height())
{
@ -91,11 +90,9 @@ namespace mapnik
using repository::confix;
using fusion::tuple;
std::ostream_iterator<char> output_stream_iterator(output_stream_);
// generate XML header.
generate(
output_stream_iterator,
output_iterator_,
string << eol << string << eol,
XML_DECLARATION, SVG_DTD);
@ -104,7 +101,7 @@ namespace mapnik
// which is taken from the map's dimensions.
generate(
output_stream_iterator,
output_iterator_,
confix("<", ">")[
"svg width=" << confix('"', '"')[int_ << string]
<< " height=" << confix('"', '"')[int_ << string]
@ -118,13 +115,14 @@ namespace mapnik
{
// generate background color as a rectangle that spans the whole image.
generate(
output_stream_iterator,
output_iterator_,
confix("<", "/>")[
"rect x=" << confix('"', '"')[int_]
<< " y=" << confix('"', '"')[int_]
<< " width=" << confix('"', '"')[int_ << string]
<< " height=" << confix('"', '"')[int_ << string]
<< " style=" << confix('"', '"')["fill: " << string]],
<< " style=" << confix('"', '"')["fill: " << string]]
<< eol,
0, 0, tuple<int, std::string>(width_, "px"), tuple<int, std::string>(height_, "px"), bgcolor->to_hex_string());
}
}
@ -132,11 +130,13 @@ namespace mapnik
template <typename T>
void svg_renderer<T>::end_map_processing(Map const& map)
{
using karma::format;
using karma::generate;
using karma::lit;
// generate SVG root element closing tag.
output_stream_ << format(lit("\n</svg>"));
generate(
output_iterator_,
lit("</svg>"));
#ifdef MAPNIK_DEBUG
std::clog << "end map processing" << std::endl;
@ -163,6 +163,5 @@ namespace mapnik
#endif
}
template class svg_renderer<std::ostringstream>;
template class svg_renderer<std::ofstream>;
template class svg_renderer<std::ostream_iterator<char> >;
}

View file

@ -11,6 +11,7 @@
// std
#include <string>
#include <sstream>
#include <iterator>
/**
* This test case tests all the generators inside svg_renderer,
@ -26,13 +27,14 @@
BOOST_AUTO_TEST_CASE(combined_test_case)
{
using namespace mapnik;
typedef svg_renderer<std::ostringstream> svg_ren;
typedef svg_renderer<std::ostream_iterator<char> > svg_ren;
Map map(800, 600);
map.set_background(color_factory::from_string("white"));
std::ostringstream output_stream;
svg_ren renderer(map, output_stream);
std::ostream_iterator<char> output_stream_iterator(output_stream);
svg_ren renderer(map, output_stream_iterator);
renderer.apply();
std::string expected_output =
@ -48,7 +50,7 @@ BOOST_AUTO_TEST_CASE(combined_test_case)
+"\n"
+"</svg>";
std::string actual_output = renderer.get_output_stream().str();
std::string actual_output = output_stream.str();
BOOST_CHECK_EQUAL(actual_output, expected_output);
}

View file

@ -9,6 +9,7 @@
// std
#include <sstream>
#include <iterator>
/**
* This test is meant to see if the empty
@ -24,7 +25,8 @@ BOOST_AUTO_TEST_CASE(compile_test_case)
try
{
std::ostringstream output_stream;
svg_renderer<std::ostringstream> renderer(map, output_stream);
std::ostream_iterator<char> output_stream_iterator(output_stream);
svg_renderer<std::ostream_iterator<char> > renderer(map, output_stream_iterator);
renderer.apply();
}
catch(...)

View file

@ -19,6 +19,7 @@
// stl
#include <fstream>
#include <iterator>
namespace filesystem = boost::filesystem;
@ -39,7 +40,7 @@ namespace filesystem = boost::filesystem;
BOOST_AUTO_TEST_CASE(file_output_test_case)
{
using namespace mapnik;
typedef svg_renderer<std::ofstream> svg_ren;
typedef svg_renderer<std::ostream_iterator<char> > svg_ren;
Map map(800, 600);
map.set_background(color_factory::from_string("blue"));
@ -49,7 +50,9 @@ BOOST_AUTO_TEST_CASE(file_output_test_case)
if(output_stream)
{
svg_ren renderer(map, output_stream);
std::ostream_iterator<char> output_stream_iterator(output_stream);
svg_ren renderer(map, output_stream_iterator);
renderer.apply();
output_stream.close();