deduce_domain.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. ///////////////////////////////////////////////////////////////////////////////
  2. /// \file deduce_domain.hpp
  3. /// Contains definition of deduce_domain\<\> class templates
  4. /// for finding the domain that is common among the specified
  5. /// domains
  6. //
  7. // Copyright 2010 Daniel Wallin, Eric Niebler. Distributed under the Boost
  8. // Software License, Version 1.0. (See accompanying file
  9. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Many thanks to Daniel Wallin who first implemented this code. Thanks
  12. // also to Jeremiah Willcock, John Bytheway and Krishna Achuthan who
  13. // offered alternate solutions to this tricky programming problem.
  14. #ifndef BOOST_PROTO_DEDUCE_DOMAIN_HPP_EAN_05_22_2010
  15. #define BOOST_PROTO_DEDUCE_DOMAIN_HPP_EAN_05_22_2010
  16. #include <boost/config.hpp>
  17. #include <boost/preprocessor/cat.hpp>
  18. #include <boost/preprocessor/facilities/intercept.hpp>
  19. #include <boost/preprocessor/iteration/local.hpp>
  20. #include <boost/preprocessor/iteration/iterate.hpp>
  21. #include <boost/preprocessor/repetition/enum_params.hpp>
  22. #include <boost/preprocessor/repetition/enum_binary_params.hpp>
  23. #include <boost/preprocessor/repetition/repeat_from_to.hpp>
  24. #include <boost/preprocessor/arithmetic/inc.hpp>
  25. #include <boost/mpl/assert.hpp>
  26. #include <boost/type_traits/is_same.hpp>
  27. #include <boost/proto/proto_fwd.hpp>
  28. #ifndef BOOST_PROTO_ASSERT_VALID_DOMAIN
  29. # define BOOST_PROTO_ASSERT_VALID_DOMAIN(DOM) BOOST_MPL_ASSERT_NOT((boost::is_same<DOM, boost::proto::detail::not_a_domain>))
  30. #endif
  31. namespace boost
  32. {
  33. namespace proto
  34. {
  35. namespace detail
  36. {
  37. template<typename Domain>
  38. struct domain_
  39. : domain_<typename Domain::proto_super_domain>
  40. {
  41. typedef Domain type;
  42. typedef domain_<typename Domain::proto_super_domain> base;
  43. #ifdef BOOST_NO_CXX11_DECLTYPE
  44. using base::deduce98;
  45. static int const index = base::index + 1;
  46. static typename sized_type<index>::type deduce98(domain_<Domain>*);
  47. #else
  48. using base::deduce0x;
  49. static Domain deduce0x(domain_<Domain>*);
  50. #endif
  51. };
  52. template<>
  53. struct domain_<not_a_domain>
  54. {
  55. typedef not_a_domain type;
  56. #ifdef BOOST_NO_CXX11_DECLTYPE
  57. static int const index = 1;
  58. static sized_type<1>::type deduce98(void*);
  59. #else
  60. static not_a_domain deduce0x(void*);
  61. #endif
  62. };
  63. template<>
  64. struct domain_<default_domain>
  65. : domain_<not_a_domain>
  66. {};
  67. template<>
  68. struct domain_<basic_default_domain>
  69. : domain_<not_a_domain>
  70. {};
  71. sized_type<1>::type default_test(void*, void*);
  72. sized_type<2>::type default_test(domain_<default_domain>*, void*);
  73. sized_type<2>::type default_test(domain_<basic_default_domain>*, void*);
  74. sized_type<3>::type default_test(void*, domain_<default_domain>*);
  75. sized_type<3>::type default_test(void*, domain_<basic_default_domain>*);
  76. sized_type<4>::type default_test(domain_<default_domain>*, domain_<default_domain>*);
  77. sized_type<4>::type default_test(domain_<basic_default_domain>*, domain_<default_domain>*);
  78. sized_type<4>::type default_test(domain_<default_domain>*, domain_<basic_default_domain>*);
  79. sized_type<4>::type default_test(domain_<basic_default_domain>*, domain_<basic_default_domain>*);
  80. #ifdef BOOST_NO_CXX11_DECLTYPE
  81. template<int N, typename Domain>
  82. struct nth_domain
  83. : nth_domain<N - 1, typename Domain::base>
  84. {};
  85. template<typename Domain>
  86. struct nth_domain<0, Domain>
  87. : Domain
  88. {};
  89. #endif
  90. template<typename D0>
  91. struct common_domain1
  92. {
  93. typedef D0 type;
  94. };
  95. template<typename E0>
  96. struct deduce_domain1
  97. : domain_of<E0>
  98. {};
  99. template<
  100. typename D0
  101. , typename D1
  102. , int DefaultCase = sizeof(proto::detail::default_test((domain_<D0>*)0, (domain_<D1>*)0))
  103. >
  104. struct common_domain2
  105. {
  106. #ifdef BOOST_NO_CXX11_DECLTYPE
  107. static int const index = domain_<D0>::index - sizeof(domain_<D0>::deduce98((domain_<D1>*)0));
  108. typedef typename nth_domain<index, domain_<D0> >::type type;
  109. #else
  110. typedef decltype(domain_<D0>::deduce0x((domain_<D1>*)0)) type;
  111. #endif
  112. };
  113. template<typename D0, typename D1>
  114. struct common_domain2<D0, D1, 2>
  115. {
  116. typedef D1 type;
  117. };
  118. template<typename D0, typename D1>
  119. struct common_domain2<D0, D1, 3>
  120. {
  121. typedef D0 type;
  122. };
  123. template<typename D0>
  124. struct common_domain2<D0, default_domain, 4>
  125. {
  126. typedef D0 type;
  127. };
  128. template<typename D0>
  129. struct common_domain2<D0, basic_default_domain, 4>
  130. {
  131. typedef D0 type;
  132. };
  133. template<typename D1>
  134. struct common_domain2<default_domain, D1, 4>
  135. {
  136. typedef D1 type;
  137. };
  138. template<typename D1>
  139. struct common_domain2<basic_default_domain, D1, 4>
  140. {
  141. typedef D1 type;
  142. };
  143. template<>
  144. struct common_domain2<default_domain, default_domain, 4>
  145. {
  146. typedef default_domain type;
  147. };
  148. template<>
  149. struct common_domain2<basic_default_domain, default_domain, 4>
  150. {
  151. typedef default_domain type;
  152. };
  153. template<>
  154. struct common_domain2<default_domain, basic_default_domain, 4>
  155. {
  156. typedef default_domain type;
  157. };
  158. template<>
  159. struct common_domain2<basic_default_domain, basic_default_domain, 4>
  160. {
  161. typedef basic_default_domain type;
  162. };
  163. template<typename E0, typename E1>
  164. struct deduce_domain2
  165. : common_domain2<
  166. typename domain_of<E0>::type
  167. , typename domain_of<E1>::type
  168. >
  169. {};
  170. #include <boost/proto/detail/deduce_domain_n.hpp>
  171. }
  172. }
  173. }
  174. #endif // BOOST_PROTO_DEDUCE_DOMAIN_HPP_EAN_05_22_2010