named_template_params.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // (C) Copyright Jeremy Siek 2001.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Revision History:
  6. // 04 Oct 2001 David Abrahams
  7. // Changed name of "bind" to "select" to avoid problems with MSVC.
  8. #ifndef BOOST_DETAIL_NAMED_TEMPLATE_PARAMS_HPP
  9. #define BOOST_DETAIL_NAMED_TEMPLATE_PARAMS_HPP
  10. #include <boost/config.hpp>
  11. #include <boost/type_traits/conversion_traits.hpp>
  12. #include <boost/type_traits/composite_traits.hpp> // for is_reference
  13. #if defined(BOOST_BORLANDC)
  14. #include <boost/type_traits/ice.hpp>
  15. #endif
  16. namespace boost {
  17. namespace detail {
  18. struct default_argument { };
  19. struct dummy_default_gen {
  20. template <class Base, class Traits>
  21. struct select {
  22. typedef default_argument type;
  23. };
  24. };
  25. // This class template is a workaround for MSVC.
  26. template <class Gen> struct default_generator {
  27. typedef detail::dummy_default_gen type;
  28. };
  29. template <class T> struct is_default {
  30. enum { value = false };
  31. typedef type_traits::no_type type;
  32. };
  33. template <> struct is_default<default_argument> {
  34. enum { value = true };
  35. typedef type_traits::yes_type type;
  36. };
  37. struct choose_default {
  38. template <class Arg, class DefaultGen, class Base, class Traits>
  39. struct select {
  40. typedef typename default_generator<DefaultGen>::type Gen;
  41. typedef typename Gen::template select<Base,Traits>::type type;
  42. };
  43. };
  44. struct choose_arg {
  45. template <class Arg, class DefaultGen, class Base, class Traits>
  46. struct select {
  47. typedef Arg type;
  48. };
  49. };
  50. #if defined(BOOST_BORLANDC)
  51. template <class UseDefault>
  52. struct choose_arg_or_default { typedef choose_arg type; };
  53. template <>
  54. struct choose_arg_or_default<type_traits::yes_type> {
  55. typedef choose_default type;
  56. };
  57. #else
  58. template <bool UseDefault>
  59. struct choose_arg_or_default { typedef choose_arg type; };
  60. template <>
  61. struct choose_arg_or_default<true> {
  62. typedef choose_default type;
  63. };
  64. #endif
  65. template <class Arg, class DefaultGen, class Base, class Traits>
  66. class resolve_default {
  67. #if defined(BOOST_BORLANDC)
  68. typedef typename choose_arg_or_default<typename is_default<Arg>::type>::type Selector;
  69. #else
  70. // This usually works for Borland, but I'm seeing weird errors in
  71. // iterator_adaptor_test.cpp when using this method.
  72. enum { is_def = is_default<Arg>::value };
  73. typedef typename choose_arg_or_default<is_def>::type Selector;
  74. #endif
  75. public:
  76. typedef typename Selector
  77. ::template select<Arg, DefaultGen, Base, Traits>::type type;
  78. };
  79. // To differentiate an unnamed parameter from a traits generator
  80. // we use is_convertible<X, iter_traits_gen_base>.
  81. struct named_template_param_base { };
  82. template <class X>
  83. struct is_named_param_list {
  84. enum { value = is_convertible<X, named_template_param_base>::value };
  85. };
  86. struct choose_named_params {
  87. template <class Prev> struct select { typedef Prev type; };
  88. };
  89. struct choose_default_arg {
  90. template <class Prev> struct select {
  91. typedef detail::default_argument type;
  92. };
  93. };
  94. template <bool Named> struct choose_default_dispatch_;
  95. template <> struct choose_default_dispatch_<true> {
  96. typedef choose_named_params type;
  97. };
  98. template <> struct choose_default_dispatch_<false> {
  99. typedef choose_default_arg type;
  100. };
  101. // The use of inheritance here is a Solaris Forte 6 workaround.
  102. template <bool Named> struct choose_default_dispatch
  103. : public choose_default_dispatch_<Named> { };
  104. template <class PreviousArg>
  105. struct choose_default_argument {
  106. enum { is_named = is_named_param_list<PreviousArg>::value };
  107. typedef typename choose_default_dispatch<is_named>::type Selector;
  108. typedef typename Selector::template select<PreviousArg>::type type;
  109. };
  110. // This macro assumes that there is a class named default_##TYPE
  111. // defined before the application of the macro. This class should
  112. // have a single member class template named "select" with two
  113. // template parameters: the type of the class being created (e.g.,
  114. // the iterator_adaptor type when creating iterator adaptors) and
  115. // a traits class. The select class should have a single typedef
  116. // named "type" that produces the default for TYPE. See
  117. // boost/iterator_adaptors.hpp for an example usage. Also,
  118. // applications of this macro must be placed in namespace
  119. // boost::detail.
  120. #define BOOST_NAMED_TEMPLATE_PARAM(TYPE) \
  121. struct get_##TYPE##_from_named { \
  122. template <class Base, class NamedParams, class Traits> \
  123. struct select { \
  124. typedef typename NamedParams::traits NamedTraits; \
  125. typedef typename NamedTraits::TYPE TYPE; \
  126. typedef typename resolve_default<TYPE, \
  127. default_##TYPE, Base, NamedTraits>::type type; \
  128. }; \
  129. }; \
  130. struct pass_thru_##TYPE { \
  131. template <class Base, class Arg, class Traits> struct select { \
  132. typedef typename resolve_default<Arg, \
  133. default_##TYPE, Base, Traits>::type type; \
  134. };\
  135. }; \
  136. template <int NamedParam> \
  137. struct get_##TYPE##_dispatch { }; \
  138. template <> struct get_##TYPE##_dispatch<1> { \
  139. typedef get_##TYPE##_from_named type; \
  140. }; \
  141. template <> struct get_##TYPE##_dispatch<0> { \
  142. typedef pass_thru_##TYPE type; \
  143. }; \
  144. template <class Base, class X, class Traits> \
  145. class get_##TYPE { \
  146. enum { is_named = is_named_param_list<X>::value }; \
  147. typedef typename get_##TYPE##_dispatch<is_named>::type Selector; \
  148. public: \
  149. typedef typename Selector::template select<Base, X, Traits>::type type; \
  150. }; \
  151. template <> struct default_generator<default_##TYPE> { \
  152. typedef default_##TYPE type; \
  153. }
  154. } // namespace detail
  155. } // namespace boost
  156. #endif // BOOST_DETAIL_NAMED_TEMPLATE_PARAMS_HPP