Merge pull request #39 from sdebionne/variant/expand

[expand] Add variant support
This commit is contained in:
brunolalande 2014-05-23 23:03:55 +01:00
commit d563f3706d
2 changed files with 72 additions and 10 deletions

View File

@ -3,6 +3,7 @@
// Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
// Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
// Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
// Copyright (c) 2014 Samuel Debionne, Grenoble, France.
// Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
// (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
@ -28,6 +29,9 @@
#include <boost/geometry/strategies/compare.hpp>
#include <boost/geometry/policies/compare.hpp>
#include <boost/variant/static_visitor.hpp>
#include <boost/variant/apply_visitor.hpp>
namespace boost { namespace geometry
{
@ -249,6 +253,51 @@ struct expand<Box, Segment, StrategyLess, StrategyGreater, box_tag, segment_tag>
#endif // DOXYGEN_NO_DISPATCH
namespace resolve_variant {
template <typename Geometry>
struct expand
{
template <typename Box>
static inline void apply(Box& box, Geometry const& geometry)
{
concept::check<Box>();
concept::check<Geometry const>();
concept::check_concepts_and_equal_dimensions<Box, Geometry const>();
dispatch::expand<Box, Geometry>::apply(box, geometry);
}
};
template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
struct expand<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
{
template <typename Box>
struct visitor: boost::static_visitor<void>
{
Box& m_box;
visitor(Box& box) : m_box(box) {}
template <typename Geometry>
void operator()(Geometry const& geometry) const
{
return expand<Geometry>::apply(m_box, geometry);
}
};
template <class Box>
static inline void
apply(Box& box,
boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry)
{
return boost::apply_visitor(visitor<Box>(box), geometry);
}
};
} // namespace resolve_variant
/***
*!
\brief Expands a box using the extend (envelope) of another geometry (box, point)
@ -290,9 +339,7 @@ inline void expand(Box& box, Geometry const& geometry,
template <typename Box, typename Geometry>
inline void expand(Box& box, Geometry const& geometry)
{
concept::check_concepts_and_equal_dimensions<Box, Geometry const>();
dispatch::expand<Box, Geometry>::apply(box, geometry);
resolve_variant::expand<Geometry>::apply(box, geometry);
}
}} // namespace boost::geometry

View File

@ -17,6 +17,16 @@
#include <boost/geometry/io/wkt/read.hpp>
#include <boost/geometry/algorithms/assign.hpp>
#include <boost/geometry/io/dsv/write.hpp>
#include <boost/variant/variant.hpp>
template <typename Box>
inline std::string to_dsv(const Box& box)
{
std::ostringstream out;
out << bg::dsv(box, ",", "(", ")", ",", "", "");
return out.str();
}
template <typename Geometry, typename Box>
@ -29,10 +39,13 @@ void test_expand(Box& box,
bg::expand(box, geometry);
std::ostringstream out;
out << bg::dsv(box, ",", "(", ")", ",", "", "");
BOOST_CHECK_EQUAL(to_dsv(box), expected);
BOOST_CHECK_EQUAL(out.str(), expected);
#if !defined(GEOMETRY_TEST_DEBUG)
bg::expand(box, boost::variant<Geometry>(geometry));
BOOST_CHECK_EQUAL(to_dsv(box), expected);
#endif
}
template <typename Geometry, typename Box>
@ -43,13 +56,15 @@ void test_expand_other_strategy(Box& box,
Geometry geometry;
bg::read_wkt(wkt, geometry);
bg::expand(box, geometry);
std::ostringstream out;
out << bg::dsv(box, ",", "(", ")", ",", "", "");
BOOST_CHECK_EQUAL(to_dsv(box), expected);
BOOST_CHECK_EQUAL(out.str(), expected);
#if !defined(GEOMETRY_TEST_DEBUG)
bg::expand(box, boost::variant<Geometry>(geometry));
BOOST_CHECK_EQUAL(to_dsv(box), expected);
#endif
}