any_range.hpp 6.5 KB

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