as_variant.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #ifndef BOOST_PP_IS_ITERATING
  7. #if !defined(BOOST_SPIRIT_AS_VARIANT_NOVEMBER_16_2007_0420PM)
  8. #define BOOST_SPIRIT_AS_VARIANT_NOVEMBER_16_2007_0420PM
  9. #include <boost/preprocessor/iterate.hpp>
  10. #include <boost/preprocessor/repetition/enum_params.hpp>
  11. #include <boost/preprocessor/repetition/enum_binary_params.hpp>
  12. #include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
  13. #include <boost/preprocessor/repetition/repeat_from_to.hpp>
  14. #include <boost/variant/variant_fwd.hpp>
  15. #include <boost/fusion/include/size.hpp>
  16. #include <boost/fusion/include/begin.hpp>
  17. #include <boost/fusion/include/next.hpp>
  18. #include <boost/fusion/include/value_of.hpp>
  19. #include <boost/mpl/fold.hpp>
  20. #include <boost/mpl/vector.hpp>
  21. #include <boost/mpl/push_back.hpp>
  22. #include <boost/mpl/equal_to.hpp>
  23. #include <boost/mpl/contains.hpp>
  24. #include <boost/mpl/limits/list.hpp>
  25. #include <boost/type_traits/is_same.hpp>
  26. namespace boost { namespace spirit { namespace detail
  27. {
  28. template <int size>
  29. struct as_variant_impl;
  30. #if !defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES)
  31. #else
  32. template <>
  33. struct as_variant_impl<0>
  34. {
  35. template <typename Iterator>
  36. struct apply
  37. {
  38. typedef variant<> type;
  39. };
  40. };
  41. #endif
  42. #define BOOST_FUSION_NEXT_ITERATOR(z, n, data) \
  43. typedef typename fusion::result_of::next<BOOST_PP_CAT(I, n)>::type \
  44. BOOST_PP_CAT(I, BOOST_PP_INC(n));
  45. #define BOOST_FUSION_NEXT_CALL_ITERATOR(z, n, data) \
  46. typename gen::BOOST_PP_CAT(I, BOOST_PP_INC(n)) \
  47. BOOST_PP_CAT(i, BOOST_PP_INC(n)) = fusion::next(BOOST_PP_CAT(i, n));
  48. #define BOOST_FUSION_VALUE_OF_ITERATOR(z, n, data) \
  49. typedef typename fusion::result_of::value_of<BOOST_PP_CAT(I, n)>::type \
  50. BOOST_PP_CAT(T, n);
  51. #define BOOST_PP_FILENAME_1 <boost/spirit/home/support/detail/as_variant.hpp>
  52. #if !defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES)
  53. #define BOOST_PP_ITERATION_LIMITS (1, BOOST_MPL_LIMIT_LIST_SIZE)
  54. #else
  55. #define BOOST_PP_ITERATION_LIMITS (1, BOOST_VARIANT_LIMIT_TYPES)
  56. #endif
  57. #include BOOST_PP_ITERATE()
  58. #undef BOOST_FUSION_NEXT_ITERATOR
  59. #undef BOOST_FUSION_NEXT_CALL_ITERATOR
  60. #undef BOOST_FUSION_VALUE_OF_ITERATOR
  61. template <typename Sequence>
  62. struct as_variant
  63. {
  64. // build a variant generator being able to generate a variant holding
  65. // all of the types as given in the typelist
  66. typedef typename
  67. detail::as_variant_impl<fusion::result_of::size<Sequence>::value>
  68. gen;
  69. // use this generator to create the actual variant
  70. typedef typename gen::template apply<
  71. typename fusion::result_of::begin<Sequence>::type
  72. >::type
  73. type;
  74. };
  75. }}}
  76. #endif
  77. #else // defined(BOOST_PP_IS_ITERATING)
  78. ///////////////////////////////////////////////////////////////////////////////
  79. //
  80. // Preprocessor vertical repetition code
  81. //
  82. ///////////////////////////////////////////////////////////////////////////////
  83. #define N BOOST_PP_ITERATION()
  84. template <>
  85. struct as_variant_impl<N>
  86. {
  87. template <typename I0>
  88. struct apply
  89. {
  90. BOOST_PP_REPEAT(N, BOOST_FUSION_NEXT_ITERATOR, _)
  91. BOOST_PP_REPEAT(N, BOOST_FUSION_VALUE_OF_ITERATOR, _)
  92. typedef variant<BOOST_PP_ENUM_PARAMS(N, T)> type;
  93. };
  94. };
  95. #undef N
  96. #endif // defined(BOOST_PP_IS_ITERATING)