inherit.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #if !defined(BOOST_PP_IS_ITERATING)
  2. ///// header body
  3. #ifndef BOOST_MPL_INHERIT_HPP_INCLUDED
  4. #define BOOST_MPL_INHERIT_HPP_INCLUDED
  5. // Copyright Aleksey Gurtovoy 2001-2004
  6. //
  7. // Distributed under the Boost Software License, Version 1.0.
  8. // (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // See http://www.boost.org/libs/mpl for documentation.
  12. // $Id$
  13. // $Date$
  14. // $Revision$
  15. #if !defined(BOOST_MPL_PREPROCESSING_MODE)
  16. # include <boost/mpl/empty_base.hpp>
  17. # include <boost/mpl/aux_/na_spec.hpp>
  18. # include <boost/mpl/aux_/lambda_support.hpp>
  19. #endif
  20. #include <boost/mpl/aux_/config/use_preprocessed.hpp>
  21. #if !defined(BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS) \
  22. && !defined(BOOST_MPL_PREPROCESSING_MODE)
  23. # define BOOST_MPL_PREPROCESSED_HEADER inherit.hpp
  24. # include <boost/mpl/aux_/include_preprocessed.hpp>
  25. #else
  26. # include <boost/mpl/limits/arity.hpp>
  27. # include <boost/mpl/aux_/preprocessor/params.hpp>
  28. # include <boost/mpl/aux_/preprocessor/default_params.hpp>
  29. # include <boost/mpl/aux_/preprocessor/enum.hpp>
  30. # include <boost/mpl/aux_/config/ctps.hpp>
  31. # include <boost/mpl/aux_/config/dtp.hpp>
  32. # include <boost/preprocessor/iterate.hpp>
  33. # include <boost/preprocessor/dec.hpp>
  34. # include <boost/preprocessor/cat.hpp>
  35. namespace boost { namespace mpl {
  36. // 'inherit<T1,T2,..,Tn>' metafunction; returns an unspecified class type
  37. // produced by public derivation from all metafunction's parameters
  38. // (T1,T2,..,Tn), except the parameters of 'empty_base' class type;
  39. // regardless the position and number of 'empty_base' parameters in the
  40. // metafunction's argument list, derivation from them is always a no-op;
  41. // for instance:
  42. // inherit<her>::type == her
  43. // inherit<her,my>::type == struct unspecified : her, my {};
  44. // inherit<empty_base,her>::type == her
  45. // inherit<empty_base,her,empty_base,empty_base>::type == her
  46. // inherit<her,empty_base,my>::type == struct unspecified : her, my {};
  47. // inherit<empty_base,empty_base>::type == empty_base
  48. #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  49. template<
  50. typename BOOST_MPL_AUX_NA_PARAM(T1)
  51. , typename BOOST_MPL_AUX_NA_PARAM(T2)
  52. >
  53. struct inherit2
  54. : T1, T2
  55. {
  56. typedef inherit2 type;
  57. BOOST_MPL_AUX_LAMBDA_SUPPORT(2, inherit2, (T1,T2))
  58. };
  59. template< typename T1 >
  60. struct inherit2<T1,empty_base>
  61. {
  62. typedef T1 type;
  63. BOOST_MPL_AUX_LAMBDA_SUPPORT_SPEC(2, inherit2, (T1,empty_base))
  64. };
  65. template< typename T2 >
  66. struct inherit2<empty_base,T2>
  67. {
  68. typedef T2 type;
  69. BOOST_MPL_AUX_LAMBDA_SUPPORT_SPEC(2, inherit2, (empty_base,T2))
  70. };
  71. // needed to disambiguate the previous two in case when both
  72. // T1 and T2 == empty_base
  73. template<>
  74. struct inherit2<empty_base,empty_base>
  75. {
  76. typedef empty_base type;
  77. BOOST_MPL_AUX_LAMBDA_SUPPORT_SPEC(2, inherit2, (empty_base,empty_base))
  78. };
  79. #else
  80. namespace aux {
  81. template< bool C1, bool C2 >
  82. struct inherit2_impl
  83. {
  84. template< typename Derived, typename T1, typename T2 > struct result_
  85. : T1, T2
  86. {
  87. typedef Derived type_;
  88. };
  89. };
  90. template<>
  91. struct inherit2_impl<false,true>
  92. {
  93. template< typename Derived, typename T1, typename T2 > struct result_
  94. : T1
  95. {
  96. typedef T1 type_;
  97. };
  98. };
  99. template<>
  100. struct inherit2_impl<true,false>
  101. {
  102. template< typename Derived, typename T1, typename T2 > struct result_
  103. : T2
  104. {
  105. typedef T2 type_;
  106. };
  107. };
  108. template<>
  109. struct inherit2_impl<true,true>
  110. {
  111. template< typename Derived, typename T1, typename T2 > struct result_
  112. {
  113. typedef T1 type_;
  114. };
  115. };
  116. } // namespace aux
  117. template<
  118. typename BOOST_MPL_AUX_NA_PARAM(T1)
  119. , typename BOOST_MPL_AUX_NA_PARAM(T2)
  120. >
  121. struct inherit2
  122. : aux::inherit2_impl<
  123. is_empty_base<T1>::value
  124. , is_empty_base<T2>::value
  125. >::template result_< inherit2<T1,T2>,T1,T2 >
  126. {
  127. typedef typename inherit2::type_ type;
  128. BOOST_MPL_AUX_LAMBDA_SUPPORT(2, inherit2, (T1,T2))
  129. };
  130. #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  131. BOOST_MPL_AUX_NA_SPEC(2, inherit2)
  132. #define BOOST_PP_ITERATION_PARAMS_1 \
  133. (3,(3, BOOST_MPL_LIMIT_METAFUNCTION_ARITY, <boost/mpl/inherit.hpp>))
  134. #include BOOST_PP_ITERATE()
  135. }}
  136. #endif // BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS
  137. #endif // BOOST_MPL_INHERIT_HPP_INCLUDED
  138. ///// iteration
  139. #else
  140. #define n_ BOOST_PP_FRAME_ITERATION(1)
  141. template<
  142. BOOST_MPL_PP_DEFAULT_PARAMS(n_, typename T, na)
  143. >
  144. struct BOOST_PP_CAT(inherit,n_)
  145. : inherit2<
  146. typename BOOST_PP_CAT(inherit,BOOST_PP_DEC(n_))<
  147. BOOST_MPL_PP_PARAMS(BOOST_PP_DEC(n_), T)
  148. >::type
  149. , BOOST_PP_CAT(T,n_)
  150. >
  151. {
  152. BOOST_MPL_AUX_LAMBDA_SUPPORT(
  153. n_
  154. , BOOST_PP_CAT(inherit,n_)
  155. , (BOOST_MPL_PP_PARAMS(n_, T))
  156. )
  157. };
  158. BOOST_MPL_AUX_NA_SPEC(n_, BOOST_PP_CAT(inherit,n_))
  159. #if n_ == BOOST_MPL_LIMIT_METAFUNCTION_ARITY
  160. /// primary template
  161. template<
  162. BOOST_MPL_PP_DEFAULT_PARAMS(n_, typename T, empty_base)
  163. >
  164. struct inherit
  165. : BOOST_PP_CAT(inherit,n_)<BOOST_MPL_PP_PARAMS(n_, T)>
  166. {
  167. };
  168. // 'na' specialization
  169. template<>
  170. struct inherit< BOOST_MPL_PP_ENUM(5, na) >
  171. {
  172. template<
  173. #if !defined(BOOST_MPL_CFG_NO_DEFAULT_PARAMETERS_IN_NESTED_TEMPLATES)
  174. BOOST_MPL_PP_DEFAULT_PARAMS(n_, typename T, empty_base)
  175. #else
  176. BOOST_MPL_PP_PARAMS(n_, typename T)
  177. #endif
  178. >
  179. struct apply
  180. : inherit< BOOST_MPL_PP_PARAMS(n_, T) >
  181. {
  182. };
  183. };
  184. BOOST_MPL_AUX_NA_SPEC_LAMBDA(n_, inherit)
  185. BOOST_MPL_AUX_NA_SPEC_ARITY(n_, inherit)
  186. BOOST_MPL_AUX_NA_SPEC_TEMPLATE_ARITY(n_, n_, inherit)
  187. #endif
  188. #undef n_
  189. #endif // BOOST_PP_IS_ITERATING