PageRenderTime 61ms CodeModel.GetById 25ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 1ms

/Src/Dependencies/Boost/boost/proto/transform/arg.hpp

http://hadesmem.googlecode.com/
C++ Header | 380 lines | 215 code | 35 blank | 130 comment | 0 complexity | 80d04965681fc829cdd6391c3ad60efa MD5 | raw file
  1///////////////////////////////////////////////////////////////////////////////
  2/// \file arg.hpp
  3/// Contains definition of the argN transforms.
  4//
  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)
  8
  9#ifndef BOOST_PROTO_TRANSFORM_ARG_HPP_EAN_11_01_2007
 10#define BOOST_PROTO_TRANSFORM_ARG_HPP_EAN_11_01_2007
 11
 12#include <boost/proto/proto_fwd.hpp>
 13#include <boost/proto/traits.hpp>
 14#include <boost/proto/transform/impl.hpp>
 15#include <boost/type_traits/is_array.hpp>
 16
 17namespace boost { namespace proto
 18{
 19
 20    /// \brief A PrimitiveTransform that returns the current expression
 21    /// unmodified
 22    ///
 23    /// Example:
 24    ///
 25    /// \code
 26    /// proto::terminal<int>::type i = {42};
 27    /// proto::terminal<int>::type & j = proto::_expr()(i);
 28    /// assert( boost::addressof(i) == boost::addressof(j) );
 29    /// \endcode
 30    struct _expr : transform<_expr>
 31    {
 32        template<typename Expr, typename State, typename Data>
 33        struct impl : transform_impl<Expr, State, Data>
 34        {
 35            typedef Expr result_type;
 36
 37            /// Returns the current expression.
 38            /// \param e The current expression.
 39            /// \return \c e
 40            /// \throw nothrow
 41            #ifdef BOOST_PROTO_STRICT_RESULT_OF
 42            result_type
 43            #else
 44            typename impl::expr_param 
 45            #endif
 46            operator()(
 47                typename impl::expr_param e
 48              , typename impl::state_param
 49              , typename impl::data_param
 50            ) const
 51            {
 52                return e;
 53            }
 54        };
 55    };
 56
 57    /// \brief A PrimitiveTransform that returns the current state
 58    /// unmodified
 59    ///
 60    /// Example:
 61    ///
 62    /// \code
 63    /// proto::terminal<int>::type i = {42};
 64    /// char ch = proto::_state()(i, 'a');
 65    /// assert( ch == 'a' );
 66    /// \endcode
 67    struct _state : transform<_state>
 68    {
 69        template<typename Expr, typename State, typename Data>
 70        struct impl : transform_impl<Expr, State, Data>
 71        {
 72            typedef State result_type;
 73
 74            /// Returns the current state.
 75            /// \param s The current state.
 76            /// \return \c s
 77            /// \throw nothrow
 78            #ifdef BOOST_PROTO_STRICT_RESULT_OF
 79            result_type
 80            #else
 81            typename impl::state_param 
 82            #endif
 83            operator ()(
 84                typename impl::expr_param
 85              , typename impl::state_param s
 86              , typename impl::data_param
 87            ) const
 88            {
 89                return s;
 90            }
 91        };
 92    };
 93
 94    /// \brief A PrimitiveTransform that returns the current data
 95    /// unmodified
 96    ///
 97    /// Example:
 98    ///
 99    /// \code
100    /// proto::terminal<int>::type i = {42};
101    /// std::string str("hello");
102    /// std::string & data = proto::_data()(i, 'a', str);
103    /// assert( &str == &data );
104    /// \endcode
105    struct _data : transform<_data>
106    {
107        template<typename Expr, typename State, typename Data>
108        struct impl : transform_impl<Expr, State, Data>
109        {
110            typedef Data result_type;
111
112            /// Returns the current data.
113            /// \param d The current data.
114            /// \return \c d
115            /// \throw nothrow
116            #ifdef BOOST_PROTO_STRICT_RESULT_OF
117            result_type
118            #else
119            typename impl::data_param 
120            #endif
121            operator ()(
122                typename impl::expr_param
123              , typename impl::state_param
124              , typename impl::data_param d
125            ) const
126            {
127                return d;
128            }
129        };
130    };
131
132    /// \brief A PrimitiveTransform that returns N-th child of the current
133    /// expression.
134    ///
135    /// Example:
136    ///
137    /// \code
138    /// proto::terminal<int>::type i = {42};
139    /// proto::terminal<int>::type & j = proto::_child_c<0>()(-i);
140    /// assert( boost::addressof(i) == boost::addressof(j) );
141    /// \endcode
142    template<int N>
143    struct _child_c : transform<_child_c<N> >
144    {
145        template<typename Expr, typename State, typename Data>
146        struct impl : transform_impl<Expr, State, Data>
147        {
148            typedef
149                typename result_of::child_c<Expr, N>::type
150            result_type;
151
152            /// Returns the N-th child of \c e
153            /// \pre <tt>arity_of\<Expr\>::value \> N</tt> 
154            /// \param e The current expression.
155            /// \return <tt>proto::child_c\<N\>(e)</tt>
156            /// \throw nothrow
157            #ifdef BOOST_PROTO_STRICT_RESULT_OF
158            result_type
159            #else
160            typename result_of::child_c<typename impl::expr_param, N>::type
161            #endif
162            operator ()(
163                typename impl::expr_param e
164              , typename impl::state_param
165              , typename impl::data_param
166            ) const
167            {
168                return proto::child_c<N>(e);
169            }
170        };
171    };
172
173    /// \brief A PrimitiveTransform that returns the value of the
174    /// current terminal expression.
175    ///
176    /// Example:
177    ///
178    /// \code
179    /// proto::terminal<int>::type i = {42};
180    /// int j = proto::_value()(i);
181    /// assert( 42 == j );
182    /// \endcode
183    struct _value : transform<_value>
184    {
185        template<typename Expr, typename State, typename Data>
186        struct impl : transform_impl<Expr, State, Data>
187        {
188            typedef
189                typename result_of::value<Expr>::type
190            result_type;
191
192            /// Returns the value of the specified terminal expression.
193            /// \pre <tt>arity_of\<Expr\>::value == 0</tt>.
194            /// \param e The current expression.
195            /// \return <tt>proto::value(e)</tt>
196            /// \throw nothrow
197            #ifdef BOOST_PROTO_STRICT_RESULT_OF
198            typename mpl::if_c<is_array<result_type>::value, result_type &, result_type>::type
199            #else
200            typename result_of::value<typename impl::expr_param>::type
201            #endif
202            operator ()(
203                typename impl::expr_param e
204              , typename impl::state_param
205              , typename impl::data_param
206            ) const
207            {
208                return proto::value(e);
209            }
210        };
211    };
212
213    /// \brief A PrimitiveTransform that does nothing
214    /// and returns void.
215    struct _void : transform<_void>
216    {
217        template<typename Expr, typename State, typename Data>
218        struct impl : transform_impl<Expr, State, Data>
219        {
220            typedef void result_type;
221
222            /// Does nothing and returns void
223            void operator ()(
224                typename impl::expr_param
225              , typename impl::state_param
226              , typename impl::data_param
227            ) const
228            {}
229        };
230    };
231
232    /// \brief A unary CallableTransform that wraps its argument
233    /// in a \c boost::reference_wrapper\<\>.
234    ///
235    /// Example:
236    ///
237    /// \code
238    /// proto::terminal<int>::type i = {42};
239    /// boost::reference_wrapper<proto::terminal<int>::type> j
240    ///     = proto::when<_, proto::_byref(_)>()(i);
241    /// assert( boost::addressof(i) == boost::addressof(j.get()) );
242    /// \endcode
243    struct _byref : callable
244    {
245        template<typename Sig>
246        struct result;
247
248        template<typename This, typename T>
249        struct result<This(T)>
250        {
251            typedef boost::reference_wrapper<T const> const type;
252        };
253
254        template<typename This, typename T>
255        struct result<This(T &)>
256        {
257            typedef boost::reference_wrapper<T> const type;
258        };
259
260        /// Wrap the parameter \c t in a \c boost::reference_wrapper\<\>
261        /// \param t The object to wrap
262        /// \return <tt>boost::ref(t)</tt>
263        /// \throw nothrow
264        template<typename T>
265        boost::reference_wrapper<T> const operator ()(T &t) const
266        {
267            return boost::reference_wrapper<T>(t);
268        }
269
270        /// \overload
271        ///
272        template<typename T>
273        boost::reference_wrapper<T const> const operator ()(T const &t) const
274        {
275            return boost::reference_wrapper<T const>(t);
276        }
277    };
278
279    /// \brief A unary CallableTransform that strips references
280    /// and \c boost::reference_wrapper\<\> from its argument.
281    ///
282    /// Example:
283    ///
284    /// \code
285    /// proto::terminal<int>::type i = {42};
286    /// int j = 67;
287    /// int k = proto::when<_, proto::_byval(proto::_state)>()(i, boost::ref(j));
288    /// assert( 67 == k );
289    /// \endcode
290    struct _byval : callable
291    {
292        template<typename Sig>
293        struct result;
294
295        template<typename This, typename T>
296        struct result<This(T)>
297        {
298            typedef T type;
299        };
300
301        template<typename This, typename T>
302        struct result<This(T &)>
303          : result<This(T)>
304        {};
305
306        template<typename This, typename T>
307        struct result<This(boost::reference_wrapper<T>)>
308          : result<This(T)>
309        {};
310
311        /// \param t The object to unref
312        /// \return <tt>t</tt>
313        /// \throw nothrow
314        template<typename T>
315        T operator ()(T const &t) const
316        {
317            return t;
318        }
319
320        /// \overload
321        ///
322        template<typename T>
323        T operator ()(boost::reference_wrapper<T> const &t) const
324        {
325            return t;
326        }
327    };
328
329    /// INTERNAL ONLY
330    ///
331    template<>
332    struct is_callable<_expr>
333      : mpl::true_
334    {};
335
336    /// INTERNAL ONLY
337    ///
338    template<>
339    struct is_callable<_state>
340      : mpl::true_
341    {};
342
343    /// INTERNAL ONLY
344    ///
345    template<>
346    struct is_callable<_data>
347      : mpl::true_
348    {};
349
350    /// INTERNAL ONLY
351    ///
352    template<int N>
353    struct is_callable<_child_c<N> >
354      : mpl::true_
355    {};
356
357    /// INTERNAL ONLY
358    ///
359    template<>
360    struct is_callable<_value>
361      : mpl::true_
362    {};
363
364    /// INTERNAL ONLY
365    ///
366    template<>
367    struct is_callable<_byref>
368      : mpl::true_
369    {};
370
371    /// INTERNAL ONLY
372    ///
373    template<>
374    struct is_callable<_byval>
375      : mpl::true_
376    {};
377
378}}
379
380#endif