distance.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #if !defined(FUSION_DISTANCE_09172005_0721)
  7. #define FUSION_DISTANCE_09172005_0721
  8. #include <boost/fusion/iterator/detail/distance.hpp>
  9. #include <boost/fusion/support/category_of.hpp>
  10. #include <boost/mpl/int.hpp>
  11. #include <boost/mpl/assert.hpp>
  12. #include <boost/type_traits/is_same.hpp>
  13. #include <boost/fusion/support/tag_of.hpp>
  14. namespace boost { namespace fusion
  15. {
  16. struct random_access_traversal_tag;
  17. // Special tags:
  18. struct iterator_facade_tag; // iterator facade tag
  19. struct boost_array_iterator_tag; // boost::array iterator tag
  20. struct mpl_iterator_tag; // mpl sequence iterator tag
  21. struct std_pair_iterator_tag; // std::pair iterator tag
  22. namespace extension
  23. {
  24. template <typename Tag>
  25. struct distance_impl
  26. {
  27. // default implementation
  28. template <typename First, typename Last>
  29. struct apply : distance_detail::linear_distance<First, Last>
  30. {};
  31. };
  32. template <>
  33. struct distance_impl<iterator_facade_tag>
  34. {
  35. template <typename First, typename Last>
  36. struct apply : First::template distance<First, Last> {};
  37. };
  38. template <>
  39. struct distance_impl<boost_array_iterator_tag>;
  40. template <>
  41. struct distance_impl<mpl_iterator_tag>;
  42. template <>
  43. struct distance_impl<std_pair_iterator_tag>;
  44. }
  45. namespace result_of
  46. {
  47. template <typename First, typename Last>
  48. struct distance
  49. : extension::distance_impl<typename detail::tag_of<First>::type>::
  50. template apply<First, Last>
  51. {
  52. typedef typename extension::distance_impl<typename detail::tag_of<First>::type>::
  53. template apply<First, Last>::type distance_application;
  54. BOOST_STATIC_CONSTANT(int, value = distance_application::value);
  55. };
  56. }
  57. template <typename First, typename Last>
  58. inline typename result_of::distance<First, Last>::type
  59. distance(First const& a, Last const& b)
  60. {
  61. return result_of::distance<First, Last>::call(a,b);
  62. }
  63. }}
  64. #endif