1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
6 
7 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
8 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
9 
10 // Use, modification and distribution is subject to the Boost Software License,
11 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
12 // http://www.boost.org/LICENSE_1_0.txt)
13 
14 #ifndef BOOST_GEOMETRY_VIEWS_DETAIL_POINTS_VIEW_HPP
15 #define BOOST_GEOMETRY_VIEWS_DETAIL_POINTS_VIEW_HPP
16 
17 
18 #include <boost/range.hpp>
19 #include <boost/iterator.hpp>
20 #include <boost/iterator/iterator_facade.hpp>
21 #include <boost/iterator/iterator_categories.hpp>
22 
23 #include <boost/geometry/core/exception.hpp>
24 
25 namespace boost { namespace geometry
26 {
27 
28 namespace detail
29 {
30 
31 // Adapts pointer, on points, to a Boost.Range
32 template <typename Point, int MaxSize>
33 class points_view
34 {
35     // Iterates over a series of points (indicated by pointer
36     // to have it lightweight). Probably there is already an
37     // equivalent of this within Boost. If so, TODO: use that one.
38     // This used to be "box_iterator" and "segment_iterator".
39     // ALTERNATIVE: use boost:array and its iterators
40     struct points_iterator
41         : public boost::iterator_facade
42             <
43                 points_iterator,
44                 Point const,
45                 boost::random_access_traversal_tag
46             >
47     {
48         // Constructor: Begin iterator
points_iteratorboost::geometry::detail::points_view::points_iterator49         inline points_iterator(Point const* p)
50             : m_points(p)
51             , m_index(0)
52         {}
53 
54         // Constructor: End iterator
points_iteratorboost::geometry::detail::points_view::points_iterator55         inline points_iterator(Point const* p, bool)
56             : m_points(p)
57             , m_index(MaxSize)
58         {}
59 
60         // Constructor: default (for Range Concept checking).
points_iteratorboost::geometry::detail::points_view::points_iterator61         inline points_iterator()
62             : m_points(NULL)
63             , m_index(MaxSize)
64         {}
65 
66         typedef std::ptrdiff_t difference_type;
67 
68     private:
69         friend class boost::iterator_core_access;
70 
dereferenceboost::geometry::detail::points_view::points_iterator71         inline Point const& dereference() const
72         {
73             if (m_index >= 0 && m_index < MaxSize)
74             {
75                 return m_points[m_index];
76             }
77 
78             // If it index larger (or smaller) return first point
79             // (assuming initialized)
80             return m_points[0];
81         }
82 
equalboost::geometry::detail::points_view::points_iterator83         inline bool equal(points_iterator const& other) const
84         {
85             return other.m_index == this->m_index;
86         }
87 
incrementboost::geometry::detail::points_view::points_iterator88         inline void increment()
89         {
90             m_index++;
91         }
92 
decrementboost::geometry::detail::points_view::points_iterator93         inline void decrement()
94         {
95             m_index--;
96         }
97 
distance_toboost::geometry::detail::points_view::points_iterator98         inline difference_type distance_to(points_iterator const& other) const
99         {
100             return other.m_index - this->m_index;
101         }
102 
advanceboost::geometry::detail::points_view::points_iterator103         inline void advance(difference_type n)
104         {
105             m_index += n;
106         }
107 
108         Point const* m_points;
109         difference_type m_index;
110     };
111 
112 public :
113 
114     typedef points_iterator const_iterator;
115     typedef points_iterator iterator; // must be defined
116 
begin() const117     const_iterator begin() const { return const_iterator(m_points); }
end() const118     const_iterator end() const { return const_iterator(m_points, true); }
119 
120     // It may NOT be used non-const, so commented:
121     //iterator begin() { return m_begin; }
122     //iterator end() { return m_end; }
123 
124 protected :
125 
126     template <typename CopyPolicy>
points_view(CopyPolicy const & copy)127     explicit points_view(CopyPolicy const& copy)
128     {
129        copy.apply(m_points);
130     }
131 
132 private :
133     // Copy points here - box might define them otherwise
134     Point m_points[MaxSize];
135 };
136 
137 }
138 
139 }} // namespace boost::geometry
140 
141 
142 #endif // BOOST_GEOMETRY_VIEWS_DETAIL_POINTS_VIEW_HPP
143