Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:50:33

0001 ///////////////////////////////////////////////////////////////////////////////
0002 /// \file make_expr.hpp
0003 /// Definition of the \c make_expr() and \c unpack_expr() utilities for
0004 /// building Proto expression nodes from child nodes or from a Fusion
0005 /// sequence of child nodes, respectively.
0006 //
0007 //  Copyright 2008 Eric Niebler. Distributed under the Boost
0008 //  Software License, Version 1.0. (See accompanying file
0009 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0010 
0011 #ifndef BOOST_PROTO_MAKE_EXPR_HPP_EAN_04_01_2005
0012 #define BOOST_PROTO_MAKE_EXPR_HPP_EAN_04_01_2005
0013 
0014 #include <boost/preprocessor/cat.hpp>
0015 #include <boost/preprocessor/arithmetic/inc.hpp>
0016 #include <boost/preprocessor/arithmetic/dec.hpp>
0017 #include <boost/preprocessor/arithmetic/sub.hpp>
0018 #include <boost/preprocessor/punctuation/comma_if.hpp>
0019 #include <boost/preprocessor/iteration/iterate.hpp>
0020 #include <boost/preprocessor/facilities/intercept.hpp>
0021 #include <boost/preprocessor/repetition/enum.hpp>
0022 #include <boost/preprocessor/repetition/enum_params.hpp>
0023 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
0024 #include <boost/preprocessor/repetition/enum_shifted_params.hpp>
0025 #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
0026 #include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>
0027 #include <boost/preprocessor/repetition/repeat.hpp>
0028 #include <boost/ref.hpp>
0029 #include <boost/mpl/if.hpp>
0030 #include <boost/mpl/assert.hpp>
0031 #include <boost/mpl/eval_if.hpp>
0032 #include <boost/utility/enable_if.hpp>
0033 #include <boost/type_traits/add_const.hpp>
0034 #include <boost/type_traits/add_reference.hpp>
0035 #include <boost/type_traits/remove_cv.hpp>
0036 #include <boost/proto/proto_fwd.hpp>
0037 #include <boost/proto/traits.hpp>
0038 #include <boost/proto/domain.hpp>
0039 #include <boost/proto/generate.hpp>
0040 #include <boost/fusion/include/at_c.hpp>
0041 #include <boost/fusion/include/begin.hpp>
0042 #include <boost/fusion/include/next.hpp>
0043 #include <boost/fusion/include/value_of.hpp>
0044 #include <boost/fusion/include/size.hpp>
0045 #include <boost/proto/detail/poly_function.hpp>
0046 #include <boost/proto/detail/deprecated.hpp>
0047 
0048 #if defined(_MSC_VER)
0049 # pragma warning(push)
0050 # pragma warning(disable : 4180) // qualifier applied to function type has no meaning; ignored
0051 # pragma warning(disable : 4714) // function 'xxx' marked as __forceinline not inlined
0052 #endif
0053 
0054 namespace boost { namespace proto
0055 {
0056 /// INTERNAL ONLY
0057 ///
0058 #define BOOST_PROTO_AS_CHILD_TYPE(Z, N, DATA)                                                   \
0059     typename boost::proto::detail::protoify<                                                    \
0060         BOOST_PP_CAT(BOOST_PP_TUPLE_ELEM(3, 0, DATA), N)                                        \
0061       , BOOST_PP_TUPLE_ELEM(3, 2, DATA)                                                         \
0062     >::result_type                                                                              \
0063     /**/
0064 
0065 /// INTERNAL ONLY
0066 ///
0067 #define BOOST_PROTO_AS_CHILD(Z, N, DATA)                                                        \
0068     boost::proto::detail::protoify<                                                             \
0069         BOOST_PP_CAT(BOOST_PP_TUPLE_ELEM(3, 0, DATA), N)                                        \
0070       , BOOST_PP_TUPLE_ELEM(3, 2, DATA)                                                         \
0071     >()(BOOST_PP_CAT(BOOST_PP_TUPLE_ELEM(3, 1, DATA), N))                                       \
0072     /**/
0073 
0074     namespace detail
0075     {
0076         template<typename T, typename Domain>
0077         struct protoify
0078           : Domain::template as_expr<T>
0079         {};
0080 
0081         template<typename T, typename Domain>
0082         struct protoify<T &, Domain>
0083           : Domain::template as_child<T>
0084         {};
0085 
0086         template<typename T, typename Domain>
0087         struct protoify<boost::reference_wrapper<T>, Domain>
0088           : Domain::template as_child<T>
0089         {};
0090 
0091         template<typename T, typename Domain>
0092         struct protoify<boost::reference_wrapper<T> const, Domain>
0093           : Domain::template as_child<T>
0094         {};
0095 
0096         // Definition of detail::unpack_expr_
0097         #include <boost/proto/detail/unpack_expr_.hpp>
0098 
0099         // Definition of detail::make_expr_
0100         #include <boost/proto/detail/make_expr_.hpp>
0101     }
0102 
0103     namespace result_of
0104     {
0105         /// \brief Metafunction that computes the return type of the
0106         /// \c make_expr() function, with a domain deduced from the
0107         /// domains of the children.
0108         ///
0109         /// Use the <tt>result_of::make_expr\<\></tt> metafunction to
0110         /// compute the return type of the \c make_expr() function.
0111         ///
0112         /// In this specialization, the domain is deduced from the
0113         /// domains of the child types. (If
0114         /// <tt>is_domain\<A0\>::value</tt> is \c true, then another
0115         /// specialization is selected.)
0116         template<
0117             typename Tag
0118           , BOOST_PP_ENUM_PARAMS(BOOST_PROTO_MAX_ARITY, typename A)
0119           , typename Void1  // = void
0120           , typename Void2  // = void
0121         >
0122         struct make_expr
0123         {
0124             /// Same as <tt>result_of::make_expr\<Tag, D, A0, ... AN\>::type</tt>
0125             /// where \c D is the deduced domain, which is calculated as follows:
0126             ///
0127             /// For each \c x in <tt>[0,N)</tt> (proceeding in order beginning with
0128             /// <tt>x=0</tt>), if <tt>domain_of\<Ax\>::type</tt> is not
0129             /// \c default_domain, then \c D is <tt>domain_of\<Ax\>::type</tt>.
0130             /// Otherwise, \c D is \c default_domain.
0131             typedef
0132                 typename detail::make_expr_<
0133                     Tag
0134                   , deduce_domain
0135                     BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PROTO_MAX_ARITY, A)
0136                 >::result_type
0137             type;
0138         };
0139 
0140         /// \brief Metafunction that computes the return type of the
0141         /// \c make_expr() function, within the specified domain.
0142         ///
0143         /// Use the <tt>result_of::make_expr\<\></tt> metafunction to compute
0144         /// the return type of the \c make_expr() function.
0145         template<
0146             typename Tag
0147           , typename Domain
0148             BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PROTO_MAX_ARITY, typename A)
0149         >
0150         struct make_expr<
0151             Tag
0152           , Domain
0153             BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PROTO_MAX_ARITY, A)
0154           , typename Domain::proto_is_domain_
0155         >
0156         {
0157             /// If \c Tag is <tt>tag::terminal</tt>, then \c type is a
0158             /// typedef for <tt>boost::result_of\<Domain(expr\<tag::terminal,
0159             /// term\<A0\> \>)\>::type</tt>.
0160             ///
0161             /// Otherwise, \c type is a typedef for <tt>boost::result_of\<Domain(expr\<Tag,
0162             /// listN\< as_child\<A0\>::type, ... as_child\<AN\>::type\>)
0163             /// \>::type</tt>, where \c N is the number of non-void template
0164             /// arguments, and <tt>as_child\<A\>::type</tt> is evaluated as
0165             /// follows:
0166             ///
0167             /// \li If <tt>is_expr\<A\>::value</tt> is \c true, then the
0168             /// child type is \c A.
0169             /// \li If \c A is <tt>B &</tt> or <tt>cv boost::reference_wrapper\<B\></tt>,
0170             /// and <tt>is_expr\<B\>::value</tt> is \c true, then the
0171             /// child type is <tt>B &</tt>.
0172             /// \li If <tt>is_expr\<A\>::value</tt> is \c false, then the
0173             /// child type is <tt>boost::result_of\<Domain(expr\<tag::terminal, term\<A\> \>
0174             /// )\>::type</tt>.
0175             /// \li If \c A is <tt>B &</tt> or <tt>cv boost::reference_wrapper\<B\></tt>,
0176             /// and <tt>is_expr\<B\>::value</tt> is \c false, then the
0177             /// child type is <tt>boost::result_of\<Domain(expr\<tag::terminal, term\<B &\> \>
0178             /// )\>::type</tt>.
0179             typedef
0180                 typename detail::make_expr_<
0181                     Tag
0182                   , Domain
0183                     BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PROTO_MAX_ARITY, A)
0184                 >::result_type
0185             type;
0186         };
0187 
0188         /// \brief Metafunction that computes the return type of the
0189         /// \c unpack_expr() function, with a domain deduced from the
0190         /// domains of the children.
0191         ///
0192         /// Use the <tt>result_of::unpack_expr\<\></tt> metafunction to
0193         /// compute the return type of the \c unpack_expr() function.
0194         ///
0195         /// \c Sequence is a Fusion Forward Sequence.
0196         ///
0197         /// In this specialization, the domain is deduced from the
0198         /// domains of the child types. (If
0199         /// <tt>is_domain\<Sequence>::value</tt> is \c true, then another
0200         /// specialization is selected.)
0201         template<
0202             typename Tag
0203           , typename Sequence
0204           , typename Void1  // = void
0205           , typename Void2  // = void
0206         >
0207         struct unpack_expr
0208         {
0209             /// Let \c S be the type of a Fusion Random Access Sequence
0210             /// equivalent to \c Sequence. Then \c type is the
0211             /// same as <tt>result_of::make_expr\<Tag,
0212             /// fusion::result_of::value_at_c\<S, 0\>::type, ...
0213             /// fusion::result_of::value_at_c\<S, N-1\>::type\>::type</tt>,
0214             /// where \c N is the size of \c S.
0215             typedef
0216                 typename detail::unpack_expr_<
0217                     Tag
0218                   , deduce_domain
0219                   , Sequence
0220                   , fusion::result_of::size<Sequence>::type::value
0221                 >::type
0222             type;
0223         };
0224 
0225         /// \brief Metafunction that computes the return type of the
0226         /// \c unpack_expr() function, within the specified domain.
0227         ///
0228         /// Use the <tt>result_of::make_expr\<\></tt> metafunction to compute
0229         /// the return type of the \c make_expr() function.
0230         template<typename Tag, typename Domain, typename Sequence>
0231         struct unpack_expr<Tag, Domain, Sequence, typename Domain::proto_is_domain_>
0232         {
0233             /// Let \c S be the type of a Fusion Random Access Sequence
0234             /// equivalent to \c Sequence. Then \c type is the
0235             /// same as <tt>result_of::make_expr\<Tag, Domain,
0236             /// fusion::result_of::value_at_c\<S, 0\>::type, ...
0237             /// fusion::result_of::value_at_c\<S, N-1\>::type\>::type</tt>,
0238             /// where \c N is the size of \c S.
0239             typedef
0240                 typename detail::unpack_expr_<
0241                     Tag
0242                   , Domain
0243                   , Sequence
0244                   , fusion::result_of::size<Sequence>::type::value
0245                 >::type
0246             type;
0247         };
0248     }
0249 
0250     namespace functional
0251     {
0252         /// \brief A callable function object equivalent to the
0253         /// \c proto::make_expr() function.
0254         ///
0255         /// In all cases, <tt>functional::make_expr\<Tag, Domain\>()(a0, ... aN)</tt>
0256         /// is equivalent to <tt>proto::make_expr\<Tag, Domain\>(a0, ... aN)</tt>.
0257         ///
0258         /// <tt>functional::make_expr\<Tag\>()(a0, ... aN)</tt>
0259         /// is equivalent to <tt>proto::make_expr\<Tag\>(a0, ... aN)</tt>.
0260         template<typename Tag, typename Domain  /* = deduce_domain*/>
0261         struct make_expr
0262         {
0263             BOOST_PROTO_CALLABLE()
0264             BOOST_PROTO_POLY_FUNCTION()
0265 
0266             template<typename Sig>
0267             struct result;
0268 
0269             template<typename This, typename A0>
0270             struct result<This(A0)>
0271             {
0272                 typedef
0273                     typename result_of::make_expr<
0274                         Tag
0275                       , Domain
0276                       , A0
0277                     >::type
0278                 type;
0279             };
0280 
0281             /// Construct an expression node with tag type \c Tag
0282             /// and in the domain \c Domain.
0283             ///
0284             /// \return <tt>proto::make_expr\<Tag, Domain\>(a0,...aN)</tt>
0285             template<typename A0>
0286             BOOST_FORCEINLINE
0287             typename result_of::make_expr<
0288                 Tag
0289               , Domain
0290               , A0 const
0291             >::type const
0292             operator ()(A0 const &a0) const
0293             {
0294                 return proto::detail::make_expr_<
0295                     Tag
0296                   , Domain
0297                   , A0 const
0298                 >()(a0);
0299             }
0300 
0301             // Additional overloads generated by the preprocessor ...
0302             #include <boost/proto/detail/make_expr_funop.hpp>
0303 
0304             /// INTERNAL ONLY
0305             ///
0306             template<
0307                 BOOST_PP_ENUM_BINARY_PARAMS(
0308                     BOOST_PROTO_MAX_ARITY
0309                   , typename A
0310                   , = void BOOST_PP_INTERCEPT
0311                 )
0312             >
0313             struct impl
0314               : detail::make_expr_<
0315                     Tag
0316                   , Domain
0317                     BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PROTO_MAX_ARITY, A)
0318                 >
0319             {};
0320         };
0321 
0322         /// \brief A callable function object equivalent to the
0323         /// \c proto::unpack_expr() function.
0324         ///
0325         /// In all cases, <tt>functional::unpack_expr\<Tag, Domain\>()(seq)</tt>
0326         /// is equivalent to <tt>proto::unpack_expr\<Tag, Domain\>(seq)</tt>.
0327         ///
0328         /// <tt>functional::unpack_expr\<Tag\>()(seq)</tt>
0329         /// is equivalent to <tt>proto::unpack_expr\<Tag\>(seq)</tt>.
0330         template<typename Tag, typename Domain /* = deduce_domain*/>
0331         struct unpack_expr
0332         {
0333             BOOST_PROTO_CALLABLE()
0334 
0335             template<typename Sig>
0336             struct result;
0337 
0338             template<typename This, typename Sequence>
0339             struct result<This(Sequence)>
0340             {
0341                 typedef
0342                     typename result_of::unpack_expr<
0343                         Tag
0344                       , Domain
0345                       , typename remove_reference<Sequence>::type
0346                     >::type
0347                 type;
0348             };
0349 
0350             /// Construct an expression node with tag type \c Tag
0351             /// and in the domain \c Domain.
0352             ///
0353             /// \param sequence A Fusion Forward Sequence
0354             /// \return <tt>proto::unpack_expr\<Tag, Domain\>(sequence)</tt>
0355             template<typename Sequence>
0356             BOOST_FORCEINLINE
0357             typename result_of::unpack_expr<Tag, Domain, Sequence const>::type const
0358             operator ()(Sequence const &sequence) const
0359             {
0360                 return proto::detail::unpack_expr_<
0361                     Tag
0362                   , Domain
0363                   , Sequence const
0364                   , fusion::result_of::size<Sequence>::type::value
0365                 >::call(sequence);
0366             }
0367         };
0368 
0369     } // namespace functional
0370 
0371     /// \brief Construct an expression of the requested tag type
0372     /// with a domain and with the specified arguments as children.
0373     ///
0374     /// This function template may be invoked either with or without
0375     /// specifying a \c Domain argument. If no domain is specified,
0376     /// the domain is deduced by examining in order the domains of
0377     /// the given arguments and taking the first that is not
0378     /// \c default_domain, if any such domain exists, or
0379     /// \c default_domain otherwise.
0380     ///
0381     /// Let \c wrap_(x) be defined such that:
0382     /// \li If \c x is a <tt>boost::reference_wrapper\<\></tt>,
0383     /// \c wrap_(x) is equivalent to <tt>as_child\<Domain\>(x.get())</tt>.
0384     /// \li Otherwise, \c wrap_(x) is equivalent to
0385     /// <tt>as_expr\<Domain\>(x)</tt>.
0386     ///
0387     /// Let <tt>make_\<Tag\>(b0,...bN)</tt> be defined as
0388     /// <tt>expr\<Tag, listN\<C0,...CN\> \>::make(c0,...cN)</tt>
0389     /// where \c Bx is the type of \c bx.
0390     ///
0391     /// \return <tt>Domain()(make_\<Tag\>(wrap_(a0),...wrap_(aN)))</tt>.
0392     template<typename Tag, typename A0>
0393     BOOST_FORCEINLINE
0394     typename lazy_disable_if<
0395         is_domain<A0>
0396       , result_of::make_expr<
0397             Tag
0398           , A0 const
0399         >
0400     >::type const
0401     make_expr(A0 const &a0)
0402     {
0403         return proto::detail::make_expr_<
0404             Tag
0405           , deduce_domain
0406           , A0 const
0407         >()(a0);
0408     }
0409 
0410     /// \overload
0411     ///
0412     template<typename Tag, typename Domain, typename C0>
0413     BOOST_FORCEINLINE
0414     typename result_of::make_expr<
0415         Tag
0416       , Domain
0417       , C0 const
0418     >::type const
0419     make_expr(C0 const &c0)
0420     {
0421         return proto::detail::make_expr_<
0422             Tag
0423           , Domain
0424           , C0 const
0425         >()(c0);
0426     }
0427 
0428     // Additional overloads generated by the preprocessor...
0429     #include <boost/proto/detail/make_expr.hpp>
0430 
0431     /// \brief Construct an expression of the requested tag type
0432     /// with a domain and with childres from the specified Fusion
0433     /// Forward Sequence.
0434     ///
0435     /// This function template may be invoked either with or without
0436     /// specifying a \c Domain argument. If no domain is specified,
0437     /// the domain is deduced by examining in order the domains of the
0438     /// elements of \c sequence and taking the first that is not
0439     /// \c default_domain, if any such domain exists, or
0440     /// \c default_domain otherwise.
0441     ///
0442     /// Let \c s be a Fusion Random Access Sequence equivalent to \c sequence.
0443     /// Let <tt>wrap_\<N\>(s)</tt>, where \c s has type \c S, be defined
0444     /// such that:
0445     /// \li If <tt>fusion::result_of::value_at_c\<S,N\>::type</tt> is a reference,
0446     /// <tt>wrap_\<N\>(s)</tt> is equivalent to
0447     /// <tt>as_child\<Domain\>(fusion::at_c\<N\>(s))</tt>.
0448     /// \li Otherwise, <tt>wrap_\<N\>(s)</tt> is equivalent to
0449     /// <tt>as_expr\<Domain\>(fusion::at_c\<N\>(s))</tt>.
0450     ///
0451     /// Let <tt>make_\<Tag\>(b0,...bN)</tt> be defined as
0452     /// <tt>expr\<Tag, listN\<B0,...BN\> \>::make(b0,...bN)</tt>
0453     /// where \c Bx is the type of \c bx.
0454     ///
0455     /// \param sequence a Fusion Forward Sequence.
0456     /// \return <tt>Domain()(make_\<Tag\>(wrap_\<0\>(s),...wrap_\<N-1\>(s)))</tt>,
0457     /// where N is the size of \c Sequence.
0458     template<typename Tag, typename Sequence>
0459     BOOST_FORCEINLINE
0460     typename lazy_disable_if<
0461         is_domain<Sequence>
0462       , result_of::unpack_expr<Tag, Sequence const>
0463     >::type const
0464     unpack_expr(Sequence const &sequence)
0465     {
0466         return proto::detail::unpack_expr_<
0467             Tag
0468           , deduce_domain
0469           , Sequence const
0470           , fusion::result_of::size<Sequence>::type::value
0471         >::call(sequence);
0472     }
0473 
0474     /// \overload
0475     ///
0476     template<typename Tag, typename Domain, typename Sequence2>
0477     BOOST_FORCEINLINE
0478     typename result_of::unpack_expr<Tag, Domain, Sequence2 const>::type const
0479     unpack_expr(Sequence2 const &sequence2)
0480     {
0481         return proto::detail::unpack_expr_<
0482             Tag
0483           , Domain
0484           , Sequence2 const
0485           , fusion::result_of::size<Sequence2>::type::value
0486         >::call(sequence2);
0487     }
0488 
0489     /// INTERNAL ONLY
0490     ///
0491     template<typename Tag, typename Domain>
0492     struct is_callable<functional::make_expr<Tag, Domain> >
0493       : mpl::true_
0494     {};
0495 
0496     /// INTERNAL ONLY
0497     ///
0498     template<typename Tag, typename Domain>
0499     struct is_callable<functional::unpack_expr<Tag, Domain> >
0500       : mpl::true_
0501     {};
0502 
0503 }}
0504 
0505 #if defined(_MSC_VER)
0506 # pragma warning(pop)
0507 #endif
0508 
0509 #endif // BOOST_PROTO_MAKE_EXPR_HPP_EAN_04_01_2005