any_ns.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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(BOOST_SPIRIT_ANY_NS_MARCH_13_2007_0827AM)
  7. #define BOOST_SPIRIT_ANY_NS_MARCH_13_2007_0827AM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/mpl/bool.hpp>
  12. #include <boost/fusion/include/equal_to.hpp>
  13. #include <boost/fusion/include/next.hpp>
  14. #include <boost/fusion/include/deref.hpp>
  15. #include <boost/fusion/include/begin.hpp>
  16. #include <boost/fusion/include/end.hpp>
  17. #include <boost/fusion/include/any.hpp>
  18. #include <boost/spirit/home/support/unused.hpp>
  19. namespace boost { namespace spirit
  20. {
  21. // A non-short circuiting (ns) version of the any algorithm (uses
  22. // | instead of ||.
  23. namespace detail
  24. {
  25. template <typename First1, typename Last, typename First2, typename F>
  26. inline bool
  27. any_ns(First1 const&, First2 const&, Last const&, F const&, mpl::true_)
  28. {
  29. return false;
  30. }
  31. template <typename First1, typename Last, typename First2, typename F>
  32. inline bool
  33. any_ns(First1 const& first1, First2 const& first2, Last const& last, F& f, mpl::false_)
  34. {
  35. // cast bool to int to avoid -Wbitwise-instead-of-logical warning
  36. return (0 != (static_cast<int>(f(*first1, *first2)) |
  37. detail::any_ns(
  38. fusion::next(first1)
  39. , fusion::next(first2)
  40. , last
  41. , f
  42. , fusion::result_of::equal_to<
  43. typename fusion::result_of::next<First1>::type, Last>())));
  44. }
  45. template <typename First, typename Last, typename F>
  46. inline bool
  47. any_ns(First const&, Last const&, F const&, mpl::true_)
  48. {
  49. return false;
  50. }
  51. template <typename First, typename Last, typename F>
  52. inline bool
  53. any_ns(First const& first, Last const& last, F& f, mpl::false_)
  54. {
  55. // cast bool to int to avoid -Wbitwise-instead-of-logical warning
  56. return (0 != (static_cast<int>(f(*first)) |
  57. detail::any_ns(
  58. fusion::next(first)
  59. , last
  60. , f
  61. , fusion::result_of::equal_to<
  62. typename fusion::result_of::next<First>::type, Last>())));
  63. }
  64. }
  65. template <typename Sequence1, typename Sequence2, typename F>
  66. inline bool
  67. any_ns(Sequence1 const& seq1, Sequence2& seq2, F f)
  68. {
  69. return detail::any_ns(
  70. fusion::begin(seq1)
  71. , fusion::begin(seq2)
  72. , fusion::end(seq1)
  73. , f
  74. , fusion::result_of::equal_to<
  75. typename fusion::result_of::begin<Sequence1>::type
  76. , typename fusion::result_of::end<Sequence1>::type>());
  77. }
  78. template <typename Sequence, typename F>
  79. inline bool
  80. any_ns(Sequence const& seq, unused_type, F f)
  81. {
  82. return detail::any_ns(
  83. fusion::begin(seq)
  84. , fusion::end(seq)
  85. , f
  86. , fusion::result_of::equal_to<
  87. typename fusion::result_of::begin<Sequence>::type
  88. , typename fusion::result_of::end<Sequence>::type>());
  89. }
  90. }}
  91. #endif