wrapped_boost_array.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2010-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef GEOMETRY_TEST_TEST_GEOMETRIES_WRAPPED_BOOST_ARRAY_HPP
  8. #define GEOMETRY_TEST_TEST_GEOMETRIES_WRAPPED_BOOST_ARRAY_HPP
  9. #include <cstddef>
  10. #include <boost/array.hpp>
  11. #include <boost/range.hpp>
  12. #include <boost/geometry/core/mutable_range.hpp>
  13. #include <boost/geometry/core/tag.hpp>
  14. #include <boost/geometry/core/tags.hpp>
  15. namespace test
  16. {
  17. template <typename Point, std::size_t Count>
  18. struct wrapped_boost_array
  19. {
  20. inline wrapped_boost_array() : size(0) {}
  21. boost::array<Point, Count> array;
  22. std::size_t size;
  23. };
  24. } // namespace test
  25. // 1a: adapt to Boost.Range
  26. namespace boost
  27. {
  28. using namespace test;
  29. template <typename Point, std::size_t Count>
  30. struct range_mutable_iterator<wrapped_boost_array<Point, Count> >
  31. : public range_mutable_iterator<boost::array<Point, Count> >
  32. {};
  33. template <typename Point, std::size_t Count>
  34. struct range_const_iterator<wrapped_boost_array<Point, Count> >
  35. : public range_const_iterator<boost::array<Point, Count> >
  36. {};
  37. } // namespace 'boost'
  38. // 1b) adapt to Boost.Range with ADP
  39. namespace test
  40. {
  41. template <typename Point, std::size_t Count>
  42. inline typename boost::range_iterator
  43. <
  44. wrapped_boost_array<Point, Count>
  45. >::type range_begin(wrapped_boost_array<Point, Count>& ar)
  46. {
  47. return ar.array.begin();
  48. }
  49. template <typename Point, std::size_t Count>
  50. inline typename boost::range_iterator
  51. <
  52. wrapped_boost_array<Point, Count> const
  53. >::type range_begin(wrapped_boost_array<Point, Count> const& ar)
  54. {
  55. return ar.array.begin();
  56. }
  57. template <typename Point, std::size_t Count>
  58. inline typename boost::range_iterator
  59. <
  60. wrapped_boost_array<Point, Count>
  61. >::type range_end(wrapped_boost_array<Point, Count>& ar)
  62. {
  63. typename boost::range_iterator
  64. <
  65. wrapped_boost_array<Point, Count>
  66. >::type it = ar.array.begin();
  67. return it + ar.size;
  68. }
  69. template <typename Point, std::size_t Count>
  70. inline typename boost::range_iterator
  71. <
  72. wrapped_boost_array<Point, Count> const
  73. >::type range_end(wrapped_boost_array<Point, Count> const& ar)
  74. {
  75. typename boost::range_iterator
  76. <
  77. wrapped_boost_array<Point, Count> const
  78. >::type it = ar.array.begin();
  79. return it + ar.size;
  80. }
  81. }
  82. // 2: adapt to Boost.Geometry
  83. namespace boost { namespace geometry { namespace traits
  84. {
  85. template <typename Point, std::size_t Count>
  86. struct tag< wrapped_boost_array<Point, Count> >
  87. {
  88. typedef linestring_tag type;
  89. };
  90. template <typename Point, std::size_t Count>
  91. struct clear< wrapped_boost_array<Point, Count> >
  92. {
  93. static inline void apply(wrapped_boost_array<Point, Count>& ar)
  94. {
  95. ar.size = 0;
  96. }
  97. };
  98. template <typename Point, std::size_t Count>
  99. struct push_back< wrapped_boost_array<Point, Count> >
  100. {
  101. static inline void apply(wrapped_boost_array<Point, Count>& ar, Point const& point)
  102. {
  103. // BOOST_ASSERT((ar.size < Count));
  104. ar.array[ar.size++] = point;
  105. }
  106. };
  107. template <typename Point, std::size_t Count>
  108. struct resize< wrapped_boost_array<Point, Count> >
  109. {
  110. static inline void apply(wrapped_boost_array<Point, Count>& ar, std::size_t new_size)
  111. {
  112. BOOST_ASSERT(new_size < Count);
  113. ar.size = new_size;
  114. }
  115. };
  116. }}} // namespace bg::traits
  117. #endif // GEOMETRY_TEST_TEST_GEOMETRIES_WRAPPED_BOOST_ARRAY_HPP