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