Fixed c08_custom_non_std_example.cpp and now it also compiles for gcc

[SVN r68936]
This commit is contained in:
Barend Gehrels 2011-02-15 22:34:33 +00:00
parent 95e61f338e
commit 2bd6df2ce3
2 changed files with 20 additions and 57 deletions

View File

@ -30,7 +30,7 @@ exe c04_b_custom_triangle_example : c04_b_custom_triangle_example.cpp ;
exe c06_custom_polygon_example : c06_custom_polygon_example.cpp ; exe c06_custom_polygon_example : c06_custom_polygon_example.cpp ;
exe c07_custom_ring_pointer_example : c07_custom_ring_pointer_example.cpp ; exe c07_custom_ring_pointer_example : c07_custom_ring_pointer_example.cpp ;
# exe c08_custom_non_std_example : c08_custom_non_std_example.cpp ; exe c08_custom_non_std_example : c08_custom_non_std_example.cpp ;
exe c09_custom_fusion_example : c09_custom_fusion_example.cpp ; exe c09_custom_fusion_example : c09_custom_fusion_example.cpp ;
exe c10_custom_cs_example : c10_custom_cs_example.cpp ; exe c10_custom_cs_example : c10_custom_cs_example.cpp ;

View File

@ -7,11 +7,6 @@
// //
// Custom polygon example // Custom polygon example
#ifndef _MSC_VER
#warning "Currently only works for MSVC"
int main() { return 0; }
#else
#include <iostream> #include <iostream>
#include <boost/iterator.hpp> #include <boost/iterator.hpp>
@ -42,13 +37,14 @@ class my_polygon
public : public :
void add_point(my_point const& p) { points.push_back(p); } void add_point(my_point const& p) { points.push_back(p); }
// Const access
my_point const& get_point(std::size_t i) const my_point const& get_point(std::size_t i) const
{ {
assert(i < points.size()); assert(i < points.size());
return points[i]; return points[i];
} }
// Non const access // Mutable access
my_point & get_point(std::size_t i) my_point & get_point(std::size_t i)
{ {
assert(i < points.size()); assert(i < points.size());
@ -59,16 +55,7 @@ class my_polygon
int point_count() const { return points.size(); } int point_count() const { return points.size(); }
void erase_all() { points.clear(); } void erase_all() { points.clear(); }
// TEMPORARY: inline void set_size(int n) { points.resize(n); }
inline void resize(int n) { points.resize(n); }
// Note: it IS possible to have different method names;
// however, there should (probably) be two different
// iterators then or an iterator with a specified policy).
// Note 2: if there is a set_point function, the iterator
// does not have a way to dereference and non-const
// iterators will not work!
}; };
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -158,7 +145,7 @@ private:
// 2a) meta-functions // 2a) meta-functions
namespace boost namespace boost
{ {
template<> struct range_iterator<my_polygon> template<> struct range_mutable_iterator<my_polygon>
{ {
typedef custom_iterator<false> type; typedef custom_iterator<false> type;
}; };
@ -198,50 +185,29 @@ inline custom_iterator<true> range_end(my_polygon const& polygon)
return custom_iterator<true>(true, polygon); return custom_iterator<true>(true, polygon);
} }
// RangeEx
inline std::size_t range_size(my_polygon const& polygon)
// 3) optional, for writable geometries only, implement push_back/resize/clear
namespace boost { namespace geometry { namespace traits
{ {
return polygon.point_count();
}
template<> struct push_back<my_polygon>
// 3) optional, for writable geometries only, implement back_inserter (=push_back)
class custom_insert_iterator
{ {
my_polygon* m_polygon; static inline void apply(my_polygon& polygon, my_point const& point)
public:
typedef std::output_iterator_tag iterator_category;
// Not relevant for output iterator
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
typedef void const_reference;
explicit custom_insert_iterator(my_polygon& x)
: m_polygon(&x)
{}
custom_insert_iterator& operator=(my_point const & p)
{ {
m_polygon->add_point(p); polygon.add_point(point);
return *this;
} }
custom_insert_iterator& operator*() { return *this; }
custom_insert_iterator& operator++() { return *this; }
custom_insert_iterator& operator++(int) { return *this; }
}; };
template<> struct resize<my_polygon>
namespace std
{ {
custom_insert_iterator back_inserter(my_polygon& polygon) static inline void apply(my_polygon& polygon, std::size_t new_size)
{ {
return custom_insert_iterator(polygon); polygon.set_size(new_size);
} }
} };
}}}
// 4) register with Boost.Geometry // 4) register with Boost.Geometry
@ -304,8 +270,8 @@ int main()
my_polygon container2; my_polygon container2;
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
{ {
// Use here the std:: / Boost.Geometry way of inserting (but the my_polygon way of getting) // Use here the Boost.Geometry internal way of inserting (but the my_polygon way of getting)
*(std::back_inserter(container2)++) = container1.get_point(i); boost::geometry::traits::push_back<my_polygon>::apply(container2, container1.get_point(i));
} }
std::cout << "Second container is not closed:" << std::endl; std::cout << "Second container is not closed:" << std::endl;
@ -324,6 +290,3 @@ int main()
return 0; return 0;
} }
#endif