copy_on_dereference_geometries.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2015, Oracle and/or its affiliates.
  4. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  5. // Licensed under the Boost Software License version 1.0.
  6. // http://www.boost.org/users/license.html
  7. #ifndef GEOMETRY_TEST_TEST_GEOMETRIES_COPY_ON_DEREFERENCE_GEOMETRIES_HPP
  8. #define GEOMETRY_TEST_TEST_GEOMETRIES_COPY_ON_DEREFERENCE_GEOMETRIES_HPP
  9. #include <cstddef>
  10. #include <iterator>
  11. #include <vector>
  12. #include <boost/iterator/iterator_facade.hpp>
  13. #include <boost/iterator/iterator_categories.hpp>
  14. #include <boost/geometry/core/tag.hpp>
  15. template <typename RandomAccessIterator>
  16. class copy_on_dereference_iterator
  17. : public boost::iterator_facade
  18. <
  19. copy_on_dereference_iterator<RandomAccessIterator>,
  20. typename std::iterator_traits<RandomAccessIterator>::value_type,
  21. boost::random_access_traversal_tag,
  22. typename std::iterator_traits<RandomAccessIterator>::value_type,
  23. typename std::iterator_traits<RandomAccessIterator>::difference_type
  24. >
  25. {
  26. private:
  27. typedef boost::iterator_facade
  28. <
  29. copy_on_dereference_iterator<RandomAccessIterator>,
  30. typename std::iterator_traits<RandomAccessIterator>::value_type,
  31. boost::random_access_traversal_tag,
  32. typename std::iterator_traits<RandomAccessIterator>::value_type,
  33. typename std::iterator_traits<RandomAccessIterator>::difference_type
  34. > base_type;
  35. public:
  36. typedef typename base_type::reference reference;
  37. typedef typename base_type::difference_type difference_type;
  38. copy_on_dereference_iterator() {}
  39. copy_on_dereference_iterator(RandomAccessIterator it) : m_it(it) {}
  40. template <typename OtherRAI>
  41. copy_on_dereference_iterator
  42. (copy_on_dereference_iterator<OtherRAI> const& other)
  43. : m_it(other.m_it)
  44. {}
  45. private:
  46. friend class boost::iterator_core_access;
  47. template <typename OtherRAI>
  48. friend class copy_on_dereference_iterator;
  49. inline reference dereference() const { return *m_it; }
  50. inline void increment() { ++m_it; }
  51. inline void decrement() { --m_it; }
  52. inline void advance(difference_type n) { m_it += n; }
  53. template <typename OtherRAI>
  54. inline bool equal(copy_on_dereference_iterator<OtherRAI> const& other) const
  55. {
  56. return m_it == other.m_it;
  57. }
  58. template <typename OtherRAI>
  59. inline difference_type
  60. distance_to(copy_on_dereference_iterator<OtherRAI> const& other) const
  61. {
  62. return std::distance(m_it, other.m_it);
  63. }
  64. private:
  65. RandomAccessIterator m_it;
  66. };
  67. template <typename Value>
  68. class range_copy_on_dereference : private std::vector<Value>
  69. {
  70. private:
  71. typedef std::vector<Value> base_type;
  72. public:
  73. typedef typename base_type::size_type size_type;
  74. typedef copy_on_dereference_iterator
  75. <
  76. typename base_type::const_iterator
  77. > const_iterator;
  78. typedef const_iterator iterator;
  79. inline iterator begin()
  80. {
  81. return iterator(base_type::begin());
  82. }
  83. inline iterator end()
  84. {
  85. return iterator(base_type::end());
  86. }
  87. inline const_iterator begin() const
  88. {
  89. return const_iterator(base_type::begin());
  90. }
  91. inline const_iterator end() const
  92. {
  93. return const_iterator(base_type::end());
  94. }
  95. inline void clear()
  96. {
  97. base_type::clear();
  98. }
  99. inline void push_back(Value const& value)
  100. {
  101. base_type::push_back(value);
  102. }
  103. inline void resize(std::size_t n)
  104. {
  105. base_type::resize(n);
  106. }
  107. inline size_type size() const
  108. {
  109. return base_type::size();
  110. }
  111. };
  112. template <typename Point>
  113. struct multipoint_copy_on_dereference : range_copy_on_dereference<Point>
  114. {};
  115. template <typename Point>
  116. struct linestring_copy_on_dereference : range_copy_on_dereference<Point>
  117. {};
  118. namespace boost { namespace geometry
  119. {
  120. namespace traits
  121. {
  122. template <typename Point>
  123. struct tag< multipoint_copy_on_dereference<Point> >
  124. {
  125. typedef multi_point_tag type;
  126. };
  127. template <typename Point>
  128. struct tag< linestring_copy_on_dereference<Point> >
  129. {
  130. typedef linestring_tag type;
  131. };
  132. } // namespace traits
  133. }} // namespace boost::geometry
  134. #endif // GEOMETRY_TEST_TEST_GEOMETRIES_COPY_ON_DEREFERENCE_GEOMETRIES_HPP