]> git.lyx.org Git - lyx.git/blob - boost/boost/mpl/if.hpp
Boost 1.31.0
[lyx.git] / boost / boost / mpl / if.hpp
1
2 #ifndef BOOST_MPL_IF_HPP_INCLUDED
3 #define BOOST_MPL_IF_HPP_INCLUDED
4
5 // + file: boost/mpl/if.hpp
6 // + last modified: 17/sep/03
7
8 // Copyright (c) 2000-03 Boost.org
9 //
10 // Permission to use, copy, modify, distribute and sell this software
11 // and its documentation for any purpose is hereby granted without fee, 
12 // provided that the above copyright notice appears in all copies and 
13 // that both the copyright notice and this permission notice appear in 
14 // supporting documentation. No representations are made about the 
15 // suitability of this software for any purpose. It is provided "as is" 
16 // without express or implied warranty.
17 //
18 // See http://www.boost.org/libs/mpl for documentation.
19
20 #include "boost/mpl/void.hpp"
21 #include "boost/mpl/aux_/value_wknd.hpp"
22 #include "boost/mpl/aux_/static_cast.hpp"
23 #include "boost/mpl/aux_/void_spec.hpp"
24 #include "boost/mpl/aux_/lambda_support.hpp"
25 #include "boost/mpl/aux_/config/workaround.hpp"
26 #include "boost/config.hpp"
27
28 #if !defined(BOOST_MPL_NO_FULL_LAMBDA_SUPPORT)
29 #   include "boost/mpl/arg_fwd.hpp"
30 #endif
31
32 namespace boost {
33 namespace mpl {
34
35 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
36
37 BOOST_MPL_AUX_AGLORITHM_NAMESPACE_BEGIN
38
39 template<
40       bool C
41     , typename T1
42     , typename T2
43     >
44 struct if_c
45 {
46     typedef T1 type;
47 };
48
49 template<
50       typename T1
51     , typename T2
52     >
53 struct if_c<false,T1,T2>
54 {
55     typedef T2 type;
56 };
57
58 template<
59       typename BOOST_MPL_AUX_VOID_SPEC_PARAM(C)
60     , typename BOOST_MPL_AUX_VOID_SPEC_PARAM(T1)
61     , typename BOOST_MPL_AUX_VOID_SPEC_PARAM(T2)
62     >
63 struct if_
64 {
65  private:
66     // agurt, 02/jan/03: two-step 'type' definition for the sake of aCC 
67     typedef if_c<
68 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x561))
69           BOOST_MPL_AUX_VALUE_WKND(C)::value
70 #else
71           BOOST_MPL_AUX_STATIC_CAST(bool, BOOST_MPL_AUX_VALUE_WKND(C)::value)
72 #endif
73         , T1
74         , T2
75         > almost_type_;
76  
77  public:
78     typedef typename almost_type_::type type;
79     
80     BOOST_MPL_AUX_LAMBDA_SUPPORT(3,if_,(C,T1,T2))
81 };
82
83 #else
84
85 // no partial class template specialization
86
87 namespace aux {
88
89 template< bool C >
90 struct if_impl
91 {
92     template< typename T1, typename T2 > struct result_
93     {
94         typedef T1 type;
95     };
96 };
97
98 template<>
99 struct if_impl<false>
100 {
101     template< typename T1, typename T2 > struct result_
102     { 
103         typedef T2 type;
104     };
105 };
106
107 } // namespace aux
108
109 template<
110       bool C_
111     , typename T1
112     , typename T2
113     >
114 struct if_c
115 {
116     typedef typename aux::if_impl< C_ >
117         ::template result_<T1,T2>::type type;
118 };
119
120 // (almost) copy & paste in order to save one more 
121 // recursively nested template instantiation to user
122 template<
123       typename BOOST_MPL_AUX_VOID_SPEC_PARAM(C_)
124     , typename BOOST_MPL_AUX_VOID_SPEC_PARAM(T1)
125     , typename BOOST_MPL_AUX_VOID_SPEC_PARAM(T2)
126     >
127 struct if_
128 {
129     enum { msvc70_wknd_ = C_::value };
130
131     typedef typename aux::if_impl< BOOST_MPL_AUX_STATIC_CAST(bool, msvc70_wknd_) >
132         ::template result_<T1,T2>::type type;
133
134     BOOST_MPL_AUX_LAMBDA_SUPPORT(3,if_,(C_,T1,T2))
135 };
136
137 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
138
139 BOOST_MPL_AUX_AGLORITHM_NAMESPACE_END
140
141 BOOST_MPL_AUX_ALGORITHM_VOID_SPEC(3, if_)
142
143
144 #if !defined(BOOST_MPL_NO_FULL_LAMBDA_SUPPORT)
145
146 // Aleksey, check it out: lazy if_ evaluation in lambdas!
147 // I think this doesn't handle the case of
148 //
149 //    _1<foo<_2>, bar<_2>, baz<_2> >
150 //
151 // (or however it is that you express that... when the ordinary bind3
152 // computes the function based on the actual arguments). That leads me
153 // to think that some kind of true currying might be a better
154 // approach, e.g.:
155 //
156 //
157 //  boost::mpl::bind3<
158 //         boost::mpl::quote3<boost::mpl::if_>
159 //       , boost::mpl::bind1<boost::mpl::quote1<boost::is_reference>, boost::mpl::arg<1> >
160 //       , boost::mpl::arg<1>
161 //       , boost::mpl::bind1<boost::mpl::quote1<add_ptr>, boost::mpl::arg<1> > 
162 //  >::apply<...>
163 //
164 // becomes:
165 //
166 //  boost::mpl::bind<
167 //         boost::mpl::quote3<boost::mpl::if_>
168 //  >::bind<
169 //       , boost::mpl::bind1<boost::mpl::quote1<boost::is_reference>,
170 //         boost::mpl::arg<1> >
171 //  >::bind<
172 //         boost::mpl::arg<1>
173 //  >::bind<
174 //       boost::mpl::bind1<boost::mpl::quote1<add_ptr>, boost::mpl::arg<1> > 
175 //  >::apply<...>
176 //
177 // so that after the 2nd bind we have a different function depending
178 // on the result of is_reference.
179
180 template <class T1, class T2, class T3, class T4> struct bind3;
181 template <template <class T1, class T2, class T3> class F, class tag> struct quote3;
182
183 namespace aux
184 {
185   template <
186       typename T
187     , BOOST_MPL_PP_PARAMS(BOOST_MPL_METAFUNCTION_MAX_ARITY, typename U)
188   > struct resolve_bind_arg;
189
190   template<
191         typename T
192       , typename Arg
193       >
194   struct replace_unnamed_arg;
195 }
196
197 template<
198       typename T1, typename T2, typename T3
199     >
200 struct bind3<quote3<if_, void_>, T1, T2, T3>
201 {
202     template<
203           typename U1 = void_, typename U2 = void_, typename U3 = void_
204         , typename U4 = void_, typename U5 = void_
205         >
206     struct apply
207     {
208      private:
209         typedef quote3<if_, void_> a0;
210         typedef mpl::arg< 1> n1;
211         
212         typedef aux::replace_unnamed_arg< T1,n1 > r1;
213         typedef typename r1::type a1;
214         typedef typename r1::next_arg n2;
215         typedef typename aux::resolve_bind_arg< a1,U1,U2,U3,U4,U5 >::type t1;
216
217         typedef aux::replace_unnamed_arg< T2,n2 > r2;
218         typedef typename r2::type a2;
219         typedef typename r2::next_arg n3;
220         typedef typename aux::resolve_bind_arg< a2,U1,U2,U3,U4,U5 > f2;
221
222         typedef aux::replace_unnamed_arg< T3,n3 > r3;
223         typedef typename r3::type a3;
224         typedef typename r3::next_arg n4;
225         typedef typename aux::resolve_bind_arg< a3,U1,U2,U3,U4,U5 > f3;
226
227         typedef typename if_<t1,f2,f3>::type f_;
228      public:
229         typedef typename f_::type type;
230     };
231 };
232 #endif
233
234 } // namespace mpl
235 } // namespace boost
236
237 #endif // BOOST_MPL_IF_HPP_INCLUDED