[geometry] fixed c08 example w.r.t. iterators

[SVN r85294]
This commit is contained in:
Barend Gehrels 2013-08-10 22:18:27 +00:00
parent 1fa129395f
commit 914f558a3c

View File

@ -65,41 +65,59 @@ class my_polygon
// Adaption: implement iterator and range-extension, and register with Boost.Geometry // Adaption: implement iterator and range-extension, and register with Boost.Geometry
// 1) implement iterator (const and non-const versions) // 1) implement iterator (const and non-const versions)
template <bool IsConst> template<typename MyPolygon>
struct custom_iterator : public boost::iterator_facade struct custom_iterator : public boost::iterator_facade
< <
custom_iterator<IsConst>, custom_iterator<MyPolygon>,
my_point, my_point,
boost::random_access_traversal_tag, boost::random_access_traversal_tag,
typename boost::geometry::add_const_if_c<IsConst, my_point>::type& typename boost::mpl::if_
<
boost::is_const<MyPolygon>,
my_point const,
my_point
>::type&
> >
{ {
// Constructor for begin() // Constructor for begin()
explicit custom_iterator(typename boost::geometry::add_const_if_c<IsConst, my_polygon>::type& polygon) explicit custom_iterator(MyPolygon& polygon)
: m_polygon(&polygon) : m_polygon(&polygon)
, m_index(0) , m_index(0)
{} {}
// Constructor for end() // Constructor for end()
explicit custom_iterator(bool, typename boost::geometry::add_const_if_c<IsConst, my_polygon>::type& polygon) explicit custom_iterator(bool, MyPolygon& polygon)
: m_polygon(&polygon) : m_polygon(&polygon)
, m_index(polygon.point_count()) , m_index(polygon.point_count())
{} {}
// Default constructor
explicit custom_iterator()
: m_polygon(NULL)
, m_index(-1)
{}
typedef typename boost::mpl::if_
<
boost::is_const<MyPolygon>,
my_point const,
my_point
>::type my_point_type;
private: private:
friend class boost::iterator_core_access; friend class boost::iterator_core_access;
typedef boost::iterator_facade typedef boost::iterator_facade
< <
custom_iterator<IsConst>, custom_iterator<MyPolygon>,
my_point, my_point,
boost::random_access_traversal_tag, boost::random_access_traversal_tag,
typename boost::geometry::add_const_if_c<IsConst, my_point>::type& my_point_type&
> facade; > facade;
typename boost::geometry::add_const_if_c<IsConst, my_polygon>::type* m_polygon; MyPolygon* m_polygon;
int m_index; int m_index;
bool equal(custom_iterator const& other) const bool equal(custom_iterator const& other) const
@ -134,7 +152,7 @@ private:
} }
// const and non-const dereference of this iterator // const and non-const dereference of this iterator
typename boost::geometry::add_const_if_c<IsConst, my_point>::type& dereference() const my_point_type& dereference() const
{ {
return m_polygon->get_point(m_index); return m_polygon->get_point(m_index);
} }
@ -150,12 +168,12 @@ namespace boost
{ {
template<> struct range_mutable_iterator<my_polygon> template<> struct range_mutable_iterator<my_polygon>
{ {
typedef custom_iterator<false> type; typedef custom_iterator<my_polygon> type;
}; };
template<> struct range_const_iterator<my_polygon> template<> struct range_const_iterator<my_polygon>
{ {
typedef custom_iterator<true> type; typedef custom_iterator<my_polygon const> type;
}; };
// RangeEx // RangeEx
@ -168,24 +186,24 @@ namespace boost
// 2b) free-standing function for Boost.Range ADP // 2b) free-standing function for Boost.Range ADP
inline custom_iterator<false> range_begin(my_polygon& polygon) inline custom_iterator<my_polygon> range_begin(my_polygon& polygon)
{ {
return custom_iterator<false>(polygon); return custom_iterator<my_polygon>(polygon);
} }
inline custom_iterator<true> range_begin(my_polygon const& polygon) inline custom_iterator<my_polygon const> range_begin(my_polygon const& polygon)
{ {
return custom_iterator<true>(polygon); return custom_iterator<my_polygon const>(polygon);
} }
inline custom_iterator<false> range_end(my_polygon& polygon) inline custom_iterator<my_polygon> range_end(my_polygon& polygon)
{ {
return custom_iterator<false>(true, polygon); return custom_iterator<my_polygon>(true, polygon);
} }
inline custom_iterator<true> range_end(my_polygon const& polygon) inline custom_iterator<my_polygon const> range_end(my_polygon const& polygon)
{ {
return custom_iterator<true>(true, polygon); return custom_iterator<my_polygon const>(true, polygon);
} }
@ -210,6 +228,14 @@ template<> struct resize<my_polygon>
} }
}; };
template<> struct clear<my_polygon>
{
static inline void apply(my_polygon& polygon)
{
polygon.erase_all();
}
};
}}} }}}
@ -225,8 +251,8 @@ BOOST_GEOMETRY_REGISTER_RING(my_polygon)
void walk_using_iterator(my_polygon const& polygon) void walk_using_iterator(my_polygon const& polygon)
{ {
for (custom_iterator<true> it = custom_iterator<true>(polygon); for (custom_iterator<my_polygon const> it = custom_iterator<my_polygon const>(polygon);
it != custom_iterator<true>(true, polygon); it != custom_iterator<my_polygon const>(true, polygon);
++it) ++it)
{ {
std::cout << boost::geometry::dsv(*it) << std::endl; std::cout << boost::geometry::dsv(*it) << std::endl;