apply_wrap.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright Aleksey Gurtovoy 2000-2004
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/mpl for documentation.
  8. // $Id$
  9. // $Date$
  10. // $Revision$
  11. #include <boost/mpl/apply_wrap.hpp>
  12. #include <boost/mpl/limits/arity.hpp>
  13. #include <boost/mpl/aux_/preprocessor/params.hpp>
  14. #include <boost/mpl/aux_/preprocessor/enum.hpp>
  15. #include <boost/mpl/aux_/test.hpp>
  16. #include <boost/preprocessor/repeat.hpp>
  17. #include <boost/preprocessor/comma_if.hpp>
  18. #include <boost/preprocessor/dec.hpp>
  19. #include <boost/preprocessor/if.hpp>
  20. #include <boost/preprocessor/cat.hpp>
  21. #if !defined(BOOST_MPL_CFG_NO_DEFAULT_PARAMETERS_IN_NESTED_TEMPLATES)
  22. # define APPLY_0_FUNC_DEF(i) \
  23. struct f0 \
  24. { \
  25. template< typename T = int > struct apply { typedef char type; }; \
  26. }; \
  27. /**/
  28. #else
  29. # define APPLY_0_FUNC_DEF(i) \
  30. struct f0 \
  31. { \
  32. template< typename T > struct apply { typedef char type; }; \
  33. }; \
  34. /**/
  35. #endif
  36. #define APPLY_N_FUNC_DEF(i) \
  37. struct first##i \
  38. { \
  39. template< BOOST_MPL_PP_PARAMS(i, typename U) > \
  40. struct apply { typedef U1 type; }; \
  41. }; \
  42. \
  43. struct last##i \
  44. { \
  45. template< BOOST_MPL_PP_PARAMS(i, typename U) > \
  46. struct apply { typedef BOOST_PP_CAT(U,i) type; }; \
  47. }; \
  48. /**/
  49. #define APPLY_FUNC_DEF(z, i, unused) \
  50. BOOST_PP_IF( \
  51. i \
  52. , APPLY_N_FUNC_DEF \
  53. , APPLY_0_FUNC_DEF \
  54. )(i) \
  55. /**/
  56. namespace { namespace test {
  57. BOOST_PP_REPEAT(
  58. BOOST_MPL_LIMIT_METAFUNCTION_ARITY
  59. , APPLY_FUNC_DEF
  60. , unused
  61. )
  62. struct g0 { struct apply { typedef char type; }; };
  63. }}
  64. #define APPLY_0_TEST(i, apply_) \
  65. typedef apply_<test::f##i>::type t; \
  66. { MPL_ASSERT(( boost::is_same<t, char> )); } \
  67. /**/
  68. #define APPLY_N_TEST(i, apply_) \
  69. typedef apply_< \
  70. test::first##i \
  71. , char \
  72. BOOST_PP_COMMA_IF(BOOST_PP_DEC(i)) \
  73. BOOST_MPL_PP_ENUM(BOOST_PP_DEC(i), int) \
  74. >::type t1##i; \
  75. \
  76. typedef apply_< \
  77. test::last##i \
  78. , BOOST_MPL_PP_ENUM(BOOST_PP_DEC(i), int) \
  79. BOOST_PP_COMMA_IF(BOOST_PP_DEC(i)) char \
  80. >::type t2##i; \
  81. { MPL_ASSERT(( boost::is_same<t1##i, char> )); } \
  82. { MPL_ASSERT(( boost::is_same<t2##i, char> )); } \
  83. /**/
  84. #define APPLY_TEST(z, i, unused) \
  85. BOOST_PP_IF( \
  86. i \
  87. , APPLY_N_TEST \
  88. , APPLY_0_TEST \
  89. )(i, BOOST_PP_CAT(apply_wrap,i)) \
  90. /**/
  91. MPL_TEST_CASE()
  92. {
  93. BOOST_PP_REPEAT(
  94. BOOST_MPL_LIMIT_METAFUNCTION_ARITY
  95. , APPLY_TEST
  96. , unused
  97. )
  98. #if !defined(BOOST_MPL_CFG_NO_HAS_APPLY)
  99. {
  100. typedef apply_wrap0<test::g0>::type t;
  101. MPL_ASSERT(( boost::is_same<t, char> ));
  102. }
  103. #endif
  104. }