any_range.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright Neil Groves 2010. Use, modification and
  2. // distribution is subject to the Boost Software License, Version
  3. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. //
  7. // For more information, see http://www.boost.org/libs/range/
  8. //
  9. #ifndef BOOST_RANGE_ANY_RANGE_HPP_INCLUDED
  10. #define BOOST_RANGE_ANY_RANGE_HPP_INCLUDED
  11. #include <boost/config.hpp>
  12. #include <boost/iterator/iterator_categories.hpp>
  13. #include <boost/iterator/iterator_traits.hpp>
  14. #include <boost/iterator/iterator_facade.hpp>
  15. #include <boost/iterator/iterator_adaptor.hpp>
  16. #include <boost/range/detail/any_iterator.hpp>
  17. #include <boost/range/concepts.hpp>
  18. #include <boost/range/reference.hpp>
  19. #include <boost/range/value_type.hpp>
  20. #include <boost/range/iterator_range_core.hpp>
  21. namespace boost
  22. {
  23. namespace range_detail
  24. {
  25. // If T is use_default, return the result of Default, otherwise
  26. // return T.
  27. //
  28. // This is an implementation artifact used to pick intelligent default
  29. // values when the user specified boost::use_default as a template
  30. // parameter.
  31. template<
  32. class T,
  33. class Default
  34. >
  35. struct any_range_default_help
  36. : mpl::eval_if<
  37. is_same<T, use_default>
  38. , Default
  39. , mpl::identity<T>
  40. >
  41. {
  42. };
  43. template<
  44. class WrappedRange
  45. , class Value
  46. , class Reference
  47. >
  48. struct any_range_value_type
  49. {
  50. # ifdef BOOST_ITERATOR_REF_CONSTNESS_KILLS_WRITABILITY
  51. typedef typename any_range_default_help<
  52. Value
  53. , mpl::eval_if<
  54. is_same<Reference, use_default>
  55. , range_value<
  56. typename remove_const<WrappedRange>
  57. ::type>
  58. , remove_reference<Reference>
  59. >
  60. >::type type;
  61. # else
  62. typedef typename any_range_default_help<
  63. Value
  64. , range_value<
  65. typename remove_const<WrappedRange>
  66. ::type>
  67. >::type type;
  68. # endif
  69. };
  70. template<
  71. class Value
  72. , class Traversal
  73. , class Reference = Value&
  74. , class Difference = std::ptrdiff_t
  75. , class Buffer = use_default
  76. >
  77. class any_range
  78. : public iterator_range<
  79. any_iterator<
  80. Value
  81. , Traversal
  82. , Reference
  83. , Difference
  84. , typename any_range_default_help<
  85. Buffer
  86. , mpl::identity<any_iterator_default_buffer>
  87. >::type
  88. >
  89. >
  90. {
  91. typedef iterator_range<
  92. any_iterator<
  93. Value
  94. , Traversal
  95. , Reference
  96. , Difference
  97. , typename any_range_default_help<
  98. Buffer
  99. , mpl::identity<any_iterator_default_buffer>
  100. >::type
  101. >
  102. > base_type;
  103. struct enabler {};
  104. struct disabler {};
  105. public:
  106. any_range()
  107. {
  108. }
  109. any_range(const any_range& other)
  110. : base_type(other)
  111. {
  112. }
  113. template<class WrappedRange>
  114. any_range(WrappedRange& wrapped_range)
  115. : base_type(boost::begin(wrapped_range),
  116. boost::end(wrapped_range))
  117. {
  118. }
  119. template<class WrappedRange>
  120. any_range(const WrappedRange& wrapped_range)
  121. : base_type(boost::begin(wrapped_range),
  122. boost::end(wrapped_range))
  123. {
  124. }
  125. template<
  126. class OtherValue
  127. , class OtherTraversal
  128. , class OtherReference
  129. , class OtherDifference
  130. >
  131. any_range(const any_range<
  132. OtherValue
  133. , OtherTraversal
  134. , OtherReference
  135. , OtherDifference
  136. , Buffer
  137. >& other)
  138. : base_type(boost::begin(other), boost::end(other))
  139. {
  140. }
  141. template<class Iterator>
  142. any_range(Iterator first, Iterator last)
  143. : base_type(first, last)
  144. {
  145. }
  146. };
  147. template<
  148. class WrappedRange
  149. , class Value = use_default
  150. , class Traversal = use_default
  151. , class Reference = use_default
  152. , class Difference = use_default
  153. , class Buffer = use_default
  154. >
  155. struct any_range_type_generator
  156. {
  157. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<WrappedRange> ));
  158. typedef any_range<
  159. typename any_range_value_type<
  160. WrappedRange
  161. , Value
  162. , typename any_range_default_help<
  163. Reference
  164. , range_reference<WrappedRange>
  165. >::type
  166. >::type
  167. , typename any_range_default_help<
  168. Traversal
  169. , iterator_traversal<
  170. typename range_iterator<WrappedRange>::type
  171. >
  172. >::type
  173. , typename any_range_default_help<
  174. Reference
  175. , range_reference<WrappedRange>
  176. >::type
  177. , typename any_range_default_help<
  178. Difference
  179. , range_difference<WrappedRange>
  180. >::type
  181. , typename any_range_default_help<
  182. Buffer
  183. , mpl::identity<any_iterator_default_buffer>
  184. >::type
  185. > type;
  186. };
  187. } // namespace range_detail
  188. using range_detail::any_range;
  189. using range_detail::any_range_type_generator;
  190. } // namespace boost
  191. #endif // include guard