1 ///////////////////////////////////////////////////////////////////////////////
3 /// Contains definition of the argN transforms.
5 // Copyright 2008 Eric Niebler. Distributed under the Boost
6 // Software License, Version 1.0. (See accompanying file
7 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_PROTO_TRANSFORM_ARG_HPP_EAN_11_01_2007
10 #define BOOST_PROTO_TRANSFORM_ARG_HPP_EAN_11_01_2007
12 #include <boost/mpl/if.hpp>
13 #include <boost/proto/proto_fwd.hpp>
14 #include <boost/proto/traits.hpp>
15 #include <boost/proto/transform/impl.hpp>
16 #include <boost/type_traits/is_array.hpp>
17 #include <boost/proto/transform/env.hpp>
19 namespace boost { namespace proto
22 /// \brief A PrimitiveTransform that returns the current expression
28 /// proto::terminal<int>::type i = {42};
29 /// proto::terminal<int>::type & j = proto::_expr()(i);
30 /// assert( boost::addressof(i) == boost::addressof(j) );
32 struct _expr : transform<_expr>
34 template<typename Expr, typename State, typename Data>
35 struct impl : transform_impl<Expr, State, Data>
37 typedef Expr result_type;
39 /// Returns the current expression.
40 /// \param e The current expression.
43 BOOST_PROTO_RETURN_TYPE_STRICT_LOOSE(result_type, typename impl::expr_param)
45 typename impl::expr_param e
46 , typename impl::state_param
47 , typename impl::data_param
55 /// \brief A PrimitiveTransform that returns the current state
61 /// proto::terminal<int>::type i = {42};
62 /// char ch = proto::_state()(i, 'a');
63 /// assert( ch == 'a' );
65 struct _state : transform<_state>
67 template<typename Expr, typename State, typename Data>
68 struct impl : transform_impl<Expr, State, Data>
70 typedef State result_type;
72 /// Returns the current state.
73 /// \param s The current state.
76 BOOST_PROTO_RETURN_TYPE_STRICT_LOOSE(result_type, typename impl::state_param)
78 typename impl::expr_param
79 , typename impl::state_param s
80 , typename impl::data_param
88 /// \brief A PrimitiveTransform that returns the current data
94 /// proto::terminal<int>::type i = {42};
95 /// std::string str("hello");
96 /// std::string & data = proto::_data()(i, 'a', str);
97 /// assert( &str == &data );
99 struct _data : transform<_data>
101 template<typename Expr, typename State, typename Data>
105 , _env_var<data_type>
107 >::type::template impl<Expr, State, Data>
111 /// \brief A PrimitiveTransform that returns N-th child of the current
117 /// proto::terminal<int>::type i = {42};
118 /// proto::terminal<int>::type & j = proto::_child_c<0>()(-i);
119 /// assert( boost::addressof(i) == boost::addressof(j) );
122 struct _child_c : transform<_child_c<N> >
124 template<typename Expr, typename State, typename Data>
125 struct impl : transform_impl<Expr, State, Data>
128 typename result_of::child_c<Expr, N>::type
131 /// Returns the N-th child of \c e
132 /// \pre <tt>arity_of\<Expr\>::value \> N</tt>
133 /// \param e The current expression.
134 /// \return <tt>proto::child_c\<N\>(e)</tt>
136 #ifdef BOOST_PROTO_STRICT_RESULT_OF
139 typename result_of::child_c<typename impl::expr_param, N>::type
142 typename impl::expr_param e
143 , typename impl::state_param
144 , typename impl::data_param
147 return proto::child_c<N>(e);
152 /// \brief A PrimitiveTransform that returns the value of the
153 /// current terminal expression.
158 /// proto::terminal<int>::type i = {42};
159 /// int j = proto::_value()(i);
160 /// assert( 42 == j );
162 struct _value : transform<_value>
164 template<typename Expr, typename State, typename Data>
165 struct impl : transform_impl<Expr, State, Data>
168 typename result_of::value<Expr>::type
171 /// Returns the value of the specified terminal expression.
172 /// \pre <tt>arity_of\<Expr\>::value == 0</tt>.
173 /// \param e The current expression.
174 /// \return <tt>proto::value(e)</tt>
176 #ifdef BOOST_PROTO_STRICT_RESULT_OF
177 typename mpl::if_c<is_array<result_type>::value, result_type &, result_type>::type
179 typename result_of::value<typename impl::expr_param>::type
182 typename impl::expr_param e
183 , typename impl::state_param
184 , typename impl::data_param
187 return proto::value(e);
192 /// \brief A PrimitiveTransform that does nothing
193 /// and returns void.
194 struct _void : transform<_void>
196 template<typename Expr, typename State, typename Data>
197 struct impl : transform_impl<Expr, State, Data>
199 typedef void result_type;
201 /// Does nothing and returns void
203 typename impl::expr_param
204 , typename impl::state_param
205 , typename impl::data_param
211 /// \brief A unary CallableTransform that wraps its argument
212 /// in a \c boost::reference_wrapper\<\>.
217 /// proto::terminal<int>::type i = {42};
218 /// boost::reference_wrapper<proto::terminal<int>::type> j
219 /// = proto::when<_, proto::_byref(_)>()(i);
220 /// assert( boost::addressof(i) == boost::addressof(j.get()) );
222 struct _byref : callable
224 template<typename Sig>
227 template<typename This, typename T>
228 struct result<This(T)>
230 typedef boost::reference_wrapper<T const> const type;
233 template<typename This, typename T>
234 struct result<This(T &)>
236 typedef boost::reference_wrapper<T> const type;
239 /// Wrap the parameter \c t in a \c boost::reference_wrapper\<\>
240 /// \param t The object to wrap
241 /// \return <tt>boost::ref(t)</tt>
244 boost::reference_wrapper<T> const operator ()(T &t) const
246 return boost::reference_wrapper<T>(t);
252 boost::reference_wrapper<T const> const operator ()(T const &t) const
254 return boost::reference_wrapper<T const>(t);
258 /// \brief A unary CallableTransform that strips references
259 /// and \c boost::reference_wrapper\<\> from its argument.
264 /// proto::terminal<int>::type i = {42};
266 /// int k = proto::when<_, proto::_byval(proto::_state)>()(i, boost::ref(j));
267 /// assert( 67 == k );
269 struct _byval : callable
271 template<typename Sig>
274 template<typename This, typename T>
275 struct result<This(T)>
280 template<typename This, typename T>
281 struct result<This(T &)>
285 template<typename This, typename T>
286 struct result<This(boost::reference_wrapper<T>)>
290 /// \param t The object to unref
291 /// \return <tt>t</tt>
294 T operator ()(T const &t) const
302 T operator ()(boost::reference_wrapper<T> const &t) const
311 struct is_callable<_expr>
318 struct is_callable<_state>
325 struct is_callable<_data>
332 struct is_callable<_child_c<N> >
339 struct is_callable<_value>
346 struct is_callable<_byref>
353 struct is_callable<_byval>